6
|
1 /**
|
|
2 *
|
|
3 * Copyright INRA-URGI 2009-2010
|
|
4 *
|
|
5 * This software is governed by the CeCILL license under French law and
|
|
6 * abiding by the rules of distribution of free software. You can use,
|
|
7 * modify and/ or redistribute the software under the terms of the CeCILL
|
|
8 * license as circulated by CEA, CNRS and INRIA at the following URL
|
|
9 * "http://www.cecill.info".
|
|
10 *
|
|
11 * As a counterpart to the access to the source code and rights to copy,
|
|
12 * modify and redistribute granted by the license, users are provided only
|
|
13 * with a limited warranty and the software's author, the holder of the
|
|
14 * economic rights, and the successive licensors have only limited
|
|
15 * liability.
|
|
16 *
|
|
17 * In this respect, the user's attention is drawn to the risks associated
|
|
18 * with loading, using, modifying and/or developing or reproducing the
|
|
19 * software by the user in light of its specific status of free software,
|
|
20 * that may mean that it is complicated to manipulate, and that also
|
|
21 * therefore means that it is reserved for developers and experienced
|
|
22 * professionals having in-depth computer knowledge. Users are therefore
|
|
23 * encouraged to load and test the software's suitability as regards their
|
|
24 * requirements in conditions enabling the security of their systems and/or
|
|
25 * data to be ensured and, more generally, to use and operate it in the
|
|
26 * same conditions as regards security.
|
|
27 *
|
|
28 * The fact that you are presently reading this means that you have had
|
|
29 * knowledge of the CeCILL license and that you accept its terms.
|
|
30 *
|
|
31 */
|
|
32 import java.util.*;
|
|
33 import java.awt.*;
|
|
34 import java.awt.event.ActionEvent;
|
|
35 import java.awt.event.ActionListener;
|
|
36 import java.io.*;
|
|
37 import javax.swing.*;
|
|
38 import javax.swing.filechooser.*;
|
|
39 import javax.swing.border.*;
|
|
40 import javax.swing.SwingUtilities;
|
|
41
|
|
42
|
|
43 public class ProgramOption {
|
|
44 boolean input;
|
|
45 String identifier;
|
|
46 String type;
|
|
47 String comment;
|
|
48 boolean compulsory;
|
|
49 String[] format;
|
|
50 String formatIdentifier;
|
|
51 ProgramOption associatedOption;
|
|
52 String defaultValue;
|
|
53 String[] choices;
|
|
54 JComponent component;
|
|
55 JPanel panel;
|
|
56
|
|
57
|
|
58 public ProgramOption() {
|
|
59 this.input = true;
|
|
60 this.identifier = null;
|
|
61 this.type = null;
|
|
62 this.comment = null;
|
|
63 this.compulsory = false;
|
|
64 this.format = null;
|
|
65 this.formatIdentifier = null;
|
|
66 this.associatedOption = null;
|
|
67 this.defaultValue = "";
|
|
68 this.choices = null;
|
|
69 this.component = null;
|
|
70 this.panel = null;
|
|
71 }
|
|
72
|
|
73
|
|
74 public void setInput(boolean input) {
|
|
75 this.input = input;
|
|
76 }
|
|
77
|
|
78
|
|
79 public void setIdentifier(String identifier) {
|
|
80 this.identifier = identifier;
|
|
81 }
|
|
82
|
|
83
|
|
84 public void setType(String type) {
|
|
85 this.type = type;
|
|
86 }
|
|
87
|
|
88
|
|
89 public void setComment(String comment) {
|
|
90 this.comment = comment;
|
|
91 }
|
|
92
|
|
93
|
|
94 public void setCompulsory(boolean compulsory) {
|
|
95 this.compulsory = compulsory;
|
|
96 }
|
|
97
|
|
98
|
|
99 public void setFormat(String[] format) {
|
|
100 this.format = format;
|
|
101 }
|
|
102
|
|
103
|
|
104 public void setFormat(String format) {
|
|
105 this.format = new String[1];
|
|
106 this.format[0] = format;
|
|
107 }
|
|
108
|
|
109
|
|
110 public void setFormatIdentifier(String formatIdentifier) {
|
|
111 this.formatIdentifier = formatIdentifier;
|
|
112 }
|
|
113
|
|
114
|
|
115 public void setAssociatedOption(ProgramOption option) {
|
|
116 this.associatedOption = option;
|
|
117 }
|
|
118
|
|
119
|
|
120 public void setChoices(String[] choices) {
|
|
121 this.choices = choices;
|
|
122 }
|
|
123
|
|
124
|
|
125 public void setDefault(String defaultValue) {
|
|
126 this.defaultValue = defaultValue;
|
|
127 }
|
|
128
|
|
129
|
|
130 public boolean isInput() {
|
|
131 return this.input;
|
|
132 }
|
|
133
|
|
134
|
|
135 public JPanel getPanel() {
|
|
136 if (this.panel != null) {
|
|
137 return this.panel;
|
|
138 }
|
|
139 String comment = this.comment;
|
|
140 if (this.compulsory) {
|
|
141 comment += " [*]";
|
|
142 }
|
|
143
|
|
144 GridLayout horizontalLayout = new GridLayout(1, 0);
|
|
145 this.panel = new JPanel(false);
|
|
146 this.panel.setLayout(horizontalLayout);
|
|
147 JLabel label = new JLabel(comment);
|
|
148
|
|
149 if (this.type == null) {
|
|
150 System.out.println("Error! Option '" + this.identifier + "' is not set!");
|
|
151 }
|
|
152
|
|
153 if (("int".compareToIgnoreCase(this.type) == 0) || ("float".compareToIgnoreCase(this.type) == 0) || ("string".compareToIgnoreCase(this.type) == 0) || (("file".compareToIgnoreCase(this.type) == 0) && (!this.input))) {
|
|
154 this.component = new JTextField();
|
|
155 if (this.defaultValue != null) {
|
|
156 ((JTextField) this.component).setText(this.defaultValue);
|
|
157 }
|
|
158 label.setLabelFor(this.component);
|
|
159 this.panel.add(label);
|
|
160 this.panel.add(this.component);
|
|
161 }
|
|
162 else if ("file".compareToIgnoreCase(this.type) == 0) {
|
|
163 this.component = new JList(Global.fileNames);
|
|
164 ((JList) this.component).setVisibleRowCount(1);
|
|
165 JScrollPane scroll = new JScrollPane(this.component);
|
|
166
|
|
167 Box box = Box.createHorizontalBox();
|
|
168 box.add(Box.createRigidArea(new Dimension(0, 100)));
|
|
169 box.add(scroll);
|
|
170
|
|
171 label.setLabelFor(this.component);
|
|
172 this.panel.add(label);
|
|
173 this.panel.add(box);
|
|
174 }
|
|
175 else if ("boolean".compareToIgnoreCase(this.type) == 0) {
|
|
176 this.component = new JCheckBox();
|
|
177 if ((this.defaultValue != null) && (this.defaultValue.compareToIgnoreCase("true") == 0)) {
|
|
178 ((JCheckBox) this.component).setSelected(true);
|
|
179 }
|
|
180 label.setLabelFor(this.component);
|
|
181 this.panel.add(label);
|
|
182 this.panel.add(this.component);
|
|
183 }
|
|
184 else if ("format".compareToIgnoreCase(this.type) == 0) {
|
|
185 Vector < String > formats = new Vector < String > ();
|
|
186 for (String format: this.format) {
|
|
187 formats.addAll(Global.formats.getFormats(format).getFormats());
|
|
188 }
|
|
189 this.component = new JComboBox(formats);
|
|
190 label.setLabelFor(this.component);
|
|
191 this.panel.add(label);
|
|
192 this.panel.add(this.component);
|
|
193 }
|
|
194 else if ("files".compareToIgnoreCase(this.type) == 0) {
|
|
195 JButton button = new JButton("file...");
|
|
196 this.component = new JTextField();
|
|
197 label.setLabelFor(this.component);
|
|
198 this.panel.add(label);
|
|
199 this.panel.add(this.component);
|
|
200 this.panel.add(button);
|
|
201 Global.otherFileConcatenationChooser.put(button, (JTextField) this.component);
|
|
202 }
|
|
203 else if ("directory".compareToIgnoreCase(this.type) == 0) {
|
|
204 JButton button = new JButton("directory...");
|
|
205 this.component = new JTextField();
|
|
206 label.setLabelFor(this.component);
|
|
207 this.panel.add(label);
|
|
208 JPanel rightPanel = new JPanel(false);
|
|
209 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.LINE_AXIS));
|
|
210 rightPanel.add(this.component);
|
|
211 rightPanel.add(button);
|
|
212 this.panel.add(rightPanel);
|
|
213 Global.otherDirectoriesChooser.put(button, (JTextField) this.component);
|
|
214 }
|
|
215 else if ("choice".compareToIgnoreCase(this.type) == 0) {
|
|
216 this.component = new JComboBox(this.choices);
|
|
217 label.setLabelFor(this.component);
|
|
218 this.panel.add(label);
|
|
219 this.panel.add(this.component);
|
|
220 }
|
|
221 else {
|
|
222 System.out.println("Do not know how to read type " + this.type);
|
|
223 }
|
|
224
|
|
225 return this.panel;
|
|
226 }
|
|
227
|
|
228
|
|
229 public JComponent getComponent() {
|
|
230 if (component == null) {
|
|
231 this.getPanel();
|
|
232 }
|
|
233 return this.component;
|
|
234 }
|
|
235
|
|
236
|
|
237 private String getValue() {
|
|
238 if (("int".equals(this.type)) || ("float".equals(this.type)) || ("string".equals(this.type)) || (("file".equals(this.type)) && (! this.input)) || ("directory".equals(this.type)) || ("files".equals(this.type))) {
|
|
239 String s = ((JTextField) this.component).getText();
|
|
240 if ("None".equals(s)) {
|
|
241 return "";
|
|
242 }
|
|
243 return s;
|
|
244 }
|
|
245 if ("file".equals(this.type)) {
|
|
246 return (String) ((JList) this.component).getSelectedValue();
|
|
247 }
|
|
248 if ("boolean".equals(this.type)) {
|
|
249 return ((JCheckBox) this.component).isSelected()? "true": "false";
|
|
250 }
|
|
251 if ("format".equals(this.type)) {
|
|
252 return (String) ((JComboBox) this.component).getSelectedItem();
|
|
253 }
|
|
254 return null;
|
|
255 }
|
|
256
|
|
257
|
|
258 public String checkValue() {
|
|
259 String value = this.getValue();
|
|
260 if ((this.compulsory) && ((value == null) || ("".equals(value)))) {
|
|
261 return "Option '" + this.comment + "' has no value... Please specify it.\n";
|
|
262 }
|
|
263 if ("int".equals(this.type)) {
|
|
264 if ((value != null) && (! "".equals(value)) && (! "None".equals(value))) {
|
|
265 try {
|
|
266 int i = Integer.parseInt(value);
|
|
267 }
|
|
268 catch (NumberFormatException e) {
|
|
269 return "Option '" + this.comment + "' should be an integer... Please correct it.\n";
|
|
270 }
|
|
271 }
|
|
272 }
|
|
273 else if ("float".equals(this.type)) {
|
|
274 if ((value != null) && (! "".equals(value))) {
|
|
275 try {
|
|
276 float i = Float.parseFloat(value);
|
|
277 }
|
|
278 catch (NumberFormatException e) {
|
|
279 return "Option '" + this.comment + "' should be a float... Please correct it.\n";
|
|
280 }
|
|
281 }
|
|
282 }
|
|
283 return null;
|
|
284 }
|
|
285
|
|
286
|
|
287 public LinkedList <String> getCommand() {
|
|
288 LinkedList <String> list = new LinkedList <String> ();
|
|
289
|
|
290 if (("int".equals(this.type)) || ("float".equals(this.type)) || ("string".equals(this.type)) || (("file".equals(this.type)) && (! this.input)) || ("format".equals(this.type)) || ("directory".equals(this.type)) || ("files".equals(this.type))) {
|
|
291 String value = this.getValue();
|
|
292 if (value.length() == 0) {
|
|
293 return list;
|
|
294 }
|
|
295 list.add(this.identifier);
|
|
296 list.add(value);
|
|
297 return list;
|
|
298 }
|
|
299 if ("file".equals(this.type)) {
|
|
300 String fileName = (String) ((JList) this.component).getSelectedValue();
|
|
301 if (fileName == null) {
|
|
302 return list;
|
|
303 }
|
|
304 list.add(this.identifier);
|
|
305 list.add(this.getValue());
|
|
306 return list;
|
|
307 }
|
|
308 if ("boolean".equals(this.type)) {
|
|
309 if ("true".equals(this.getValue())) {
|
|
310 list.add(this.identifier);
|
|
311 }
|
|
312 return list;
|
|
313 }
|
|
314 return null;
|
|
315 }
|
|
316
|
|
317
|
|
318 public File getOutputFile() {
|
|
319 if (this.input) return null;
|
|
320 String format = "";
|
|
321 if (this.format != null) {
|
|
322 format = this.format[0];
|
|
323 }
|
|
324 if (this.associatedOption != null) {
|
|
325 format = this.associatedOption.getValue();
|
|
326 }
|
|
327 return new File(this.getValue() + "." + format, Global.formats.getFormatType(format), format);
|
|
328 }
|
|
329 }
|