View Javadoc

1   /*
2    *                Doelan development code
3    *
4    * This code may be freely distributed and modified under the
5    * terms of the GNU General Public Licence.  This should
6    * be distributed with the code. If you do not have a copy,
7    * see:
8    *
9    *      http://www.gnu.org/copyleft/gpl.txt
10   *
11   * Copyright (c) 2004-2005 ENS Microarray Platform
12   * Copyright for this code is held jointly by the individual
13   * authors.  These should be listed in @author doc comments.
14   *
15   * For more information on the Doelan project and its aims,
16   * or to join the Doelan mailing list, visit the home page
17   * at:
18   *
19   *      http://www.transcriptome.ens.fr/doelan
20   */
21  
22  package fr.ens.transcriptome.doelan.gui;
23  
24  import java.awt.Container;
25  import java.awt.GridBagConstraints;
26  import java.awt.Point;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.awt.event.KeyEvent;
30  import java.util.Arrays;
31  
32  import javax.swing.JButton;
33  import javax.swing.JComboBox;
34  import javax.swing.JLabel;
35  import javax.swing.JOptionPane;
36  import javax.swing.JPanel;
37  
38  import fr.ens.transcriptome.doelan.DoelanException;
39  import fr.ens.transcriptome.doelan.DoelanRegistery;
40  import fr.ens.transcriptome.doelan.data.QualityTestSuiteList;
41  import fr.ens.transcriptome.doelan.data.QualityTestSuiteURL;
42  import fr.ens.transcriptome.doelan.io.QualityTestIOFactory;
43  import fr.ens.transcriptome.doelan.io.QualityTestSuiteListIO;
44  import fr.ens.transcriptome.nividic.util.SystemUtils;
45  
46  /***
47   * Suite chooser widget.
48   * @author Laurent Jourdren
49   */
50  public class SuiteChooserWidget {
51  
52    private JComboBox chipTypeList = new JComboBox();
53    private JComboBox testSuiteList = new JComboBox();
54    private MainTabWidget mainTabWidget;
55    private JPanel panel;
56    private int yPosition;
57  
58    private String currentChipTypeName;
59    private String currentTestSuiteName;
60    private boolean advancedButtons;
61  
62    private JButton advancedButton;
63    private JButton loadButton;
64    private JButton newButton;
65    private JButton renameButton;
66    private JButton deleteButton;
67    //private JButton saveButton;
68  
69    private QualityTestSuiteList data;
70  
71    //
72    // Getters
73    //
74  
75    /***
76     * Get the test suite list.
77     * @return The test suite list
78     */
79    public QualityTestSuiteList getTestSuiteList() {
80      return data;
81    }
82  
83    public String getCurrentChipTypeName() {
84      return currentChipTypeName;
85    }
86  
87    public String getCurrentTestSuiteName() {
88      return currentTestSuiteName;
89    }
90  
91    /***
92     * Get the panel.
93     * @return Returns the panel
94     */
95    public JPanel getPanel() {
96      return panel;
97    }
98  
99    /***
100    * Get the y position of the panel.
101    * @return Returns the yPosition
102    */
103   public int getYPosition() {
104     return yPosition;
105   }
106 
107   /***
108    * Get the mainTabWidget.
109    * @return Returns the mainTabWidget
110    */
111   public MainTabWidget getMainTabWidget() {
112     return mainTabWidget;
113   }
114 
115   //
116   // Setters
117   //
118 
119   /***
120    * Set the test suite list.
121    * @param list The suite list
122    */
123   public void setTestSuiteList(final QualityTestSuiteList list) {
124     data = list;
125     updateChipList();
126   }
127 
128   private void setCurrentChipTypeName(final String name) {
129     currentChipTypeName = name;
130   }
131 
132   private void setCurrentTestSuiteName(final String name) {
133     currentTestSuiteName = name;
134   }
135 
136   /***
137    * Set the y position of the widget.
138    * @param position The yPosition to set
139    */
140   public void setYPosition(final int position) {
141     yPosition = position;
142   }
143 
144   /***
145    * Set the panel.
146    * @param panel The panel to set
147    */
148   public void setPanel(final JPanel panel) {
149     this.panel = panel;
150   }
151 
152   /***
153    * Set the common window.
154    * @param mainTabWidget The mainTabWidget to set
155    */
156   public void setMainTabWidget(final MainTabWidget commonWindow) {
157     this.mainTabWidget = commonWindow;
158   }
159 
160   //
161   // Other methods
162   //
163 
164   /***
165    * Return the quality test suite url selected.
166    * @return the quality test suite url object selected
167    */
168   public QualityTestSuiteURL getQualityTestSuiteSelected() {
169 
170     if (this.data == null)
171       return null;
172 
173     QualityTestSuiteURL[] suites = this.data
174         .getTestSuiteURLs(getCurrentChipTypeName());
175 
176     if (suites == null)
177       return null;
178     final String testSuiteName = getCurrentTestSuiteName();
179     for (int i = 0; i < suites.length; i++) {
180       if (suites[i].getName().equals(testSuiteName))
181         return suites[i];
182     }
183 
184     return null;
185   }
186 
187   private void showAdvancedButtons(final boolean show) {
188 
189     this.newButton.setVisible(show);
190     this.deleteButton.setVisible(show);
191     this.renameButton.setVisible(show);
192     //this.saveButton.setVisible(show);
193     this.loadButton.setVisible(show);
194 
195     if (show == false) {
196       this.advancedButton.setText("Show advanced options");
197       this.advancedButton
198           .setToolTipText("Press this button to show advanced options");
199     } else {
200       this.advancedButton.setText("Hide advanced options");
201       this.advancedButton
202           .setToolTipText("Press this button to hide advanced options");
203     }
204 
205     this.advancedButtons = show;
206 
207   }
208 
209   private void save() {
210 
211     String[] chipTypes = getTestSuiteList().getChipTypes();
212     if (chipTypes == null) {
213       JOptionPane.showMessageDialog(panel, "Nothing to save");
214       return;
215     }
216 
217     for (int i = 0; i < chipTypes.length; i++) {
218       QualityTestSuiteURL[] urls = getTestSuiteList().getTestSuiteURLs(
219           chipTypes[i]);
220       for (int j = 0; j < urls.length; j++)
221         if (urls[j].getURL() == null) {
222           JOptionPane.showMessageDialog(panel, "Test suite not save");
223           return;
224         }
225     }
226 
227     QualityTestSuiteListIO qtslio = new QualityTestIOFactory()
228         .createQualityTestSuiteListIO(DoelanRegistery
229             .getTestSuiteListFilename());
230 
231     try {
232       qtslio.write(data);
233       //JOptionPane.showMessageDialog(panel, "Test suite directory saved.");
234     } catch (DoelanException err) {
235       JOptionPane.showMessageDialog(panel, err.getMessage());
236     }
237 
238   }
239 
240   private void init() {
241 
242     chipTypeList.setToolTipText("Click to select a chip type");
243     testSuiteList.setToolTipText("Click to select a test suite");
244 
245     final JPanel selectorPanel = getPanel();
246 
247     GridBagConstraints gridBagConstraints;
248 
249     JLabel jLabel3 = new JLabel("Select the related microarray type:");
250     jLabel3.setDisplayedMnemonic(KeyEvent.VK_M);
251     jLabel3.setLabelFor(chipTypeList);
252     gridBagConstraints = new java.awt.GridBagConstraints();
253     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
254     gridBagConstraints.gridx = 0;
255     gridBagConstraints.gridy = getYPosition();
256 
257     selectorPanel.add(jLabel3, gridBagConstraints);
258 
259     gridBagConstraints = new java.awt.GridBagConstraints();
260     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
261     gridBagConstraints.gridx = 1;
262     gridBagConstraints.gridy = getYPosition();
263     gridBagConstraints.gridwidth = 4;
264     selectorPanel.add(chipTypeList, gridBagConstraints);
265 
266     JLabel jLabel4 = new JLabel("Load the test suite you want to apply:");
267     jLabel4.setDisplayedMnemonic(KeyEvent.VK_T);
268     jLabel4.setDisplayedMnemonicIndex(9);
269     jLabel4.setLabelFor(testSuiteList);
270     gridBagConstraints = new java.awt.GridBagConstraints();
271     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
272     gridBagConstraints.gridx = 0;
273     gridBagConstraints.gridy = getYPosition() + 1;
274     selectorPanel.add(jLabel4, gridBagConstraints);
275 
276     gridBagConstraints = new java.awt.GridBagConstraints();
277     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
278     gridBagConstraints.gridx = 1;
279     gridBagConstraints.gridy = getYPosition() + 1;
280     gridBagConstraints.gridwidth = 4;
281     selectorPanel.add(testSuiteList, gridBagConstraints);
282 
283     // Test suite manager
284 
285     if (!DoelanRegistery.isAppletMode()) {
286 
287       gridBagConstraints = new java.awt.GridBagConstraints();
288       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
289       gridBagConstraints.gridx = 1;
290       gridBagConstraints.gridy = getYPosition() + 2;
291       gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 3);
292       loadButton = new JButton("Load test");
293       loadButton.setToolTipText("Press this button to load the test suite");
294 
295       selectorPanel.add(loadButton, gridBagConstraints);
296 
297       gridBagConstraints = new java.awt.GridBagConstraints();
298       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
299       gridBagConstraints.gridx = 2;
300       gridBagConstraints.gridy = getYPosition() + 2;
301       gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 3);
302       newButton = new JButton("New");
303       newButton
304           .setToolTipText("Press this button to create a new test suite or a new chip type");
305 
306       selectorPanel.add(newButton, gridBagConstraints);
307 
308       gridBagConstraints = new java.awt.GridBagConstraints();
309       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
310       gridBagConstraints.gridx = 3;
311       gridBagConstraints.gridy = getYPosition() + 2;
312       gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 3);
313       renameButton = new JButton("Rename");
314       renameButton
315           .setToolTipText("Press this button to rename a test suite or a chip type");
316 
317       selectorPanel.add(renameButton, gridBagConstraints);
318 
319       gridBagConstraints = new java.awt.GridBagConstraints();
320       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
321       gridBagConstraints.gridx = 4;
322       gridBagConstraints.gridy = getYPosition() + 2;
323       gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0);
324       deleteButton = new JButton("Delete");
325       deleteButton
326           .setToolTipText("Press this button to delete a test suite or a chip type");
327 
328       selectorPanel.add(deleteButton, gridBagConstraints);
329 
330       /*
331        * gridBagConstraints = new java.awt.GridBagConstraints();
332        * gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
333        * gridBagConstraints.gridx = 5; gridBagConstraints.gridy = getYPosition() +
334        * 3; gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0);
335        * saveButton = new JButton("Save"); selectorPanel.add(saveButton,
336        * gridBagConstraints);
337        */
338 
339       gridBagConstraints = new java.awt.GridBagConstraints();
340       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
341       gridBagConstraints.gridx = 4;
342       gridBagConstraints.gridy = getYPosition() + 3;
343       gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 0);
344       this.advancedButton = new JButton();
345 
346       selectorPanel.add(advancedButton, gridBagConstraints);
347 
348       gridBagConstraints = new java.awt.GridBagConstraints();
349       gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
350       gridBagConstraints.gridx = 1;
351       gridBagConstraints.gridy = getYPosition() + 3;
352       gridBagConstraints.gridwidth = 3;
353       gridBagConstraints.ipadx = 300;
354       gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 3);
355       final JLabel label = new JLabel();
356       selectorPanel.add(label, gridBagConstraints);
357 
358       showAdvancedButtons(false);
359 
360       if (!SystemUtils.isMacOsX()) {
361         loadButton.setMnemonic(KeyEvent.VK_L);
362         newButton.setMnemonic(KeyEvent.VK_N);
363         renameButton.setMnemonic(KeyEvent.VK_R);
364         deleteButton.setMnemonic(KeyEvent.VK_D);
365         advancedButton.setMnemonic(KeyEvent.VK_A);
366       }
367 
368       advancedButton.addActionListener(new ActionListener() {
369         public void actionPerformed(final ActionEvent e) {
370 
371           if (SuiteChooserWidget.this.advancedButtons)
372             SuiteChooserWidget.this.advancedButtons = false;
373           else
374             SuiteChooserWidget.this.advancedButtons = true;
375 
376           SuiteChooserWidget.this
377               .showAdvancedButtons(SuiteChooserWidget.this.advancedButtons);
378 
379         }
380       });
381 
382       loadButton.addActionListener(new ActionListener() {
383         public void actionPerformed(final ActionEvent e) {
384           if (getCurrentTestSuiteName() == null)
385             JOptionPane.showMessageDialog(panel, "No test suite selected");
386           else
387             getMainTabWidget().load();
388         }
389       });
390 
391       newButton.addActionListener(new ActionListener() {
392         public void actionPerformed(final ActionEvent e) {
393 
394           NewTestSuiteWidget ntsw = new NewTestSuiteWidget(data,
395               getCurrentChipTypeName());
396 
397           ntsw.setLocation(newButton.getLocationOnScreen());
398           ntsw.show();
399 
400           updateChipList();
401           updateSuiteList(getCurrentChipTypeName());
402           save();
403         }
404       });
405 
406       renameButton.addActionListener(new ActionListener() {
407         public void actionPerformed(final ActionEvent e) {
408           RenameTestSuiteWidget rtsw = new RenameTestSuiteWidget(data,
409               getCurrentChipTypeName(), getQualityTestSuiteSelected());
410 
411           rtsw.setLocation(renameButton.getLocationOnScreen());
412           //rtsw.show();
413 
414           updateChipList();
415           updateSuiteList(getCurrentChipTypeName());
416           save();
417         }
418       });
419 
420       deleteButton.addActionListener(new ActionListener() {
421         public void actionPerformed(final ActionEvent e) {
422 
423           if (getCurrentTestSuiteName() == null) {
424             int response = JOptionPane.showConfirmDialog(panel,
425                 new String[] {"Remove \"" + getCurrentChipTypeName()
426                     + "\" chip type ?"}, "Remove chip type",
427                 JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
428 
429             switch (response) {
430             case JOptionPane.YES_OPTION:
431               /*
432                * if (!data.removeChipType(getCurrentChipTypeName()))
433                * JOptionPane.showMessageDialog(panel, "Unable to remove this
434                * chip type");
435                */
436 
437               try {
438                 data.removeChipType(getCurrentChipTypeName());
439               } catch (DoelanException e1) {
440                 JOptionPane.showMessageDialog(panel, e1.getMessage());
441               }
442 
443               break;
444 
445             default:
446               break;
447             }
448           } else {
449             int response = JOptionPane.showConfirmDialog(panel,
450                 new String[] {"Remove \"" + getCurrentTestSuiteName()
451                     + "\" test suite ?"}, "Remove test suite",
452                 JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
453 
454             switch (response) {
455             case JOptionPane.YES_OPTION:
456 
457               try {
458                 /*
459                  * if (!data.removeTestSuite(getQualityTestSuiteSelected()))
460                  * JOptionPane.showMessageDialog(panel, "Unable to remove this
461                  * test suite");
462                  */
463                 data.removeTestSuite(getQualityTestSuiteSelected());
464               } catch (DoelanException e1) {
465                 JOptionPane.showMessageDialog(panel, e1.getMessage());
466               }
467 
468               break;
469 
470             default:
471               break;
472             }
473           }
474           updateChipList();
475           updateSuiteList(getCurrentChipTypeName());
476           save();
477         }
478       });
479 
480       /*
481        * saveButton.addActionListener(new ActionListener() { public void
482        * actionPerformed(final ActionEvent e) { String[] chipTypes =
483        * getTestSuiteList().getChipTypes(); if (chipTypes == null) {
484        * JOptionPane.showMessageDialog(panel, "Nothing to save"); return; } for
485        * (int i = 0; i < chipTypes.length; i++) { QualityTestSuiteURL[] urls =
486        * getTestSuiteList().getTestSuiteURLs( chipTypes[i]); for (int j = 0; j <
487        * urls.length; j++) if (urls[j].getURL() == null) {
488        * JOptionPane.showMessageDialog(panel, "Test suite not save"); return; } }
489        * QualityTestSuiteListIO qtslio = new QualityTestIOFactory()
490        * .createQualityTestSuiteListIO(DoelanRegistery
491        * .getTestSuiteListFilename()); try { qtslio.write(data);
492        * JOptionPane.showMessageDialog(panel, "Test suite directory saved."); }
493        * catch (DoelanException err) { JOptionPane.showMessageDialog(panel,
494        * err.getMessage()); } } });
495        */
496     }
497 
498     // Update Suite list when a chip type is selected
499     this.chipTypeList.addActionListener(new ActionListener() {
500       public void actionPerformed(final ActionEvent e) {
501         JComboBox combo = (JComboBox) e.getSource();
502 
503         setCurrentChipTypeName((String) combo.getSelectedItem());
504         updateSuiteList(getCurrentChipTypeName());
505       }
506     });
507 
508     //  Update Suite list when a chip type is selected
509     this.testSuiteList.addActionListener(new ActionListener() {
510       public void actionPerformed(final ActionEvent e) {
511         JComboBox combo = (JComboBox) e.getSource();
512         setCurrentTestSuiteName((String) combo.getSelectedItem());
513       }
514     });
515 
516   }
517 
518   //
519   // Constructor
520   //
521 
522   private void updateChipList() {
523 
524     this.chipTypeList.removeAllItems();
525     String[] elements = data.getChipTypes();
526 
527     Arrays.sort(elements);
528     for (int i = 0; i < elements.length; i++) {
529       this.chipTypeList.addItem(elements[i]);
530     }
531     if (elements.length != 0) {
532       updateSuiteList(elements[0]);
533       this.chipTypeList.setEnabled(true);
534       if (!DoelanRegistery.isAppletMode()) {
535         this.deleteButton.setEnabled(true);
536         this.renameButton.setEnabled(true);
537       }
538 
539     } else {
540       updateSuiteList(null);
541       this.chipTypeList.setEnabled(false);
542       if (!DoelanRegistery.isAppletMode()) {
543         this.deleteButton.setEnabled(false);
544         this.renameButton.setEnabled(false);
545       }
546       if (!DoelanRegistery.isAppletMode()) showAdvancedButtons(true);
547 
548     }
549 
550   }
551 
552   private void updateSuiteList(final String chip) {
553 
554     if (chip == null) {
555       this.mainTabWidget.showStartButton(false);
556       if (!DoelanRegistery.isAppletMode()) {
557         this.testSuiteList.setEnabled(false);
558         this.loadButton.setEnabled(false);
559       }
560       return;
561     }
562 
563     this.testSuiteList.removeAllItems();
564 
565     QualityTestSuiteURL[] suites = data.getTestSuiteURLs(chip);
566 
567     if (suites.length == 0) {
568       //setCurrentChipTypeName(null);
569       setCurrentTestSuiteName(null);
570       this.testSuiteList.setEnabled(false);
571       if (!DoelanRegistery.isAppletMode())
572         this.loadButton.setEnabled(false);
573       this.mainTabWidget.showStartButton(false);
574       return;
575     }
576     this.testSuiteList.setEnabled(true);
577     if (!DoelanRegistery.isAppletMode())
578       this.loadButton.setEnabled(true);
579     this.mainTabWidget.showStartButton(true);
580 
581     String[] names = new String[suites.length];
582 
583     for (int i = 0; i < suites.length; i++)
584       names[i] = suites[i].getName();
585     Arrays.sort(names);
586 
587     for (int i = 0; i < names.length; i++)
588       testSuiteList.addItem(names[i]);
589 
590     if (names.length == 0)
591       setCurrentTestSuiteName(null);
592     else
593       setCurrentTestSuiteName(names[0]);
594   }
595 
596   private static void setMiddleLocation(Container object) {
597 
598     Container c = object;
599 
600     while (c.getParent() != null)
601       c = c.getParent();
602 
603     int posY = (c.getHeight() / 2) - (object.getHeight() / 2);
604     int posX = (c.getWidth() / 2) - (object.getWidth() / 2);
605 
606     Point rp = c.getLocationOnScreen();
607     Point p = c.getLocation();
608 
609     object.setLocation((int) (rp.getX() - p.getX() + posX), (int) (rp.getY()
610         - p.getY() + posY));
611 
612   }
613 
614   /***
615    * Public constructor.
616    * @param panel Panel of the widget
617    * @param position of the widget in the panel
618    */
619   public SuiteChooserWidget(final MainTabWidget cw, final JPanel panel,
620       final int position) {
621 
622     setPanel(panel);
623     setYPosition(position);
624     setMainTabWidget(cw);
625     init();
626   }
627 
628 }