Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / algorithm / AnalysisExtentPanel.java @ 338

History | View | Annotate | Download (23.4 KB)

1 59 nbrodin
package es.unex.sextante.gui.algorithm;
2
3
import info.clearthought.layout.TableLayout;
4
import info.clearthought.layout.TableLayoutConstants;
5
6
import java.awt.Dimension;
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
import java.awt.event.KeyAdapter;
12
import java.awt.event.KeyEvent;
13
import java.awt.geom.Rectangle2D;
14
15
import javax.swing.BorderFactory;
16
import javax.swing.ButtonGroup;
17
import javax.swing.ComboBoxModel;
18
import javax.swing.DefaultComboBoxModel;
19
import javax.swing.JButton;
20
import javax.swing.JComboBox;
21
import javax.swing.JLabel;
22
import javax.swing.JPanel;
23
import javax.swing.JRadioButton;
24
import javax.swing.JTextField;
25
26
import es.unex.sextante.core.AnalysisExtent;
27
import es.unex.sextante.core.GeoAlgorithm;
28
import es.unex.sextante.core.NamedExtent;
29
import es.unex.sextante.core.Sextante;
30
import es.unex.sextante.dataObjects.ILayer;
31
import es.unex.sextante.dataObjects.IRasterLayer;
32
import es.unex.sextante.exceptions.WrongAnalysisExtentException;
33
import es.unex.sextante.gui.core.SextanteGUI;
34
import es.unex.sextante.gui.exceptions.TooLargeGridExtentException;
35
import es.unex.sextante.gui.grass.GrassAlgorithm;
36
import es.unex.sextante.outputs.Output;
37
import es.unex.sextante.outputs.OutputRasterLayer;
38
39
/**
40 153 mcompany
 * A panel to select a extent to be used for generating output layers from a
41
 * given algorithm
42 59 nbrodin
 *
43
 * @author volaya
44
 */
45 153 mcompany
public class AnalysisExtentPanel extends javax.swing.JPanel {
46 59 nbrodin
47 153 mcompany
  protected static final int BIG_SIZE = 10000000;
48 59 nbrodin
49 153 mcompany
  private JTextField jTextFieldCols;
50 59 nbrodin
51 153 mcompany
  private JTextField jTextFieldRows;
52 59 nbrodin
53 153 mcompany
  protected JTextField jTextFieldCellSize;
54 59 nbrodin
55 153 mcompany
  protected JTextField jTextFieldXMax;
56 59 nbrodin
57 153 mcompany
  protected JTextField jTextFieldXMin;
58 59 nbrodin
59 153 mcompany
  protected JTextField jTextFieldYMax;
60 59 nbrodin
61 153 mcompany
  protected JTextField jTextFieldYMin;
62 59 nbrodin
63 153 mcompany
  private JLabel jLabelRowsCols;
64 59 nbrodin
65 153 mcompany
  private JLabel jLabelCellSize;
66 59 nbrodin
67 153 mcompany
  private JLabel jLabelRangeY;
68 59 nbrodin
69 153 mcompany
  private JLabel jLabelRangeX;
70 59 nbrodin
71 153 mcompany
  protected JComboBox jComboBoxLayers;
72 59 nbrodin
73 153 mcompany
  private JComboBox jComboBoxViews;
74 59 nbrodin
75 153 mcompany
  protected JPanel jPanelAnalysisExtentValues;
76 59 nbrodin
77 153 mcompany
  private JPanel jPanelAnalysisExtentOptions;
78 59 nbrodin
79 153 mcompany
  private JRadioButton jRadioButtonAdjustToInputDataExtent;
80 59 nbrodin
81 153 mcompany
  private JRadioButton jRadioButtonViewExtent;
82 59 nbrodin
83 153 mcompany
  private JRadioButton jRadioButtonUserDefinedExtent;
84 59 nbrodin
85 153 mcompany
  private JRadioButton jRadioButtonExtentFromLayer;
86 59 nbrodin
87 153 mcompany
  private ButtonGroup jButtonGroup;
88 59 nbrodin
89 153 mcompany
  protected final GeoAlgorithm m_Algorithm;
90 59 nbrodin
91 153 mcompany
  protected final ILayer[] m_Layers;
92 59 nbrodin
93 153 mcompany
  /**
94
   * Creates a new panel
95
   *
96
   * @param algorithm the algorithm
97
   */
98
  public AnalysisExtentPanel(final GeoAlgorithm algorithm) {
99 59 nbrodin
100 153 mcompany
    super();
101 59 nbrodin
102 153 mcompany
    m_Layers = SextanteGUI.getInputFactory().getLayers();
103
    m_Algorithm = algorithm;
104 59 nbrodin
105 153 mcompany
    initGUI();
106 59 nbrodin
107 153 mcompany
  }
108 59 nbrodin
109 153 mcompany
  private void initGUI() {
110 59 nbrodin
111 153 mcompany
    final TableLayout jPanelParametersRasterLayout = new TableLayout(
112
        new double[][] {
113
            { 1.0, TableLayoutConstants.FILL, 1.0 },
114
            { 1.0, TableLayoutConstants.MINIMUM, TableLayoutConstants.FILL, 1.0 } });
115
    jPanelParametersRasterLayout.setHGap(5);
116
    jPanelParametersRasterLayout.setVGap(5);
117
    this.setLayout(jPanelParametersRasterLayout);
118 59 nbrodin
119 153 mcompany
    this.add(getJPanelAnalysisExtentOptions(), "1, 1");
120
    this.add(getJPanelAnalysisExtentValues(), "1, 2");
121
    if (m_Algorithm.canDefineOutputExtentFromInput()) {
122
      getJPanelAnalysisExtentValues().setVisible(false);
123
    }
124
    else {
125
      getJPanelAnalysisExtentValues().setVisible(true);
126
    }
127 59 nbrodin
128 153 mcompany
  }
129 59 nbrodin
130 153 mcompany
  /**
131
   * Assigns the selected extent to the algorithm
132
   *
133
   * @throws TooLargeGridExtentException if the dimensions of the extent are too
134
   *           large (too many cells) and the user should be queried to make
135
   *           sure that the input values are correct
136
   * @throws WrongAnalysisExtentException if the input values are not correct
137
   */
138
  public void assignExtent() throws TooLargeGridExtentException,
139
      WrongAnalysisExtentException {
140 59 nbrodin
141 153 mcompany
    m_Algorithm.setAnalysisExtent((AnalysisExtent) null);
142 59 nbrodin
143 153 mcompany
    if (getJRadioButtonAdjustToInputDataExtent().isSelected()) {
144
      if (!m_Algorithm.adjustOutputExtent()) {
145
        throw new WrongAnalysisExtentException(
146
            Sextante.getText("Wrong_or_missing_region"));
147
      }
148
      else {
149
        return;
150
      }
151 59 nbrodin
152 153 mcompany
    }
153 59 nbrodin
154 153 mcompany
    final AnalysisExtent outputExtent = new AnalysisExtent();
155 315 omartinez
    boolean recalculateForCell = m_Algorithm.getRecalculateForCell();
156 153 mcompany
    try {
157
      outputExtent
158
          .setCellSize(Double.parseDouble(jTextFieldCellSize.getText()));
159
      outputExtent.setXRange(Double.parseDouble(jTextFieldXMin.getText()),
160 315 omartinez
          Double.parseDouble(jTextFieldXMax.getText()), recalculateForCell);
161 153 mcompany
      outputExtent.setYRange(Double.parseDouble(jTextFieldYMin.getText()),
162 315 omartinez
          Double.parseDouble(jTextFieldYMax.getText()), recalculateForCell);
163 153 mcompany
      m_Algorithm.setAnalysisExtent(outputExtent);
164
    }
165
    catch (final NumberFormatException e) {
166
      // Sextante.addErrorToLog(e);
167
      if (m_Algorithm instanceof GrassAlgorithm) {
168
        // GRASS vector output modules can always fall back to a default extent
169
        outputExtent.setXRange(0.0, 1.0, true);
170
        outputExtent.setYRange(0.0, 1.0, true);
171
        m_Algorithm.setAnalysisExtent(outputExtent);
172
        if (m_Algorithm.getName().contains("v.in.region")) {
173
          // we absolutely need a region for these commands!
174
          throw new WrongAnalysisExtentException(
175
              Sextante.getText("Wrong_or_missing_region"));
176
        }
177
        for (int i = 0; i < m_Algorithm.getNumberOfOutputObjects(); i++) {
178
          final Output out = m_Algorithm.getOutputObjects().getOutput(i);
179
          // Raster import modules also do not need an extent setting
180
          if ((out instanceof OutputRasterLayer)
181
              && !m_Algorithm.getName().contains("r.in.")) {
182
            // But if we have another raster output, then we need an extent!
183
            throw new WrongAnalysisExtentException(
184
                Sextante.getText("Wrong_or_missing_region"));
185
          }
186
        }
187 59 nbrodin
      }
188 153 mcompany
      else {
189
        throw new WrongAnalysisExtentException(
190
            Sextante.getText("Wrong_or_missing_raster_extent_definition"));
191
      }
192
    }
193 59 nbrodin
194 153 mcompany
    final int numCells = outputExtent.getNX() * outputExtent.getNY();
195
    /*
196
     * benducke: let's just assume the other knows what (s)he is doing... if
197
     * ((numCells > BIG_SIZE) || (Integer.MAX_VALUE / outputExtent.getNY() <
198
     * outputExtent.getNX())) { throw new
199
     * TooLargeGridExtentException(Sextante.getText
200
     * ("The_selected_grid_extent_seems_too_large") + "(" +
201
     * Integer.toString(outputExtent.getNX()) + " X " +
202
     * Integer.toString(outputExtent.getNY()) + ")\n" +
203
     * Sextante.getText("Are_you_sure_you_want_to_use_it?")); }
204
     */
205
  }
206 59 nbrodin
207 153 mcompany
  private ButtonGroup getJButtonGroup() {
208 59 nbrodin
209 153 mcompany
    if (jButtonGroup == null) {
210
      jButtonGroup = new ButtonGroup();
211
    }
212
    return jButtonGroup;
213 59 nbrodin
214 153 mcompany
  }
215 59 nbrodin
216 153 mcompany
  protected JRadioButton getJRadioButtonUserDefinedExtent() {
217 59 nbrodin
218 153 mcompany
    if (jRadioButtonUserDefinedExtent == null) {
219
      jRadioButtonUserDefinedExtent = new JRadioButton();
220
      jRadioButtonUserDefinedExtent.setText(Sextante.getText("User_defined"));
221
      jRadioButtonUserDefinedExtent
222
          .addActionListener(new java.awt.event.ActionListener() {
223 59 nbrodin
224 153 mcompany
            public void actionPerformed(final java.awt.event.ActionEvent e) {
225
              setTextFieldsEnabled(true);
226
              extentHasChanged();
227
            }
228
          });
229
    }
230 59 nbrodin
231 153 mcompany
    return jRadioButtonUserDefinedExtent;
232
  }
233 59 nbrodin
234 153 mcompany
  protected JRadioButton getJRadioButtonAdjustToInputDataExtent() {
235 59 nbrodin
236 153 mcompany
    if (jRadioButtonAdjustToInputDataExtent == null) {
237
      jRadioButtonAdjustToInputDataExtent = new JRadioButton();
238
      jRadioButtonAdjustToInputDataExtent.setText(Sextante
239
          .getText("Fit_to_input_layers"));
240
      jRadioButtonAdjustToInputDataExtent
241
          .addActionListener(new java.awt.event.ActionListener() {
242 59 nbrodin
243 153 mcompany
            public void actionPerformed(final java.awt.event.ActionEvent e) {
244
              getJPanelAnalysisExtentValues().setVisible(false);
245 59 nbrodin
            }
246 153 mcompany
          });
247
    }
248 59 nbrodin
249 153 mcompany
    return jRadioButtonAdjustToInputDataExtent;
250
  }
251 59 nbrodin
252 153 mcompany
  private JRadioButton getJRadioButtonViewExtent() {
253
    if (jRadioButtonViewExtent == null) {
254
      jRadioButtonViewExtent = new JRadioButton();
255
      jRadioButtonViewExtent.setText(Sextante.getText("Use_extent_from_view"));
256
      jRadioButtonViewExtent
257
          .addActionListener(new java.awt.event.ActionListener() {
258 59 nbrodin
259
            public void actionPerformed(final java.awt.event.ActionEvent evt) {
260 153 mcompany
              try {
261
                getJComboBoxViews().setSelectedIndex(
262
                    getJComboBoxViews().getSelectedIndex());
263
                setTextFieldsEnabled(false);
264
              }
265
              catch (final Exception e) {}
266 59 nbrodin
            }
267 153 mcompany
          });
268
    }
269
    return jRadioButtonViewExtent;
270
  }
271 59 nbrodin
272 153 mcompany
  JRadioButton getJRadioButtonExtentFromLayer() {
273
    if (jRadioButtonExtentFromLayer == null) {
274
      jRadioButtonExtentFromLayer = new JRadioButton();
275
      jRadioButtonExtentFromLayer.setText(Sextante
276
          .getText("Use_extent_from_layer"));
277
      jRadioButtonExtentFromLayer
278
          .addActionListener(new java.awt.event.ActionListener() {
279 59 nbrodin
280 153 mcompany
            public void actionPerformed(final java.awt.event.ActionEvent e) {
281
              getJComboBoxLayers().setSelectedIndex(
282
                  getJComboBoxLayers().getSelectedIndex());
283
              setTextFieldsEnabled(false);
284
            }
285
          });
286
    }
287
    return jRadioButtonExtentFromLayer;
288
  }
289 59 nbrodin
290 153 mcompany
  protected JPanel getJPanelAnalysisExtentOptions() {
291
    if (jPanelAnalysisExtentOptions == null) {
292
      jPanelAnalysisExtentOptions = new JPanel();
293
      final TableLayout jPanelRasterExtentOptionsLayout = new TableLayout(
294
          new double[][] {
295
              { 253.0, TableLayoutConstants.FILL },
296
              { TableLayoutConstants.MINIMUM, TableLayoutConstants.MINIMUM,
297
                  TableLayoutConstants.MINIMUM, TableLayoutConstants.MINIMUM } });
298
      jPanelRasterExtentOptionsLayout.setHGap(5);
299
      jPanelRasterExtentOptionsLayout.setVGap(5);
300
      jPanelAnalysisExtentOptions.setLayout(jPanelRasterExtentOptionsLayout);
301
      jPanelAnalysisExtentOptions.setPreferredSize(new java.awt.Dimension(660,
302
          200));
303
      jPanelAnalysisExtentOptions.setBorder(BorderFactory
304
          .createTitledBorder(Sextante.getText("Extent_from")));
305
      jPanelAnalysisExtentOptions.add(getJRadioButtonUserDefinedExtent(),
306
          "0, 1");
307
      jPanelAnalysisExtentOptions.add(getJRadioButtonViewExtent(), "0, 2");
308
      jPanelAnalysisExtentOptions.add(getJRadioButtonExtentFromLayer(), "0, 3");
309
      getJButtonGroup().add(getJRadioButtonUserDefinedExtent());
310
      getJButtonGroup().add(getJRadioButtonViewExtent());
311
      getJButtonGroup().add(getJRadioButtonExtentFromLayer());
312
      if (m_Algorithm.canDefineOutputExtentFromInput()) {
313
        jPanelAnalysisExtentOptions.add(
314
            getJRadioButtonAdjustToInputDataExtent(), "0, 0");
315
        getJButtonGroup().add(getJRadioButtonAdjustToInputDataExtent());
316
        getJRadioButtonAdjustToInputDataExtent().setSelected(true);
317 59 nbrodin
      }
318 153 mcompany
      else {
319
        getJRadioButtonUserDefinedExtent().setSelected(true);
320
      }
321
      jPanelAnalysisExtentOptions.add(getJComboBoxViews(), "1, 2");
322
      jPanelAnalysisExtentOptions.add(getJComboBoxLayers(), "1, 3");
323 59 nbrodin
324 153 mcompany
    }
325
    return jPanelAnalysisExtentOptions;
326
  }
327 59 nbrodin
328 153 mcompany
  protected JPanel getJPanelAnalysisExtentValues() {
329 59 nbrodin
330 153 mcompany
    if (jPanelAnalysisExtentValues == null) {
331
      jPanelAnalysisExtentValues = new JPanel();
332
      final TableLayout jPanelRasterExtentValuesLayout = new TableLayout(
333
          new double[][] {
334
              { 255.0, TableLayoutConstants.FILL, TableLayoutConstants.FILL },
335
              { TableLayoutConstants.MINIMUM, TableLayoutConstants.MINIMUM,
336
                  TableLayoutConstants.MINIMUM, TableLayoutConstants.MINIMUM,
337
                  TableLayoutConstants.FILL } });
338
      jPanelRasterExtentValuesLayout.setHGap(5);
339
      jPanelRasterExtentValuesLayout.setVGap(5);
340
      jPanelAnalysisExtentValues.setLayout(jPanelRasterExtentValuesLayout);
341
      jPanelAnalysisExtentValues.setBorder(BorderFactory
342
          .createTitledBorder(Sextante.getText("Extent__values")));
343
      jPanelAnalysisExtentValues.setPreferredSize(new java.awt.Dimension(660,
344
          145));
345
      jPanelAnalysisExtentValues.add(getJLabelRangeX(), "0, 0");
346
      jPanelAnalysisExtentValues.add(getJLabelCellSize(), "0, 2");
347
      jPanelAnalysisExtentValues.add(getJTextFieldCellSize(), "1, 2");
348
      jPanelAnalysisExtentValues.add(getJTextFieldXMin(), "1, 0");
349
      jPanelAnalysisExtentValues.add(getJTextFieldXMax(), "2, 0");
350
      jPanelAnalysisExtentValues.add(getJTextFieldYMin(), "1, 1");
351
      jPanelAnalysisExtentValues.add(getJTextFieldYMax(), "2, 1");
352
      jPanelAnalysisExtentValues.add(getJLabelRangeY(), "0, 1");
353
      jPanelAnalysisExtentValues.add(getJLabelRowsCols(), "0, 3");
354
      jPanelAnalysisExtentValues.add(getJTextFieldRows(), "1, 3");
355
      jPanelAnalysisExtentValues.add(getJTextFieldCols(), "2, 3");
356
      jPanelAnalysisExtentValues.add(getJButtonExtentFromPoints(), "2,2");
357
    }
358
    return jPanelAnalysisExtentValues;
359
  }
360
361
  private JButton getJButtonExtentFromPoints() {
362
363
    final JButton button = new JButton("...");
364
    button.setSize(new Dimension(30, 30));
365
    button.addActionListener(new ActionListener() {
366
367
      public void actionPerformed(final ActionEvent arg0) {
368
        final ExtentFromSavedPointsDialog dialog = new ExtentFromSavedPointsDialog();
369
        dialog.pack();
370
        dialog.setVisible(true);
371
        final double[] ret = dialog.getPoints();
372
        if (ret != null) {
373
          getJTextFieldXMin().setText(Double.toString(ret[0]));
374
          getJTextFieldYMin().setText(Double.toString(ret[1]));
375
          getJTextFieldXMax().setText(Double.toString(ret[2]));
376
          getJTextFieldYMax().setText(Double.toString(ret[3]));
377
        }
378 59 nbrodin
      }
379 153 mcompany
    });
380
    return button;
381
  }
382 59 nbrodin
383 153 mcompany
  protected JComboBox getJComboBoxLayers() {
384 59 nbrodin
385 153 mcompany
    if (jComboBoxLayers == null) {
386
      getJRadioButtonExtentFromLayer().setEnabled(m_Layers.length > 0);
387
      final ComboBoxModel jComboBoxLayersModel = new DefaultComboBoxModel(
388
          m_Layers);
389
      jComboBoxLayers = new JComboBox();
390
      jComboBoxLayers.setModel(jComboBoxLayersModel);
391
      jComboBoxLayers.setEnabled(m_Layers.length > 0);
392 59 nbrodin
393 153 mcompany
      jComboBoxLayers.addActionListener(new java.awt.event.ActionListener() {
394 59 nbrodin
395 153 mcompany
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
396
          double dCoord;
397
          if (getJRadioButtonExtentFromLayer().isSelected()) {
398
            try {
399
              final ILayer layer = (ILayer) jComboBoxLayers.getSelectedItem();
400
              Rectangle2D fullExtent = layer.getFullExtent();
401
              dCoord = fullExtent.getMinX();
402
              getJTextFieldXMin().setText(new Double(dCoord).toString());
403
              dCoord = fullExtent.getMaxX();
404
              getJTextFieldXMax().setText(new Double(dCoord).toString());
405
              dCoord = fullExtent.getMinY();
406
              getJTextFieldYMin().setText(new Double(dCoord).toString());
407
              dCoord = fullExtent.getMaxY();
408
              getJTextFieldYMax().setText(new Double(dCoord).toString());
409
              if (layer instanceof IRasterLayer) {
410
                getJTextFieldCellSize().setText(
411
                    new Double(((IRasterLayer) layer).getLayerCellSize())
412
                        .toString());
413
              }
414
              extentHasChanged();
415 59 nbrodin
            }
416 153 mcompany
            catch (final Exception e) {}
417
          }
418
        }
419 59 nbrodin
      });
420 153 mcompany
    }
421
    return jComboBoxLayers;
422
  }
423 59 nbrodin
424 153 mcompany
  private JComboBox getJComboBoxViews() {
425 59 nbrodin
426 153 mcompany
    if (jComboBoxViews == null) {
427
      NamedExtent[] predefinedExtents = SextanteGUI.getInputFactory()
428
          .getPredefinedExtents();
429
      boolean enabled = predefinedExtents.length > 1 || (predefinedExtents.length == 1 && predefinedExtents[0] != null);
430
      getJRadioButtonViewExtent().setEnabled(enabled);
431
      final ComboBoxModel jComboBoxViewsModel = new DefaultComboBoxModel(
432
          predefinedExtents);
433
      jComboBoxViews = new JComboBox();
434
      jComboBoxViews.setModel(jComboBoxViewsModel);
435
      jComboBoxViews.setEnabled(enabled);
436 59 nbrodin
437 153 mcompany
      jComboBoxViews.addActionListener(new java.awt.event.ActionListener() {
438 59 nbrodin
439 153 mcompany
        public void actionPerformed(final java.awt.event.ActionEvent evt) {
440
          if (getJRadioButtonViewExtent().isSelected()) {
441
            try {
442
              final NamedExtent ne = (NamedExtent) jComboBoxViews
443
                  .getSelectedItem();
444
              if (ne != null) {
445
                final Rectangle2D extent = ne.getExtent();
446
                getJTextFieldXMin().setText(
447
                    new Double(extent.getMinX()).toString());
448
                getJTextFieldXMax().setText(
449
                    new Double(extent.getMaxX()).toString());
450
                getJTextFieldYMin().setText(
451
                    new Double(extent.getMinY()).toString());
452
                getJTextFieldYMax().setText(
453
                    new Double(extent.getMaxY()).toString());
454
                setTextFieldsEnabled(false);
455
                extentHasChanged();
456
              }
457
            }
458
            catch (final Exception e) {
459
              Sextante.addErrorToLog(e);
460
            }
461
          }
462
        }
463
      });
464
    }
465
    return jComboBoxViews;
466
  }
467 59 nbrodin
468 153 mcompany
  protected JLabel getJLabelRangeX() {
469 59 nbrodin
470 153 mcompany
    if (jLabelRangeX == null) {
471
      jLabelRangeX = new JLabel();
472
      jLabelRangeX.setText(Sextante.getText("Range_X"));
473
    }
474
    return jLabelRangeX;
475 59 nbrodin
476 153 mcompany
  }
477 59 nbrodin
478 153 mcompany
  protected JTextField getNewJTextField() {
479 59 nbrodin
480 153 mcompany
    final JTextField jTextField = new JTextField();
481
    jTextField.addFocusListener(new FocusAdapter() {
482 59 nbrodin
483 153 mcompany
      @Override
484
      public void focusLost(final FocusEvent e) {
485
        final JTextField textField = (JTextField) e.getSource();
486
        final String content = textField.getText();
487
        if (content.length() != 0) {
488
          try {
489
            final double d = Double.parseDouble(content);
490
            extentHasChanged();
491
          }
492
          catch (final NumberFormatException nfe) {
493
            getToolkit().beep();
494
            textField.requestFocus();
495
          }
496
        }
497 59 nbrodin
      }
498 153 mcompany
    });
499
    jTextField.addKeyListener(new KeyAdapter() {
500 59 nbrodin
501 153 mcompany
      @Override
502
      public void keyTyped(final KeyEvent event) {
503
        validateKeyTyping(event);
504 59 nbrodin
      }
505 153 mcompany
    });
506 59 nbrodin
507 153 mcompany
    return jTextField;
508
  }
509 59 nbrodin
510 153 mcompany
  protected JTextField getJTextFieldXMin() {
511
    if (jTextFieldXMin == null) {
512
      jTextFieldXMin = getNewJTextField();
513
    }
514
    return jTextFieldXMin;
515
  }
516 59 nbrodin
517 153 mcompany
  protected JTextField getJTextFieldXMax() {
518
    if (jTextFieldXMax == null) {
519
      jTextFieldXMax = getNewJTextField();
520
    }
521
    return jTextFieldXMax;
522
  }
523 59 nbrodin
524 153 mcompany
  protected JTextField getJTextFieldYMin() {
525
    if (jTextFieldYMin == null) {
526
      jTextFieldYMin = getNewJTextField();
527
    }
528
    return jTextFieldYMin;
529
  }
530 59 nbrodin
531 153 mcompany
  protected JTextField getJTextFieldYMax() {
532
    if (jTextFieldYMax == null) {
533
      jTextFieldYMax = getNewJTextField();
534
    }
535
    return jTextFieldYMax;
536
  }
537 59 nbrodin
538 153 mcompany
  protected JLabel getJLabelRangeY() {
539
    if (jLabelRangeY == null) {
540
      jLabelRangeY = new JLabel();
541
      jLabelRangeY.setText(Sextante.getText("Range_Y"));
542
    }
543
    return jLabelRangeY;
544
  }
545 59 nbrodin
546 153 mcompany
  protected JLabel getJLabelCellSize() {
547
    if (jLabelCellSize == null) {
548
      jLabelCellSize = new JLabel();
549
      jLabelCellSize.setText(Sextante.getText("Cell_size"));
550
    }
551
    return jLabelCellSize;
552
  }
553 59 nbrodin
554 153 mcompany
  protected JTextField getJTextFieldCellSize() {
555
    if (jTextFieldCellSize == null) {
556
      jTextFieldCellSize = new JTextField();
557
      jTextFieldCellSize.setText("1");
558
      jTextFieldCellSize.addFocusListener(new FocusAdapter() {
559 59 nbrodin
560 153 mcompany
        @Override
561
        public void focusLost(final FocusEvent e) {
562
          final JTextField textField = (JTextField) e.getSource();
563
          final String content = textField.getText();
564
          if (content.length() != 0) {
565
            try {
566
              final double d = Double.parseDouble(content);
567
              if (d <= 0) {
568
                throw new NumberFormatException();
569
              }
570
              else {
571
                extentHasChanged();
572
              }
573
            }
574
            catch (final NumberFormatException nfe) {
575
              getToolkit().beep();
576
              textField.requestFocus();
577
            }
578
          }
579 59 nbrodin
580 153 mcompany
        }
581
      });
582
      jTextFieldCellSize.addKeyListener(new KeyAdapter() {
583 59 nbrodin
584 153 mcompany
        @Override
585
        public void keyTyped(final KeyEvent event) {
586
          validateKeyTyping(event);
587
        }
588
      });
589
    }
590
    return jTextFieldCellSize;
591
  }
592 59 nbrodin
593 153 mcompany
  protected JLabel getJLabelRowsCols() {
594
    if (jLabelRowsCols == null) {
595
      jLabelRowsCols = new JLabel();
596
      jLabelRowsCols.setText(Sextante.getText("Number_of_rows-cols"));
597
    }
598
    return jLabelRowsCols;
599
  }
600 59 nbrodin
601 153 mcompany
  protected JTextField getJTextFieldRows() {
602
    if (jTextFieldRows == null) {
603
      jTextFieldRows = new JTextField();
604
      jTextFieldRows.setEnabled(false);
605
    }
606
    return jTextFieldRows;
607
  }
608 59 nbrodin
609 153 mcompany
  protected JTextField getJTextFieldCols() {
610
    if (jTextFieldCols == null) {
611
      jTextFieldCols = new JTextField();
612
      jTextFieldCols.setEnabled(false);
613
    }
614
    return jTextFieldCols;
615
  }
616 59 nbrodin
617 153 mcompany
  private void validateKeyTyping(final KeyEvent event) {
618
    String text = ((JTextField) event.getSource()).getText();
619
    switch (event.getKeyChar()) {
620
      case KeyEvent.VK_ENTER:
621
        extentHasChanged();
622
        break;
623
      default:
624
        text += event.getKeyChar();
625
        break;
626
    }
627
  }
628 59 nbrodin
629 153 mcompany
  protected void setTextFieldsEnabled(final boolean bEnabled) {
630 59 nbrodin
631 153 mcompany
    getJPanelAnalysisExtentValues().setVisible(true);
632
    getJTextFieldXMin().setEnabled(bEnabled);
633
    getJTextFieldXMax().setEnabled(bEnabled);
634
    getJTextFieldYMin().setEnabled(bEnabled);
635
    getJTextFieldYMax().setEnabled(bEnabled);
636 59 nbrodin
637 153 mcompany
  }
638 59 nbrodin
639 153 mcompany
  protected void extentHasChanged() {
640 59 nbrodin
641 153 mcompany
    double dRangeX;
642
    double dRangeY;
643
    double dCellSize;
644
    int iRows;
645
    int iCols;
646 59 nbrodin
647 153 mcompany
    try {
648
      dRangeX = Math.abs(Double.parseDouble(getJTextFieldXMax().getText())
649
          - Double.parseDouble(getJTextFieldXMin().getText()));
650
      dRangeY = Math.abs(Double.parseDouble(getJTextFieldYMax().getText())
651
          - Double.parseDouble(getJTextFieldYMin().getText()));
652
      dCellSize = Double.parseDouble(getJTextFieldCellSize().getText());
653
      iRows = (int) Math.floor(dRangeY / dCellSize);
654
      iCols = (int) Math.floor(dRangeX / dCellSize);
655
      getJTextFieldRows().setText(Integer.toString(iRows));
656
      getJTextFieldCols().setText(Integer.toString(iCols));
657
    }
658
    catch (final NumberFormatException e) {
659
      return;
660
    }
661 59 nbrodin
662 153 mcompany
  }
663 59 nbrodin
664 153 mcompany
  public void setExtent(final AnalysisExtent ge) {
665 59 nbrodin
666 153 mcompany
    getJRadioButtonUserDefinedExtent().setSelected(true);
667
    setTextFieldsEnabled(true);
668 59 nbrodin
669 153 mcompany
    getJTextFieldCellSize().setText(Double.toString(ge.getCellSize()));
670
    getJTextFieldXMin().setText(Double.toString(ge.getXMin()));
671
    getJTextFieldXMax().setText(Double.toString(ge.getXMax()));
672
    getJTextFieldYMin().setText(Double.toString(ge.getYMin()));
673
    getJTextFieldYMax().setText(Double.toString(ge.getYMax()));
674
    extentHasChanged();
675 59 nbrodin
676 153 mcompany
  }
677 59 nbrodin
678 153 mcompany
  public void setAutoExtent() {
679 59 nbrodin
680 153 mcompany
    getJRadioButtonAdjustToInputDataExtent().setSelected(true);
681
    setTextFieldsEnabled(false);
682
    getJPanelAnalysisExtentValues().setVisible(false);
683 59 nbrodin
684 153 mcompany
  }
685 59 nbrodin
686
}