Statistics
| Revision:

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

History | View | Annotate | Download (11.8 KB)

1
package es.unex.sextante.gui.grass;
2

    
3
import java.io.File;
4
import java.net.MalformedURLException;
5
import java.net.URL;
6
import java.util.ArrayList;
7
import java.util.HashMap;
8
import java.util.Iterator;
9
import java.util.Set;
10

    
11
import javax.swing.ImageIcon;
12

    
13
import es.unex.sextante.core.GeoAlgorithm;
14
import es.unex.sextante.core.Sextante;
15
import es.unex.sextante.gui.core.IAlgorithmProvider;
16
import es.unex.sextante.gui.core.IToolboxRightButtonAction;
17
import es.unex.sextante.gui.core.NameAndIcon;
18
import es.unex.sextante.gui.core.SextanteGUI;
19
import es.unex.sextante.gui.core.ToolboxAction;
20
import es.unex.sextante.gui.exceptions.WrongGrassFolderException;
21
import es.unex.sextante.gui.r.RUtils;
22
import es.unex.sextante.gui.settings.Setting;
23
import es.unex.sextante.gui.settings.SextanteGrassSettings;
24
import es.unex.sextante.gui.settings.SextanteRSettings;
25

    
26
public class GrassAlgorithmProvider
27
         implements
28
            IAlgorithmProvider {
29

    
30
   private static HashMap<String, GeoAlgorithm> m_Algs     = new HashMap<String, GeoAlgorithm>();
31

    
32
   private static StringBuffer                  m_Message  = new StringBuffer();
33

    
34
   private final static ImageIcon               GRASS_ICON = new ImageIcon(
35
                                                                    GrassAlgorithmProvider.class.getClassLoader().getResource(
36
                                                                             "images/grass.png"));
37

    
38

    
39
   private static GrassAlgorithm createAlgorithm(final String sFile) {
40

    
41
      final GrassAlgorithm alg = new GrassAlgorithm();
42
      try {
43
         alg.initialize(sFile);
44
         return alg;
45
      }
46
      catch (final UnwrappableGrassProcessException e) {
47
         return null;
48
      }
49

    
50
   }
51

    
52

    
53
   public void initialize() {
54

    
55
      GrassUtils.createTempMapsetName();
56
      createAlgorithmsMap();
57

    
58
   }
59

    
60

    
61
   private void createAlgorithmsMap() {
62

    
63
      m_Algs.clear();
64

    
65
      if ( getGrassDescriptionFolder() == null ) {
66
              SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_ACTIVATE, new Boolean(false).toString());
67
              return;
68
      }
69

    
70
      if ( getGrassDescriptionFolder().length() < 1 ) {
71
              SextanteGUI.setSettingParameterValue(SextanteGrassSettings.GRASS_ACTIVATE, new Boolean(false).toString());
72
              return;
73
      }
74

    
75
      try {
76
         final File file = new File(getGrassDescriptionFolder());
77
         final String[] files = file.list();
78
         if (files != null) {
79
            for (final String element : files) {
80
               if (element.endsWith(".xml")) {
81
                  final GrassAlgorithm alg = createAlgorithm(getGrassDescriptionFolder() + File.separator + element);
82
                  if ((alg != null) && !GrassBlackList.isInBlackList(alg)) {
83
                     m_Algs.put(alg.getCommandLineName(), alg);
84
                  }
85
               }
86
            }
87
         }
88
      }
89
      catch (final Exception e) {
90
         m_Algs.clear();
91
      }
92

    
93
   }
94

    
95

    
96
   /**
97
    * Deletes algorithm descriptions and help files
98
    */
99
   public static void deleteDescriptionFiles() {
100

    
101
      final File file = new File(getGrassDescriptionFolder());
102
      final String[] files = file.list();
103

    
104
      if (files != null) {
105
         for (final String element : files) {
106
            new File(file.getAbsoluteFile() + File.separator + element).delete();
107
         }
108
      }
109

    
110
   }
111

    
112

    
113
   /**
114
    * Creates xml files calling grass commands using the --interface-description modifier
115
    */
116
   public static int createAlgorithmsDescriptionFiles() throws WrongGrassFolderException {
117

    
118
      int iAlgorithms = 0;
119
      final StringBuffer sb = new StringBuffer();
120
      try {
121

    
122
         //Get modules from "bin" folder
123
         final String sFolder = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
124
         File file = new File(sFolder + File.separator + "bin");
125
         String[] files = file.list();
126
         if (files != null) {
127
            if (Sextante.isUnix() || Sextante.isMacOSX()) {
128
               for (int i = 0; i < files.length; i++) {
129
                  if ((files[i].startsWith("v.") || files[i].startsWith("r.")) && !files[i].startsWith("r3.")
130
                      && !files[i].equals("r.mapcalc") && !files[i].equals("r3.mapcalc")) {
131
                     final String sCommand = files[i];
132
                     if (!GrassBlackList.isInBlackList(sCommand)) {
133
                        iAlgorithms++;
134
                        sb.append(sCommand + " --interface-description > \"" + getGrassDescriptionFolder() + File.separator
135
                                  + sCommand + ".xml\"\n");
136
                     }
137
                  }
138
               }
139
               //Get modules from "scripts" folder
140
               file = new File(sFolder + File.separator + "scripts");
141
               files = file.list();
142
               if (files != null) {
143
                  for (int i = 0; i < files.length; i++) {
144
                     if ((files[i].startsWith("v.") || files[i].startsWith("r.")) && !files[i].equals("r.out.gdal.sh")
145
                         && !files[i].startsWith("r3.")) {
146
                        final String sCommand = files[i];
147
                        if (!GrassBlackList.isInBlackList(sCommand)) {
148
                           iAlgorithms++;
149
                           sb.append(sCommand + " --interface-description > \"" + getGrassDescriptionFolder() + File.separator
150
                                     + sCommand + ".xml\"\n");
151
                        }
152
                     }
153
                  }
154
               }
155
            }
156
            else {//Windows: scripts and C binaries are all in one folder
157
               for (int i = 0; i < files.length; i++) {
158
                  if ((files[i].endsWith(".exe") || files[i].endsWith(".bat"))
159
                      //if ( (files[i].endsWith(".exe") )
160
                      && (files[i].startsWith("v.") || files[i].startsWith("r.")) && !files[i].startsWith("r3.")
161
                      && !files[i].contains("r.out.gdal.sh") && !files[i].equals("r.mapcalc.exe")
162
                      && !files[i].equals("r3.mapcalc.exe")) {
163
                     final String sCommand = files[i].substring(0, files[i].length() - 4);
164
                     if (!GrassBlackList.isInBlackList(sCommand)) {
165
                        iAlgorithms++;
166
                        if (files[i].endsWith(".bat")) {
167
                           sb.append("cmd.exe /C " + sCommand + " --interface-description > \"" + getGrassDescriptionFolder()
168
                                     + File.separator + sCommand + ".xml\"\n");
169
                        }
170
                        else {
171
                           sb.append(sCommand + " --interface-description > \"" + getGrassDescriptionFolder() + File.separator
172
                                     + sCommand + ".xml\"\n");
173
                        }
174
                     }
175
                  }
176
               }
177
            }
178
         }
179

    
180
         if (iAlgorithms == 0) {
181
            throw new WrongGrassFolderException();
182
         }
183
      }
184
      catch (final Exception e) {
185
         throw new WrongGrassFolderException();
186
      }
187

    
188
      try {
189
         GrassUtils.createTempMapset();
190
         GrassUtils.runGRASS(sb, "Creating GRASS algorithm descriptions", null);
191
      }
192
      catch (final Exception e) {
193
         throw new WrongGrassFolderException();
194
      }
195

    
196
      Sextante.addInfoToLog("SEXTANTE GRASS interface: Done setting up GRASS.");
197

    
198
      //createAlgorithmSiblings();
199

    
200
      return iAlgorithms;
201
   }
202

    
203

    
204
   /**
205
    * Returns the folder where grass description files (xml file generated using the grass --interface-description modifier) are
206
    * located
207
    *
208
    * @return Returns the folder where grass description files (xml file generated using the grass --interface-description
209
    *         modifier) are located
210
    */
211
   public static String getGrassDescriptionFolder() {
212

    
213
      final String sPath = SextanteGUI.getSextanteInstallPath() + File.separator + "grass" + File.separator + "description";
214
      //System.getProperty("user.home") + File.separator + "sextante" + File.separator + "grass";
215

    
216
      final File file = new File(sPath);
217
      if (!file.exists()) {
218
         file.mkdir();
219
      }
220

    
221
      return sPath;
222

    
223
   }
224

    
225

    
226
   /**
227
    * Returns the algorithm corresponding to a given grass algorithm name
228
    *
229
    * @param sAlgName
230
    *                the name of the grass algorithm (the grass command to execute it)
231
    * @return the algorithm corresponding to the passed grass algorithm name
232
    */
233
   public static GrassAlgorithm getGrassAlgorithm(final String sAlgName) {
234

    
235

    
236
      return (GrassAlgorithm) m_Algs.get(sAlgName);
237

    
238
   }
239

    
240

    
241
   public HashMap<String, GeoAlgorithm> getAlgorithms() {
242

    
243
      if (Boolean.parseBoolean(SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_ACTIVATE))) {
244
         return m_Algs;
245
      }
246
      else {
247
         return new HashMap<String, GeoAlgorithm>();
248
      }
249
   }
250

    
251

    
252
   public String getName() {
253

    
254
      return "GRASS";
255

    
256
   }
257

    
258

    
259
   public HashMap<String, Class> getCustomModelerParameterPanels() {
260

    
261
      final HashMap<String, Class> map = new HashMap<String, Class>();
262
      final Set<String> set = m_Algs.keySet();
263
      final Iterator<String> iter = set.iterator();
264
      while (iter.hasNext()) {
265
         map.put(iter.next(), GrassModelerParametersPanel.class);
266
      }
267

    
268
      return map;
269

    
270
   }
271

    
272

    
273
   public HashMap<String, Class> getCustomParameterPanels() {
274

    
275
      final HashMap<String, Class> map = new HashMap<String, Class>();
276
      final Set<String> set = m_Algs.keySet();
277
      final Iterator<String> iter = set.iterator();
278
      while (iter.hasNext()) {
279
         map.put(iter.next(), GrassParametersPanel.class);
280
      }
281

    
282
      return map;
283

    
284
   }
285

    
286

    
287
   public ImageIcon getIcon() {
288

    
289
      return GRASS_ICON;
290

    
291
   }
292

    
293

    
294
   public Setting getSettings() {
295

    
296
      return new SextanteGrassSettings();
297

    
298
   }
299

    
300

    
301
   public void update() {
302

    
303
      createAlgorithmsMap();
304

    
305
   }
306

    
307

    
308
   public Object getAlgorithmHelp(final GeoAlgorithm alg) {
309

    
310

    
311
      final String sFolder = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
312
      String sName = alg.getName();
313
      if (sName.contains("(")) {//sibling
314
         sName = sName.substring(0, sName.indexOf("(")).trim();
315
      }
316
      final String sFilename = sName + ".html";
317
      final String sURLPath = "file:///" + sFolder + File.separator + "docs" + File.separator + "html" + File.separator
318
                              + sFilename;
319

    
320
      try {
321
         return new URL(sURLPath);
322
      }
323
      catch (final MalformedURLException e1) {
324
         return null;
325
      }
326
   }
327

    
328

    
329
   public String getAlgorithmHelpFilename(final GeoAlgorithm alg,
330
                                          final boolean forceCurrentLocale) {
331

    
332
      final String sFolder = SextanteGUI.getSettingParameterValue(SextanteGrassSettings.GRASS_FOLDER);
333
      final String sFilename = alg.getName() + ".html";
334
      final String sFullPath = sFolder + File.separator + "docs" + File.separator + "html" + File.separator + sFilename;
335

    
336
      return sFullPath;
337

    
338
   }
339

    
340

    
341
   public boolean canEditHelp() {
342

    
343
      return false;
344

    
345
   }
346

    
347

    
348
   public static void deleteAlgorithms() {
349

    
350
      m_Algs.clear();
351

    
352
   }
353

    
354

    
355
   public HashMap<NameAndIcon, ArrayList<ToolboxAction>> getToolboxActions() {
356

    
357
      return new HashMap<NameAndIcon, ArrayList<ToolboxAction>>();
358

    
359
   }
360

    
361

    
362
   public IToolboxRightButtonAction[] getToolboxRightButtonActions() {
363

    
364
      return new IToolboxRightButtonAction[0];
365

    
366
   }
367

    
368

    
369
   public static void addMessage(final String s) {
370

    
371

    
372
      m_Message.append(s + "\n");
373

    
374
   }
375

    
376

    
377
   public static void publishMessage(final String sDescription) {
378

    
379
      Sextante.getLogger().addToLog(m_Message.toString(), "GRASS", sDescription);
380

    
381
   }
382

    
383

    
384
}