Statistics
| Revision:

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

History | View | Annotate | Download (34.8 KB)

1

    
2

    
3
package es.unex.sextante.gui.core;
4

    
5
import java.awt.Color;
6
import java.awt.Frame;
7
import java.io.BufferedReader;
8
import java.io.BufferedWriter;
9
import java.io.File;
10
import java.io.FileNotFoundException;
11
import java.io.FileReader;
12
import java.io.FileWriter;
13
import java.io.IOException;
14
import java.io.Writer;
15
import java.net.URL;
16
import java.util.ArrayList;
17
import java.util.HashMap;
18
import java.util.Iterator;
19
import java.util.Set;
20

    
21
import javax.swing.ImageIcon;
22
import javax.swing.JDialog;
23
import javax.swing.JOptionPane;
24

    
25
import es.unex.sextante.core.GeoAlgorithm;
26
import es.unex.sextante.core.IInputFactory;
27
import es.unex.sextante.core.OutputFactory;
28
import es.unex.sextante.core.Sextante;
29
import es.unex.sextante.gui.cmd.ScriptAlgorithmProvider;
30
import es.unex.sextante.gui.help.HelpIO;
31
import es.unex.sextante.gui.modeler.ModelerAlgorithmProvider;
32
import es.unex.sextante.gui.settings.Setting;
33
import es.unex.sextante.gui.settings.SextanteGeneralSettings;
34
import es.unex.sextante.gui.settings.SextanteGrassSettings;
35
import es.unex.sextante.gui.settings.SextanteModelerSettings;
36
import es.unex.sextante.gui.settings.SextanteRSettings;
37
import es.unex.sextante.gui.settings.SextanteSagaSettings;
38
import es.unex.sextante.gui.settings.SextanteScriptsSettings;
39

    
40

    
41
/**
42
 * This class centralizes most actions related to the SextanteGUI, containing methods to show dialogs and retrieve basic values
43
 * used by GUI elements
44
 */
45
public class SextanteGUI {
46
   public static String                               resourcePrefix              = "org.gvsig.toolbox";
47
   public static final int                            HISTORY                     = 0;
48
   public static final int                            COMMANDLINE                 = 1;
49

    
50
   private static IInputFactory                       m_InputFactory;
51
   private static OutputFactory                       m_OutputFactory;
52
   private static IGUIFactory                         m_GUIFactory                = new DefaultGUIFactory();
53

    
54
   private static IPostProcessTaskFactory             m_PostProcessTaskFactory;
55

    
56
   private static JDialog                             m_LastCommandOriginParentPanel;
57
   private static int                                 m_iLastCommandOrigin;
58
   private static Frame                               m_MainFrame;
59

    
60
   private static boolean                             m_bShowOnlyActiveAlgorithms = false;
61
   private static String                              m_sOutputFolder;
62
   private static HashMap<String, String>             m_Settings                  = new HashMap<String, String>();
63
   private static HashMap<String, String>             m_DefaultSettings           = new HashMap<String, String>();
64
   private static IDataRenderer                       m_Renderer;
65
   private static String m_sSextanteHomePath;
66
   private static String m_sSextanteInstallPath;
67

    
68

    
69
   private final static HashMap<String, Class>        m_ParametersPanel           = new HashMap<String, Class>();
70
   private final static HashMap<String, Class>        m_ModelerParametersPanel    = new HashMap<String, Class>();
71

    
72
   private final static ArrayList<IAlgorithmProvider> m_AlgorithmProviders        = new ArrayList<IAlgorithmProvider>();
73

    
74
   public final static ImageIcon                      SEXTANTE_ICON               = new ImageIcon(
75
                                                                                           SextanteGUI.class.getClassLoader().getResource(
76
                                                                                                    "images/module2.png"));
77

    
78
   public final static Color COLOR_WHITE = new Color(255, 255, 255, 255);
79
   public final static Color COLOR_GREY = new Color(128, 128, 128, 255);
80
   public final static Color COLOR_BLACK = new Color(0, 0, 0, 255);
81
   public final static Color COLOR_RED = new Color (255, 51, 51, 255);
82
   public final static Color COLOR_ORANGE = new Color(255, 103, 51, 255);
83
   public final static Color COLOR_YELLOW = new Color(255, 255, 51, 255);
84
   public final static Color COLOR_GREEN_LIGHT = new Color(51, 255, 51, 255);
85
   public final static Color COLOR_GREEN_DARK = new Color(0, 184, 0, 255);
86
   public final static Color COLOR_BLUE_LIGHT = new Color (102, 179, 255, 255);
87
   public final static Color COLOR_BLUE_DARK = new Color (0, 102, 204, 255);
88
   public final static Color COLOR_PINK = new Color(255, 153, 255, 255);
89
   public final static Color COLOR_PURPLE = new Color(163, 71, 255, 255);
90
   public final static Color COLOR_BROWN_LIGHT = new Color(255, 168, 82, 255);
91
   public final static Color COLOR_BROWN_DARK = new Color(204, 102, 0, 255);
92

    
93

    
94
   static {
95

    
96
      m_AlgorithmProviders.add(new ScriptAlgorithmProvider());
97
      m_AlgorithmProviders.add(new ModelerAlgorithmProvider());
98

    
99
   }
100

    
101

    
102
   /**
103
    * Sets a new main frame. This will be used as the parent frame by SEXTANTE GUI elements
104
    *
105
    * @param frame
106
    *                The main frame
107
    */
108
   public static void setMainFrame(final Frame frame) {
109

    
110
      m_MainFrame = frame;
111

    
112
   }
113

    
114

    
115
   /**
116
    * Returns the current main frame
117
    *
118
    * @return the current main frame
119
    */
120
   public static Frame getMainFrame() {
121

    
122
      return m_MainFrame;
123

    
124
   }
125

    
126

    
127
   /** ************************************************************ */
128

    
129
   /**
130
    * Returns the current input factory
131
    *
132
    * @see IInputFactory.
133
    * @return the current input factory
134
    */
135
   public static IInputFactory getInputFactory() {
136

    
137
      return m_InputFactory;
138

    
139
   }
140

    
141

    
142
   /**
143
    * Sets a new input factory as the current one
144
    *
145
    * @param inputFactory
146
    *                the new input factory
147
    */
148
   public static void setInputFactory(final IInputFactory inputFactory) {
149

    
150
      m_InputFactory = inputFactory;
151

    
152
   }
153

    
154

    
155
   /**
156
    * Returns the current OutputFactory
157
    *
158
    * @return the current OutputFactory
159
    */
160
   public static OutputFactory getOutputFactory() {
161

    
162
      return m_OutputFactory;
163

    
164
   }
165

    
166

    
167
   /**
168
    * sets a new output factory
169
    *
170
    * @param outputFactory
171
    *                the new output factory
172
    */
173
   public static void setOutputFactory(final OutputFactory outputFactory) {
174

    
175
      m_OutputFactory = outputFactory;
176
      m_OutputFactory.setDefaultNoDataValue(getDefaultNoDataValue());
177

    
178
   }
179

    
180

    
181
   /**
182
    * Returns the current GUIFactory
183
    *
184
    * @return the current GUIFactory
185
    */
186
   public static IGUIFactory getGUIFactory() {
187

    
188
      return m_GUIFactory;
189

    
190
   }
191

    
192

    
193
   /**
194
    * sets a new GUI factory
195
    *
196
    * @param guiFactory
197
    *                the new GUI factory
198
    */
199
   public static void setGUIFactory(final IGUIFactory guiFactory) {
200

    
201
      m_GUIFactory = guiFactory;
202

    
203
   }
204

    
205

    
206
   /**
207
    * Returns the task to post-process the algorithm outputs, usually to add them to the GUI of the GIS app.
208
    *
209
    * @param alg
210
    *                the algorithm to postprocess. Since this task will mainly deal with output results, the algorithm should have
211
    *                been previously executed, so it contains non-null output values
212
    * @param bShowResultsDialog
213
    *                if this parameter is true, the task will show the results dialog if the algorithm has produced some kind of
214
    *                output other than layers or tables. If it is false, those results will be added to the set of current
215
    *                results, but the results dialog will not be shown
216
    *
217
    * @return a task to postprocess the given algorithm
218
    */
219
   public static Runnable getPostProcessTask(final GeoAlgorithm alg,
220
                                             final boolean bShowResultsDialog) {
221

    
222
      return m_PostProcessTaskFactory.getPostProcessTask(alg, bShowResultsDialog);
223

    
224
   }
225

    
226

    
227
   /**
228
    * Sets the current post process task factory
229
    *
230
    * @param factory
231
    *                the new post-process task factory
232
    */
233
   public static void setPostProcessTaskFactory(final IPostProcessTaskFactory factory) {
234

    
235
      m_PostProcessTaskFactory = factory;
236

    
237
   }
238

    
239

    
240
   /** ******************************************* */
241

    
242
   /**
243
    * Adds an algorithm provider. This has to be done before calling initialize(), so the providers can be initialized as well.
244
    *
245
    * @param provider
246
    *                the algorithm provider to add
247
    */
248
   public static void addAlgorithmProvider(final IAlgorithmProvider provider) {
249

    
250
      //providers are added at the beginning of the array, because models and scripts might depend on them
251
      m_AlgorithmProviders.add(0, provider);
252

    
253
   }
254

    
255

    
256
   /**
257
    * Checks if a portable directory exists and has the required access privileges.
258
    * Non-existing directories will be created if possible.
259
    * Displays a GUI warning message if there are problems, and exits with a corresponding error code.
260
    *
261
    * @param dir String with name of folder/directory to be checked. Use only a relative path here.
262
    * @param read_only set to "true" if only read access is required
263
    * @param provider name of the algorithm provider for which this folder is being checked.
264
    * @return "0" if all is OK, "1" if directory does not exist and could not be created, "2" if access is forbidden, "3" if a _file_ with that name already exists
265
    */
266
   public static int checkDir ( String dir, boolean read_only, String provider ) {
267

    
268
          final File f = new File ( SextanteGUI.getSextanteHomePath() + File.separator + dir );
269

    
270
          if ( f.exists() ) {
271
                  if ( f.isFile() ) {
272
                          JOptionPane.showMessageDialog(null,
273
                                          Sextante.getText("portable_dir_error") + " " + f.getAbsolutePath() + ".\n" +
274
                                          Sextante.getText("portable_dir_is_file") + "\n" +
275
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
276
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
277
                          return ( 3 );
278
                  }
279
                  if ( read_only ) {
280
                          if ( f.canRead() ) {
281
                                  return ( 0 );
282
                          }
283
                  }
284
                  if ( f.canWrite() ) {
285
                          return ( 0 );
286
                  }
287
                  if ( read_only ) {
288
                          JOptionPane.showMessageDialog(null,
289
                                          Sextante.getText("portable_dir_error") + " " + f.getAbsolutePath() +
290
                                          Sextante.getText("portable_dir_error_ro") + "\n" + ":" +
291
                                          Sextante.getText("portable_dir_no_access") + "\n" +
292
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
293
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
294
                  } else {
295
                          JOptionPane.showMessageDialog(null,
296
                                          Sextante.getText("portable_dir_error") + " " + f.getAbsolutePath() +
297
                                          Sextante.getText("portable_dir_error_rw") + "\n" + ":" +
298
                                          Sextante.getText("portable_dir_no_access") + "\n" +
299
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
300
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
301
                  }
302
                  return ( 2 );
303
          }
304

    
305
          /* directory does not exist: attempt to create it */
306
          if ( f.mkdir() == false ) {
307
                  JOptionPane.showMessageDialog(null,
308
                                  Sextante.getText("portable_dir_error") + " " + f.getAbsolutePath() + ".\n" +
309
                                  Sextante.getText("portable_dir_no_create") + "\n" +
310
                                  Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
311
                                  , "Inane warning", JOptionPane.WARNING_MESSAGE);
312
                  return ( 1 );
313
          }
314

    
315
          return ( 0 );
316
   }
317

    
318

    
319
   /**
320
    * Checks if a file exists in one of the portable folders, and if it has the required access privileges.
321
    * Displays a GUI warning message if there are problems, and exits with a corresponding error code.
322
    *
323
    * @param file String with name of file to be checked. Use only a relative path here.
324
    * @param read_only set to "true" if only read access is required
325
    * @param provider name of the algorithm provider for which this file is being checked.
326
    * @return "0" if all is OK, "1" if file does not exist, "2" if access is forbidden, "3" if a _directory_ with that name already exists
327
    */
328
   public static int checkFile ( String file, boolean read_only, String provider ) {
329

    
330
          final File f = new File ( SextanteGUI.getSextanteHomePath() + File.separator + file );
331

    
332
          if ( f.exists() ) {
333
                  if ( f.isDirectory() ) {
334
                          JOptionPane.showMessageDialog(null,
335
                                          Sextante.getText("portable_file_error") + " " + f.getAbsolutePath() + ".\n" +
336
                                          Sextante.getText("portable_file_is_dir") + "\n" +
337
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
338
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
339
                          return ( 3 );
340
                  }
341
                  if ( read_only ) {
342
                          if ( f.canRead() ) {
343
                                  return ( 0 );
344
                          }
345
                  }
346
                  if ( f.canWrite() ) {
347
                          return ( 0 );
348
                  }
349
                  if ( read_only ) {
350
                          JOptionPane.showMessageDialog(null,
351
                                          Sextante.getText("portable_file_error") + " " + f.getAbsolutePath() +
352
                                          Sextante.getText("portable_file_error_ro") + "\n" + ":" +
353
                                          Sextante.getText("portable_file_no_access") + "\n" +
354
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
355
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
356
                  } else {
357
                          JOptionPane.showMessageDialog(null,
358
                                          Sextante.getText("portable_file_error") + " " + f.getAbsolutePath() +
359
                                          Sextante.getText("portable_file_error_rw") + "\n" + ":" +
360
                                          Sextante.getText("portable_file_no_access") + "\n" +
361
                                          Sextante.getText("portable_provider_not_usable") + " <html></i>" + provider + "+</i>+</html>."
362
                                          , "Inane warning", JOptionPane.WARNING_MESSAGE);
363
                  }
364
                  return ( 2 );
365
          }
366

    
367
          return ( 1 );
368
   }
369

    
370

    
371
   /**
372
    * Portable SEXTANTE requires that external providers' (e.g. GRASS) algorithms
373
    * and user-editable scripts are stored in folders within the SEXTANTE extension
374
    * folder. In some case, read access to these folders is fine, in others r/w is required.
375
    * This method checks whether an algorithm provider has been set to be portable and
376
    * if so, it makes sure that the required folder(s) is/are present with the required
377
    * access rights. It will attempt to create any missing folders and will display a
378
    * warning if anythign goes wrong.
379
    */
380
   public static void checkPortableFolders () {
381

    
382
           String sPath;
383
           int result;
384

    
385

    
386
           sPath = new String ("");
387

    
388
           if ( Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteRSettings.R_PORTABLE)) == true ) {
389
                   /* set R binaries folder */
390
                   result = checkDir ( Sextante.PORTABLE_R_FOLDER, true, "R" );
391
                   sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_R_FOLDER;
392
                   SextanteGUI.setSettingParameterValue(SextanteRSettings.R_FOLDER, sPath);
393

    
394
                   /* set R user scripts folder */
395
                   result = checkDir ( Sextante.PORTABLE_R_SCRIPTS_FOLDER, false, "R user scripts" );
396
                   sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_R_SCRIPTS_FOLDER;
397
                   SextanteGUI.setSettingParameterValue(SextanteRSettings.R_SCRIPTS_FOLDER, sPath);
398
           }
399
           if ( Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_PORTABLE)) == true ) {
400
                   /* set GRASS binaries folder */
401
                   result = checkDir ( Sextante.PORTABLE_GRASS_FOLDER, true, "GRASS GIS" );
402
                   sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_GRASS_FOLDER;
403
                   SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER, sPath);
404
                   /* set GRASS shell support (MSYS) binaries folder (Windows only) */
405
                   if ( Sextante.isWindows() ) {
406
                           result = checkFile ( Sextante.PORTABLE_MSYS_FILE, true, "GRASS GIS shell scripts" );
407
                           sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_MSYS_FILE;
408
                           SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_WIN_SHELL, sPath);
409
                   }
410
           }
411
           if ( Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteSagaSettings.SAGA_PORTABLE)) == true ) {
412
                   /* set SAGA binaries folder */
413
                   result = checkDir ( Sextante.PORTABLE_SAGA_FOLDER, true, "SAGA GIS" );
414
                   sPath = SextanteGUI.getSextanteInstallPath() + File.separator + Sextante.PORTABLE_SAGA_FOLDER;
415
                   SextanteGUI.setSettingParameterValue(SextanteSagaSettings.SAGA_FOLDER, sPath);
416
           }
417
           if ( Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteScriptsSettings.SCRIPTS_PORTABLE)) == true ) {
418
                   /* set user scripts folder */
419
                   result = checkDir ( Sextante.PORTABLE_SCRIPTS_FOLDER, true, "Toolbox user scripts" );
420
                   sPath = SextanteGUI.getSextanteHomePath() + File.separator + Sextante.PORTABLE_SCRIPTS_FOLDER;
421
                   SextanteGUI.setSettingParameterValue(SextanteScriptsSettings.SCRIPTS_FOLDER, sPath);
422
           }
423
           if ( Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteModelerSettings.MODELS_PORTABLE)) == true ) {
424
                   /* set user models folder */
425
                   result = checkDir ( Sextante.PORTABLE_MODELS_FOLDER, true, "Toolbox user models" );
426
                   sPath = SextanteGUI.getSextanteHomePath() + File.separator + Sextante.PORTABLE_MODELS_FOLDER;
427
                   SextanteGUI.setSettingParameterValue(SextanteModelerSettings.MODELS_FOLDER, sPath);
428
           }
429
   }
430

    
431

    
432
   /**
433
    * Initializes the GUI parameters and resources. It takes GUI resources (custom panels, etc.) from classpath jars. Should be
434
    * called before setting parameters using other methods from this class, except for the setSextantePath() and
435
    * setCustomDefaultSettings() methods.
436
    */
437
   public static void initialize() {
438

    
439
      GUIResources.addResourcesFromClasspath();
440
      _initialize();
441

    
442
   }
443

    
444

    
445
   /**
446
    * Initializes the GUI parameters and resources. It takes GUI resources (custom panels, etc.) from the specified urls. Should
447
    * be called before setting parameters using other methods from this class, except for the setSextantePath() method.
448
    *
449
    * @param jars
450
    *                the urls of the jar files with custom GUI resources
451
    */
452
   public static void initialize(final URL[] jars) {
453

    
454
      GUIResources.addResourcesFromURLs(jars);
455
      _initialize();
456

    
457
   }
458

    
459

    
460
   /**
461
    * Initializes the GUI parameters and resources. It takes GUI resources (custom panels, etc.) from the specified folder. Should
462
    * be called before setting parameters using other methods from this class, except for the setSextantePath() method.
463
    *
464
    * @param sFolder
465
    *                the folder with jars with custom GUI resources
466
    */
467
   public static void initialize(final String sFolder) {
468

    
469
      GUIResources.addResourcesFromFolder(sFolder);
470
      _initialize();
471

    
472

    
473
   }
474

    
475

    
476
   private static void _initialize() {
477

    
478
      setDefaultSettings();
479
      readConfigFile();
480

    
481
      checkPortableFolders();
482

    
483
      //this loads built-in SEXTANTE algorithms and resources
484
      loadResources();
485

    
486
      //and this loads additional algorithms from algorithm providers
487
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
488
         final IAlgorithmProvider provider = m_AlgorithmProviders.get(i);
489
         provider.initialize();
490
         if (provider.getAlgorithms().size() > 0) {
491
            Sextante.addAlgorithmsFromProvider(provider.getName(), provider.getAlgorithms());
492
            addCustomModelerParametersPanel(provider.getCustomModelerParameterPanels());
493
            addCustomParametersPanel(provider.getCustomParameterPanels());
494
         }
495
      }
496

    
497
   }
498

    
499

    
500
   private static void setDefaultSettings() {
501

    
502
      final Setting[] baseSettings = new Setting[] { new SextanteGeneralSettings() };
503
      final ArrayList<IAlgorithmProvider> providers = SextanteGUI.getAlgorithmProviders();
504
      final Setting[] settings = new Setting[baseSettings.length + providers.size()];
505
      System.arraycopy(baseSettings, 0, settings, 0, baseSettings.length);
506
      for (int i = 0; i < providers.size(); i++) {
507
         settings[i + baseSettings.length] = providers.get(i).getSettings();
508
      }
509

    
510
      for (int i = 0; i < settings.length; i++) {
511
         final HashMap<String, String> map = settings[i].getInitValues();
512
         m_Settings.putAll(map);
513
      }
514

    
515
      if (m_DefaultSettings != null) {
516
         m_Settings.putAll(m_DefaultSettings);
517
      }
518

    
519
   }
520

    
521

    
522
   /**
523
    * This methods sets default setting values different from the hard-coded ones. It should be called right before initializing
524
    * this class
525
    *
526
    * @param map
527
    *                the map with new default settings
528
    */
529
   public static void setCustomDefaultSettings(final HashMap<String, String> map) {
530

    
531
      m_DefaultSettings = map;
532

    
533
   }
534

    
535

    
536
   private static void loadResources() {
537

    
538
      final String[] sPanelClassName = GUIResources.getParameterPanelClassNames();
539
      for (final String element : sPanelClassName) {
540
         try {
541
            final Class panelClass = Class.forName(element);
542
            final String sAlgClassName = element.substring(0, element.indexOf("ParametersPanel")) + "Algorithm";
543
            final Class algClass = Class.forName(sAlgClassName);
544
            final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance();
545
            m_ParametersPanel.put(alg.getCommandLineName(), panelClass);
546
         }
547
         catch (final Exception e) {/*ignore it if there are problems*/
548
         }
549
      }
550

    
551
      final String[] sModelerPanelClassName = GUIResources.getModelerParameterPanelClassNames();
552
      for (final String element : sModelerPanelClassName) {
553
         try {
554
            final Class panelClass = Class.forName(element);
555
            final String sAlgClassName = element.substring(0, element.indexOf("ModelerParametersPanel")) + "Algorithm";
556
            final Class algClass = Class.forName(sAlgClassName);
557
            final GeoAlgorithm alg = (GeoAlgorithm) algClass.newInstance();
558
            m_ModelerParametersPanel.put(alg.getCommandLineName(), panelClass);
559
         }
560
         catch (final Exception e) {/*ignore it if there are problems*/
561
         }
562
      }
563

    
564

    
565
   }
566

    
567

    
568
   /**
569
    * Returns the path to help files
570
    *
571
    * @return the path to help files
572
    */
573
   public static String getHelpPath() {
574

    
575
      return getSextanteInstallPath() + File.separator + "help";
576

    
577
   }
578

    
579

    
580
   /**
581
    * Sets the current path to sextante folder. This is the folder where help files and additional software should be located.
582
    * This should be done before initializing this class, since this folder is used by some algorithm providers
583
    *
584
    * @param sPath
585
    *                the path to sextante help and additional programs
586
    * @deprecated Use {@link #setSextanteInstallPath(String)} or {@link #setSextanteHomePath(String)}
587
    */
588
   public static void setSextantePath(final String sPath) {
589

    
590
      m_sSextanteInstallPath = sPath;
591

    
592
   }
593

    
594

    
595
   /**
596
    * Returns the path to sextante help and additional programs
597
    *
598
    * @return the path to sextante help and additional programs
599
        *
600
    * @deprecated Use {@link #getSextanteInstallPath()} or {@link #getSextanteHomePath()}
601
    */
602
   public static String getSextantePath() {
603

    
604
      return m_sSextanteInstallPath;
605

    
606
   }
607

    
608
   public static String getSextanteHomePath() {
609
           return m_sSextanteHomePath;
610
   }
611

    
612
   public static String getSextanteInstallPath() {
613
           return m_sSextanteInstallPath;
614
   }
615

    
616
   public static void setSextanteHomePath(String path) {
617
           m_sSextanteHomePath = path;
618

    
619
   }
620

    
621
   public static void setSextanteInstallPath(String path) {
622
           m_sSextanteInstallPath = path;
623

    
624
   }
625

    
626

    
627

    
628
   /**
629
    * Returns the default folder for output data.
630
    *
631
    * @return the default folder for output data.
632
    */
633
   public static String getOutputFolder() {
634

    
635
      if ((m_sOutputFolder == null) || m_sOutputFolder.trim().equals("")) {
636
         return System.getProperty("user.home");
637
      }
638
      else {
639
         return m_sOutputFolder;
640
      }
641

    
642
   }
643

    
644

    
645
   /**
646
    * Returns the default value to represent "No Data" (null) raster cells.
647
    *
648
    * @return the current "No Data" value.
649
    */
650
   private static Double getDefaultNoDataValue() {
651

    
652
      try {
653
         final double dNoDataValue = Double.parseDouble(SextanteGUI.getSettingParameterValue(SextanteGeneralSettings.DEFAULT_NO_DATA_VALUE));
654
         return dNoDataValue;
655
      }
656
      catch (final Exception e) {
657
         return new Double(-99999d);
658
      }
659

    
660
   }
661

    
662

    
663
   /**
664
    * Loads configuration parameters from the config file
665
    */
666
   private static void readConfigFile() {
667

    
668
      BufferedReader input = null;
669
      try {
670
         input = new BufferedReader(new FileReader(getConfigFile()));
671
         String sLine = null;
672
         while ((sLine = input.readLine()) != null) {
673
            final String[] sTokens = sLine.split("=");
674
            if (sTokens.length == 2) {
675
               m_Settings.put(sTokens[0], sTokens[1]);
676
            }
677
         }
678
      }
679
      catch (final FileNotFoundException e) {
680
      }
681
      catch (final IOException e) {
682
      }
683
      finally {
684
         try {
685
            if (input != null) {
686
               input.close();
687
            }
688
         }
689
         catch (final IOException e) {
690
            Sextante.addErrorToLog(e);
691
         }
692
      }
693

    
694
   }
695

    
696

    
697
   /**
698
    * Saves current settings to the config file
699
    */
700
   public static void saveSettings() {
701

    
702
      Writer output = null;
703
      try {
704
         output = new BufferedWriter(new FileWriter(getConfigFile()));
705
         final Set<String> set = m_Settings.keySet();
706
         final Iterator<String> iter = set.iterator();
707
         while (iter.hasNext()) {
708
            final String sKey = iter.next();
709
            final String sValue = m_Settings.get(sKey);
710
            output.write(sKey + "=" + sValue + "\n");
711
         }
712
      }
713
      catch (final IOException e) {
714
         Sextante.addErrorToLog(e);
715
      }
716
      finally {
717
         if (output != null) {
718
            try {
719
               output.close();
720
            }
721
            catch (final IOException e) {
722
               Sextante.addErrorToLog(e);
723
            }
724
         }
725
      }
726

    
727
   }
728

    
729

    
730
   /**
731
    * Returns the path to the SEXTANTE config file.
732
    *
733
    * @return A string with the full the path and name of the SEXTANTE config file.
734
    */
735
   private static String getConfigFile() {
736

    
737
      final String sPath = getUserFolder();
738

    
739
      return sPath + File.separator + "sextante.settings";
740

    
741
   }
742

    
743

    
744
   /**
745
    * Returns the folder in which the SEXTANTE config file is stored.
746
    * By default, this is the folder "sextante" in the user's home folder.
747
    * But it is also possible to set a different folder through the
748
    * Java property "SEXTANTE.confDir" when launching the VM:
749
    *
750
    *   java -DSEXTANTE.confDir=<path>
751
    *
752
    * @return A string with the full path and name of the config folder.
753
    */
754
   public static String getUserFolder() {
755

    
756
      String sPath;
757
      String sConfDir;
758

    
759

    
760
      // default is to use system's home folder setting
761
      sPath = System.getProperty("user.home") + File.separator + "sextante";
762

    
763
      // check if SEXTANTE.confDir has been set
764
      sConfDir = System.getProperty("SEXTANTE.confDir");
765
      if ( sConfDir != null ) {
766
              sConfDir = sConfDir.trim();
767
                      if ( sConfDir.length() > 0 ) {
768
                              // check if we have to append a separator char
769
                              if ( sConfDir.endsWith(File.separator) ) {
770
                                      sPath = sConfDir;
771
                              } else {
772
                                      sPath = sConfDir + File.separator;
773
                                }
774
                        }
775
      }
776

    
777
      final File sextanteFolder = new File(sPath);
778
      if (!sextanteFolder.exists()) {
779
              sextanteFolder.mkdir();
780
      }
781
      return sPath;
782

    
783
   }
784

    
785

    
786
   /**
787
    * Returns the type of the last element from which a command-line command was executed
788
    *
789
    * @return the type of the element from which a command-line command was executed. SextanteGUI.HISTORY if the last component
790
    *         was the history panel; SextanteGUI.COMMANDLINE if it was the regular SEXTANTE console
791
    */
792
   public static int getLastCommandOrigin() {
793

    
794
      return m_iLastCommandOrigin;
795

    
796
   }
797

    
798

    
799
   /**
800
    * Sets the type of the last element from which a command-line command was executed. This has to be called from any component
801
    * that allows execution of commands
802
    *
803
    * @param iLast
804
    *                one of the following constants: SextanteGUI.HISTORY if the last component was the history panel;
805
    *                SextanteGUI.COMMANDLINE if it was the regular SEXTANTE console
806
    */
807
   public static void setLastCommandOrigin(final int iLast) {
808

    
809
      m_iLastCommandOrigin = iLast;
810

    
811
   }
812

    
813

    
814
   /**
815
    * Gets the dialog from which the last command--line command was executed. This will be used as the parent dialog for task
816
    * monitors or message dialogs generated by the execution of that command.
817
    *
818
    * @return the dialog from which the last command--line command was executed
819
    */
820
   public static JDialog getLastCommandOriginParentDialog() {
821

    
822
      return m_LastCommandOriginParentPanel;
823

    
824
   }
825

    
826

    
827
   /**
828
    * Sets the dialog (if any) that contains the element from which the last command--line command was executed
829
    *
830
    * @param parent
831
    *                the dialog (if any) that contains the element from which the last command--line command was executed
832
    */
833
   public static void setLastCommandOriginParentDialog(final JDialog parent) {
834

    
835
      m_LastCommandOriginParentPanel = parent;
836

    
837
   }
838

    
839

    
840
   /**
841
    * Returns true if only active algorithms (those that can be executed with the current data objects) should be shown in the
842
    * toolbox
843
    *
844
    * @return true if only active algorithms (those that can be executed with the current data objects) should be shown in the
845
    *         toolbox
846
    */
847
   public static boolean getShowOnlyActiveAlgorithms() {
848

    
849
      return m_bShowOnlyActiveAlgorithms;
850

    
851
   }
852

    
853

    
854
   /**
855
    * Sets whether only active algorithms (those that can be executed with the current data objects) should be shown in the
856
    * toolbox
857
    *
858
    * @param showOnlyActiveAlgorithms
859
    *                must be true if only active algorithms (those that can be executed with the current data objects) should be
860
    *                shown in the toolbox
861
    */
862
   public static void setShowOnlyActiveAlgorithms(final boolean showOnlyActiveAlgorithms) {
863

    
864
      m_bShowOnlyActiveAlgorithms = showOnlyActiveAlgorithms;
865

    
866
   }
867

    
868

    
869
   /**
870
    * puts a collection of settings into the settings map
871
    *
872
    * @param settings
873
    *                the map with settings values.
874
    */
875
   public static void setSettings(final HashMap<String, String> settings) {
876

    
877
      m_Settings.putAll(settings);
878

    
879
   }
880

    
881

    
882
   /**
883
    * Modifies the passed string, so it can be used as a safe data object name (without special characters)
884
    *
885
    * @param sName
886
    *                the name to modify
887
    * @return the modified safe name (with no special characters)
888
    */
889
   public static String modifyResultName(String sName) {
890

    
891
      sName = sName.replaceAll("\\.", "_");
892
      sName = sName.replaceAll(" ", "_");
893
      sName = sName.replaceAll("\\[", "_");
894
      sName = sName.replaceAll("\\]", "_");
895
      sName = sName.replaceAll("\\(", "_");
896
      sName = sName.replaceAll("\\)", "_");
897
      sName = sName.replaceAll("á", "a");
898
      sName = sName.replaceAll("é", "e");
899
      sName = sName.replaceAll("í", "i");
900
      sName = sName.replaceAll("ó", "o");
901
      sName = sName.replaceAll("ú", "u");
902
      sName = sName.replaceAll("ñ", "n");
903

    
904
      return sName;
905

    
906
   }
907

    
908

    
909
   private static void addCustomParametersPanel(final HashMap<String, Class> map) {
910

    
911
      m_ParametersPanel.putAll(map);
912

    
913
   }
914

    
915

    
916
   private static void addCustomModelerParametersPanel(final HashMap<String, Class> map) {
917

    
918
      m_ModelerParametersPanel.putAll(map);
919

    
920
   }
921

    
922

    
923
   public static Class getModelerParametersPanel(final String sName) {
924

    
925
      return m_ModelerParametersPanel.get(sName);
926

    
927
   }
928

    
929

    
930
   public static Class getParametersPanel(final String sName) {
931

    
932
      return m_ParametersPanel.get(sName);
933

    
934
   }
935

    
936

    
937
   public static ImageIcon getAlgorithmIcon(final GeoAlgorithm alg) {
938

    
939
      final String sName = Sextante.getAlgorithmProviderName(alg);
940
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
941
         if (m_AlgorithmProviders.get(i).getName().equals(sName)) {
942
            return m_AlgorithmProviders.get(i).getIcon();
943
         }
944
      }
945
      return SEXTANTE_ICON;
946

    
947
   }
948

    
949

    
950
   public static ArrayList<IAlgorithmProvider> getAlgorithmProviders() {
951

    
952
      return m_AlgorithmProviders;
953

    
954
   }
955

    
956

    
957
   public static String getSettingParameterValue(final String sParameterName) {
958

    
959
      return m_Settings.get(sParameterName);
960

    
961
   }
962

    
963

    
964
   public static String setSettingParameterValue(final String sParameterName,
965
                                                 final String sValue) {
966

    
967
      return m_Settings.put(sParameterName, sValue);
968

    
969
   }
970

    
971

    
972
   public static void updateAlgorithmProvider(final Class providerClass) {
973

    
974
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
975
         final IAlgorithmProvider provider = m_AlgorithmProviders.get(i);
976
         if (providerClass.isInstance(provider)) {
977
            provider.update();
978
            Sextante.addAlgorithmsFromProvider(provider.getName(), provider.getAlgorithms());
979
         }
980
      }
981

    
982
   }
983

    
984

    
985
   public static Object getAlgorithmHelp(final GeoAlgorithm alg) {
986

    
987
      final String sName = Sextante.getAlgorithmProviderName(alg);
988
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
989
         if (m_AlgorithmProviders.get(i).getName().equals(sName)) {
990
            return m_AlgorithmProviders.get(i).getAlgorithmHelp(alg);
991
         }
992
      }
993
      String sFilename;
994
      String sPath;
995
      if (sName.equals("SEXTANTE")) {
996
         sFilename = alg.getCommandLineName() + ".xml";
997
         sPath = HelpIO.getHelpPath(alg, false);
998
         return HelpIO.getHelpAsHTMLCode(alg, sPath + File.separator + sFilename);
999
      }
1000
      else {
1001
         return ""; //TODO:create default help page
1002
      }
1003

    
1004
   }
1005

    
1006

    
1007
   public static String getAlgorithmHelpFilename(final GeoAlgorithm alg,
1008
                                                 final boolean bForceCurrentLocale) {
1009

    
1010
      final String sName = Sextante.getAlgorithmProviderName(alg);
1011
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
1012
         if (m_AlgorithmProviders.get(i).getName().equals(sName)) {
1013
            return m_AlgorithmProviders.get(i).getAlgorithmHelpFilename(alg, bForceCurrentLocale);
1014
         }
1015
      }
1016
      String sFilename;
1017
      String sPath;
1018
      if (sName.equals("SEXTANTE")) {
1019
         sFilename = alg.getCommandLineName() + ".xml";
1020
         sPath = HelpIO.getHelpPath(alg, false);
1021
         return sPath + File.separator + sFilename;
1022
      }
1023
      else {
1024
         return ""; //TODO:create default help page
1025
      }
1026

    
1027
   }
1028

    
1029

    
1030
   public static HashMap<NameAndIcon, ArrayList<ToolboxAction>> getToolboxActions() {
1031

    
1032

    
1033
      final HashMap<NameAndIcon, ArrayList<ToolboxAction>> map = new HashMap<NameAndIcon, ArrayList<ToolboxAction>>();
1034
      for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
1035
         final IAlgorithmProvider provider = m_AlgorithmProviders.get(i);
1036
         map.putAll(provider.getToolboxActions());
1037
      }
1038
      map.putAll(SextanteGUI.getGUIFactory().getToolboxActions());
1039
      return map;
1040
   }
1041

    
1042

    
1043
   public static IToolboxRightButtonAction[] getToolboxRightButtonActions() {
1044

    
1045
           final ArrayList<IToolboxRightButtonAction> list = new ArrayList<IToolboxRightButtonAction>();
1046
           for (int i = 0; i < m_AlgorithmProviders.size(); i++) {
1047
                   final IAlgorithmProvider provider = m_AlgorithmProviders.get(i);
1048
                   final IToolboxRightButtonAction[] actions = provider.getToolboxRightButtonActions();
1049
                   for (int j = 0; j < actions.length; j++) {
1050
                           list.add(actions[j]);
1051
                   }
1052
           }
1053
      return list.toArray(new IToolboxRightButtonAction[0]);
1054

    
1055
   }
1056

    
1057

    
1058
   /**
1059
    * Returns the renderer to use for layers created by SEXTANTE algorithms
1060
    *
1061
    */
1062
   public static IDataRenderer getDataRenderer() {
1063

    
1064
      return m_Renderer;
1065

    
1066
   }
1067

    
1068

    
1069
   public static void setDataRenderer(final IDataRenderer renderer) {
1070

    
1071
      m_Renderer = renderer;
1072

    
1073
   }
1074
}