Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / settings / SextanteGrassSettingsPanel.java @ 119

History | View | Annotate | Download (24.2 KB)

1
package es.unex.sextante.gui.settings;
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.image.BufferedImage;
10
import java.io.BufferedReader;
11
import java.io.File;
12
import java.io.FileInputStream;
13
import java.io.InputStreamReader;
14
import java.net.URL;
15
import java.util.HashMap;
16

    
17
import javax.imageio.ImageIO;
18
import javax.swing.ImageIcon;
19
import javax.swing.JButton;
20
import javax.swing.JCheckBox;
21
import javax.swing.JLabel;
22
import javax.swing.JOptionPane;
23
import javax.swing.JPanel;
24
import javax.swing.JSeparator;
25
import javax.swing.SwingConstants;
26

    
27
import es.unex.sextante.core.GeoAlgorithm;
28
import es.unex.sextante.core.Sextante;
29
import es.unex.sextante.gui.algorithm.FileSelectionPanel;
30
import es.unex.sextante.gui.core.SextanteGUI;
31
import es.unex.sextante.gui.exceptions.WrongGrassFolderException;
32
import es.unex.sextante.gui.exceptions.WrongGrassMapsetFolderException;
33
import es.unex.sextante.gui.exceptions.WrongGrassWinShellException;
34
import es.unex.sextante.gui.grass.GrassAlgorithmProvider;
35

    
36
public class SextanteGrassSettingsPanel extends SettingPanel {
37

    
38
        // Officially supported GRASS version range
39
        public static final int MAJOR_MIN = 6;
40
        public static final int MAJOR_MAX = 6;
41
        public static final int MINOR_MIN = 4;
42
        public static final int MINOR_MAX = 4;
43

    
44
        private JCheckBox jActivateCheckBox;
45
        private JCheckBox jPortableCheckBox;
46
        private JLabel jLabelGrassFolder;
47
        private FileSelectionPanel jGrassFolder;
48
        private JLabel jLabelWinShell;
49
        private FileSelectionPanel jWinShellFile;
50
        private JLabel jLabelUpdate;
51
        private JButton jButtonUpdate;
52
        private JCheckBox jCheckBoxLatLon;
53
        private JCheckBox jCheckBox3DV;
54
        private JCheckBox jCheckBoxNoVectBBox;
55
        private JCheckBox jCheckBoxInPolylines;
56
        private JCheckBox jCheckBoxCleanPolygons;
57
        private JCheckBox jCheckBoxSextanteNull;
58
        private JCheckBox jCheckBoxCompatibility;
59

    
60
        @Override
61
        protected void initGUI() {
62

    
63
                final TableLayout thisLayout = new TableLayout(new double[][] {
64
                                { SextanteConfigurationDialog.SPACER_SMALL,
65
                                                TableLayoutConstants.FILL, TableLayoutConstants.FILL,
66
                                                SextanteConfigurationDialog.SPACER_SMALL },
67
                                {
68
                                                SextanteConfigurationDialog.SPACER_SMALL,
69
                                                TableLayoutConstants.MINIMUM, // row 1
70
                                                TableLayoutConstants.MINIMUM, // row 2
71
                                                TableLayoutConstants.MINIMUM, // row 3
72
                                                TableLayoutConstants.MINIMUM, // row 4
73
                                                TableLayoutConstants.MINIMUM, // row 5
74
                                                TableLayoutConstants.MINIMUM, // row 6
75
                                                TableLayoutConstants.MINIMUM, // row 7
76
                                                TableLayoutConstants.MINIMUM, // row 8
77
                                                TableLayoutConstants.MINIMUM, // row 9
78
                                                TableLayoutConstants.MINIMUM, // row 10
79
                                                TableLayoutConstants.MINIMUM, // row 11
80
                                                TableLayoutConstants.MINIMUM, // row 12
81
                                                TableLayoutConstants.MINIMUM, // row 13
82
                                                TableLayoutConstants.MINIMUM, // row 14
83
                                                TableLayoutConstants.MINIMUM, // row 15
84
                                                TableLayoutConstants.FILL,
85
                                                TableLayoutConstants.MINIMUM, // row 17
86
                                                SextanteConfigurationDialog.SPACER_SMALL } });
87
                thisLayout.setHGap(5);
88
                thisLayout.setVGap(5);
89
                this.setLayout(thisLayout);
90
                {
91
                        jActivateCheckBox = new JCheckBox(Sextante.getText("ActivateProvider") + " GRASS GIS");
92
                        final String sActivate = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_ACTIVATE);
93
                        final boolean bActivate = Boolean.parseBoolean(sActivate);
94
                        jActivateCheckBox.setSelected(bActivate);
95
                        this.add(jActivateCheckBox, "1, 1");
96

    
97
                        this.add(new JSeparator(SwingConstants.HORIZONTAL), "1, 2, 2, 2");
98
                        /* ----------------------------------------------------------- */
99

    
100
                        jPortableCheckBox = new JCheckBox(Sextante.getText("Portable"));
101
                        final String sActivatePortable = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_PORTABLE);
102
                        final boolean bActivatePortable = Boolean.parseBoolean(sActivatePortable);
103
                        jPortableCheckBox.setEnabled(bActivate);
104
                        jPortableCheckBox.setSelected(bActivatePortable);
105
                        this.add(jPortableCheckBox, "1, 3");
106

    
107
                        jLabelGrassFolder = new JLabel();
108
                        jLabelGrassFolder.setEnabled(bActivate);
109
                        if (bActivatePortable == true) {
110
                                jLabelGrassFolder.setEnabled(false);
111
                        }
112
                        this.add(jLabelGrassFolder, "1, 4");
113
                        jLabelGrassFolder.setText(Sextante.getText("GRASS_folder"));
114
                        jGrassFolder = new FileSelectionPanel(true, true, (String[]) null, Sextante.getText("selector_choose_folder"));
115
                        jGrassFolder.getTextField().setEnabled(bActivate);
116
                        jGrassFolder.getButton().setEnabled(bActivate);
117
                        if (bActivatePortable == true) {
118
                                jGrassFolder.getTextField().setEnabled(false);
119
                                jGrassFolder.getButton().setEnabled(false);
120
                        }
121
                        final String sFolder = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
122
                        jGrassFolder.setFilepath(sFolder);
123
                        this.add(jGrassFolder, "2, 4");
124

    
125
                        /* the file selector for sh.exe will only be active if we are running on Windows */
126
                        jLabelWinShell = new JLabel();
127
                        jLabelWinShell.setEnabled(bActivate && Sextante.isWindows());
128
                        if (bActivatePortable == true) {
129
                                jLabelWinShell.setEnabled(false);
130
                        }
131
                        this.add(jLabelWinShell, "1, 5");
132
                        jLabelWinShell.setText("Windows " + Sextante.getText("grass_windows_shell"));
133
                        jWinShellFile = new FileSelectionPanel(true, true, new String[] {"sh.exe", null}, Sextante.getText("selector_choose_file"));
134
                        jWinShellFile.getTextField().setEnabled(bActivate && Sextante.isWindows());
135
                        jWinShellFile.getButton().setEnabled(bActivate && Sextante.isWindows());
136
                        if (bActivatePortable == true) {
137
                                jWinShellFile.getTextField().setEnabled(false);
138
                                jWinShellFile.getButton().setEnabled(false);
139
                        }
140
                        final String sScriptsFolder = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_WIN_SHELL);
141
                        jWinShellFile.setFilepath(sScriptsFolder);
142
                        this.add(jWinShellFile, "2, 5");
143

    
144
                        this.add(new JSeparator(SwingConstants.HORIZONTAL), "1, 6, 2, 6");
145
                        /* ----------------------------------------------------------- */
146

    
147
                        jLabelUpdate = new JLabel();
148
                        jLabelUpdate.setText( Sextante.getText("update_library") );
149
                        jLabelUpdate.setEnabled(bActivate);
150
                        this.add(jLabelUpdate, "1, 7");
151
                        jButtonUpdate = new JButton(Sextante.getText("load_GRASS_modules"));
152
                        jButtonUpdate.setEnabled(bActivate);
153
                        this.add(jButtonUpdate, "2, 7");
154

    
155
                        this.add(new JSeparator(SwingConstants.HORIZONTAL), "1, 8, 2, 8");
156
                        /* ----------------------------------------------------------- */
157

    
158
                        jCheckBoxLatLon = new JCheckBox(Sextante.getText ("GRASS_lat_lon_mode"));
159
                        jCheckBoxLatLon.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
160
                                        (SextanteGrassSettings.GRASS_LAT_LON_MODE)).booleanValue());
161
                        this.add(jCheckBoxLatLon, "1, 9, 2, 9");
162

    
163
                        jCheckBox3DV = new JCheckBox();
164
                        jCheckBox3DV.setText(Sextante.getText("grass_input_3d"));
165
                        jCheckBox3DV.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
166
                                        (SextanteGrassSettings.GRASS_3D_V_MODE)).booleanValue());
167
                        this.add(jCheckBox3DV, "1, 10, 2, 10");
168

    
169
                        jCheckBoxNoVectBBox = new JCheckBox(Sextante.getText("grass_no_vect_bbox"));
170
                        jCheckBoxNoVectBBox.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
171
                                        (SextanteGrassSettings.GRASS_NO_VECT_BBOX)).booleanValue());
172
                        this.add(jCheckBoxNoVectBBox, "1, 11, 2, 11");
173

    
174
                        jCheckBoxInPolylines = new JCheckBox();
175
                        jCheckBoxInPolylines.setText(Sextante.getText("grass_import_polylines"));
176
                        jCheckBoxInPolylines.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
177
                                        (SextanteGrassSettings.GRASS_IN_POLYLINES)).booleanValue());
178
                        this.add(jCheckBoxInPolylines, "1, 12, 2, 12");
179

    
180
                        jCheckBoxCleanPolygons = new JCheckBox();
181
                        jCheckBoxCleanPolygons.setText(Sextante.getText("grass_clean_polygons"));
182
                        jCheckBoxCleanPolygons.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
183
                                        (SextanteGrassSettings.GRASS_CLEAN_POLYGONS)).booleanValue());
184
                        this.add(jCheckBoxCleanPolygons, "1, 13, 2, 13");
185

    
186
                        jCheckBoxSextanteNull = new JCheckBox();
187
                        jCheckBoxSextanteNull.setText(Sextante.getText("grass_use_sextante_null"));
188
                        jCheckBoxSextanteNull.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
189
                                        (SextanteGrassSettings.GRASS_USE_SEXTANTE_NULL)).booleanValue());
190
                        this.add(jCheckBoxSextanteNull, "1, 14, 2, 14");
191

    
192
                        jCheckBoxCompatibility = new JCheckBox();
193
                        jCheckBoxCompatibility.setText(Sextante.getText("grass_compatibility_mode"));
194
                        jCheckBoxCompatibility.setSelected(new Boolean(SextanteGUI.getSettingParameterValue
195
                                        (SextanteGrassSettings.GRASS_COMPATIBILITY_MODE)).booleanValue());
196
                        this.add(jCheckBoxCompatibility, "1, 15, 2, 15");
197

    
198
                }
199

    
200
                /* add provider logo and URL */
201
                final URL res = getClass().getClassLoader().getResource("images/grass.smlogo.gif");
202
                if (res != null) {
203
                        final ImageIcon logo = new ImageIcon(res);
204
                        JLabel logoLabel = new JLabel(logo);
205
                        logoLabel.setIconTextGap(4);
206
                        logoLabel.setVerticalTextPosition(SwingConstants.BOTTOM);
207
                        logoLabel.setText("<html><i><a href=http://grass.osgeo.org/>http://grass.osgeo.org/</a></i></html>");
208
                        this.add(logoLabel,"1, 17, 2, 17");
209
                }
210

    
211
                /**********************************/
212
                /** Action listeners for widgets **/
213
                /**********************************/
214
                {
215
                        jActivateCheckBox.addActionListener(new ActionListener() {
216
                                public void actionPerformed(final ActionEvent arg0) {
217
                                        setCursor(new Cursor(Cursor.WAIT_CURSOR));
218
                                        SextanteGUI.setSettingParameterValue(
219
                                                        SextanteGrassSettings.GRASS_ACTIVATE, new Boolean(
220
                                                                        jActivateCheckBox.isSelected()).toString());
221
                                        SextanteGUI.updateAlgorithmProvider(GrassAlgorithmProvider.class);
222
                                        /* toggle remaining widgets on or off */
223
                                        boolean active = jActivateCheckBox.isSelected();
224
                                        jPortableCheckBox.setEnabled(active);
225
                                        jLabelGrassFolder.setEnabled(active);
226
                                        jGrassFolder.getTextField().setEnabled(active);
227
                                        jGrassFolder.getButton().setEnabled(active);
228
                                        if ( Sextante.isWindows()) {
229
                                                jLabelWinShell.setEnabled(active);
230
                                                jWinShellFile.getTextField().setEnabled(active);
231
                                                jWinShellFile.getButton().setEnabled(active);
232
                                        }
233
                                        jLabelUpdate.setEnabled(active);
234
                                        jButtonUpdate.setEnabled(active);
235
                                        jCheckBoxLatLon.setEnabled(active);
236
                                        jCheckBox3DV.setEnabled(active);
237
                                        jCheckBoxNoVectBBox.setEnabled(active);
238
                                        jCheckBoxInPolylines.setEnabled(active);
239
                                        jCheckBoxCleanPolygons.setEnabled(active);
240
                                        jCheckBoxSextanteNull.setEnabled(active);
241
                                        jCheckBoxCompatibility.setEnabled(active);
242
                                        jActivateCheckBox.getParent().repaint();
243
                                        active = jPortableCheckBox.isSelected();
244
                                        if ( active == true ) {
245
                                                jLabelGrassFolder.setEnabled(false);
246
                                                jGrassFolder.getTextField().setEnabled(false);
247
                                                jGrassFolder.getButton().setEnabled(false);
248
                                                if ( Sextante.isWindows()) {
249
                                                        jLabelWinShell.setEnabled(false);
250
                                                        jWinShellFile.getTextField().setEnabled(false);
251
                                                        jWinShellFile.getButton().setEnabled(false);
252
                                                }
253
                                        }
254
                                        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
255
                                }
256
                        });
257
                }
258

    
259
              jPortableCheckBox.addActionListener(new ActionListener() {
260
                  public void actionPerformed(final ActionEvent arg0) {
261
                     setCursor(new Cursor(Cursor.WAIT_CURSOR));
262
                     SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_PORTABLE,
263
                              new Boolean(jPortableCheckBox.isSelected()).toString());
264
                     //Set portable GRASS bin dir
265
                     SextanteGUI.checkDir ( Sextante.PORTABLE_GRASS_FOLDER, true, "GRASS GIS" );
266
                     String sPath = new String (SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_GRASS_FOLDER);
267
                     SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER, sPath);
268
                     jGrassFolder.setFilepath(sPath);
269
                     if ( Sextante.isWindows()) {
270
                             //Set portable sh.exe file
271
                             SextanteGUI.checkFile ( Sextante.PORTABLE_MSYS_FILE, false, "GRASS GIS shell scripts" );
272
                             sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_MSYS_FILE;
273
                             SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_WIN_SHELL, sPath);
274
                             jWinShellFile.setFilepath(sPath);
275
                     }
276
                     //Activate/deactivate the remaining widgets on this page
277
                     final boolean active = jPortableCheckBox.isSelected();
278
                     if ( active == true ) {
279
                                        jLabelGrassFolder.setEnabled(false);
280
                                        jGrassFolder.getTextField().setEnabled(false);
281
                                        jGrassFolder.getButton().setEnabled(false);
282
                                        if ( Sextante.isWindows()) {
283
                                                jLabelWinShell.setEnabled(false);
284
                                                jWinShellFile.getTextField().setEnabled(false);
285
                                                jWinShellFile.getButton().setEnabled(false);
286
                                        }
287
                     } else {
288
                                        jLabelGrassFolder.setEnabled(true);
289
                                        jGrassFolder.getTextField().setEnabled(true);
290
                                        jGrassFolder.getButton().setEnabled(true);
291
                                        if ( Sextante.isWindows()) {
292
                                                jLabelWinShell.setEnabled(true);
293
                                                jWinShellFile.getTextField().setEnabled(true);
294
                                                jWinShellFile.getButton().setEnabled(true);
295
                                        }
296
                                }
297
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
298
                  }
299
               });
300

    
301
              /* trigger update of GRASS modules registry */
302
              jButtonUpdate.addActionListener(new ActionListener() {
303
                      public void actionPerformed(final ActionEvent evt) {
304
                              jButtonSetupGRASSActionPerformed(evt);
305
                      }
306
              });
307
        }
308

    
309

    
310
        @Override
311
        public HashMap<String, String> getValues() {
312

    
313
                final HashMap<String, String> map = new HashMap<String, String>();
314

    
315
                map.put(SextanteGrassSettings.GRASS_ACTIVATE, new Boolean(jActivateCheckBox.isSelected()).toString());
316
                map.put(SextanteGrassSettings.GRASS_PORTABLE, new Boolean(jPortableCheckBox.isSelected()).toString());
317
                final String GrassPath = jWinShellFile.getFilepath();
318
                if (GrassPath != null) {
319
                        map.put(SextanteGrassSettings.GRASS_FOLDER, jGrassFolder.getFilepath());
320
                }
321
                if (Sextante.isWindows()) {
322
                        final String shellPath = jWinShellFile.getFilepath();
323
                        if (shellPath != null) {
324
                                map.put(SextanteGrassSettings.GRASS_WIN_SHELL, shellPath);
325
                        }
326
                }
327
                map.put(SextanteGrassSettings.GRASS_LAT_LON_MODE, new Boolean(jCheckBoxLatLon.isSelected()).toString());
328
                map.put(SextanteGrassSettings.GRASS_3D_V_MODE, new Boolean(jCheckBox3DV.isSelected()).toString());
329
                map.put(SextanteGrassSettings.GRASS_NO_VECT_BBOX, new Boolean(jCheckBoxNoVectBBox.isSelected()).toString());
330
                map.put(SextanteGrassSettings.GRASS_IN_POLYLINES, new Boolean(jCheckBoxInPolylines.isSelected()).toString());
331
                map.put(SextanteGrassSettings.GRASS_CLEAN_POLYGONS, new Boolean(jCheckBoxCleanPolygons.isSelected()).toString());
332
                map.put(SextanteGrassSettings.GRASS_USE_SEXTANTE_NULL, new Boolean(jCheckBoxSextanteNull.isSelected()).toString());
333
                map.put(SextanteGrassSettings.GRASS_COMPATIBILITY_MODE, new Boolean(jCheckBoxCompatibility.isSelected()).toString());
334
                return map;
335
        }
336

    
337
        // Attempt to setup GRASS
338
        private void jButtonSetupGRASSActionPerformed(final ActionEvent evt) {
339

    
340
                // we set these values here in advance, since they are needed to perform
341
                // grass initialization. Have to look for a workaround to avoid this...
342
                final HashMap<String, String> map = getValues();
343
                SextanteGUI.setSettings(map);
344

    
345
                GrassAlgorithmProvider.deleteDescriptionFiles();
346
                GrassAlgorithmProvider.deleteAlgorithms();
347

    
348
                boolean failed = false;
349
                // 1: GRASS installation folder
350
                try {
351
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
352
                        checkGrassFolder();
353
                        this.setCursor(Cursor.getDefaultCursor());
354
                } catch (final WrongGrassFolderException e) {
355
                        this.setCursor(Cursor.getDefaultCursor());
356
                        JOptionPane.showMessageDialog(null, Sextante
357
                                        .getText("grass_error_binaries_folder"), Sextante
358
                                        .getText("grass_error_title"), JOptionPane.ERROR_MESSAGE);
359
                        jGrassFolder.setFilepath("");
360
                        failed = true;
361
                } finally {
362
                        this.setCursor(Cursor.getDefaultCursor());
363
                }
364
                // 2: On Windows: sh.exe
365
                if (Sextante.isWindows() && (failed == false)) {
366
                        try {
367
                                this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
368
                                if (checkGrassWinShell(jWinShellFile.getFilepath()) > 0) {
369
                                        this.setCursor(Cursor.getDefaultCursor());
370
                                        JOptionPane.showMessageDialog(null, Sextante
371
                                                        .getText("grass_warning_missing_cmd"), Sextante
372
                                                        .getText("grass_warning_title"),
373
                                                        JOptionPane.WARNING_MESSAGE);
374
                                }
375
                                this.setCursor(Cursor.getDefaultCursor());
376
                        } catch (final WrongGrassWinShellException e) {
377
                                this.setCursor(Cursor.getDefaultCursor());
378
                                JOptionPane.showMessageDialog(null, Sextante
379
                                                .getText("grass_error_win_shell_binary")
380
                                                + "\n" + Sextante.getText("grass_shell_url"), Sextante
381
                                                .getText("grass_error_title"),
382
                                                JOptionPane.ERROR_MESSAGE);
383
                                failed = true;
384
                                GrassAlgorithmProvider.deleteAlgorithms();
385
                                SextanteGUI.getGUIFactory().updateToolbox();
386
                                jWinShellFile.setFilepath("");
387
                        } finally {
388
                                this.setCursor(Cursor.getDefaultCursor());
389
                        }
390
                }
391

    
392
                // 3: GRASS version
393
                if (!isSupported() && (failed == false)) {
394
                        this.setCursor(Cursor.getDefaultCursor());
395
                        JOptionPane.showMessageDialog(null, Sextante
396
                                        .getText("grass_warning_version"), Sextante
397
                                        .getText("grass_warning_title"),
398
                                        JOptionPane.WARNING_MESSAGE);
399
                }
400

    
401
                // Setup GRASS!!!
402
                int num_algs = 0;
403
                try {
404
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
405
                        num_algs = GrassAlgorithmProvider
406
                                        .createAlgorithmsDescriptionFiles();
407
                        this.setCursor(Cursor.getDefaultCursor());
408
                } catch (final Exception e) {
409
                        this.setCursor(Cursor.getDefaultCursor());
410
                        JOptionPane.showMessageDialog(null, Sextante
411
                                        .getText("grass_error_setup"), "",
412
                                        JOptionPane.ERROR_MESSAGE);
413
                } finally {
414
                        this.setCursor(Cursor.getDefaultCursor());
415
                }
416
                this.setCursor(Cursor.getDefaultCursor());
417
                if (num_algs > 0) {
418
                        // Success
419
                        SextanteGUI.updateAlgorithmProvider(GrassAlgorithmProvider.class);
420
                        final HashMap<String, GeoAlgorithm> algs = Sextante.getAlgorithms()
421
                                        .get("GRASS");
422
                        int iNumAlgs = 0;
423
                        if (algs != null) {
424
                                iNumAlgs = algs.size();
425
                        }
426
                        JOptionPane.showMessageDialog(null, Sextante
427
                                        .getText("grass_info_setup_success")
428
                                        + " " + iNumAlgs + ". ", Sextante
429
                                        .getText("grass_info_title"),
430
                                        JOptionPane.INFORMATION_MESSAGE);
431
                }
432
        }
433

    
434
        private void checkGrassFolder() throws WrongGrassFolderException {
435

    
436
                final String grassFolder = jGrassFolder.getFilepath();
437
                // Minimal set of GRASS modules we need
438
                final String[] check_modules = { "g.region", "g.remove", "r.in.gdal",
439
                                "r.info", "r.null", "r.out.gdal", "v.in.ogr", "v.info",
440
                                "v.out.ogr", null };
441

    
442
                // Check if this is actually a valid shell and throw an error, if it's
443
                // not.
444
                if (grassFolder == null) {
445
                        throw new WrongGrassFolderException();
446
                }
447

    
448
                if (grassFolder.length() < 2) {
449
                        throw new WrongGrassFolderException();
450
                }
451

    
452
                if (grassFolder.trim().equals("")) {
453
                        throw new WrongGrassFolderException();
454
                }
455

    
456
                File check = new File(grassFolder);
457
                if (!check.exists()) {
458
                        throw new WrongGrassFolderException();
459
                }
460

    
461
                check = new File(grassFolder + File.separator + "etc" + File.separator
462
                                + "VERSIONNUMBER");
463
                if (!check.exists()) {
464
                        throw new WrongGrassFolderException();
465
                }
466

    
467
                // Check for minimal set of GRASS modules
468
                int i = 0;
469
                while (check_modules[i] != null) {
470
                        if (Sextante.isUnix() || Sextante.isMacOSX()) {
471
                                check = new File(grassFolder + File.separator + "bin"
472
                                                + File.separator + check_modules[i]);
473
                        } else {
474
                                check = new File(grassFolder + File.separator + "bin"
475
                                                + File.separator + check_modules[i] + ".exe");
476
                        }
477
                        if (!check.exists()) {
478
                                throw new WrongGrassFolderException();
479
                        }
480
                        i++;
481
                }
482

    
483
        }
484

    
485
        private int checkGrassWinShell(final String grassWinShell)
486
                        throws WrongGrassWinShellException {
487

    
488
                int num_missing = 0;
489

    
490
                // Minimal set of command line tools we need to run GRASS scripts
491
                final String[] check_commands = { "which", "gawk.exe", "cut.exe",
492
                                "grep.exe", "basename.exe", "sed.exe", "install.exe",
493
                                "curl.exe", "bc.exe", "wc.exe", "paste.exe", "head.exe",
494
                                "tail.exe", "cat.exe", "expr.exe", "xargs.exe", "ls.exe",
495
                                "sort.exe", "cs2cs.exe", "gdalwarp.exe", "unzip.exe" };
496

    
497
                // r.in.aster needs gdalwarp
498
                // r.in.srtm needs unzip
499
                // bc.exe needs readline5.dll
500
                // v.in.garmin needs gpstrans and gardump
501
                // v.in/out.gpsbabel need gpsbabel
502
                // some scripts need curl
503
                // some scripts need cs2cs
504

    
505
                // Check if this is actually a valid shell and throw an error, if it's
506
                // not.
507
                if (grassWinShell == null) {
508
                        throw new WrongGrassWinShellException();
509
                }
510

    
511
                if (grassWinShell.length() < 2) {
512
                        throw new WrongGrassWinShellException();
513
                }
514

    
515
                if (grassWinShell.trim().equals("")) {
516
                        throw new WrongGrassWinShellException();
517
                }
518

    
519
                final File check = new File(grassWinShell);
520
                if (!check.exists()) {
521
                        throw new WrongGrassWinShellException();
522
                }
523

    
524
                // Look for required (recommended) binaries in "bin" folders of MSYS
525
                // and GRASS installation.
526
                // Note: if they are missing here, they could still be in some
527
                // user-defined
528
                // folder in %PATH%, so we only issue a warning and log all "missing"
529
                // binary names.
530
                String shToolsPath = grassWinShell;
531
                shToolsPath = shToolsPath.substring(0, shToolsPath
532
                                .lastIndexOf(File.separator));
533
                String GrassPath = jGrassFolder.getFilepath();
534
                GrassPath = GrassPath + File.separator + "bin";
535
                for (final String element : check_commands) {
536
                        boolean missing = false;
537
                        final File check_sh = new File(shToolsPath + File.separator
538
                                        + element);
539
                        final File check_grass = new File(GrassPath + File.separator
540
                                        + element);
541
                        if (!check_sh.exists() && !check_grass.exists()) {
542
                                missing = true;
543
                        }
544
                        if (missing == true) {
545
                                Sextante
546
                                                .addWarningToLog("SEXTANTE GRASS interface: External command "
547
                                                                + element
548
                                                                + " not found in either '"
549
                                                                + shToolsPath + "' or '" + GrassPath + "'");
550
                                num_missing++;
551
                        }
552
                }
553

    
554
                return (num_missing);
555
        }
556

    
557
        /*
558
         * Returns the major version number of the GRASS version we are running.
559
         * Returns "-1" if anything goes wrong.
560
         */
561
        private static int getGrassMajorVersion() {
562

    
563
                final String sFolder = SextanteGUI
564
                                .getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
565
                InputStreamReader isr = null;
566
                BufferedReader br = null;
567
                try {
568
                        final File file = new File(sFolder + File.separator + "etc"
569
                                        + File.separator + "VERSIONNUMBER");
570
                        isr = new InputStreamReader(new FileInputStream(file));
571
                        br = new BufferedReader(isr);
572
                        final String sLine = br.readLine();
573
                        final String[] sNumbers = sLine.split("\\.");
574
                        final String sMajor = sNumbers[0];
575
                        return (Integer.parseInt(sMajor));
576
                } catch (final Exception e) {
577
                        return (-1);
578
                } finally {
579
                        try {
580
                                br.close();
581
                                isr.close();
582
                        } catch (final Exception e) {
583
                                return (-1);
584
                        }
585
                }
586
        }
587

    
588
        /*
589
         * Returns the minor version number of the GRASS version we are running.
590
         * Returns "-1" if anything goes wrong.
591
         */
592
        private static int getGrassMinorVersion() {
593

    
594
                final String sFolder = SextanteGUI
595
                                .getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
596
                InputStreamReader isr = null;
597
                BufferedReader br = null;
598
                try {
599
                        final File file = new File(sFolder + File.separator + "etc"
600
                                        + File.separator + "VERSIONNUMBER");
601
                        isr = new InputStreamReader(new FileInputStream(file));
602
                        br = new BufferedReader(isr);
603
                        final String sLine = br.readLine();
604
                        final String[] sNumbers = sLine.split("\\.");
605
                        final String sMinor = sNumbers[1];
606
                        return (Integer.parseInt(sMinor));
607
                } catch (final Exception e) {
608
                        return (-1);
609
                } finally {
610
                        try {
611
                                br.close();
612
                                isr.close();
613
                        } catch (final Exception e) {
614
                                return (-1);
615
                        }
616
                }
617
        }
618

    
619
        /*
620
         * Returns true only if the detected GRASS version is not too old or too
621
         * young to be supported by this interface.
622
         */
623
        public static boolean isSupported() {
624
                if ((getGrassMajorVersion() < MAJOR_MIN)
625
                                || (getGrassMajorVersion() > MAJOR_MAX)) {
626
                        return (false);
627
                }
628
                if ((getGrassMinorVersion() < MINOR_MIN)
629
                                || (getGrassMinorVersion() > MINOR_MAX)) {
630
                        return (false);
631
                }
632
                return (true);
633
        }
634

    
635
}