1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
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,
106
107 6, 6,
108 6, 6);
109
110
111
112
113
114
115
116
117
118
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
180
181
182
183
184
185
186
187
188
189
190
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
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 }