View Javadoc

1   package org.lcsim.geometry.compact.converter;
2   
3   import java.awt.Dimension;
4   import java.io.BufferedInputStream;
5   import java.io.BufferedOutputStream;
6   import java.io.File;
7   import java.io.FileInputStream;
8   import java.io.FileOutputStream;
9   import java.io.InputStream;
10  import java.io.OutputStream;
11  import java.util.List;
12  import java.util.prefs.Preferences;
13  import javax.swing.DefaultComboBoxModel;
14  import javax.swing.JFileChooser;
15  import javax.swing.JOptionPane;
16  import javax.swing.JTextField;
17  import javax.swing.UIManager;
18  import javax.swing.filechooser.FileFilter;
19  import org.lcsim.geometry.compact.converter.Main.InvalidArgumentException;
20  
21  /**
22   *
23   * @author  tonyj
24   */
25  public class MainGUI extends javax.swing.JFrame
26  {
27     private Preferences prefs = Preferences.userNodeForPackage(MainGUI.class);
28     private List<Converter> converters;
29     
30     /** Creates new form MainGUI */
31     public MainGUI(String inFile, String outFile, String format)
32     {
33        initComponents();
34        inputTextField.setText(inFile == null ? prefs.get("input","") : inFile);
35        outputTextField.setText(outFile == null ? prefs.get("output","") : outFile);
36        DefaultComboBoxModel model = new DefaultComboBoxModel();
37        converters = Main.getConverters();
38        for (Converter c : converters)
39        {
40           model.addElement(c.getOutputFormat());
41        }
42        converterComboBox.setModel(model);
43        if (format == null) format = prefs.get("format",null);
44        if (format != null) converterComboBox.setSelectedItem(format);
45     }
46     
47     /** This method is called from within the constructor to
48      * initialize the form.
49      * WARNING: Do NOT modify this code. The content of this method is
50      * always regenerated by the Form Editor.
51      */
52     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
53     private void initComponents()
54     {
55        java.awt.GridBagConstraints gridBagConstraints;
56        javax.swing.JLabel jLabel1;
57        javax.swing.JLabel jLabel2;
58        javax.swing.JLabel jLabel3;
59        javax.swing.JLabel jLabel4;
60        javax.swing.JPanel jPanel1;
61        javax.swing.JPanel jPanel2;
62        javax.swing.JSeparator jSeparator1;
63  
64        jLabel1 = new javax.swing.JLabel();
65        jSeparator1 = new javax.swing.JSeparator();
66        jPanel1 = new javax.swing.JPanel();
67        jLabel2 = new javax.swing.JLabel();
68        inputTextField = new javax.swing.JTextField();
69        inputBrowseButton = new javax.swing.JButton();
70        jLabel3 = new javax.swing.JLabel();
71        outputTextField = new javax.swing.JTextField();
72        outputBrowseButton = new javax.swing.JButton();
73        jLabel4 = new javax.swing.JLabel();
74        converterComboBox = new javax.swing.JComboBox();
75        jPanel2 = new javax.swing.JPanel();
76        convertButton = new javax.swing.JButton();
77        exitButton = new javax.swing.JButton();
78  
79        getContentPane().setLayout(new java.awt.GridBagLayout());
80  
81        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
82        setTitle("GeomConverter");
83        addWindowListener(new java.awt.event.WindowAdapter()
84        {
85           public void windowClosing(java.awt.event.WindowEvent evt)
86           {
87              MainGUI.this.windowClosing(evt);
88           }
89        });
90  
91        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
92        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
93        jLabel1.setText("GeomConverter");
94        jLabel1.setOpaque(true);
95        gridBagConstraints = new java.awt.GridBagConstraints();
96        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
97        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
98        getContentPane().add(jLabel1, gridBagConstraints);
99  
100       gridBagConstraints = new java.awt.GridBagConstraints();
101       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
102       gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
103       getContentPane().add(jSeparator1, gridBagConstraints);
104 
105       jPanel1.setLayout(new java.awt.GridBagLayout());
106 
107       jPanel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(10, 10, 10, 10)));
108       jLabel2.setText("Input");
109       gridBagConstraints = new java.awt.GridBagConstraints();
110       gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
111       gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
112       jPanel1.add(jLabel2, gridBagConstraints);
113 
114       gridBagConstraints = new java.awt.GridBagConstraints();
115       gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
116       jPanel1.add(inputTextField, gridBagConstraints);
117 
118       inputBrowseButton.setText("Browse...");
119       inputBrowseButton.addActionListener(new java.awt.event.ActionListener()
120       {
121          public void actionPerformed(java.awt.event.ActionEvent evt)
122          {
123             inputBrowseButtonActionPerformed(evt);
124          }
125       });
126 
127       gridBagConstraints = new java.awt.GridBagConstraints();
128       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
129       jPanel1.add(inputBrowseButton, gridBagConstraints);
130 
131       jLabel3.setText("Output");
132       gridBagConstraints = new java.awt.GridBagConstraints();
133       gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
134       gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
135       jPanel1.add(jLabel3, gridBagConstraints);
136 
137       gridBagConstraints = new java.awt.GridBagConstraints();
138       gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
139       gridBagConstraints.weightx = 1.0;
140       jPanel1.add(outputTextField, gridBagConstraints);
141 
142       outputBrowseButton.setText("Browse...");
143       outputBrowseButton.addActionListener(new java.awt.event.ActionListener()
144       {
145          public void actionPerformed(java.awt.event.ActionEvent evt)
146          {
147             outputBrowseButtonActionPerformed(evt);
148          }
149       });
150 
151       gridBagConstraints = new java.awt.GridBagConstraints();
152       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
153       jPanel1.add(outputBrowseButton, gridBagConstraints);
154 
155       jLabel4.setText("Format");
156       gridBagConstraints = new java.awt.GridBagConstraints();
157       gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
158       gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
159       jPanel1.add(jLabel4, gridBagConstraints);
160 
161       gridBagConstraints = new java.awt.GridBagConstraints();
162       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
163       gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
164       jPanel1.add(converterComboBox, gridBagConstraints);
165 
166       gridBagConstraints = new java.awt.GridBagConstraints();
167       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
168       gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
169       gridBagConstraints.weightx = 1.0;
170       gridBagConstraints.weighty = 1.0;
171       getContentPane().add(jPanel1, gridBagConstraints);
172 
173       convertButton.setText("Convert");
174       convertButton.addActionListener(new java.awt.event.ActionListener()
175       {
176          public void actionPerformed(java.awt.event.ActionEvent evt)
177          {
178             convertButtonActionPerformed(evt);
179          }
180       });
181 
182       jPanel2.add(convertButton);
183 
184       exitButton.setText("Exit");
185       exitButton.addActionListener(new java.awt.event.ActionListener()
186       {
187          public void actionPerformed(java.awt.event.ActionEvent evt)
188          {
189             exitButtonActionPerformed(evt);
190          }
191       });
192 
193       jPanel2.add(exitButton);
194 
195       gridBagConstraints = new java.awt.GridBagConstraints();
196       gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
197       getContentPane().add(jPanel2, gridBagConstraints);
198 
199       pack();
200    }
201    // </editor-fold>//GEN-END:initComponents
202 
203    private void convertButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_convertButtonActionPerformed
204    {//GEN-HEADEREND:event_convertButtonActionPerformed
205       try
206       {
207          String inFile = inputTextField.getText();
208          InputStream in = new BufferedInputStream(new FileInputStream(inFile));
209          String outFile = outputTextField.getText();
210          OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
211          getCurrentConverter().convert(inFile,in,out);
212          out.close();
213          in.close();
214          // No Exception
215          JOptionPane.showMessageDialog(this,"Conversion successful","Success",JOptionPane.INFORMATION_MESSAGE);
216       }
217       catch (Exception x)
218       {
219          x.printStackTrace();
220          JOptionPane.showMessageDialog(this,x.toString(),"Error",JOptionPane.ERROR_MESSAGE);
221       }
222    }//GEN-LAST:event_convertButtonActionPerformed
223 
224    private void outputBrowseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_outputBrowseButtonActionPerformed
225    {//GEN-HEADEREND:event_outputBrowseButtonActionPerformed
226       doBrowse(outputTextField, false, getCurrentConverter().getFileFilter());
227    }//GEN-LAST:event_outputBrowseButtonActionPerformed
228    
229    private void windowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_windowClosing
230    {//GEN-HEADEREND:event_windowClosing
231       exit();
232    }//GEN-LAST:event_windowClosing
233    
234    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
235    {//GEN-HEADEREND:event_exitButtonActionPerformed
236       exit();
237    }//GEN-LAST:event_exitButtonActionPerformed
238    
239    private void inputBrowseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_inputBrowseButtonActionPerformed
240    {//GEN-HEADEREND:event_inputBrowseButtonActionPerformed
241       doBrowse(inputTextField,true,new CompactFileFilter());
242    }//GEN-LAST:event_inputBrowseButtonActionPerformed
243    
244    private void doBrowse(JTextField field, boolean input, FileFilter filter)
245    {
246       JFileChooser chooser = new JFileChooser();
247       chooser.setCurrentDirectory(new File(field.getText()));
248       chooser.addChoosableFileFilter(filter);
249       chooser.setApproveButtonText("Select");
250       chooser.setApproveButtonMnemonic('S');
251       int rc = input ? chooser.showOpenDialog(this) : chooser.showSaveDialog(this);
252       if (rc == JFileChooser.APPROVE_OPTION)
253       {
254          field.setText(chooser.getSelectedFile().getAbsolutePath());
255       }      
256    }
257    private Converter getCurrentConverter()
258    {
259       return converters.get(converterComboBox.getSelectedIndex());
260    }
261    /**
262     * @param args the command line arguments
263     */
264    public static void main(String args[])
265    {
266       try
267       {
268          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
269       }
270       catch (Exception x)
271       {
272          // Too bad
273       }
274       Main main = new Main();
275       try
276       {
277          main.parseArgs(args);
278       }
279       catch (InvalidArgumentException x)
280       {
281          String message = x.getMessage();
282          if (message != null) System.err.println(message);
283       }
284       MainGUI gui = new MainGUI(main.getInputFile(), main.getOutputFile(), main.getFormat());
285       gui.setSize(new Dimension(500,300));
286       gui.setLocationByPlatform(true);
287       gui.setVisible(true);
288    }
289    private void exit()
290    {
291       prefs.put("input",inputTextField.getText());
292       prefs.put("output", outputTextField.getText());
293       prefs.put("format", converterComboBox.getSelectedItem().toString());
294       System.exit(0);
295    }
296    
297    
298    // Variables declaration - do not modify//GEN-BEGIN:variables
299    private javax.swing.JButton convertButton;
300    private javax.swing.JComboBox converterComboBox;
301    private javax.swing.JButton exitButton;
302    private javax.swing.JButton inputBrowseButton;
303    private javax.swing.JTextField inputTextField;
304    private javax.swing.JButton outputBrowseButton;
305    private javax.swing.JTextField outputTextField;
306    // End of variables declaration//GEN-END:variables
307    
308    
309    private static class CompactFileFilter extends FileFilter
310    {
311       public boolean accept(java.io.File file)
312       {
313          return file.isDirectory() || file.getName().endsWith(".xml");
314       }
315       
316       public String getDescription()
317       {
318          return "Compact Geometry Description (*.xml)";
319       }
320    }
321 }