📄 printerdialog.java
字号:
oneside.setSelected(true); else duplex.setSelected(true); } } else { oneside.setEnabled(false); calendar.setEnabled(false); duplex.setEnabled(false); } } } /** * Handles the chromaticity attributes. * @author Wolfgang Baer (WBaer@gmx.de) */ final class Color extends JPanel implements ActionListener { private JRadioButton bw, color; Color() { bw = new JRadioButton(getLocalizedString("rbt.blackwhite")); bw.addActionListener(this); color = new JRadioButton(getLocalizedString("rbt.color")); color.addActionListener(this); ButtonGroup group = new ButtonGroup(); group.add(bw); group.add(color); GridBagLayout layout = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(layout); setBorder(new TitledBorder(getLocalizedString("title.color"))); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 5, 5); c.gridx = 0; c.gridy = 0; add(bw, c); c.gridx = 0; c.gridy = 1; add(color, c); } public void actionPerformed(ActionEvent e) { if (e.getSource() == bw) atts.add(Chromaticity.MONOCHROME); else atts.add(Chromaticity.COLOR); } /** * Called to update for new selected * print service. Tests if currently * selected attributes are supported. */ void updateForSelectedService() { if (categorySupported(Chromaticity.class)) { bw.setEnabled(true); color.setEnabled(true); Object defaultValue = defaultValue(Chromaticity.class); Attribute chromaticity = attribute(Chromaticity.class); if (chromaticity != null) { if (chromaticity.equals(Chromaticity.MONOCHROME)) bw.setSelected(true); else color.setSelected(true); } else { if (defaultValue.equals(Chromaticity.MONOCHROME)) bw.setSelected(true); else color.setSelected(true); } } else { bw.setEnabled(false); color.setEnabled(false); } } } private Quality quality_panel; private JobAttributes jobAttr_panel; private SidesPanel sides_panel; private Color chromaticy_panel; /** * Creates the panel for appearance attributes. */ public AppearancePanel() { setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); quality_panel = new Quality(); jobAttr_panel = new JobAttributes(); sides_panel = new SidesPanel(); chromaticy_panel = new Color(); JPanel layout_panel = new JPanel(); layout_panel.setLayout(new BoxLayout(layout_panel, BoxLayout.LINE_AXIS)); layout_panel.add(chromaticy_panel); layout_panel.add(Box.createRigidArea(new Dimension(10, 0))); layout_panel.add(quality_panel); JPanel layout2_panel = new JPanel(); layout2_panel.setLayout(new BoxLayout(layout2_panel, BoxLayout.LINE_AXIS)); layout2_panel.add(sides_panel); layout2_panel.add(Box.createRigidArea(new Dimension(10, 0))); layout2_panel.add(jobAttr_panel); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); add(layout_panel); add(Box.createRigidArea(new Dimension(0, 12))); add(layout2_panel); } /** * Calls update on all internal panels to adjust * for a new selected print service. */ void update() { quality_panel.updateForSelectedService(); jobAttr_panel.updateForSelectedService(); sides_panel.updateForSelectedService(); chromaticy_panel.updateForSelectedService(); } } // on main contentpane private JButton ok_bt; private JButton cancel_bt; // the tabs private GeneralPanel general_panel; private PageSetupPanel pagesetup_panel; private AppearancePanel appearance_panel; private PrintService[] services; private PrintService defaultService; private PrintService selectedService; private DocFlavor flavor; private PrintRequestAttributeSet attributes; private boolean onlyPageDialog; private PrintRequestAttributeSet atts; private final static ResourceBundle messages; static { messages = ResourceBundle.getBundle("gnu/javax/print/PrinterDialog"); } // TODO LowPriority: Include checks so that if a specific value formerly // selected is no more supported by the new service changes to the default. /** * Class private constructs a printer dialog. * * @param gc the screen to use. <code>null</code> is default screen. * @param services the print services to browse (not null). * @param defaultService the default service. If <code>null</code> * the first of the print services in the services array will be used. * @param flavor the flavours to be printed. * @param attributes the attributes requested. Will be updated * by selections done by the user in the dialog. * @param onlyPageDialog if true a page settings only dialog is constructed. * * @throws HeadlessException if GraphicsEnvironment is headless */ private PrinterDialog(GraphicsConfiguration gc, PrintService[] services, PrintService defaultService, DocFlavor flavor, PrintRequestAttributeSet attributes, boolean onlyPageDialog, String title) throws HeadlessException { super((Frame)null, title, true, gc); setResizable(false); setDefaultCloseOperation(DISPOSE_ON_CLOSE); // check and remove service not supporting the flavor if (flavor != null) { ArrayList list = new ArrayList(services.length); for(int i=0; i < services.length; i++) if (services[i].isDocFlavorSupported(flavor)) list.add(services[i]); if (defaultService != null && (! list.contains(defaultService))) defaultService = (PrintService) list.get(0); PrintService[] newServices = new PrintService[list.size()]; this.services = (PrintService[]) list.toArray(newServices); } else this.services = services; if (defaultService == null) this.defaultService = services[0]; else this.defaultService = defaultService; this.selectedService = this.defaultService; this.flavor = flavor; // the attributes given by the user this.attributes = attributes; // the one to work with during browsing this.atts = new HashPrintRequestAttributeSet(attributes); this.onlyPageDialog = onlyPageDialog; initUI(onlyPageDialog); pack(); updateAll(); } /** * Constructs a page settings only dialog. * * @param gc the screen to use. <code>null</code> is default screen. * @param service the print service for the page dialog. * the first of the print services in the services array will be used. * @param flavor the flavours to be printed. * @param attributes the attributes requested. Will be updated * by selections done by the user in the dialog. * * @throws HeadlessException if GraphicsEnvironment is headless */ public PrinterDialog(GraphicsConfiguration gc, PrintService service, DocFlavor flavor, PrintRequestAttributeSet attributes) throws HeadlessException { this(gc, new PrintService[] {service}, service, flavor, attributes, true, getLocalizedString("title.pagedialog")); } /** * Constructs a printer dialog. * * @param gc the screen to use. <code>null</code> is default screen. * @param services the print services to browse (not null). * @param defaultService the default service. If <code>null</code> * the first of the print services in the services array will be used. * @param flavor the flavours to be printed. * @param attributes the attributes requested. Will be updated * by selections done by the user in the dialog. * * @throws HeadlessException if GraphicsEnvironment is headless */ public PrinterDialog(GraphicsConfiguration gc, PrintService[] services, PrintService defaultService, DocFlavor flavor, PrintRequestAttributeSet attributes) throws HeadlessException { this(gc, services, defaultService, flavor, attributes, false, getLocalizedString("title.printdialog")); } // initializes the gui parts private void initUI(boolean onlyPageDialog) { JPanel buttonPane = new JPanel(); if (onlyPageDialog) { JPanel pane = new JPanel(); pane.setLayout(new BorderLayout()); pagesetup_panel = new PageSetupPanel(); pane.add(pagesetup_panel, BorderLayout.CENTER); ok_bt = new JButton(getLocalizedString("bt.OK")); ok_bt.addActionListener(this); cancel_bt = new JButton(getLocalizedString("bt.cancel")); cancel_bt.addActionListener(this); getContentPane().add(pane, BorderLayout.CENTER); } else { general_panel = new GeneralPanel(); pagesetup_panel = new PageSetupPanel(); appearance_panel = new AppearancePanel(); JTabbedPane pane = new JTabbedPane(); pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); ok_bt = new JButton(getLocalizedString("bt.print")); ok_bt.addActionListener(this); cancel_bt = new JButton(getLocalizedString("bt.cancel")); cancel_bt.addActionListener(this); // populate jtabbedpane pane.addTab(getLocalizedString("tab.general"), general_panel); pane.addTab(getLocalizedString("tab.pagesetup"), pagesetup_panel); pane.addTab(getLocalizedString("tab.appearance"), appearance_panel); // Put everything together getContentPane().add(pane, BorderLayout.CENTER); } buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); buttonPane.add(Box.createHorizontalGlue()); buttonPane.add(ok_bt); buttonPane.add(Box.createRigidArea(new Dimension(5, 0))); buttonPane.add(cancel_bt); getContentPane().add(buttonPane, BorderLayout.PAGE_END); } /** * Returns the modified attributes set. * @return The attributes. */ public PrintRequestAttributeSet getAttributes() { return attributes; } /** * Returns the print service selected by the user. * @return The selected print service. */ public PrintService getSelectedPrintService() { return selectedService; } /** * Sets the currently selected print service. * * @param service the service selected. */ protected void setSelectedPrintService(PrintService service) { selectedService = service; } /** * Returns the print service array. * @return The print services. */ protected PrintService[] getPrintServices() { return services; } /** * Calls update on all panels to adjust * for a new selected print service. */ void updateAll() { pagesetup_panel.update(); if (! onlyPageDialog) { general_panel.update(); appearance_panel.update(); } } boolean categorySupported(Class category) { return getSelectedPrintService(). isAttributeCategorySupported(category); } Object defaultValue(Class category) { return getSelectedPrintService(). getDefaultAttributeValue(category); } Attribute attribute(Class category) { return atts.get(category); } /** * Action handler for Print/Cancel buttons. * If cancel is pressed we reset the attributes * and the selected service. * * @param e the ActionEvent */ public void actionPerformed(ActionEvent e) { if (e.getSource() == ok_bt) { setVisible(false); attributes.addAll(atts); dispose(); } else { setVisible(false); selectedService = null; dispose(); } } /** * Retrieves localized messages from the resource bundle. * * @param key the key * @return The localized value for the key. */ static final String getLocalizedString(String key) { return messages.getString(key); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -