Mercurial > repos > yufei-luo > s_mart
comparison SMART/Java/ProgramOption.java @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
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 = new String[choices.length+1]; | |
122 this.choices[0] = "---"; | |
123 for (int i = 0; i < choices.length; i++) { | |
124 this.choices[i+1] = choices[i]; | |
125 } | |
126 } | |
127 | |
128 | |
129 public void setDefault(String defaultValue) { | |
130 this.defaultValue = defaultValue; | |
131 } | |
132 | |
133 | |
134 public boolean isInput() { | |
135 return this.input; | |
136 } | |
137 | |
138 | |
139 public boolean checkSettings() { | |
140 if (this.identifier == null) { | |
141 return false; | |
142 } | |
143 if (this.type == null) { | |
144 return false; | |
145 } | |
146 if (this.comment == null) { | |
147 return false; | |
148 } | |
149 if (this.comment == null) { | |
150 return false; | |
151 } | |
152 if (("choice".compareToIgnoreCase(this.type) == 0) && (this.choices == null)) { | |
153 return false; | |
154 } | |
155 return true; | |
156 } | |
157 | |
158 | |
159 public JPanel getPanel() { | |
160 if (this.panel != null) { | |
161 return this.panel; | |
162 } | |
163 String comment = this.comment; | |
164 if (this.compulsory) { | |
165 comment += " [*]"; | |
166 } | |
167 | |
168 GridLayout horizontalLayout = new GridLayout(1, 0); | |
169 this.panel = new JPanel(false); | |
170 this.panel.setLayout(horizontalLayout); | |
171 JLabel label = new JLabel(comment); | |
172 | |
173 if (this.type == null) { | |
174 System.out.println("Error! Option '" + this.identifier + "' is not set!"); | |
175 } | |
176 | |
177 if (("int".compareToIgnoreCase(this.type) == 0) || ("float".compareToIgnoreCase(this.type) == 0) || ("string".compareToIgnoreCase(this.type) == 0) || (("file".compareToIgnoreCase(this.type) == 0) && (!this.input))) { | |
178 this.component = new JTextField(); | |
179 if (this.defaultValue != null) { | |
180 ((JTextField) this.component).setText(this.defaultValue); | |
181 } | |
182 label.setLabelFor(this.component); | |
183 this.panel.add(label); | |
184 this.panel.add(this.component); | |
185 } | |
186 else if ("file".compareToIgnoreCase(this.type) == 0) { | |
187 this.component = new JComboBox(Global.fileNames); | |
188 label.setLabelFor(this.component); | |
189 this.panel.add(label); | |
190 this.panel.add(this.component); | |
191 } | |
192 else if ("boolean".compareToIgnoreCase(this.type) == 0) { | |
193 this.component = new JCheckBox(); | |
194 if ((this.defaultValue != null) && (this.defaultValue.compareToIgnoreCase("true") == 0)) { | |
195 ((JCheckBox) this.component).setSelected(true); | |
196 } | |
197 label.setLabelFor(this.component); | |
198 this.panel.add(label); | |
199 this.panel.add(this.component); | |
200 } | |
201 else if ("format".compareToIgnoreCase(this.type) == 0) { | |
202 Vector < String > formats = new Vector < String > (); | |
203 for (String format: this.format) { | |
204 if (Global.formats.getFormats(format) == null) { | |
205 System.out.println("Do not know how to handle format '" + format + "'."); | |
206 } | |
207 formats.addAll(Global.formats.getFormats(format).getFormats()); | |
208 } | |
209 this.component = new JComboBox(formats); | |
210 label.setLabelFor(this.component); | |
211 this.panel.add(label); | |
212 this.panel.add(this.component); | |
213 } | |
214 else if ("files".compareToIgnoreCase(this.type) == 0) { | |
215 JButton button = new JButton("file..."); | |
216 this.component = new JTextField(); | |
217 label.setLabelFor(this.component); | |
218 this.panel.add(label); | |
219 this.panel.add(this.component); | |
220 this.panel.add(button); | |
221 Global.otherFileConcatenationChooser.put(button, (JTextField) this.component); | |
222 } | |
223 else if ("directory".compareToIgnoreCase(this.type) == 0) { | |
224 JButton button = new JButton("directory..."); | |
225 this.component = new JTextField(); | |
226 label.setLabelFor(this.component); | |
227 this.panel.add(label); | |
228 JPanel rightPanel = new JPanel(false); | |
229 rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.LINE_AXIS)); | |
230 rightPanel.add(this.component); | |
231 rightPanel.add(button); | |
232 this.panel.add(rightPanel); | |
233 Global.otherDirectoriesChooser.put(button, (JTextField) this.component); | |
234 } | |
235 else if ("choice".compareToIgnoreCase(this.type) == 0) { | |
236 this.component = new JComboBox(this.choices); | |
237 label.setLabelFor(this.component); | |
238 this.panel.add(label); | |
239 this.panel.add(this.component); | |
240 } | |
241 else { | |
242 System.out.println("Do not know how to read type " + this.type); | |
243 } | |
244 | |
245 return this.panel; | |
246 } | |
247 | |
248 | |
249 public JComponent getComponent() { | |
250 if (component == null) { | |
251 this.getPanel(); | |
252 } | |
253 return this.component; | |
254 } | |
255 | |
256 | |
257 private String getValue() { | |
258 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))) { | |
259 String s = ((JTextField) this.component).getText(); | |
260 if ("None".equals(s)) { | |
261 return ""; | |
262 } | |
263 return s; | |
264 } | |
265 if ("file".equals(this.type)) { | |
266 return (String) ((JComboBox) this.component).getSelectedItem(); | |
267 } | |
268 if ("boolean".equals(this.type)) { | |
269 return ((JCheckBox) this.component).isSelected()? "true": "false"; | |
270 } | |
271 if ("format".equals(this.type)) { | |
272 return (String) ((JComboBox) this.component).getSelectedItem(); | |
273 } | |
274 if ("choice".equals(this.type)) { | |
275 String s = (String) ((JComboBox) this.component).getSelectedItem(); | |
276 if ("---".equals(s)) { | |
277 return ""; | |
278 } | |
279 return s; | |
280 } | |
281 System.out.println("Do not know how to get value of '" + this.type + "' (" + this.identifier + ")."); | |
282 return null; | |
283 } | |
284 | |
285 | |
286 public String checkValue() { | |
287 String value = this.getValue(); | |
288 if ((this.compulsory) && ((value == null) || ("".equals(value)))) { | |
289 return "Option '" + this.comment + "' has no value... Please specify it.\n"; | |
290 } | |
291 if ("int".equals(this.type)) { | |
292 if ((value != null) && (! "".equals(value)) && (! "None".equals(value))) { | |
293 try { | |
294 int i = Integer.parseInt(value); | |
295 } | |
296 catch (NumberFormatException e) { | |
297 return "Option '" + this.comment + "' should be an integer... Please correct it.\n"; | |
298 } | |
299 } | |
300 } | |
301 else if ("float".equals(this.type)) { | |
302 if ((value != null) && (! "".equals(value))) { | |
303 try { | |
304 float i = Float.parseFloat(value); | |
305 } | |
306 catch (NumberFormatException e) { | |
307 return "Option '" + this.comment + "' should be a float... Please correct it.\n"; | |
308 } | |
309 } | |
310 } | |
311 return null; | |
312 } | |
313 | |
314 | |
315 public LinkedList <String> getCommand() { | |
316 LinkedList <String> list = new LinkedList <String> (); | |
317 | |
318 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)) || ("choice".equals(this.type))) { | |
319 String value = this.getValue(); | |
320 if (value.length() == 0) { | |
321 return list; | |
322 } | |
323 list.add(this.identifier); | |
324 list.add(value); | |
325 return list; | |
326 } | |
327 if ("file".equals(this.type)) { | |
328 String fileName = (String) ((JComboBox) this.component).getSelectedItem(); | |
329 if (fileName == null) { | |
330 return list; | |
331 } | |
332 list.add(this.identifier); | |
333 list.add(this.getValue()); | |
334 return list; | |
335 } | |
336 if (("boolean".equals(this.type)) || ("bool".equals(this.type))) { | |
337 if ("true".equals(this.getValue())) { | |
338 list.add(this.identifier); | |
339 } | |
340 return list; | |
341 } | |
342 System.out.println("Cannot get type of option " + this.type + " (" + this.identifier + "): " + this.getValue()); | |
343 return null; | |
344 } | |
345 | |
346 | |
347 public File getOutputFile() { | |
348 if (this.input) return null; | |
349 String format = ""; | |
350 if (this.format != null) { | |
351 format = this.format[0]; | |
352 } | |
353 if (this.associatedOption != null) { | |
354 format = this.associatedOption.getValue(); | |
355 } | |
356 return new File(this.getValue(), Global.formats.getFormatType(format), format); | |
357 } | |
358 } |