Revision 244 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.lib/org.gvsig.geoprocess.lib.sextante/src/main/java/org/gvsig/geoprocess/lib/sextante/SextanteGeoProcessManager.java

View differences:

SextanteGeoProcessManager.java
20 20
 */
21 21
package org.gvsig.geoprocess.lib.sextante;
22 22

  
23
import java.io.File;
24
import java.io.InputStream;
25
import java.net.URL;
26
import java.util.ArrayList;
23 27
import java.util.HashMap;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Locale;
24 31
import java.util.Map;
32
import java.util.Map.Entry;
33
import java.util.Set;
25 34

  
35
import javax.swing.ImageIcon;
36

  
37
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
26 38
import es.unex.sextante.core.GeoAlgorithm;
39
import es.unex.sextante.core.OutputObjectsSet;
40
import es.unex.sextante.core.ParametersSet;
27 41
import es.unex.sextante.core.Sextante;
42
import es.unex.sextante.docEngines.html.HTMLDoc;
43
import es.unex.sextante.exceptions.NullParameterAdditionalInfoException;
44
import es.unex.sextante.gui.algorithm.DefaultParametersPanel;
45
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel;
46
import es.unex.sextante.gui.core.IAlgorithmProvider;
47
import es.unex.sextante.gui.core.ILogHandler;
48
import es.unex.sextante.gui.help.HelpElement;
49
import es.unex.sextante.gui.help.ImageAndDescription;
50
import es.unex.sextante.gui.modeler.DefaultModelerParametersPanel;
51
import es.unex.sextante.gui.settings.Setting;
52
import es.unex.sextante.outputs.Output;
53
import es.unex.sextante.outputs.Output3DRasterLayer;
54
import es.unex.sextante.outputs.OutputChart;
55
import es.unex.sextante.outputs.OutputNumericalValue;
56
import es.unex.sextante.outputs.OutputRasterLayer;
57
import es.unex.sextante.outputs.OutputTable;
58
import es.unex.sextante.outputs.OutputText;
59
import es.unex.sextante.outputs.OutputVectorLayer;
60
import es.unex.sextante.parameters.Parameter;
61
import es.unex.sextante.parameters.ParameterVectorLayer;
28 62

  
63
import org.kxml2.io.KXmlParser;
64
import org.xmlpull.v1.XmlPullParser;
65

  
66
import org.gvsig.andami.iconthemes.IIconTheme;
67
import org.gvsig.app.ApplicationLocator;
29 68
import org.gvsig.geoprocess.lib.api.GeoProcess;
30 69
import org.gvsig.geoprocess.lib.api.GeoProcessManager;
70
import org.gvsig.geoprocess.lib.sextante.gui.settings.GeoprocessSetting;
71
import org.gvsig.tools.ToolsLocator;
72
import org.gvsig.tools.i18n.I18nManager;
31 73

  
32 74
/**
33 75
 * Implementation for the {@link GeoProcessManager} interface based on the
......
36 78
 * @author gvSIG Team
37 79
 * @version $Id$
38 80
 */
39
public class SextanteGeoProcessManager implements GeoProcessManager {
81
public class SextanteGeoProcessManager implements GeoProcessManager,
82
    IAlgorithmProvider {
40 83

  
41
    public void registerGeoProcess(Class<? extends GeoProcess> geoProcessClazz,
42
        Map<String, String> localeStrings) {
43
        if (GeoAlgorithm.class.isAssignableFrom(geoProcessClazz)) {
44
            Sextante.addGeoalgorithm("gvSIG", geoProcessClazz,
45
                new HashMap<String, String>(localeStrings));
84
    private static final String XML_PARSER_ENCODING = "ISO-8859-1";
85
    private static String HELP = "help";
86
    private Map<String, GeoAlgorithm> geoprocesses;
87
    private final IIconTheme currentTheme;
88
    private I18nManager i18nManager;
89

  
90
    /**
91
     * Manager constructor.
92
     */
93
    public SextanteGeoProcessManager() {
94
        i18nManager = ToolsLocator.getI18nManager();
95
        geoprocesses = new HashMap<String, GeoAlgorithm>();
96
        currentTheme =
97
            ApplicationLocator.getManager().getIconThemeManager().getCurrent();
98
    }
99

  
100
    public String getTranslation(String label) {
101
        // Just in case an algorithm is using a sextante label
102
        String text = i18nManager.getTranslation(label);
103
        return text == null ? Sextante.getText(label) : text;
104
    }
105

  
106
    public void registerGeoProcess(GeoProcess geoProcess) {
107
        if (geoProcess instanceof GeoAlgorithm) {
108
            GeoAlgorithm geoAlgorithm = (GeoAlgorithm) geoProcess;
109
            geoprocesses.put(geoAlgorithm.getCommandLineName(), geoAlgorithm);
46 110
        } else {
47
            throw new IllegalArgumentException("The class " + geoProcessClazz
111
            throw new IllegalArgumentException("The geoprocess " + geoProcess
48 112
                + " is not an implementation of a sextante GeoAlgorithm, "
49
                + "so it can't be registered");
113
                + "so it is not supported as of now");
50 114
        }
51 115
    }
52 116

  
117
    public void registerGeoProcess(Class<? extends GeoProcess> geoProcessClazz,
118
        Map<String, String> localeStrings) {
119
        try {
120
            GeoProcess geoAlgorithm = geoProcessClazz.newInstance();
121
            registerGeoProcess(geoAlgorithm);
122
        } catch (InstantiationException e) {
123
            throw new RuntimeException(
124
                "Error creating a geoprocess instance from the class "
125
                    + geoProcessClazz, e);
126
        } catch (IllegalAccessException e) {
127
            throw new RuntimeException(
128
                "Error creating a geoprocess instance from the class "
129
                    + geoProcessClazz, e);
130
        }
131
    }
132

  
133
    public void initialize() {
134
        // Nothing to do
135
    }
136

  
137
    public String getName() {
138
        return "gvSIG";
139
    }
140

  
141
    public HashMap<String, GeoAlgorithm> getAlgorithms() {
142
        return new HashMap<String, GeoAlgorithm>(geoprocesses);
143
    }
144

  
145
    public HashMap<String, Class> getCustomModelerParameterPanels() {
146
        final HashMap<String, Class> map = new HashMap<String, Class>();
147
        final Set<String> set = getAlgorithms().keySet();
148
        final Iterator<String> iter = set.iterator();
149
        while (iter.hasNext()) {
150
            map.put(iter.next(), DefaultModelerParametersPanel.class);
151
        }
152

  
153
        return map;
154
    }
155

  
156
    public HashMap<String, Class> getCustomParameterPanels() {
157
        @SuppressWarnings("rawtypes")
158
        final HashMap<String, Class> map = new HashMap<String, Class>();
159
        final Set<Entry<String, GeoAlgorithm>> set = getAlgorithms().entrySet();
160
        final Iterator<Entry<String, GeoAlgorithm>> iter = set.iterator();
161
        while (iter.hasNext()) {
162
            Entry<String, GeoAlgorithm> entry = iter.next();
163
            GeoAlgorithm algorithm = entry.getValue();
164
            Class<? extends GeoAlgorithmParametersPanel> panelClazz = null;
165
            if (algorithm instanceof AbstractSextanteGeoProcess) {
166
                panelClazz =
167
                    ((AbstractSextanteGeoProcess) algorithm)
168
                        .getCustomParametersPanelClass();
169
            }
170
            map.put(entry.getKey(), (panelClazz == null
171
                ? DefaultParametersPanel.class : panelClazz));
172
        }
173

  
174
        return map;
175
    }
176

  
177
    public ImageIcon getIcon() {
178
        return currentTheme.get("gvsig-logo-icon");
179
    }
180

  
181
    public Setting getSettings() {
182
        return new GeoprocessSetting();
183
    }
184

  
185
    public void update() {
186
        // Nothing to do
187
    }
188

  
189
    public Object getAlgorithmHelp(GeoAlgorithm alg) {
190
        String algName = alg.getClass().getSimpleName();
191
        String helpFileName =
192
            algName + "_" + Locale.getDefault().getLanguage() + ".xml";
193
        String currentLocaleHelp = "help" + File.separator + helpFileName;
194
        InputStream is =
195
            alg.getClass().getClassLoader()
196
                .getResourceAsStream(currentLocaleHelp);
197
        if (is == null) {
198
            helpFileName = algName + ".xml";
199
            currentLocaleHelp = "help" + File.separator + helpFileName;
200
            is =
201
                alg.getClass().getClassLoader()
202
                    .getResourceAsStream(currentLocaleHelp);
203
        }
204

  
205
        if (is == null) {
206
            return null;
207
        }
208

  
209
        // The following code has been extracted from the
210
        // es.unex.sextante.gui.help.HelpIO getHelpAsHTMLCode method, as it
211
        // works with a string file path, and we need a stream from a classpath
212
        // resource.
213

  
214
        URL helpURL =
215
            alg.getClass().getClassLoader().getResource(currentLocaleHelp);
216
        String helpBaseURL = helpURL.toString();
217
        int lastIndex = helpBaseURL.length() - helpFileName.length();
218
        helpBaseURL = helpBaseURL.substring(0, lastIndex);
219
        HelpElement element;
220
        Map<String, HelpElement> elements = readHelpElements(is);
221

  
222
        HTMLDoc doc = new HTMLDoc();
223

  
224
        doc.open(alg.getName());
225
        doc.addHeader(Sextante.getText(alg.getName()), 1);
226
        doc.addHeader(Sextante.getText("Description"), 2);
227

  
228
        element = (HelpElement) elements.get("DESCRIPTION");
229
        if (element != null) {
230
            doc.addParagraph(element.getTextAsFormattedHTML());
231
            for (int j = 0; j < element.getImages().size(); j++) {
232
                ImageAndDescription iad =
233
                    (ImageAndDescription) element.getImages().get(j);
234
                doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
235
                    iad.getDescription());
236
            }
237
        }
238

  
239
        doc.addHeader(Sextante.getText("Parameters"), 2);
240

  
241
        ParametersSet params = alg.getParameters();
242

  
243
        doc.startUnorderedList();
244
        for (int i = 0; i < params.getNumberOfParameters(); i++) {
245
            Parameter param = params.getParameter(i);
246
            String sParam = param.getParameterDescription();
247
            sParam =
248
                "<b>" + sParam + "[" + getParameterTypeName(param) + "]: </b>";
249
            element = (HelpElement) elements.get(param.getParameterName());
250
            if (element != null) {
251
                sParam = sParam + element.getTextAsFormattedHTML();
252
            }
253
            doc.addListElement(sParam);
254
            if (element != null) {
255
                for (int j = 0; j < element.getImages().size(); j++) {
256
                    final ImageAndDescription iad =
257
                        (ImageAndDescription) element.getImages().get(j);
258
                    doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
259
                        iad.getDescription());
260
                }
261
            }
262

  
263
        }
264

  
265
        doc.closeUnorderedList();
266

  
267
        doc.addHeader(Sextante.getText("Outputs"), 2);
268

  
269
        element = (HelpElement) elements.get("OUTPUT_DESCRIPTION");
270
        if (element != null) {
271
            doc.addParagraph(element.getTextAsFormattedHTML());
272
            for (int j = 0; j < element.getImages().size(); j++) {
273
                final ImageAndDescription iad =
274
                    (ImageAndDescription) element.getImages().get(j);
275
                doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
276
                    iad.getDescription());
277
            }
278
        }
279

  
280
        doc.startUnorderedList();
281
        final OutputObjectsSet oo = alg.getOutputObjects();
282
        String sOutputType = "";
283
        for (int i = 0; i < oo.getOutputObjectsCount(); i++) {
284
            final Output out = oo.getOutput(i);
285
            String sOutput = out.getDescription();
286
            if (out instanceof OutputRasterLayer) {
287
                sOutputType = Sextante.getText("Raster_Layer");
288
            } else if (out instanceof Output3DRasterLayer) {
289
                sOutputType = Sextante.getText("3D_Raster_layer");
290
            } else if (out instanceof OutputVectorLayer) {
291
                sOutputType = Sextante.getText("Vector_Layer");
292
                final OutputVectorLayer ovl = (OutputVectorLayer) out;
293
                switch (ovl.getShapeType()) {
294
                case OutputVectorLayer.SHAPE_TYPE_UNDEFINED:
295
                default:
296
                    sOutputType =
297
                        sOutputType + " - " + Sextante.getText("Any_type");
298
                    break;
299
                case OutputVectorLayer.SHAPE_TYPE_LINE:
300
                    sOutputType =
301
                        sOutputType + " - " + Sextante.getText("Line");
302
                    break;
303
                case OutputVectorLayer.SHAPE_TYPE_POLYGON:
304
                    sOutputType =
305
                        sOutputType + " - " + Sextante.getText("Polygon");
306
                    break;
307
                case OutputVectorLayer.SHAPE_TYPE_POINT:
308
                    sOutputType =
309
                        sOutputType + " - " + Sextante.getText("Point");
310
                    break;
311
                }
312
            } else if (out instanceof OutputTable) {
313
                sOutputType = Sextante.getText("Table");
314
            } else if (out instanceof OutputChart) {
315
                sOutputType = Sextante.getText("graph-chart");
316
            } else if (out instanceof OutputText) {
317
                sOutputType = Sextante.getText("Text");
318
            } else if (out instanceof OutputNumericalValue) {
319
                sOutputType = Sextante.getText("Numerical_value");
320
            }
321
            sOutput = "<b>" + sOutput + "[" + sOutputType + "]: </b>";
322
            element = (HelpElement) elements.get(out.getName());
323
            if (element != null) {
324
                sOutput = sOutput + element.getTextAsFormattedHTML();
325
            }
326
            doc.addListElement(sOutput);
327
            if (element != null) {
328
                for (int j = 0; j < element.getImages().size(); j++) {
329
                    ImageAndDescription iad =
330
                        (ImageAndDescription) element.getImages().get(j);
331
                    doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
332
                        iad.getDescription());
333
                }
334
            }
335

  
336
        }
337
        doc.closeUnorderedList();
338

  
339
        doc.addHeader(Sextante.getText("Additional_information"), 2);
340

  
341
        element = (HelpElement) elements.get("ADDITIONAL_INFO");
342
        if (element != null) {
343
            doc.addParagraph(element.getTextAsFormattedHTML());
344
            for (int j = 0; j < element.getImages().size(); j++) {
345
                ImageAndDescription iad =
346
                    (ImageAndDescription) element.getImages().get(j);
347
                doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
348
                    iad.getDescription());
349
            }
350
        }
351

  
352
        doc.addHeader(Sextante.getText("Command_line"), 2);
353

  
354
        String sText = alg.getCommandLineHelp();
355
        sText = sText.replaceAll("\n", "<br>");
356
        sText = sText.replace("   ", " &nbsp ");
357
        doc.addCourierText(sText);
358

  
359
        doc.addParagraph("");
360

  
361
        element = (HelpElement) elements.get("EXTENSION_AUTHOR");
362
        if (element != null) {
363
            doc.addParagraph("<i>" + Sextante.getText("Algorithm_created_by")
364
                + " " + element.getText() + "</i>");
365
            for (int j = 0; j < element.getImages().size(); j++) {
366
                ImageAndDescription iad =
367
                    (ImageAndDescription) element.getImages().get(j);
368
                doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
369
                    iad.getDescription());
370
            }
371
        }
372

  
373
        element = (HelpElement) elements.get("HELP_AUTHOR");
374
        if (element != null) {
375
            doc.addParagraph("<i>" + Sextante.getText("Help_file_created_by")
376
                + " " + element.getText() + "</i>");
377
            for (int j = 0; j < element.getImages().size(); j++) {
378
                ImageAndDescription iad =
379
                    (ImageAndDescription) element.getImages().get(j);
380
                doc.addImageAndDescription(helpBaseURL + iad.getFilename(),
381
                    iad.getDescription());
382
            }
383
        }
384

  
385
        doc.close();
386

  
387
        return doc.getHTMLCode();
388
    }
389

  
390
    public String getAlgorithmHelpFilename(GeoAlgorithm alg,
391
        boolean bForceCurrentLocale) {
392
        // TODO Auto-generated method stub
393
        return null;
394
    }
395

  
396
    public boolean canEditHelp() {
397
        return false;
398
    }
399

  
400
    public ILogHandler getLogHandler() {
401
        return null;
402
    }
403

  
404
    private Map<String, HelpElement> readHelpElements(InputStream is) {
405
        List<ImageAndDescription> images = null;
406
        Map<String, HelpElement> elements = new HashMap<String, HelpElement>();
407
        HelpElement element = null;
408
        KXmlParser parser = new KXmlParser();
409
        try {
410
            parser.setInput(is, XML_PARSER_ENCODING);
411
            int tag = parser.nextTag();
412
            boolean bOut = false;
413
            String name;
414

  
415
            if (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
416
                while ((tag != XmlPullParser.END_DOCUMENT) && !bOut) {
417
                    switch (tag) {
418
                    case XmlPullParser.START_TAG:
419
                        name = parser.getName();
420
                        if (HELP.equals(name)) {
421
                        } else if (HelpElement.ELEMENT.equals(name)) {
422
                            images = new ArrayList<ImageAndDescription>();
423
                            String sText =
424
                                parser.getAttributeValue("", HelpElement.TEXT);
425
                            String sName =
426
                                parser.getAttributeValue("", HelpElement.NAME);
427
                            String sDescription =
428
                                parser.getAttributeValue("",
429
                                    HelpElement.DESCRIPTION);
430
                            int iType =
431
                                Integer.parseInt(parser.getAttributeValue("",
432
                                    HelpElement.TYPE));
433
                            element = new HelpElement();
434
                            element.setText(sText);
435
                            element.setName(sName);
436
                            element.setType(iType);
437
                            element.setDescription(sDescription);
438
                        } else if (ImageAndDescription.IMAGE.equals(name)) {
439
                            final ImageAndDescription iad =
440
                                new ImageAndDescription();
441
                            final String sImageFilename =
442
                                parser.getAttributeValue("",
443
                                    ImageAndDescription.FILE);
444
                            final String sDesc =
445
                                parser.getAttributeValue("",
446
                                    ImageAndDescription.DESCRIPTION);
447
                            iad.setDescription(sDesc);
448
                            iad.setFilename(sImageFilename);
449
                            images.add(iad);
450
                        }
451
                        break;
452
                    case XmlPullParser.END_TAG:
453
                        name = parser.getName();
454
                        if (HELP.equals(name)) {
455
                            bOut = true;
456
                        } else if (HelpElement.ELEMENT.equals(name)) {
457
                            element.setImages((ArrayList) images);
458
                            elements.put(element.getName(), element);
459
                        }
460
                        break;
461
                    case XmlPullParser.TEXT:
462
                        break;
463
                    }
464
                    if (!bOut) {
465
                        tag = parser.next();
466
                    }
467
                }
468
            }
469

  
470
        } catch (final Exception e) {
471
            return null;
472
        }
473
        return elements;
474
    }
475

  
476
    private String getParameterTypeName(Parameter param) {
477

  
478
        String s =
479
            Sextante.getText(param.getParameterTypeName().replace(' ', '_'));
480

  
481
        if (param instanceof ParameterVectorLayer) {
482
            try {
483
                final AdditionalInfoVectorLayer ai =
484
                    (AdditionalInfoVectorLayer) param
485
                        .getParameterAdditionalInfo();
486
                switch (ai.getShapeType()) {
487
                case AdditionalInfoVectorLayer.SHAPE_TYPE_ANY:
488
                default:
489
                    s = s + " - " + Sextante.getText("Any_type");
490
                    break;
491
                case AdditionalInfoVectorLayer.SHAPE_TYPE_LINE:
492
                    s = s + " - " + Sextante.getText("Line");
493
                    break;
494
                case AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON:
495
                    s = s + " - " + Sextante.getText("Polygon");
496
                    break;
497
                case AdditionalInfoVectorLayer.SHAPE_TYPE_POINT:
498
                    s = s + " - " + Sextante.getText("Point");
499
                    break;
500
                }
501
            } catch (final NullParameterAdditionalInfoException e) {
502
                // Nothing to do
503
            }
504
        }
505

  
506
        return s;
507

  
508
    }
53 509
}

Also available in: Unified diff