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.BorderLayout;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.KeyEvent;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.Set;
31  
32  import javax.swing.JButton;
33  import javax.swing.JDialog;
34  import javax.swing.JLabel;
35  import javax.swing.JOptionPane;
36  import javax.swing.JPanel;
37  import javax.swing.SpringLayout;
38  
39  import fr.ens.transcriptome.nividic.platform.module.Module;
40  import fr.ens.transcriptome.nividic.platform.workflow.Algorithm;
41  import fr.ens.transcriptome.nividic.util.SystemUtils;
42  import fr.ens.transcriptome.nividic.util.gui.ParameterWidget;
43  import fr.ens.transcriptome.nividic.util.gui.SpringUtilities;
44  import fr.ens.transcriptome.nividic.util.parameter.ParameterException;
45  import fr.ens.transcriptome.nividic.util.parameter.Parameters;
46  
47  /***
48   * This class describe a widhet used to edit parameters of an algorithm.
49   * @author Laurent Jourdren
50   */
51  public class EditParametersWidget {
52  
53    //For log system
54    //private static Logger log = Logger.getLogger(EditParametersWidget.class);
55  
56    private Algorithm algorithm;
57    private JDialog dialog = new JDialog();
58  
59    /***
60     * Get the algorithm.
61     * @return The algorithm
62     */
63    public Algorithm getAlgorithm() {
64      return algorithm;
65    }
66  
67    /***
68     * Set the algorithm.
69     * @param algorithm The test to set
70     */
71    private void setAlgorithm(final Algorithm algorithm) {
72      this.algorithm = algorithm;
73    }
74  
75    /***
76     * Show the widget for editing parameters.
77     */
78    public void edit() {
79  
80      final Parameters params = getAlgorithm().getParameters();
81      if (params == null)
82        return;
83  
84      Parameters parameters = getAlgorithm().getParameters();
85  
86      String[] paramNames = parameters.getParametersNames();
87      dialog.getContentPane().setLayout(new BorderLayout());
88  
89      JPanel parametersPanel = new JPanel();
90      parametersPanel.setLayout(new SpringLayout());
91  
92      final Set paramSet = new HashSet();
93  
94      for (int i = 0; i < paramNames.length; i++) {
95  
96        ParameterWidget pw = new ParameterWidget(parametersPanel, parameters
97            .getParameter(paramNames[i]));
98  
99        parametersPanel.add(new JLabel(" "));
100       parametersPanel.add(new JLabel(" "));
101 
102       paramSet.add(pw);
103     }
104 
105     SpringUtilities.makeCompactGrid(parametersPanel, paramSet.size() * 3, 2, //rows,
106         // cols
107         6, 6, //initX, initY
108         6, 6); //xPad, yPad
109 
110     /*
111      * String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "}; int
112      * numPairs = labels.length; // Create and populate the panel. //JPanel p =
113      * new JPanel(new SpringLayout()); Container p = parametersPanel; for (int i =
114      * 0; i < numPairs; i++) { JLabel l = new JLabel(labels[i],
115      * JLabel.TRAILING); p.add(l); JTextField textField = new JTextField(10);
116      * l.setLabelFor(textField); p.add(textField); } // Lay out the panel.
117      * SpringUtilities.makeCompactGrid(p, numPairs, 2, //rows, cols 6, 6,
118      * //initX, initY 6, 6); //xPad, yPad
119      */
120 
121     dialog.getContentPane().add(parametersPanel, BorderLayout.CENTER);
122     JPanel buttonsPanel = new JPanel();
123 
124     JButton okButton = new JButton("OK");
125     JButton cancelButton = new JButton("Cancel");
126 
127     if (!SystemUtils.isMacOsX()) {
128       okButton.setMnemonic(KeyEvent.VK_O);
129       cancelButton.setMnemonic(KeyEvent.VK_C);
130     }
131 
132     buttonsPanel.add(okButton);
133     buttonsPanel.add(cancelButton);
134     dialog.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
135 
136     okButton.addActionListener(new ActionListener() {
137 
138       public void actionPerformed(final ActionEvent e) {
139 
140         Iterator it = paramSet.iterator();
141         boolean ok = true;
142         while (it.hasNext()) {
143           ParameterWidget pw = (ParameterWidget) it.next();
144           try {
145 
146             pw.putValueInParameter();
147 
148           } catch (ParameterException e1) {
149             ok = false;
150             JOptionPane.showMessageDialog(dialog, "Bad parameter : "
151                 + pw.getParameter().getName());
152           }
153 
154         }
155 
156         if (ok)
157           dialog.hide();
158       }
159 
160     });
161 
162     cancelButton.addActionListener(new ActionListener() {
163 
164       public void actionPerformed(final ActionEvent e) {
165 
166         dialog.hide();
167       }
168 
169     });
170 
171     dialog.setTitle("Edit parameters for "
172         + ((Module) getAlgorithm()).aboutModule().getName());
173 
174     dialog.setResizable(false);
175     dialog.setModal(true);
176     dialog.pack();
177 
178     /*
179      * double width = 0; double height = 0; Iterator it = paramSet.iterator();
180      * int maxSizeLabels = 0; int maxSizeValues = 0; while (it.hasNext()) {
181      * ParameterWidget pw = (ParameterWidget) it.next(); int lenLabels =
182      * pw.getMaxSizeLabels(); int lenValues = pw.getMaxSizeValues(); if
183      * (lenLabels>maxSizeLabels) maxSizeLabels = lenLabels; if
184      * (lenValues>maxSizeValues) maxSizeValues = lenValues; } it =
185      * paramSet.iterator(); while (it.hasNext()) { ParameterWidget pw =
186      * (ParameterWidget) it.next(); //pw.setSizeValues(maxSizeLabels,
187      * maxSizeValues); //pw.setSizeValues(400, 400); Dimension d = pw.getSize();
188      * width = d.getWidth() * 1.3; height += d.getHeight(); } Dimension d =
189      * buttonsPanel.getSize(); height += d.getHeight(); dialog.setSize((int)
190      * width, (int) height*2);
191      */
192     dialog.show();
193 
194   }
195 
196   /***
197    * Set the location of the widget.
198    * @param posX x position
199    * @param posY y position
200    */
201   public void setLocation(final int posX, final int posY) {
202     this.dialog.setLocation(posX, posY);
203   }
204 
205   /***
206    * Hide the widget.
207    */
208   public void close() {
209     this.dialog.hide();
210   }
211 
212   //
213   // Constructor
214   //
215 
216   /***
217    * Public consructor.
218    * @param algorithm Test to edit
219    */
220   public EditParametersWidget(final Algorithm algorithm) {
221     setAlgorithm(algorithm);
222   }
223 
224 }