From 3e3c562e70566f1a35847398bcb9a946125f08ac Mon Sep 17 00:00:00 2001 From: rodinia814 Date: Thu, 10 Mar 2011 05:21:09 +0000 Subject: [PATCH] DGP - Partial workaround for systems with no print services; simplification of the dialog git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@118 180e2498-e6e9-4542-8430-84ac67f01cd8 --- .../openrocket/gui/dialogs/PrintDialog.java | 132 +++------ .../sf/openrocket/gui/dialogs/PrintPanel.java | 19 +- .../openrocket/gui/print/PrintController.java | 20 +- .../gui/print/PrintServiceDialog.java | 275 ++++++++++-------- 4 files changed, 234 insertions(+), 212 deletions(-) diff --git a/src/net/sf/openrocket/gui/dialogs/PrintDialog.java b/src/net/sf/openrocket/gui/dialogs/PrintDialog.java index f8ffed70..cfd36be3 100644 --- a/src/net/sf/openrocket/gui/dialogs/PrintDialog.java +++ b/src/net/sf/openrocket/gui/dialogs/PrintDialog.java @@ -8,15 +8,20 @@ import net.sf.openrocket.gui.print.PDFPrintStreamDoc; import net.sf.openrocket.gui.print.PrintServiceDialog; import net.sf.openrocket.gui.print.PrintUtilities; -import javax.print.*; +import javax.print.DocFlavor; +import javax.print.DocPrintJob; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; import javax.print.attribute.Attribute; import javax.print.attribute.AttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.Destination; import javax.print.attribute.standard.Fidelity; -import javax.swing.*; -import java.awt.*; +import javax.swing.JDialog; +import java.awt.Dialog; +import java.awt.GraphicsEnvironment; +import java.awt.HeadlessException; import java.io.ByteArrayOutputStream; /** @@ -41,24 +46,23 @@ public class PrintDialog { * * @param orDocument the rocket container */ - public PrintDialog (OpenRocketDocument orDocument) { + public PrintDialog(OpenRocketDocument orDocument) { PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); PrintService svc = PrintServiceLookup.lookupDefaultPrintService(); PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); attrs.add(PrintUtilities.getDefaultMedia().getMediaSizeName()); final PrintPanel panel = new PrintPanel(orDocument, this); - PrintService ps = printDialog(null, 100, 100, services, svc, PDF, attrs, panel); - if (ps != null) { - DocPrintJob dpj = ps.createPrintJob(); - try { + try { + PrintService ps = printDialog(100, 100, services, svc, PDF, attrs, panel); + if (ps != null) { + DocPrintJob dpj = ps.createPrintJob(); ByteArrayOutputStream baos = panel.generateReport(); dpj.print(new PDFPrintStreamDoc(baos, null), attrs); } - catch (PrintException e) { - e.printStackTrace(); - //dgp - } + } + catch (Exception e) { + e.printStackTrace(); } } @@ -67,7 +71,7 @@ public class PrintDialog { * * @return a set of print attributes */ - PrintRequestAttributeSet getAttributes () { + PrintRequestAttributeSet getAttributes() { return dialog.getAttributes(); } @@ -77,14 +81,13 @@ public class PrintDialog { * * @return the Java service ui print dialog */ - JDialog getDialog () { + JDialog getDialog() { return dialog; } /** * Mimics the ServiceUI.printDialog method, but with enhancements for our own print settings tab. * - * @param gc used to select screen. null means primary or default screen. * @param x location of dialog including border in screen coordinates * @param y location of dialog including border in screen coordinates * @param services to be browsable, must be non-null. @@ -93,35 +96,28 @@ public class PrintDialog { * @param attributes on input is the initial application supplied preferences. This cannot be null but may be * empty. On output the attributes reflect changes made by the user. * @param addnl a panel to be added, as a tab, to the internal tabbed pane of the resulting print dialog - * * @return print service selected by the user, or null if the user cancelled the dialog. - * * @throws HeadlessException if GraphicsEnvironment.isHeadless() returns true. * @throws IllegalArgumentException if services is null or empty, or attributes is null, or the initial PrintService * is not in the list of browsable services. */ - private PrintService printDialog (GraphicsConfiguration gc, - int x, int y, - PrintService[] services, - PrintService defaultService, - DocFlavor flavor, - PrintRequestAttributeSet attributes, - PrintPanel addnl) - throws HeadlessException { + private PrintService printDialog(int x, int y, + PrintService[] services, + PrintService defaultService, + DocFlavor flavor, + PrintRequestAttributeSet attributes, + PrintPanel addnl) + throws HeadlessException, IllegalArgumentException { int defaultIndex = -1; if (GraphicsEnvironment.isHeadless()) { throw new HeadlessException(); } - else if ((services == null) || (services.length == 0)) { - throw new IllegalArgumentException("services must be non-null " + - "and non-empty"); - } else if (attributes == null) { throw new IllegalArgumentException("attributes must be non-null"); } - if (defaultService != null) { + if (defaultService != null && services != null) { for (int i = 0; i < services.length; i++) { if (services[i].equals(defaultService)) { defaultIndex = i; @@ -129,45 +125,21 @@ public class PrintDialog { } } + //If the default service is not found just go with the first in the list if (defaultIndex < 0) { - throw new IllegalArgumentException("services must contain " + - "defaultService"); + defaultIndex = 0; } } else { - defaultIndex = 0; - } - - Rectangle gcBounds = (gc == null) ? GraphicsEnvironment. - getLocalGraphicsEnvironment().getDefaultScreenDevice(). - getDefaultConfiguration().getBounds() : gc.getBounds(); - - dialog = new PrintServiceDialog(gc, - x + gcBounds.x, - y + gcBounds.y, - services, defaultIndex, - flavor, attributes, - (Dialog) null); - Rectangle dlgBounds = dialog.getBounds(); - - // get union of all GC bounds - GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); - GraphicsDevice[] gs = ge.getScreenDevices(); - for (GraphicsDevice g : gs) { - gcBounds = gcBounds.union(g.getDefaultConfiguration().getBounds()); - } - - // if portion of dialog is not within the gc boundary - if (!gcBounds.contains(dlgBounds)) { - // put in the center relative to parent frame/dialog - dialog.setLocationRelativeTo(null); - } - if (addnl != null && addnl.getTitle() != null) { - JTabbedPane tp = (JTabbedPane) getDescendantOfClass(JTabbedPane.class, dialog); - tp.add(addnl, addnl.getTitle(), 0); - tp.setSelectedIndex(0); + defaultIndex = -1; } + dialog = new PrintServiceDialog( + x, + y, + services, defaultIndex, + flavor, attributes, + (Dialog) null, addnl); dialog.setVisible(true); if (dialog.getStatus() == PrintServiceDialog.APPROVE) { @@ -176,7 +148,7 @@ public class PrintDialog { Class fdCategory = Fidelity.class; if (attributes.containsKey(dstCategory) && - !newas.containsKey(dstCategory)) { + !newas.containsKey(dstCategory)) { attributes.remove(dstCategory); } @@ -196,37 +168,17 @@ public class PrintDialog { } } - private Component getDescendantOfClass (Class c, Container cont) { - if (c == null || cont == null) { - return null; - } - Component[] children = (cont instanceof JMenu) - ? ((JMenu) cont).getMenuComponents() - : cont.getComponents(); - for (int i = 0, n = children.length; i < n; i++) { - Component comp = children[i]; - if (c.isInstance(comp)) { - return comp; - } - comp = getDescendantOfClass(c, (Container) comp); - if (comp != null) { - return comp; - } - } - return null; - } - /** * Removes any attributes from the given AttributeSet that are unsupported by the given PrintService/DocFlavor * combination. * - * @param ps the print service for which unsupported attributes will be determined - * @param flavor the document flavor; PDF in our case - * @param aset the set of attributes requested + * @param ps the print service for which unsupported attributes will be determined + * @param flavor the document flavor; PDF in our case + * @param aset the set of attributes requested */ - private static void removeUnsupportedAttributes (PrintService ps, - DocFlavor flavor, - AttributeSet aset) { + private static void removeUnsupportedAttributes(PrintService ps, + DocFlavor flavor, + AttributeSet aset) { AttributeSet asUnsupported = ps.getUnsupportedAttributes(flavor, aset); diff --git a/src/net/sf/openrocket/gui/dialogs/PrintPanel.java b/src/net/sf/openrocket/gui/dialogs/PrintPanel.java index 73aefd42..4bc748cd 100644 --- a/src/net/sf/openrocket/gui/dialogs/PrintPanel.java +++ b/src/net/sf/openrocket/gui/dialogs/PrintPanel.java @@ -18,13 +18,23 @@ import net.sf.openrocket.startup.Application; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.MediaSizeName; -import javax.swing.*; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JColorChooser; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.UIManager; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.filechooser.FileFilter; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; -import java.awt.*; +import java.awt.Color; +import java.awt.Desktop; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.ByteArrayOutputStream; @@ -165,6 +175,11 @@ public class PrintPanel extends JPanel implements TreeSelectionListener { return TAB_TITLE; } + @Override + public String getName() { + return getTitle(); + } + @Override public void valueChanged (final TreeSelectionEvent e) { final TreePath path = e.getNewLeadSelectionPath(); diff --git a/src/net/sf/openrocket/gui/print/PrintController.java b/src/net/sf/openrocket/gui/print/PrintController.java index 00028871..31c5d4d7 100644 --- a/src/net/sf/openrocket/gui/print/PrintController.java +++ b/src/net/sf/openrocket/gui/print/PrintController.java @@ -12,6 +12,7 @@ import com.itextpdf.text.pdf.PdfBoolean; import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfWriter; import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.gui.main.ExceptionHandler; import net.sf.openrocket.gui.print.visitor.FinSetVisitorStrategy; import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy; @@ -35,7 +36,8 @@ public class PrintController { * @param outputFile the file being written to * @param msn the paper size */ - public void print (OpenRocketDocument doc, Iterator toBePrinted, OutputStream outputFile, MediaSizeName msn) { + public void print(OpenRocketDocument doc, Iterator toBePrinted, OutputStream outputFile, + MediaSizeName msn) { Document idoc = new Document(convertWithDefault(msn)); PdfWriter writer = null; @@ -64,8 +66,8 @@ public class PrintController { break; case FIN_TEMPLATE: final FinSetVisitorStrategy finWriter = new FinSetVisitorStrategy(idoc, - writer, - stages); + writer, + stages); finWriter.writeToDocument(doc.getRocket()); break; case PARTS_DETAIL: @@ -86,8 +88,10 @@ public class PrintController { idoc.close(); } catch (DocumentException e) { + ExceptionHandler.handleErrorCondition("Could not create document.", e); } catch (ExceptionConverter ec) { + ExceptionHandler.handleErrorCondition("Could not create document.", ec); } finally { if (outputFile != null) { @@ -98,15 +102,19 @@ public class PrintController { } } } - } - private Rectangle convertWithDefault (final MediaSizeName msn) { + /** + * Convert a media size name to a rectangle that defines the bounds of the corresponding paper size. + * + * @param msn the MediaSizeName to convert + * @return the corresponding Rectangle + */ + private Rectangle convertWithDefault(final MediaSizeName msn) { Rectangle result = PaperSize.convert(msn); if (result == null) { result = PaperSize.convert(PrintUtilities.getDefaultMedia().getMediaSizeName()); } return result; } - } diff --git a/src/net/sf/openrocket/gui/print/PrintServiceDialog.java b/src/net/sf/openrocket/gui/print/PrintServiceDialog.java index 98045f88..4aedabac 100644 --- a/src/net/sf/openrocket/gui/print/PrintServiceDialog.java +++ b/src/net/sf/openrocket/gui/print/PrintServiceDialog.java @@ -12,14 +12,32 @@ import javax.print.ServiceUIFactory; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttribute; import javax.print.attribute.PrintRequestAttributeSet; -import javax.print.attribute.PrintServiceAttribute; -import javax.print.attribute.standard.*; -import javax.swing.*; +import javax.print.attribute.standard.Destination; +import javax.print.attribute.standard.Media; +import javax.print.attribute.standard.MediaSizeName; +import javax.print.attribute.standard.MediaTray; +import javax.swing.AbstractAction; +import javax.swing.ActionMap; +import javax.swing.BorderFactory; +import javax.swing.InputMap; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTabbedPane; +import javax.swing.KeyStroke; import javax.swing.border.EmptyBorder; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; -import java.awt.*; -import java.awt.event.*; +import java.awt.Container; +import java.awt.Dialog; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.Iterator; import java.util.Set; @@ -31,7 +49,8 @@ public class PrintServiceDialog extends JDialog implements ActionListener { private JButton btnCancel, btnPrint; private boolean pdfFlavorSupported = true; private PrintService services[]; - private int defaultServiceIndex, status; + private int defaultServiceIndex = -1; + private int status; private PrintRequestAttributeSet asOriginal; private HashPrintRequestAttributeSet asCurrent; private PrintService psCurrent; @@ -53,15 +72,15 @@ public class PrintServiceDialog extends JDialog implements ActionListener { private ArrayList sizes; private ArrayList sources; - private String getMediaName (String s) { + private String getMediaName(String s) { String s1 = s.replace(' ', '-'); s1 = s1.replace('#', 'n'); return PaperSize.toDisplayable(s1); } - public void itemStateChanged (ItemEvent itemevent) { + public void itemStateChanged(ItemEvent itemevent) { Object obj = itemevent.getSource(); - if (itemevent.getStateChange() == 1) { + if (itemevent.getStateChange() == ItemEvent.SELECTED) { if (obj == cbSize) { int i = cbSize.getSelectedIndex(); if (i >= 0 && i < sizes.size()) { @@ -91,7 +110,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } - public void updateInfo () { + public void updateInfo() { boolean flag = false; cbSize.removeItemListener(this); cbSize.removeAllItems(); @@ -100,7 +119,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { cbSource.addItem(getMediaName("auto-select")); sizes.clear(); sources.clear(); - if (psCurrent.isAttributeCategorySupported(Media.class)) { + if (psCurrent != null && psCurrent.isAttributeCategorySupported(Media.class)) { flag = true; Object obj = null; try { @@ -108,7 +127,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } catch (IllegalArgumentException iae) { pdfFlavorSupported = false; - //dgp + //dgp } if (obj instanceof Media[]) { Media amedia[] = (Media[]) obj; @@ -141,15 +160,16 @@ public class PrintServiceDialog extends JDialog implements ActionListener { cbSize.setEnabled(flag1); cbSource.setEnabled(false); lblSource.setEnabled(false); - if (flag) { + if (flag && psCurrent != null) { Media media = (Media) asCurrent.get(Media.class); boolean attributeValueSupported = false; try { - attributeValueSupported = psCurrent.isAttributeValueSupported(media, docFlavor, asCurrent); + attributeValueSupported = media == null ? false : psCurrent.isAttributeValueSupported(media, + docFlavor, + asCurrent); } catch (IllegalArgumentException iae) { pdfFlavorSupported = false; - //dgp } if (media == null || !attributeValueSupported) { media = (Media) psCurrent.getDefaultAttributeValue(Media.class); @@ -187,7 +207,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { cbSource.addItemListener(this); } - public MediaPanel () { + public MediaPanel() { super(new MigLayout("fill, gap rel unrel")); sizes = new ArrayList(); sources = new ArrayList(); @@ -223,12 +243,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener { * Returns a string value corresponding to this enumeration value. */ @Override - public String toString () { + public String toString() { return displayableName; } @Override - public int compareTo (final Object o) { + public int compareTo(final Object o) { String name = displayableName; if (name != null) { return name.compareTo(o.toString()); @@ -237,7 +257,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } @Override - public boolean equals (final Object o) { + public boolean equals(final Object o) { if (this == o) { return true; } @@ -251,7 +271,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } @Override - public int hashCode () { + public int hashCode() { return displayableName.hashCode(); } @@ -267,15 +287,15 @@ public class PrintServiceDialog extends JDialog implements ActionListener { private final String strTitle = "Print Service"; private JButton btnProperties; private JComboBox cbName; - private JLabel lblType, lblStatus, lblInfo; - private JLabel vType, vStatus, vInfo; private ServiceUIFactory uiFactory; private boolean changedService; - public PrintServicePanel () { + public PrintServicePanel() { super(new MigLayout("fill, gap rel unrel")); changedService = false; - uiFactory = psCurrent.getServiceUIFactory(); + if (psCurrent != null) { + uiFactory = psCurrent.getServiceUIFactory(); + } setBorder(BorderFactory.createTitledBorder(strTitle)); String as[] = new String[services.length]; for (int i = 0; i < as.length; i++) { @@ -283,7 +303,9 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } cbName = new JComboBox(as); - cbName.setSelectedIndex(defaultServiceIndex); + if (defaultServiceIndex != -1 && defaultServiceIndex < services.length) { + cbName.setSelectedIndex(defaultServiceIndex); + } cbName.addItemListener(this); cbName.addPopupMenuListener(this); JLabel jlabel = new JLabel(("Name:"), 11); @@ -293,24 +315,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener { add(cbName); btnProperties = PrintServiceDialog.createButton("Properties...", this); add(btnProperties, "wrap"); - lblStatus = new JLabel("Status:", 11); - add(lblStatus); - vStatus = new JLabel(); - add(vStatus, "wrap"); - lblType = new JLabel("Type:", 11); - vType = new JLabel(); - add(lblType); - add(vType, "wrap"); - lblInfo = new JLabel("Info:", 11); - vInfo = new JLabel(); - add(lblInfo); - add(vInfo, "wrap"); - } - - public void actionPerformed (ActionEvent actionevent) { + } + + public void actionPerformed(ActionEvent actionevent) { Object obj = actionevent.getSource(); if (obj == btnProperties && uiFactory != null) { - JDialog jdialog = (JDialog) uiFactory.getUI(3, "javax.swing.JDialog"); + JDialog jdialog = (JDialog) uiFactory.getUI(ServiceUIFactory.MAIN_UIROLE, "javax.swing.JDialog"); if (jdialog != null) { jdialog.show(); } @@ -320,96 +330,128 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } } - public void itemStateChanged (ItemEvent itemevent) { - if (itemevent.getStateChange() == 1) { + /** + * {@inheritDoc} + * + * @param itemevent the event that indicates what changed + */ + @Override + public void itemStateChanged(ItemEvent itemevent) { + if (itemevent.getStateChange() == ItemEvent.SELECTED) { int i = cbName.getSelectedIndex(); - if (i >= 0 && i < services.length && !services[i].equals(psCurrent)) { + if (services != null && i >= 0 && i < services.length && !services[i].equals(psCurrent)) { psCurrent = services[i]; uiFactory = psCurrent.getServiceUIFactory(); changedService = true; - Destination destination = (Destination) asOriginal.get( - Destination.class); - if ((destination != null) && psCurrent.isAttributeCategorySupported( - Destination.class)) { - asCurrent.add(destination); - } - else { - asCurrent.remove(Destination.class); + if (asOriginal != null) { + Destination destination = (Destination) asOriginal.get( + Destination.class); + if ((destination != null) && psCurrent.isAttributeCategorySupported( + Destination.class)) { + asCurrent.add(destination); + } + else { + asCurrent.remove(Destination.class); + } } } } } - public void popupMenuWillBecomeVisible (PopupMenuEvent popupmenuevent) { + /** + * {@inheritDoc} + * + * @param popupmenuevent + */ + @Override + public void popupMenuWillBecomeVisible(PopupMenuEvent popupmenuevent) { changedService = false; } - public void popupMenuWillBecomeInvisible (PopupMenuEvent popupmenuevent) { + /** + * {@inheritDoc} + * + * @param popupmenuevent + */ + @Override + public void popupMenuWillBecomeInvisible(PopupMenuEvent popupmenuevent) { if (changedService) { changedService = false; updatePanels(); } } - public void popupMenuCanceled (PopupMenuEvent popupmenuevent) { + /** + * {@inheritDoc} + * + * @param popupmenuevent + */ + @Override + public void popupMenuCanceled(PopupMenuEvent popupmenuevent) { } - public void updateInfo () { - PrintServiceAttribute psa = psCurrent.getAttribute(PrinterMakeAndModel.class); - if (psa != null) { - vType.setText(psa.toString()); - } - psa = psCurrent.getAttribute(PrinterIsAcceptingJobs.class); - if (psa != null) { - vStatus.setText((psa.toString())); - } - psa = psCurrent.getAttribute(PrinterInfo.class); - if (psa != null) { - vInfo.setText(psa.toString()); - } + /** + * Modify the enablement of the properties button. + */ + public void updateInfo() { btnProperties.setEnabled(uiFactory != null); } } + /** + * The panel for general print services info. + */ private class GeneralPanel extends JPanel { - public void updateInfo () { - pnlPrintService.updateInfo(); - pnlMedia.updateInfo(); - } - private PrintServicePanel pnlPrintService; private MediaPanel pnlMedia; - public GeneralPanel () { + public GeneralPanel() { super(new MigLayout("fill, gap rel unrel")); pnlPrintService = new PrintServicePanel(); add(pnlPrintService, "wrap"); pnlMedia = new MediaPanel(); add(pnlMedia, "wrap"); } - } - - public PrintServiceDialog (GraphicsConfiguration graphicsconfiguration, int i, int j, PrintService aprintservice[], - int k, DocFlavor docflavor, PrintRequestAttributeSet printrequestattributeset, - Dialog dialog, JPanel... additional) { - super(dialog, PRINT_BUTTON_LABEL, true, graphicsconfiguration); - initPrintDialog(i, j, aprintservice, k, docflavor, printrequestattributeset, additional); + public void updateInfo() { + pnlPrintService.updateInfo(); + pnlMedia.updateInfo(); + } } - void initPrintDialog (int i, int j, PrintService aprintservice[], int k, DocFlavor docflavor, - PrintRequestAttributeSet printrequestattributeset, - JPanel... additional) { - services = aprintservice; - defaultServiceIndex = k; - asOriginal = printrequestattributeset; - asCurrent = new HashPrintRequestAttributeSet(printrequestattributeset); - psCurrent = aprintservice[k]; + /** + * Constructor. + * + * @param x the x-coordinate of the new location's + * top-left corner in the parent's coordinate space + * @param y the y-coordinate of the new location's + * top-left corner in the parent's coordinate space + * @param aPrintService the array of installed print services + * @param defaultServiceIndex the default service index (index into aPrintService) + * @param docflavor the document flavor (i.e. PDF) + * @param attributeSet the set of required attributes + * @param dialog the parent + * @param additional other panels to add in tabs + */ + public PrintServiceDialog(int x, int y, PrintService[] aPrintService, + int defaultServiceIndex, DocFlavor docflavor, PrintRequestAttributeSet attributeSet, + Dialog dialog, JPanel... additional) { + super(dialog, PRINT_BUTTON_LABEL, true); + setLayout(new MigLayout("fill, gap rel unrel")); + services = aPrintService; + this.defaultServiceIndex = defaultServiceIndex; + asOriginal = attributeSet; + asCurrent = new HashPrintRequestAttributeSet(attributeSet); + + if (services != null && defaultServiceIndex < services.length && defaultServiceIndex >= 0) { + psCurrent = services[defaultServiceIndex]; + } docFlavor = docflavor; Container container = getContentPane(); - container.setLayout(new BorderLayout()); + container.setLayout(new MigLayout("fill, gap rel unrel")); +// container.setLayout(new BorderLayout()); final JTabbedPane tpTabs = new JTabbedPane(); tpTabs.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -418,36 +460,39 @@ public class PrintServiceDialog extends JDialog implements ActionListener { tpTabs.add(anAdditional, anAdditional.getName(), 0); } } - pnlGeneral = new GeneralPanel(); - tpTabs.add(GENERAL_TAB_TITLE, pnlGeneral); + if (psCurrent != null) { + pnlGeneral = new GeneralPanel(); + tpTabs.add(GENERAL_TAB_TITLE, pnlGeneral); + } - container.add(tpTabs, "Center"); + container.add(tpTabs, "growx"); updatePanels(); + JPanel jpanel = new JPanel(new MigLayout()); btnPrint = createExitButton(PRINT_BUTTON_LABEL, this); jpanel.add(btnPrint, "x 300"); getRootPane().setDefaultButton(btnPrint); - btnPrint.setEnabled(pdfFlavorSupported); + btnPrint.setEnabled(pdfFlavorSupported && psCurrent != null); btnCancel = createExitButton(CANCEL_BUTTON_LABEL, this); handleEscKey(btnCancel); jpanel.add(btnCancel, "x 380"); container.add(jpanel, "South"); addWindowListener(new WindowAdapter() { - public void windowClosing (WindowEvent windowevent) { + public void windowClosing(WindowEvent windowevent) { dispose(2); } } ); setResizable(false); - setLocation(i, j); + setLocation(x, y); pack(); } - private void handleEscKey (JButton jbutton) { + private void handleEscKey(JButton jbutton) { AbstractAction abstractaction = new AbstractAction() { - public void actionPerformed (ActionEvent actionevent) { + public void actionPerformed(ActionEvent actionevent) { dispose(2); } @@ -461,11 +506,11 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } } - public int getStatus () { + public int getStatus() { return status; } - public PrintRequestAttributeSet getAttributes () { + public PrintRequestAttributeSet getAttributes() { if (status == 1) { return asCurrent; } @@ -474,7 +519,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } } - public PrintService getPrintService () { + public PrintService getPrintService() { if (status == 1) { return psCurrent; } @@ -483,12 +528,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } } - public void dispose (int i) { + public void dispose(int i) { status = i; super.dispose(); } - public void actionPerformed (ActionEvent actionevent) { + public void actionPerformed(ActionEvent actionevent) { Object obj = actionevent.getSource(); boolean flag = false; if (obj == btnPrint) { @@ -501,11 +546,13 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } - private void updatePanels () { - pnlGeneral.updateInfo(); + private void updatePanels() { + if (pnlGeneral != null) { + pnlGeneral.updateInfo(); + } } - private static char getMnemonic (String s) { + private static char getMnemonic(String s) { if (s != null && s.length() > 0) { return s.charAt(0); } @@ -514,14 +561,14 @@ public class PrintServiceDialog extends JDialog implements ActionListener { } } - private static JButton createButton (String s, ActionListener actionlistener) { + private static JButton createButton(String s, ActionListener actionlistener) { JButton jbutton = new JButton(s); jbutton.setMnemonic(getMnemonic(s)); jbutton.addActionListener(actionlistener); return jbutton; } - private static JButton createExitButton (String s, ActionListener actionlistener) { + private static JButton createExitButton(String s, ActionListener actionlistener) { JButton jbutton = new JButton(s); jbutton.addActionListener(actionlistener); jbutton.getAccessibleContext().setAccessibleDescription(s); @@ -533,27 +580,27 @@ public class PrintServiceDialog extends JDialog implements ActionListener { private Media media; - MediaWrapper (Media theMedia) { + MediaWrapper(Media theMedia) { media = theMedia; } - Media getMedia () { + Media getMedia() { return media; } - public final Class getCategory () { + public final Class getCategory() { return this.getClass(); } - public final String getName () { + public final String getName() { return "mw"; } - public String toString () { + public String toString() { return media.toString(); } - public int hashCode () { + public int hashCode() { return media.hashCode(); } -- 2.47.2