root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / modeler / parameters / NumericalValuePanel.java @ 83
History | View | Annotate | Download (10.6 KB)
1 |
package es.unex.sextante.gui.modeler.parameters; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
import info.clearthought.layout.TableLayoutConstants; |
5 |
|
6 |
import java.awt.Cursor; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.FocusAdapter; |
10 |
import java.awt.event.FocusEvent; |
11 |
|
12 |
import javax.swing.ComboBoxModel; |
13 |
import javax.swing.DefaultComboBoxModel; |
14 |
import javax.swing.JCheckBox; |
15 |
import javax.swing.JComboBox; |
16 |
import javax.swing.JDialog; |
17 |
import javax.swing.JLabel; |
18 |
import javax.swing.JOptionPane; |
19 |
import javax.swing.JTextField; |
20 |
|
21 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
22 |
import es.unex.sextante.core.Sextante; |
23 |
import es.unex.sextante.exceptions.NullParameterAdditionalInfoException; |
24 |
import es.unex.sextante.gui.core.SextanteGUI; |
25 |
import es.unex.sextante.gui.modeler.ModelerPanel; |
26 |
import es.unex.sextante.gui.r.RAlgorithmProvider; |
27 |
import es.unex.sextante.gui.settings.SextanteRSettings; |
28 |
import es.unex.sextante.parameters.Parameter; |
29 |
import es.unex.sextante.parameters.ParameterNumericalValue; |
30 |
|
31 |
public class NumericalValuePanel |
32 |
extends
|
33 |
ParameterPanel { |
34 |
|
35 |
private static int typeFloat = 0; |
36 |
private static int typeInteger = 1; |
37 |
|
38 |
private JCheckBox jCheckBoxMin; |
39 |
private JCheckBox jCheckBoxMax; |
40 |
private JLabel jLabelType; |
41 |
private JTextField jTextFieldMin; |
42 |
private JTextField jTextFieldMax; |
43 |
private JTextField jTextFieldDefault; |
44 |
private JComboBox jComboBoxType; |
45 |
private JLabel jLabelDefault; |
46 |
|
47 |
|
48 |
public NumericalValuePanel(final JDialog parent, |
49 |
final ModelerPanel panel) {
|
50 |
|
51 |
super(parent, panel);
|
52 |
|
53 |
} |
54 |
|
55 |
|
56 |
public NumericalValuePanel(final ModelerPanel panel) { |
57 |
|
58 |
super(panel);
|
59 |
|
60 |
} |
61 |
|
62 |
|
63 |
@Override
|
64 |
protected void initGUI() { |
65 |
|
66 |
super.initGUI();
|
67 |
|
68 |
super.setTitle(Sextante.getText("modeler_add_par_numerical")); |
69 |
|
70 |
super.setPreferredSize(new java.awt.Dimension(400, 290)); |
71 |
|
72 |
try {
|
73 |
{ |
74 |
final TableLayout thisLayout = new TableLayout(new double[][] { |
75 |
{ TableLayoutConstants.MINIMUM, |
76 |
5.0,
|
77 |
TableLayoutConstants.FILL }, |
78 |
{ TableLayoutConstants.MINIMUM, |
79 |
1.0,
|
80 |
TableLayoutConstants.MINIMUM, |
81 |
1.0,
|
82 |
TableLayoutConstants.MINIMUM, |
83 |
1.0,
|
84 |
TableLayoutConstants.MINIMUM } }); |
85 |
thisLayout.setHGap(5);
|
86 |
thisLayout.setVGap(5);
|
87 |
jPanelMiddle.setLayout(thisLayout); |
88 |
{ |
89 |
jCheckBoxMin = new JCheckBox(); |
90 |
jCheckBoxMin.setText(Sextante.getText("Min_value"));
|
91 |
jPanelMiddle.add(jCheckBoxMin, "0, 0");
|
92 |
jCheckBoxMin.addActionListener(new ActionListener() { |
93 |
public void actionPerformed(final ActionEvent arg0) { |
94 |
if (jCheckBoxMin.isSelected() == true) { |
95 |
jTextFieldMin.setEnabled(true);
|
96 |
jTextFieldMin.setEditable(true);
|
97 |
} else {
|
98 |
jTextFieldMin.setEnabled(false);
|
99 |
jTextFieldMin.setEditable(false);
|
100 |
} |
101 |
} |
102 |
}); |
103 |
} |
104 |
{ |
105 |
jCheckBoxMax = new JCheckBox(); |
106 |
jCheckBoxMax.setText(Sextante.getText("Max_value"));
|
107 |
jPanelMiddle.add(jCheckBoxMax, "0, 2");
|
108 |
jCheckBoxMax.addActionListener(new ActionListener() { |
109 |
public void actionPerformed(final ActionEvent arg0) { |
110 |
if (jCheckBoxMax.isSelected() == true) { |
111 |
jTextFieldMax.setEnabled(true);
|
112 |
jTextFieldMax.setEditable(true);
|
113 |
} else {
|
114 |
jTextFieldMax.setEnabled(false);
|
115 |
jTextFieldMax.setEditable(false);
|
116 |
} |
117 |
} |
118 |
}); |
119 |
} |
120 |
{ |
121 |
jTextFieldMin = new JTextField(); |
122 |
if ( jCheckBoxMin.isSelected() == false ) { |
123 |
jTextFieldMin.setEnabled(false);
|
124 |
jTextFieldMin.setEditable(false);
|
125 |
} else {
|
126 |
jTextFieldMin.setEnabled(true);
|
127 |
jTextFieldMin.setEditable(true);
|
128 |
} |
129 |
jPanelMiddle.add(jTextFieldMin, "2, 0");
|
130 |
jTextFieldMin.addFocusListener(new FocusAdapter() { |
131 |
@Override
|
132 |
public void focusLost(final FocusEvent e) { |
133 |
checkTextFieldContent((JTextField) e.getSource());
|
134 |
} |
135 |
}); |
136 |
} |
137 |
{ |
138 |
jTextFieldMax = new JTextField(); |
139 |
if ( jCheckBoxMax.isSelected() == false ) { |
140 |
jTextFieldMax.setEnabled(false);
|
141 |
jTextFieldMax.setEditable(false);
|
142 |
} else {
|
143 |
jTextFieldMax.setEnabled(true);
|
144 |
jTextFieldMax.setEditable(true);
|
145 |
} |
146 |
jPanelMiddle.add(jTextFieldMax, "2, 2");
|
147 |
jTextFieldMax.addFocusListener(new FocusAdapter() { |
148 |
@Override
|
149 |
public void focusLost(final FocusEvent e) { |
150 |
checkTextFieldContent((JTextField) e.getSource());
|
151 |
} |
152 |
}); |
153 |
} |
154 |
{ |
155 |
jLabelDefault = new JLabel(); |
156 |
jPanelMiddle.add(jLabelDefault, "0, 4");
|
157 |
jLabelDefault.setText(Sextante.getText("Default_value"));
|
158 |
|
159 |
jTextFieldDefault = new JTextField(); |
160 |
jPanelMiddle.add(jTextFieldDefault, "2, 4");
|
161 |
jTextFieldDefault.addFocusListener(new FocusAdapter() { |
162 |
@Override
|
163 |
public void focusLost(final FocusEvent e) { |
164 |
checkTextFieldContent((JTextField) e.getSource());
|
165 |
} |
166 |
}); |
167 |
} |
168 |
{ |
169 |
jLabelType = new JLabel(); |
170 |
jPanelMiddle.add(jLabelType, "0, 6");
|
171 |
jLabelType.setText(Sextante.getText("Value_type"));
|
172 |
} |
173 |
{ |
174 |
final ComboBoxModel jComboBoxTypeModel = |
175 |
new DefaultComboBoxModel(new String[] { Sextante.getText("Float"), |
176 |
Sextante.getText("Integer") });
|
177 |
jComboBoxType = new JComboBox(); |
178 |
jPanelMiddle.add(jComboBoxType, "2, 6");
|
179 |
jComboBoxType.setModel(jComboBoxTypeModel); |
180 |
} |
181 |
} |
182 |
} |
183 |
catch (final Exception e) { |
184 |
Sextante.addErrorToLog(e); |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
|
190 |
/* Parses a text field containing a number and returns
|
191 |
the result as a truncated double value */
|
192 |
private double truncNumeric ( String number ) throws Exception { |
193 |
Integer Value;
|
194 |
try {
|
195 |
Value = Integer.parseInt(number);
|
196 |
} catch (final Exception e) { |
197 |
throw e;
|
198 |
} |
199 |
return ( (double) Value.intValue() ); |
200 |
} |
201 |
|
202 |
|
203 |
@Override
|
204 |
protected boolean prepareParameter() { |
205 |
|
206 |
double dMax, dMin;
|
207 |
double dAbsoluteMax, dAbsoluteMin;
|
208 |
double dDefault;
|
209 |
int iType;
|
210 |
|
211 |
try {
|
212 |
if (jComboBoxType.getSelectedIndex() == typeInteger ) {
|
213 |
iType = AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER; |
214 |
dAbsoluteMin = Integer.MIN_VALUE;
|
215 |
dAbsoluteMax = Integer.MAX_VALUE;
|
216 |
} |
217 |
else {
|
218 |
iType = AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE; |
219 |
dAbsoluteMin = Double.NEGATIVE_INFINITY;
|
220 |
dAbsoluteMax = Double.MAX_VALUE;
|
221 |
} |
222 |
if ( iType == AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER ) {
|
223 |
/* truncate to integer before saving */
|
224 |
dDefault = truncNumeric (jTextFieldDefault.getText()); |
225 |
} else {
|
226 |
dDefault = Double.parseDouble(jTextFieldDefault.getText());
|
227 |
} |
228 |
if (jCheckBoxMin.isSelected()) {
|
229 |
if ( iType == AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER ) {
|
230 |
/* truncate to integer before saving */
|
231 |
dMin = truncNumeric (jTextFieldMin.getText()); |
232 |
} else {
|
233 |
dMin = Double.parseDouble(jTextFieldMin.getText());
|
234 |
} |
235 |
} |
236 |
else {
|
237 |
dMin = dAbsoluteMin; |
238 |
} |
239 |
if (jCheckBoxMax.isSelected()) {
|
240 |
if ( iType == AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER ) {
|
241 |
/* truncate to integer before saving */
|
242 |
dMax = truncNumeric (jTextFieldMax.getText()); |
243 |
} else {
|
244 |
dMax = Double.parseDouble(jTextFieldMax.getText());
|
245 |
} |
246 |
} |
247 |
else {
|
248 |
dMax = dAbsoluteMax; |
249 |
} |
250 |
if ( dDefault < dMin || dDefault > dMax || dMin > dMax ) {
|
251 |
JOptionPane.showMessageDialog(null, Sextante.getText("Invalid_parameters"), Sextante.getText("Warning"), |
252 |
JOptionPane.WARNING_MESSAGE);
|
253 |
return false; |
254 |
} |
255 |
} |
256 |
catch (final Exception e) { |
257 |
JOptionPane.showMessageDialog(null, Sextante.getText("Invalid_parameters"), Sextante.getText("Warning"), |
258 |
JOptionPane.WARNING_MESSAGE);
|
259 |
return false; |
260 |
} |
261 |
|
262 |
final String sDescription = jTextFieldDescription.getText(); |
263 |
|
264 |
if (sDescription.length() != 0) { |
265 |
final AdditionalInfoNumericalValue addInfo = new AdditionalInfoNumericalValue(iType, dDefault, Math.min(dMin, dMax), Math.max( |
266 |
dMin, dMax)); |
267 |
m_Parameter = new ParameterNumericalValue();
|
268 |
m_Parameter.setParameterAdditionalInfo(addInfo); |
269 |
m_Parameter.setParameterDescription(jTextFieldDescription.getText()); |
270 |
|
271 |
m_Parameter.setColorR(m_Color.getRed()); |
272 |
m_Parameter.setColorG(m_Color.getGreen()); |
273 |
m_Parameter.setColorB(m_Color.getBlue()); |
274 |
m_Parameter.setColorAlpha(m_Color.getAlpha()); |
275 |
|
276 |
return true; |
277 |
} |
278 |
else {
|
279 |
JOptionPane.showMessageDialog(null, Sextante.getText("Invalid_description"), Sextante.getText("Warning"), |
280 |
JOptionPane.WARNING_MESSAGE);
|
281 |
return false; |
282 |
} |
283 |
|
284 |
} |
285 |
|
286 |
|
287 |
@Override
|
288 |
public void setParameter(final Parameter param) { |
289 |
|
290 |
super.setParameter(param);
|
291 |
|
292 |
boolean isInt = false; |
293 |
|
294 |
try {
|
295 |
final AdditionalInfoNumericalValue ai = (AdditionalInfoNumericalValue) param.getParameterAdditionalInfo();
|
296 |
if (ai.getType() == AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER) {
|
297 |
jComboBoxType.setSelectedIndex(typeInteger); |
298 |
isInt = true;
|
299 |
} |
300 |
else {
|
301 |
jComboBoxType.setSelectedIndex(typeFloat); |
302 |
} |
303 |
if (ai.getMaxValue() != Double.MAX_VALUE && ai.getMaxValue() != Integer.MAX_VALUE) { |
304 |
jCheckBoxMax.setSelected(true);
|
305 |
jTextFieldMax.setEnabled(true);
|
306 |
jTextFieldMax.setEditable(true);
|
307 |
if ( isInt == false ) { |
308 |
jTextFieldMax.setText(Double.toString(ai.getMaxValue()));
|
309 |
} else {
|
310 |
jTextFieldMax.setText(Integer.toString((int)ai.getMaxValue())); |
311 |
} |
312 |
} |
313 |
if (ai.getMinValue() != Double.NEGATIVE_INFINITY && ai.getMinValue() != Integer.MIN_VALUE) { |
314 |
jCheckBoxMin.setSelected(true);
|
315 |
jTextFieldMin.setEnabled(true);
|
316 |
jTextFieldMin.setEditable(true);
|
317 |
if ( isInt == false ) { |
318 |
jTextFieldMin.setText(Double.toString(ai.getMinValue()));
|
319 |
} else {
|
320 |
jTextFieldMin.setText(Integer.toString((int)ai.getMinValue())); |
321 |
} |
322 |
} |
323 |
if ( isInt == false ) { |
324 |
jTextFieldDefault.setText(Double.toString(ai.getDefaultValue()));
|
325 |
} else {
|
326 |
jTextFieldDefault.setText(Integer.toString((int)ai.getDefaultValue())); |
327 |
} |
328 |
} |
329 |
catch (final NullParameterAdditionalInfoException e) { |
330 |
e.printStackTrace(); |
331 |
} |
332 |
|
333 |
} |
334 |
|
335 |
|
336 |
@Override
|
337 |
public String getParameterDescription() { |
338 |
|
339 |
return Sextante.getText("Numerical_value"); |
340 |
|
341 |
} |
342 |
|
343 |
|
344 |
private void checkTextFieldContent(final JTextField textField) { |
345 |
|
346 |
final String content = textField.getText(); |
347 |
if (content.length() != 0) { |
348 |
try {
|
349 |
Double.parseDouble(content);
|
350 |
return;
|
351 |
} |
352 |
catch (final NumberFormatException nfe) { |
353 |
getToolkit().beep(); |
354 |
textField.requestFocus(); |
355 |
} |
356 |
} |
357 |
|
358 |
|
359 |
} |
360 |
|
361 |
|
362 |
@Override
|
363 |
public boolean parameterCanBeAdded() { |
364 |
|
365 |
return true; |
366 |
|
367 |
} |
368 |
|
369 |
} |