View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst;
24  
25  import java.awt.BorderLayout;
26  import java.awt.Color;
27  import java.awt.Container;
28  import java.awt.Dimension;
29  import java.awt.FlowLayout;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.io.File;
33  import java.util.LinkedList;
34  import javax.swing.BoxLayout;
35  import javax.swing.ButtonGroup;
36  import javax.swing.ImageIcon;
37  import javax.swing.JButton;
38  import javax.swing.JComboBox;
39  import javax.swing.JDialog;
40  import javax.swing.JFileChooser;
41  import javax.swing.JLabel;
42  import javax.swing.JOptionPane;
43  import javax.swing.JPanel;
44  import javax.swing.JRadioButton;
45  import javax.swing.JTextField;
46  import javax.swing.JCheckBox;
47  import javax.swing.event.ChangeEvent;
48  import javax.swing.event.ChangeListener;
49  import org.nuiton.util.Resource;
50  
51  import static org.nuiton.i18n.I18n.t;
52  
53  /**
54   * JRST main interface.
55   *
56   * @author chatellier
57   * @version $Revision$
58   * 
59   * Last update : $Date$
60   * By : $Author$
61   */
62  public class JRSTInterface extends JDialog {
63      
64      /** serialVersionUID */
65      private static final long serialVersionUID = 5327326730753891936L;
66  
67      private JPanel savePanel;
68  
69      private JPanel formatPanel;
70  
71      private JPanel openPanel;
72  
73      private JPanel formatLigne;
74  
75      private JPanel xslLigne;
76  
77      private JComboBox formatList;
78  
79      private JButton boutonAnnuler;
80  
81      private JButton boutonConvertir;
82  
83      private JPanel panelPrincipal;
84  
85      private JPanel boutonPanel;
86  
87      private JPanel simpleModePanel;
88  
89      private JButton boutonSaveLocation;
90  
91      private JLabel errorLbl;
92  
93      private JButton boutonOpenLocation;
94  
95      private JButton boutonXslLocation;
96  
97      private String[] listFormats;
98  
99      private JTextField saveText;
100 
101     private JTextField openText;
102 
103     private JTextField xslText;
104 
105     private JRadioButton format;
106 
107     private JRadioButton xslRadio;
108 
109     private JCheckBox simpleModeChechBox;
110 
111     private LinkedList<JPanel> ListAddXslPanel;
112 
113     private LinkedList<JTextField> ListXslText;
114 
115     private LinkedList<JButton> ListXslBouton;
116 
117     private LinkedList<JButton> ListXslBoutonLocation;
118     private boolean ecrase;
119     private boolean simpleMode;
120     private String[] commande;
121 
122     private ImageIcon open = Resource.getIcon("icone/open.png");
123     private ImageIcon delete = Resource.getIcon("icone/cancel.png");
124     private ImageIcon more = Resource.getIcon("icone/more.gif");
125 
126     private LinkedList<Container> composantsXSL;
127 
128     /**
129      * le parametre initialise les options disponibles
130      * 
131      * @param o option
132      */
133     public JRSTInterface(String o) {
134 
135         setFormats(o);
136         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
137         setTitle("JRST");
138         setLayout(new BorderLayout());
139         add(getPanelPrincipal(), BorderLayout.CENTER);
140         // pour que JRST attende que cette fenetre soit ferme
141         setModal(true);
142         setResizable(false);
143         pack();
144         setLocationRelativeTo(null);
145         setVisible(true);
146     }
147 
148     private JPanel getPanelPrincipal() {
149         if (panelPrincipal == null) {
150 
151             panelPrincipal = new JPanel();
152             panelPrincipal.setLayout(new BoxLayout(panelPrincipal,
153                     BoxLayout.Y_AXIS));
154             panelPrincipal.add(getErrorLabel());
155             panelPrincipal.add(getOuvrirPanel());
156             panelPrincipal.add(getSavePanel());
157             panelPrincipal.add(getFormatPanel());
158             panelPrincipal.add(getSimpleModePanel());
159             // panelPrincipal.add(getConvertPanel());
160             panelPrincipal.add(getBoutonPanel());
161 
162         }
163         return panelPrincipal;
164     }
165 
166     private JLabel getErrorLabel() {
167         if (errorLbl == null) {
168             errorLbl = new JLabel("");
169         }
170         return errorLbl;
171     }
172 
173     private JPanel getBoutonPanel() {
174 
175         if (boutonPanel == null) {
176             boutonPanel = new JPanel();
177             boutonPanel.setLayout(new FlowLayout());
178             boutonPanel.add(getBoutonAnnuler());
179             boutonPanel.add(getBoutonConvertir());
180 
181         }
182         return boutonPanel;
183     }
184 
185     private JCheckBox getSimpleCheckBox() {
186         if (simpleModeChechBox == null) {
187             simpleModeChechBox = new JCheckBox("Simple mode"); //simpleMode
188             simpleModeChechBox.addActionListener(new ActionListener() {
189                 public void actionPerformed(ActionEvent e) {
190                     setMode();
191                 }
192             });
193         }
194         return simpleModeChechBox;
195     }
196 
197     private JPanel getSimpleModePanel() {
198 
199         if (simpleModePanel == null) {
200             simpleModePanel = new JPanel();
201             simpleModePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
202             simpleModePanel.add(getSimpleCheckBox());
203 
204         }
205         return simpleModePanel;
206     }
207 
208     private JButton getBoutonConvertir() {
209         if (boutonConvertir == null) {
210             boutonConvertir = new JButton();
211             boutonConvertir.setText(t("btnConvert"));
212             boutonConvertir.addActionListener(new ActionListener() {
213                 public void actionPerformed(ActionEvent e) {
214                     convert();
215                 }
216             });
217         }
218         return boutonConvertir;
219     }
220 
221     private JButton getBoutonAnnuler() {
222         if (boutonAnnuler == null) {
223             boutonAnnuler = new JButton();
224             boutonAnnuler.setText(t("btnCancel"));
225             boutonAnnuler.addActionListener(new ActionListener() {
226                 public void actionPerformed(ActionEvent e) {
227                     annuler();
228                 }
229             });
230         }
231         return boutonAnnuler;
232     }
233 
234     private JPanel getFormatPanel() {
235         if (formatPanel == null) {
236             composantsXSL = new LinkedList<Container>();
237             ListAddXslPanel = new LinkedList<JPanel>();
238             ListXslBouton = new LinkedList<JButton>();
239             ListXslText = new LinkedList<JTextField>();
240             ListXslBoutonLocation = new LinkedList<JButton>();
241             formatPanel = new JPanel();
242             formatPanel.setLayout(new BoxLayout(formatPanel, BoxLayout.Y_AXIS));
243             ButtonGroup group = new ButtonGroup();
244             group.add(getFormat());
245             group.add(getXslRadio());
246             formatPanel.add(getFormatLigne());
247             formatPanel.add(getXslPanel());
248             for (JPanel p : getListAddXslPanel())
249                 formatPanel.add(p);
250             // formatPanel.add(getAddXslPanel());
251         }
252         return formatPanel;
253     }
254 
255     private LinkedList<JPanel> getListAddXslPanel() {
256         if (ListAddXslPanel.size() == 0) {
257 
258             ListAddXslPanel.add(ajoutXSL());
259             getFormatList().setEnabled(getFormat().isSelected());
260             for (Container c : composantsXSL)
261                 c.setEnabled(!getFormat().isSelected());
262         }
263         return ListAddXslPanel;
264     }
265 
266     private JPanel ajoutXSL() {
267         JButton b = new JButton(more);
268         b.setPreferredSize(new Dimension(20, 20));
269 
270         b.addActionListener(new ActionListener() {
271             public void actionPerformed(ActionEvent e) {
272                 JButton b = (JButton) e.getSource();
273                 if (b.getIcon() == more)
274 
275                     ajout();
276                 else
277                     supprimerXslLigne((JButton) e.getSource());
278             }
279         });
280 
281         composantsXSL.add(b);
282         ListXslBouton.add(b);
283         JTextField t = new JTextField();
284         t.setColumns(40);
285         t.setVisible(false);
286         composantsXSL.add(t);
287         ListXslText.add(t);
288         JButton b2 = new JButton(open);
289         b2.setPreferredSize(new Dimension(30, 30));
290         b2.addActionListener(new ActionListener() {
291             public void actionPerformed(ActionEvent e) {
292 
293                 openXslLocation((JButton) e.getSource());
294 
295             }
296 
297         });
298 
299         b2.setVisible(false);
300         composantsXSL.add(b2);
301         ListXslBoutonLocation.add(b2);
302         JPanel p = new JPanel();
303         p.setLayout(new FlowLayout(FlowLayout.LEFT));
304         p.add(b);
305         p.add(t);
306         p.add(b2);
307         return p;
308     }
309 
310     private void supprimerXslLigne(JButton button) {
311         boolean done = false;
312         for (int i = 0; i < ListXslBouton.size() && !done; i++) {
313             if (ListXslBouton.get(i) == button) {
314                 done = true;
315                 ListAddXslPanel.get(i).setVisible(false);
316                 ListXslBouton.remove(i);
317                 ListAddXslPanel.remove(i);
318                 ListXslBoutonLocation.remove(i);
319                 ListXslText.remove(i);
320                 pack();
321 
322             }
323         }
324 
325     }
326 
327     private void ajout() {
328         ListXslBoutonLocation.getLast().setVisible(true);
329         ListXslText.getLast().setVisible(true);
330         ListXslBouton.getLast().setIcon(delete);
331         ListAddXslPanel.add(ajoutXSL());
332         formatPanel.add(ListAddXslPanel.getLast());
333         pack();
334 
335     }
336 
337     private JPanel getFormatLigne() {
338         if (formatLigne == null) {
339             formatLigne = new JPanel();
340             formatLigne.setLayout(new FlowLayout(FlowLayout.LEADING));
341             formatLigne.add(getFormat());
342             formatLigne.add(getFormatList());
343 
344         }
345         return formatLigne;
346     }
347 
348     private JPanel getXslPanel() {
349         if (xslLigne == null) {
350             xslLigne = new JPanel();
351             xslLigne.setLayout(new FlowLayout(FlowLayout.TRAILING));
352             xslLigne.add(getXslRadio());
353             xslLigne.add(getXslText());
354             xslLigne.add(getBoutonXslLocation());
355 
356         }
357         return xslLigne;
358     }
359 
360     private JButton getBoutonXslLocation() {
361         if (boutonXslLocation == null) {
362             boutonXslLocation = new JButton(open);
363             boutonXslLocation.setPreferredSize(new Dimension(30, 30));
364             boutonXslLocation.addActionListener(new ActionListener() {
365                 public void actionPerformed(ActionEvent e) {
366                     openXslLocation((JButton) e.getSource());
367                 }
368 
369             });
370             boutonXslLocation.setEnabled(false);
371             ListXslBoutonLocation.add(boutonXslLocation);
372             composantsXSL.add(boutonXslLocation);
373         }
374         return boutonXslLocation;
375     }
376 
377     private void openXslLocation(JButton b) {
378         JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
379         fc.showOpenDialog(this);
380         File file = fc.getSelectedFile();
381         if (file != null) {
382             int i = 0;
383             for (JButton btmp : ListXslBoutonLocation) {
384                 if (btmp == b) {
385                     ListXslText.get(i).setText(file.getAbsolutePath());
386                 }
387 
388                 i++;
389             }
390         }
391     }
392 
393     private JTextField getXslText() {
394         if (xslText == null) {
395             xslText = new JTextField();
396             xslText.setColumns(30);
397             xslText.setEnabled(false);
398             composantsXSL.add(xslText);
399             ListXslText.add(xslText);
400         }
401         return xslText;
402     }
403 
404     private JRadioButton getFormat() {
405         if (format == null) {
406             format = new JRadioButton("Format : ");
407             format.setSelected(true);
408             format.addChangeListener(new ChangeListener() {
409 
410                 public void stateChanged(ChangeEvent e) {
411                     formatEnable();
412 
413                 }
414 
415             });
416         }
417         return format;
418     }
419 
420     private JRadioButton getXslRadio() {
421         if (xslRadio == null) {
422             xslRadio = new JRadioButton(t("externalXSL"));
423         }
424         return xslRadio;
425     }
426 
427     private void formatEnable() {
428         getFormatList().setEnabled(getFormat().isSelected());
429         for (Container c : composantsXSL)
430             c.setEnabled(!getFormat().isSelected());
431     }
432 
433     private JComboBox getFormatList() {
434         if (formatList == null) {
435             formatList = new JComboBox(listFormats);
436         }
437         return formatList;
438     }
439 
440     private JPanel getSavePanel() {
441         if (savePanel == null) {
442             savePanel = new JPanel();
443             savePanel.setLayout(new FlowLayout());
444             savePanel.add(new JLabel(t("saveAs")));
445             savePanel.add(getSaveText());
446             savePanel.add(getBoutonSaveLocation());
447         }
448         return savePanel;
449     }
450 
451     private JButton getBoutonSaveLocation() {
452         if (boutonSaveLocation == null) {
453             boutonSaveLocation = new JButton(open);
454             boutonSaveLocation.setPreferredSize(new Dimension(30, 30));
455             boutonSaveLocation.addActionListener(new ActionListener() {
456                 public void actionPerformed(ActionEvent e) {
457                     openSaveLocation();
458                 }
459 
460             });
461         }
462         return boutonSaveLocation;
463     }
464 
465     private JPanel getOuvrirPanel() {
466         if (openPanel == null) {
467             openPanel = new JPanel();
468             openPanel.setLayout(new FlowLayout());
469             openPanel.add(new JLabel(t("open")));
470             openPanel.add(getOpenText());
471             openPanel.add(getBoutonOpenLocation());
472         }
473         return openPanel;
474     }
475 
476     private JTextField getSaveText() {
477         if (saveText == null) {
478             saveText = new JTextField();
479             saveText.setColumns(31);
480         }
481         return saveText;
482     }
483 
484     private JTextField getOpenText() {
485         if (openText == null) {
486             openText = new JTextField();
487             openText.setColumns(31);
488         }
489         return openText;
490     }
491 
492     private JButton getBoutonOpenLocation() {
493         if (boutonOpenLocation == null) {
494             boutonOpenLocation = new JButton(open);
495             boutonOpenLocation.setPreferredSize(new Dimension(30, 30));
496             boutonOpenLocation.addActionListener(new ActionListener() {
497                 public void actionPerformed(ActionEvent e) {
498                     openOpenLocation();
499                 }
500 
501             });
502         }
503         return boutonOpenLocation;
504     }
505 
506     private void openOpenLocation() {
507         JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
508         fc.showOpenDialog(this);
509         File file = fc.getSelectedFile();
510         if (file != null) {
511             getOpenText().setText(file.getAbsolutePath());
512         }
513 
514     }
515 
516     private void openSaveLocation() {
517         JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
518         fc.showSaveDialog(this);
519         File file = fc.getSelectedFile();
520         if (file != null) {
521             if (file.exists()) {
522                 int choix = askEcraser();
523                 if (choix == JOptionPane.YES_OPTION) {
524                     ecrase = true;
525                     getSaveText().setText(file.getAbsolutePath());
526                 } else if (choix == JOptionPane.NO_OPTION)
527                     openSaveLocation();
528             } else
529                 getSaveText().setText(file.getAbsolutePath());
530         }
531 
532     }
533 
534     public int askEcraser() {
535         int choix = JOptionPane.showConfirmDialog(this, t("overwriteGraph?"));
536 
537         return choix;
538     }
539 
540     public void setFormats(String formats) {
541         listFormats = formats.split("\\|");
542     }
543 
544     private void annuler() {
545         System.exit(0);
546     }
547 
548     private void setMode() {
549         simpleMode = !simpleMode;
550     }
551 
552     private void convert() {
553         boolean exit = false;
554         if (getOpenText().getText().equals("")) {
555             getErrorLabel().setText(t("openEmpty?"));
556             getErrorLabel().setForeground(Color.RED);
557             pack();
558         } else {
559             if (!ecrase) {
560                 File file = new File(getSaveText().getText());
561                 if (file.exists()) {
562                     int choix = askEcraser();
563                     if (choix == JOptionPane.YES_OPTION)
564                         ecrase = true;
565                     else if (choix == JOptionPane.NO_OPTION)
566                         exit = true;
567                 }
568             }
569             if (!exit) {
570                 String cmd = "";
571                 if (ecrase)
572                     cmd += "--force ";
573                 if (simpleMode) {
574                     cmd += "--simple ";
575                 }
576                 if (getFormat().isSelected())
577                     cmd += "-t " + getFormatList().getSelectedItem();
578                 else {
579                     cmd += "-x ";
580                     for (JTextField t : ListXslText) {
581                         if (!t.getText().equals(""))
582                             cmd += t.getText() + ",";
583                     }
584                     cmd = cmd.substring(0, cmd.length() - 1);
585                 }
586                 if (getSaveText().getText().length() > 0)
587                     cmd += " -o " + getSaveText().getText();
588                 cmd += " " + getOpenText().getText() + " ";
589                 commande = cmd.trim().split(" ");
590                 dispose();
591             }
592 
593         }
594     }
595 
596     public String[] getCmd() {
597         return commande;
598     }
599 
600 }