]> git.gag.com Git - debian/openrocket/blobdiff - src/net/sf/openrocket/gui/print/PrintServiceDialog.java
DGP - Partial workaround for systems with no print services; simplification of the...
[debian/openrocket] / src / net / sf / openrocket / gui / print / PrintServiceDialog.java
index 98045f88a1fdd121d56793e24df960e3a807d089..4aedabac5de668cefc99cc23521aff640a9d0eec 100644 (file)
@@ -12,14 +12,32 @@ import javax.print.ServiceUIFactory;
 import javax.print.attribute.HashPrintRequestAttributeSet;\r
 import javax.print.attribute.PrintRequestAttribute;\r
 import javax.print.attribute.PrintRequestAttributeSet;\r
-import javax.print.attribute.PrintServiceAttribute;\r
-import javax.print.attribute.standard.*;\r
-import javax.swing.*;\r
+import javax.print.attribute.standard.Destination;\r
+import javax.print.attribute.standard.Media;\r
+import javax.print.attribute.standard.MediaSizeName;\r
+import javax.print.attribute.standard.MediaTray;\r
+import javax.swing.AbstractAction;\r
+import javax.swing.ActionMap;\r
+import javax.swing.BorderFactory;\r
+import javax.swing.InputMap;\r
+import javax.swing.JButton;\r
+import javax.swing.JComboBox;\r
+import javax.swing.JDialog;\r
+import javax.swing.JLabel;\r
+import javax.swing.JPanel;\r
+import javax.swing.JTabbedPane;\r
+import javax.swing.KeyStroke;\r
 import javax.swing.border.EmptyBorder;\r
 import javax.swing.event.PopupMenuEvent;\r
 import javax.swing.event.PopupMenuListener;\r
-import java.awt.*;\r
-import java.awt.event.*;\r
+import java.awt.Container;\r
+import java.awt.Dialog;\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ItemEvent;\r
+import java.awt.event.ItemListener;\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
 import java.util.ArrayList;\r
 import java.util.Iterator;\r
 import java.util.Set;\r
@@ -31,7 +49,8 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
     private JButton btnCancel, btnPrint;\r
     private boolean pdfFlavorSupported = true;\r
     private PrintService services[];\r
-    private int defaultServiceIndex, status;\r
+    private int defaultServiceIndex = -1;\r
+    private int status;\r
     private PrintRequestAttributeSet asOriginal;\r
     private HashPrintRequestAttributeSet asCurrent;\r
     private PrintService psCurrent;\r
@@ -53,15 +72,15 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         private ArrayList<MediaSizeName> sizes;\r
         private ArrayList sources;\r
 \r
-        private String getMediaName (String s) {\r
+        private String getMediaName(String s) {\r
             String s1 = s.replace(' ', '-');\r
             s1 = s1.replace('#', 'n');\r
             return PaperSize.toDisplayable(s1);\r
         }\r
 \r
-        public void itemStateChanged (ItemEvent itemevent) {\r
+        public void itemStateChanged(ItemEvent itemevent) {\r
             Object obj = itemevent.getSource();\r
-            if (itemevent.getStateChange() == 1) {\r
+            if (itemevent.getStateChange() == ItemEvent.SELECTED) {\r
                 if (obj == cbSize) {\r
                     int i = cbSize.getSelectedIndex();\r
                     if (i >= 0 && i < sizes.size()) {\r
@@ -91,7 +110,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         }\r
 \r
 \r
-        public void updateInfo () {\r
+        public void updateInfo() {\r
             boolean flag = false;\r
             cbSize.removeItemListener(this);\r
             cbSize.removeAllItems();\r
@@ -100,7 +119,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             cbSource.addItem(getMediaName("auto-select"));\r
             sizes.clear();\r
             sources.clear();\r
-            if (psCurrent.isAttributeCategorySupported(Media.class)) {\r
+            if (psCurrent != null && psCurrent.isAttributeCategorySupported(Media.class)) {\r
                 flag = true;\r
                 Object obj = null;\r
                 try {\r
@@ -108,7 +127,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
                 }\r
                 catch (IllegalArgumentException iae) {\r
                     pdfFlavorSupported = false;\r
-                                             //dgp\r
+                    //dgp\r
                 }\r
                 if (obj instanceof Media[]) {\r
                     Media amedia[] = (Media[]) obj;\r
@@ -141,15 +160,16 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             cbSize.setEnabled(flag1);\r
             cbSource.setEnabled(false);\r
             lblSource.setEnabled(false);\r
-            if (flag) {\r
+            if (flag && psCurrent != null) {\r
                 Media media = (Media) asCurrent.get(Media.class);\r
                 boolean attributeValueSupported = false;\r
                 try {\r
-                    attributeValueSupported = psCurrent.isAttributeValueSupported(media, docFlavor, asCurrent);\r
+                    attributeValueSupported = media == null ? false : psCurrent.isAttributeValueSupported(media,\r
+                                                                                                          docFlavor,\r
+                                                                                                          asCurrent);\r
                 }\r
                 catch (IllegalArgumentException iae) {\r
                     pdfFlavorSupported = false;\r
-                    //dgp\r
                 }\r
                 if (media == null || !attributeValueSupported) {\r
                     media = (Media) psCurrent.getDefaultAttributeValue(Media.class);\r
@@ -187,7 +207,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             cbSource.addItemListener(this);\r
         }\r
 \r
-        public MediaPanel () {\r
+        public MediaPanel() {\r
             super(new MigLayout("fill, gap rel unrel"));\r
             sizes = new ArrayList<MediaSizeName>();\r
             sources = new ArrayList();\r
@@ -223,12 +243,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
              * Returns a string value corresponding to this enumeration value.\r
              */\r
             @Override\r
-            public String toString () {\r
+            public String toString() {\r
                 return displayableName;\r
             }\r
 \r
             @Override\r
-            public int compareTo (final Object o) {\r
+            public int compareTo(final Object o) {\r
                 String name = displayableName;\r
                 if (name != null) {\r
                     return name.compareTo(o.toString());\r
@@ -237,7 +257,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             }\r
 \r
             @Override\r
-            public boolean equals (final Object o) {\r
+            public boolean equals(final Object o) {\r
                 if (this == o) {\r
                     return true;\r
                 }\r
@@ -251,7 +271,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             }\r
 \r
             @Override\r
-            public int hashCode () {\r
+            public int hashCode() {\r
                 return displayableName.hashCode();\r
             }\r
 \r
@@ -267,15 +287,15 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         private final String strTitle = "Print Service";\r
         private JButton btnProperties;\r
         private JComboBox cbName;\r
-        private JLabel lblType, lblStatus, lblInfo;\r
-        private JLabel vType, vStatus, vInfo;\r
         private ServiceUIFactory uiFactory;\r
         private boolean changedService;\r
 \r
-        public PrintServicePanel () {\r
+        public PrintServicePanel() {\r
             super(new MigLayout("fill, gap rel unrel"));\r
             changedService = false;\r
-            uiFactory = psCurrent.getServiceUIFactory();\r
+            if (psCurrent != null) {\r
+                uiFactory = psCurrent.getServiceUIFactory();\r
+            }\r
             setBorder(BorderFactory.createTitledBorder(strTitle));\r
             String as[] = new String[services.length];\r
             for (int i = 0; i < as.length; i++) {\r
@@ -283,7 +303,9 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             }\r
 \r
             cbName = new JComboBox(as);\r
-            cbName.setSelectedIndex(defaultServiceIndex);\r
+            if (defaultServiceIndex != -1 && defaultServiceIndex < services.length) {\r
+                cbName.setSelectedIndex(defaultServiceIndex);\r
+            }\r
             cbName.addItemListener(this);\r
             cbName.addPopupMenuListener(this);\r
             JLabel jlabel = new JLabel(("Name:"), 11);\r
@@ -293,24 +315,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             add(cbName);\r
             btnProperties = PrintServiceDialog.createButton("Properties...", this);\r
             add(btnProperties, "wrap");\r
-            lblStatus = new JLabel("Status:", 11);\r
-            add(lblStatus);\r
-            vStatus = new JLabel();\r
-            add(vStatus, "wrap");\r
-            lblType = new JLabel("Type:", 11);\r
-            vType = new JLabel();\r
-            add(lblType);\r
-            add(vType, "wrap");\r
-            lblInfo = new JLabel("Info:", 11);\r
-            vInfo = new JLabel();\r
-            add(lblInfo);\r
-            add(vInfo, "wrap");\r
-        }\r
-\r
-        public void actionPerformed (ActionEvent actionevent) {\r
+        }\r
+\r
+        public void actionPerformed(ActionEvent actionevent) {\r
             Object obj = actionevent.getSource();\r
             if (obj == btnProperties && uiFactory != null) {\r
-                JDialog jdialog = (JDialog) uiFactory.getUI(3, "javax.swing.JDialog");\r
+                JDialog jdialog = (JDialog) uiFactory.getUI(ServiceUIFactory.MAIN_UIROLE, "javax.swing.JDialog");\r
                 if (jdialog != null) {\r
                     jdialog.show();\r
                 }\r
@@ -320,96 +330,128 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
             }\r
         }\r
 \r
-        public void itemStateChanged (ItemEvent itemevent) {\r
-            if (itemevent.getStateChange() == 1) {\r
+        /**\r
+         * {@inheritDoc}\r
+         *\r
+         * @param itemevent  the event that indicates what changed\r
+         */\r
+        @Override\r
+        public void itemStateChanged(ItemEvent itemevent) {\r
+            if (itemevent.getStateChange() == ItemEvent.SELECTED) {\r
                 int i = cbName.getSelectedIndex();\r
-                if (i >= 0 && i < services.length && !services[i].equals(psCurrent)) {\r
+                if (services != null && i >= 0 && i < services.length && !services[i].equals(psCurrent)) {\r
                     psCurrent = services[i];\r
                     uiFactory = psCurrent.getServiceUIFactory();\r
                     changedService = true;\r
-                    Destination destination = (Destination) asOriginal.get(\r
-                            Destination.class);\r
-                    if ((destination != null) && psCurrent.isAttributeCategorySupported(\r
-                            Destination.class)) {\r
-                        asCurrent.add(destination);\r
-                    }\r
-                    else {\r
-                        asCurrent.remove(Destination.class);\r
+                    if (asOriginal != null) {\r
+                        Destination destination = (Destination) asOriginal.get(\r
+                                Destination.class);\r
+                        if ((destination != null) && psCurrent.isAttributeCategorySupported(\r
+                                Destination.class)) {\r
+                            asCurrent.add(destination);\r
+                        }\r
+                        else {\r
+                            asCurrent.remove(Destination.class);\r
+                        }\r
                     }\r
                 }\r
             }\r
         }\r
 \r
-        public void popupMenuWillBecomeVisible (PopupMenuEvent popupmenuevent) {\r
+        /**\r
+         * {@inheritDoc}\r
+         *\r
+         * @param popupmenuevent\r
+         */\r
+        @Override\r
+        public void popupMenuWillBecomeVisible(PopupMenuEvent popupmenuevent) {\r
             changedService = false;\r
         }\r
 \r
-        public void popupMenuWillBecomeInvisible (PopupMenuEvent popupmenuevent) {\r
+        /**\r
+         * {@inheritDoc}\r
+         *\r
+         * @param popupmenuevent\r
+         */\r
+        @Override\r
+        public void popupMenuWillBecomeInvisible(PopupMenuEvent popupmenuevent) {\r
             if (changedService) {\r
                 changedService = false;\r
                 updatePanels();\r
             }\r
         }\r
 \r
-        public void popupMenuCanceled (PopupMenuEvent popupmenuevent) {\r
+        /**\r
+         * {@inheritDoc}\r
+         *\r
+         * @param popupmenuevent\r
+         */\r
+        @Override\r
+        public void popupMenuCanceled(PopupMenuEvent popupmenuevent) {\r
         }\r
 \r
-        public void updateInfo () {\r
-            PrintServiceAttribute psa = psCurrent.getAttribute(PrinterMakeAndModel.class);\r
-            if (psa != null) {\r
-                vType.setText(psa.toString());\r
-            }\r
-            psa = psCurrent.getAttribute(PrinterIsAcceptingJobs.class);\r
-            if (psa != null) {\r
-                vStatus.setText((psa.toString()));\r
-            }\r
-            psa = psCurrent.getAttribute(PrinterInfo.class);\r
-            if (psa != null) {\r
-                vInfo.setText(psa.toString());\r
-            }\r
+        /**\r
+         * Modify the enablement of the properties button.\r
+         */\r
+        public void updateInfo() {\r
             btnProperties.setEnabled(uiFactory != null);\r
         }\r
 \r
     }\r
 \r
+    /**\r
+     * The panel for general print services info.\r
+     */\r
     private class GeneralPanel extends JPanel {\r
 \r
-        public void updateInfo () {\r
-            pnlPrintService.updateInfo();\r
-            pnlMedia.updateInfo();\r
-        }\r
-\r
         private PrintServicePanel pnlPrintService;\r
         private MediaPanel pnlMedia;\r
 \r
-        public GeneralPanel () {\r
+        public GeneralPanel() {\r
             super(new MigLayout("fill, gap rel unrel"));\r
             pnlPrintService = new PrintServicePanel();\r
             add(pnlPrintService, "wrap");\r
             pnlMedia = new MediaPanel();\r
             add(pnlMedia, "wrap");\r
         }\r
-    }\r
-\r
 \r
-    public PrintServiceDialog (GraphicsConfiguration graphicsconfiguration, int i, int j, PrintService aprintservice[],\r
-                               int k, DocFlavor docflavor, PrintRequestAttributeSet printrequestattributeset,\r
-                               Dialog dialog, JPanel... additional) {\r
-        super(dialog, PRINT_BUTTON_LABEL, true, graphicsconfiguration);\r
-        initPrintDialog(i, j, aprintservice, k, docflavor, printrequestattributeset, additional);\r
+        public void updateInfo() {\r
+            pnlPrintService.updateInfo();\r
+            pnlMedia.updateInfo();\r
+        }\r
     }\r
 \r
-    void initPrintDialog (int i, int j, PrintService aprintservice[], int k, DocFlavor docflavor,\r
-                          PrintRequestAttributeSet printrequestattributeset,\r
-                          JPanel... additional) {\r
-        services = aprintservice;\r
-        defaultServiceIndex = k;\r
-        asOriginal = printrequestattributeset;\r
-        asCurrent = new HashPrintRequestAttributeSet(printrequestattributeset);\r
-        psCurrent = aprintservice[k];\r
+    /**\r
+     * Constructor.\r
+     *\r
+     * @param x the <i>x</i>-coordinate of the new location's\r
+     *          top-left corner in the parent's coordinate space\r
+     * @param y the <i>y</i>-coordinate of the new location's\r
+     *          top-left corner in the parent's coordinate space\r
+     * @param aPrintService  the array of installed print services\r
+     * @param defaultServiceIndex  the default service index (index into aPrintService)\r
+     * @param docflavor  the document flavor (i.e. PDF)\r
+     * @param attributeSet  the set of required attributes\r
+     * @param dialog  the parent\r
+     * @param additional  other panels to add in tabs\r
+     */\r
+    public PrintServiceDialog(int x, int y, PrintService[] aPrintService,\r
+                              int defaultServiceIndex, DocFlavor docflavor, PrintRequestAttributeSet attributeSet,\r
+                              Dialog dialog, JPanel... additional) {\r
+        super(dialog, PRINT_BUTTON_LABEL, true);\r
+        setLayout(new MigLayout("fill, gap rel unrel"));\r
+        services = aPrintService;\r
+        this.defaultServiceIndex = defaultServiceIndex;\r
+        asOriginal = attributeSet;\r
+        asCurrent = new HashPrintRequestAttributeSet(attributeSet);\r
+\r
+        if (services != null && defaultServiceIndex < services.length && defaultServiceIndex >= 0) {\r
+            psCurrent = services[defaultServiceIndex];\r
+        }\r
         docFlavor = docflavor;\r
         Container container = getContentPane();\r
-        container.setLayout(new BorderLayout());\r
+        container.setLayout(new MigLayout("fill, gap rel unrel"));\r
+//        container.setLayout(new BorderLayout());\r
         final JTabbedPane tpTabs = new JTabbedPane();\r
         tpTabs.setBorder(new EmptyBorder(5, 5, 5, 5));\r
 \r
@@ -418,36 +460,39 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
                 tpTabs.add(anAdditional, anAdditional.getName(), 0);\r
             }\r
         }\r
-        pnlGeneral = new GeneralPanel();\r
-        tpTabs.add(GENERAL_TAB_TITLE, pnlGeneral);\r
+        if (psCurrent != null) {\r
+            pnlGeneral = new GeneralPanel();\r
+            tpTabs.add(GENERAL_TAB_TITLE, pnlGeneral);\r
+        }\r
 \r
-        container.add(tpTabs, "Center");\r
+        container.add(tpTabs, "growx");\r
         updatePanels();\r
+\r
         JPanel jpanel = new JPanel(new MigLayout());\r
         btnPrint = createExitButton(PRINT_BUTTON_LABEL, this);\r
         jpanel.add(btnPrint, "x 300");\r
         getRootPane().setDefaultButton(btnPrint);\r
-        btnPrint.setEnabled(pdfFlavorSupported);\r
+        btnPrint.setEnabled(pdfFlavorSupported && psCurrent != null);\r
 \r
         btnCancel = createExitButton(CANCEL_BUTTON_LABEL, this);\r
         handleEscKey(btnCancel);\r
         jpanel.add(btnCancel, "x 380");\r
         container.add(jpanel, "South");\r
         addWindowListener(new WindowAdapter() {\r
-            public void windowClosing (WindowEvent windowevent) {\r
+            public void windowClosing(WindowEvent windowevent) {\r
                 dispose(2);\r
             }\r
         }\r
         );\r
         setResizable(false);\r
-        setLocation(i, j);\r
+        setLocation(x, y);\r
         pack();\r
     }\r
 \r
-    private void handleEscKey (JButton jbutton) {\r
+    private void handleEscKey(JButton jbutton) {\r
         AbstractAction abstractaction = new AbstractAction() {\r
 \r
-            public void actionPerformed (ActionEvent actionevent) {\r
+            public void actionPerformed(ActionEvent actionevent) {\r
                 dispose(2);\r
             }\r
 \r
@@ -461,11 +506,11 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         }\r
     }\r
 \r
-    public int getStatus () {\r
+    public int getStatus() {\r
         return status;\r
     }\r
 \r
-    public PrintRequestAttributeSet getAttributes () {\r
+    public PrintRequestAttributeSet getAttributes() {\r
         if (status == 1) {\r
             return asCurrent;\r
         }\r
@@ -474,7 +519,7 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         }\r
     }\r
 \r
-    public PrintService getPrintService () {\r
+    public PrintService getPrintService() {\r
         if (status == 1) {\r
             return psCurrent;\r
         }\r
@@ -483,12 +528,12 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         }\r
     }\r
 \r
-    public void dispose (int i) {\r
+    public void dispose(int i) {\r
         status = i;\r
         super.dispose();\r
     }\r
 \r
-    public void actionPerformed (ActionEvent actionevent) {\r
+    public void actionPerformed(ActionEvent actionevent) {\r
         Object obj = actionevent.getSource();\r
         boolean flag = false;\r
         if (obj == btnPrint) {\r
@@ -501,11 +546,13 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
     }\r
 \r
 \r
-    private void updatePanels () {\r
-        pnlGeneral.updateInfo();\r
+    private void updatePanels() {\r
+        if (pnlGeneral != null) {\r
+            pnlGeneral.updateInfo();\r
+        }\r
     }\r
 \r
-    private static char getMnemonic (String s) {\r
+    private static char getMnemonic(String s) {\r
         if (s != null && s.length() > 0) {\r
             return s.charAt(0);\r
         }\r
@@ -514,14 +561,14 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
         }\r
     }\r
 \r
-    private static JButton createButton (String s, ActionListener actionlistener) {\r
+    private static JButton createButton(String s, ActionListener actionlistener) {\r
         JButton jbutton = new JButton(s);\r
         jbutton.setMnemonic(getMnemonic(s));\r
         jbutton.addActionListener(actionlistener);\r
         return jbutton;\r
     }\r
 \r
-    private static JButton createExitButton (String s, ActionListener actionlistener) {\r
+    private static JButton createExitButton(String s, ActionListener actionlistener) {\r
         JButton jbutton = new JButton(s);\r
         jbutton.addActionListener(actionlistener);\r
         jbutton.getAccessibleContext().setAccessibleDescription(s);\r
@@ -533,27 +580,27 @@ public class PrintServiceDialog extends JDialog implements ActionListener {
 \r
         private Media media;\r
 \r
-        MediaWrapper (Media theMedia) {\r
+        MediaWrapper(Media theMedia) {\r
             media = theMedia;\r
         }\r
 \r
-        Media getMedia () {\r
+        Media getMedia() {\r
             return media;\r
         }\r
 \r
-        public final Class getCategory () {\r
+        public final Class getCategory() {\r
             return this.getClass();\r
         }\r
 \r
-        public final String getName () {\r
+        public final String getName() {\r
             return "mw";\r
         }\r
 \r
-        public String toString () {\r
+        public String toString() {\r
             return media.toString();\r
         }\r
 \r
-        public int hashCode () {\r
+        public int hashCode() {\r
             return media.hashCode();\r
         }\r
 \r