Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / core / PostProcessTaskRunnable.java @ 1055

History | View | Annotate | Download (17 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.lib.sextante.core;
25

    
26
import java.awt.Component;
27
import java.util.List;
28

    
29
import javax.swing.BorderFactory;
30
import javax.swing.JOptionPane;
31
import javax.swing.JScrollPane;
32
import javax.swing.JTextPane;
33
import javax.swing.ScrollPaneConstants;
34
import javax.swing.SwingUtilities;
35
import javax.swing.border.BevelBorder;
36

    
37
import org.cresques.cts.IProjection;
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.extension.ProjectExtension;
42
import org.gvsig.app.project.Project;
43
import org.gvsig.app.project.ProjectManager;
44
import org.gvsig.app.project.documents.Document;
45
import org.gvsig.app.project.documents.table.TableDocument;
46
import org.gvsig.app.project.documents.view.ViewDocument;
47
import org.gvsig.app.project.documents.view.ViewManager;
48
import org.gvsig.fmap.mapcontext.MapContext;
49
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.fmap.mapcontext.layers.FLayers;
52
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
53
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
54
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools;
55

    
56
import es.unex.sextante.core.GeoAlgorithm;
57
import es.unex.sextante.core.ObjectAndDescription;
58
import es.unex.sextante.core.OutputObjectsSet;
59
import es.unex.sextante.core.ParametersSet;
60
import es.unex.sextante.core.Sextante;
61
import es.unex.sextante.dataObjects.ILayer;
62
import es.unex.sextante.dataObjects.IRasterLayer;
63
import es.unex.sextante.dataObjects.IVectorLayer;
64
import es.unex.sextante.gui.additionalResults.AdditionalResults;
65
import es.unex.sextante.gui.algorithm.iterative.SingleFeatureVectorLayer;
66
import es.unex.sextante.gui.core.SextanteGUI;
67
import es.unex.sextante.gui.settings.SextanteGeneralSettings;
68
import es.unex.sextante.outputs.FileOutputChannel;
69
import es.unex.sextante.outputs.IOutputChannel;
70
import es.unex.sextante.outputs.NullOutputChannel;
71
import es.unex.sextante.outputs.Output;
72
import es.unex.sextante.outputs.Output3DRasterLayer;
73
import es.unex.sextante.outputs.OutputRasterLayer;
74
import es.unex.sextante.outputs.OutputTable;
75
import es.unex.sextante.outputs.OutputText;
76
import es.unex.sextante.outputs.OutputVectorLayer;
77
import es.unex.sextante.outputs.OverwriteOutputChannel;
78
import es.unex.sextante.parameters.Parameter;
79
import es.unex.sextante.parameters.RasterLayerAndBand;
80
import java.lang.reflect.InvocationTargetException;
81
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer;
82
import org.gvsig.tools.locator.LocatorException;
83

    
84
public class PostProcessTaskRunnable implements Runnable {
85

    
86
    private MapContext m_MapContext;
87
    private final GeoAlgorithm m_Algorithm;
88
    private final OutputObjectsSet m_Output;
89
    private final boolean m_bShowResultsWindows;
90

    
91
    public PostProcessTaskRunnable(final GeoAlgorithm algorithm,
92
        final boolean bShowResultsWindow) {
93

    
94
        m_Output = algorithm.getOutputObjects();
95
        m_Algorithm = algorithm;
96
        m_bShowResultsWindows = bShowResultsWindow;
97

    
98
    }
99

    
100
    @Override
101
    public void run() {
102

    
103
        if (m_Output.hasLayers()) {
104
            setOutputView();
105
        }
106

    
107
        addResults();
108

    
109
    }
110

    
111
    private void setOutputView() {
112

    
113
        ViewDocument view;
114
        final ParametersSet parameters = m_Algorithm.getParameters();
115
        for (int i = 0; parameters.getNumberOfParameters() >= i; i++) {
116
            final Parameter param = parameters.getParameter(i);
117
            final Object object = param.getParameterValueAsObject();
118
            if (object instanceof ILayer) {
119
                view = getViewFromLayer((ILayer) object);
120
                m_MapContext = view.getMapContext();
121
                return;
122
            } else
123
                if (object instanceof List) {
124
                    final List list = (List) object;
125
                    for (int j = 0; j < list.size(); j++) {
126
                        final Object obj = list.get(j);
127
                        if (obj instanceof ILayer) {
128
                            view = getViewFromLayer((ILayer) obj);
129
                            m_MapContext = view.getMapContext();
130
                            return;
131
                        } else
132
                            if (obj instanceof RasterLayerAndBand) {
133
                                final RasterLayerAndBand rlab =
134
                                    (RasterLayerAndBand) obj;
135
                                view = getViewFromLayer(rlab.getRasterLayer());
136
                                m_MapContext = view.getMapContext();
137
                                return;
138
                            }
139
                    }
140
                }
141
        }
142

    
143
        // if there is no input view, ask the user
144
        final Project project =
145
            ((ProjectExtension) PluginServices
146
                .getExtension(ProjectExtension.class)).getProject();
147
        final List<Document> views = project.getDocuments(ViewManager.TYPENAME);
148
        final Object[] options = new Object[views.size() + 1];
149
        options[0] = Sextante.getText("Create_new_view");
150
        for (int i = 0; i < views.size(); i++) {
151
            options[i + 1] = views.get(i);
152
        }
153

    
154
        final Object selectedObject =
155
            JOptionPane.showInputDialog(null,
156
                Sextante.getText("Select_output_view"),
157
                Sextante.getText("Output_view"), JOptionPane.PLAIN_MESSAGE,
158
                null, options, null);
159

    
160
        if (selectedObject instanceof ViewDocument) {
161
                m_MapContext = ((ViewDocument) selectedObject).getMapContext();
162
        } else {
163
            final Document newView =
164
                ProjectManager.getInstance().createDocument(
165
                    ViewManager.TYPENAME,
166
                    "SEXTANTE (" + m_Algorithm.getName() + ")");
167
            ((ProjectExtension) PluginServices
168
                .getExtension(ProjectExtension.class)).getProject()
169
                .add(newView);
170
            final IWindow window = newView.getFactory().getMainWindow(newView);
171
            final Runnable doWorkRunnable = new Runnable() {
172

    
173
                @Override
174
                public void run() {
175
                    PluginServices.getMDIManager().addWindow(window);
176
                    m_MapContext = ((ViewDocument) newView).getMapContext();
177
                }
178
            };
179
            try {
180
                SwingUtilities.invokeAndWait(doWorkRunnable);
181
            } catch (final InterruptedException e) {
182
                Sextante.addErrorToLog(e);
183
            } catch (final InvocationTargetException e) {
184
                Sextante.addErrorToLog(e);
185
            }
186
        }
187

    
188
    }
189

    
190
    private ViewDocument getViewFromLayer(ILayer layer) {
191

    
192
        if (layer instanceof SingleFeatureVectorLayer) {
193
            layer = ((SingleFeatureVectorLayer) layer).getOriginalLayer();
194
        }
195

    
196
        final FLayer gvSIGBaseLayer = (FLayer) layer.getBaseDataObject();
197
        final Project project =
198
            ((ProjectExtension) PluginServices
199
                .getExtension(ProjectExtension.class)).getProject();
200
        final List<Document> docs = project.getDocuments(ViewManager.TYPENAME);
201

    
202
        for (int i = 0; i < docs.size(); i++) {
203
            final ViewDocument viewDoc = (ViewDocument) docs.get(i);
204
            final FLayers layers = viewDoc.getMapContext().getLayers();
205
            final LayersIterator iter = new LayersIterator(layers);
206
            while (iter.hasNext()) {
207
                final FLayer gvSIGLayer = iter.nextLayer();
208
                if (gvSIGLayer.equals(gvSIGBaseLayer)) {
209
                    return viewDoc;
210
                }
211
            }
212

    
213
        }
214

    
215
        return null;
216

    
217
    }
218

    
219
    private void addResults() {
220

    
221
        final boolean bUseInternalNames;
222
        bUseInternalNames = Boolean
223
                .parseBoolean(SextanteGUI
224
                        .getSettingParameterValue(SextanteGeneralSettings.USE_INTERNAL_NAMES));
225
        final boolean bModiFyResultsNames;
226
        bModiFyResultsNames = Boolean
227
                .parseBoolean(SextanteGUI
228
                        .getSettingParameterValue(SextanteGeneralSettings.MODIFY_NAMES));
229

    
230
        String sDescription;
231
        boolean bInvalidate = false;
232
        boolean bShowAdditionalPanel = false;
233

    
234
        if (m_MapContext != null) {
235
            m_MapContext.beginAtomicEvent();
236
        }
237

    
238
        for (int i = 0; i < m_Output.getOutputObjectsCount(); i++) {
239

    
240
            final Output out = m_Output.getOutput(i);
241
            sDescription = out.getDescription();
242
            final IOutputChannel channel = out.getOutputChannel();
243
            final Object object = out.getOutputObject();
244

    
245
            if ((out instanceof OutputRasterLayer)
246
                || (out instanceof Output3DRasterLayer)
247
                || (out instanceof OutputTable)
248
                || (out instanceof OutputVectorLayer)) {
249
                if (bUseInternalNames) {
250
                    sDescription = out.getName();
251
                } else
252
                    if (bModiFyResultsNames) {
253
                        sDescription =
254
                            SextanteGUI.modifyResultName(sDescription);
255
                    }
256
                if ((channel instanceof NullOutputChannel) || (channel == null)) {
257
                    continue;
258
                }
259
            }
260
            if (out instanceof OutputVectorLayer) {
261
                String sFilename;
262
                if (channel instanceof FileOutputChannel) {
263
                    sFilename = ((FileOutputChannel) channel).getFilename();
264
                    final FLyrVect flayer =
265
                        (FLyrVect) FileTools.openLayer(sFilename, sDescription,
266
                            (IProjection) m_Algorithm.getOutputCRS());
267
                    if (flayer != null) {
268
                        flayer.setTemporary(((FileOutputChannel)channel).isTemporary());
269
                        flayer.setName(sDescription);
270
                        m_MapContext.getLayers().addLayer(flayer);
271
                        bInvalidate = true;
272
                    }
273

    
274
                } else
275
                    if (channel instanceof OverwriteOutputChannel) {
276
                        // TODO:add support for non file based layer
277
                        final FLyrVect flayer =
278
                            (FLyrVect) ((OverwriteOutputChannel) channel)
279
                                .getLayer().getBaseDataObject();
280
                        try {
281
                            flayer.reload();
282
                            bInvalidate = true;
283
                        } catch (final ReloadLayerException e) {
284

    
285
                        }
286
                    }
287

    
288
                if (object != null) {
289
                    ((IVectorLayer) object).close();
290
                }
291

    
292
            } else
293
                if (out instanceof OutputTable) {
294
                    try {
295
                        final String sFilename =
296
                            ((FileOutputChannel) channel).getFilename();
297
                        final TableDocument table =
298
                            FileTools.openTable(sFilename, sDescription);
299
                        if (table != null) {
300
                            table.setName(sDescription);
301
                            ApplicationLocator.getManager().getProjectManager()
302
                                .getCurrentProject().add(table);
303

    
304
                            // final JScrollPane jScrollPane =
305
                            // TableTools
306
                            // .getScrollableTablePanelFromITable((ITable)
307
                            // object);
308
                            // AdditionalResults
309
                            // .addComponent(new ObjectAndDescription(
310
                            // sDescription, jScrollPane));
311
                            // bShowAdditionalPanel = true;
312
                        }
313
                    } catch (final LocatorException e) {
314
                        Sextante.addErrorToLog(e);
315
                    }
316
                } else
317
                    if (out instanceof OutputRasterLayer) {
318
                        final IRasterLayer rasterLayer = (IRasterLayer) object;
319
                        if (channel instanceof FileOutputChannel) {
320
                            final String sFilename =
321
                                ((FileOutputChannel) channel).getFilename();
322
                            final RasterLayer flayer =
323
                                (RasterLayer) FileTools.openLayer(
324
                                    sFilename,
325
                                    sDescription,
326
                                    (IProjection) m_Algorithm.getOutputCRS());
327
                            if (flayer != null) {
328
                                if (rasterLayer != null) {
329
                                    try {
330
                                        //NoData nodata = BufferLocator.getBufferManager().createNoData(rasterLayer.getNoDataValue(), null);
331
                                        //if(flayer.getRasterStore().getBandDescriptor(0).getDataType() == BufferManager.TYPE_BYTE)  {
332
                                        //            nodata.setNoDataTransparent(false);
333
                                        //}
334
                                        //flayer.setNoDataValue(nodata);
335
                                        //nodata.save();
336

    
337
                                        rasterLayer.close();
338
                                    } catch (final Exception e) {
339
                                        Sextante.addErrorToLog(e);
340
                                    }
341
                                }
342
                                flayer.setName(sDescription);
343
                                m_MapContext.getLayers().addLayer(flayer);
344
                                bInvalidate = true;
345

    
346
                            }
347
                        }
348
                    } else
349
                        if (out instanceof OutputText) {
350
                            JTextPane jTextPane;
351
                            JScrollPane jScrollPane;
352
                            jTextPane = new JTextPane();
353
                            jTextPane.setEditable(false);
354
                            jTextPane.setContentType("text/html");
355
                            jTextPane.setText((String) object);
356
                            jScrollPane = new JScrollPane();
357
                            jScrollPane.setViewportView(jTextPane);
358
                            jScrollPane
359
                                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
360
                            jTextPane.setBorder(BorderFactory
361
                                .createEtchedBorder(BevelBorder.LOWERED));
362
                            AdditionalResults
363
                                .addComponent(new ObjectAndDescription(
364
                                    sDescription, jScrollPane));
365
                            bShowAdditionalPanel = true;
366
                        } else
367
                            if (object instanceof Component) {
368
                                AdditionalResults
369
                                    .addComponent(new ObjectAndDescription(
370
                                        sDescription, object));
371
                                bShowAdditionalPanel = true;
372
                            } else
373
                                if (out instanceof Output3DRasterLayer) {
374
                                    JOptionPane.showMessageDialog(
375
                                        SextanteGUI.getMainFrame(),
376
                                        Sextante.getText("3d_not_supported"),
377
                                        Sextante.getText("Warning"),
378
                                        JOptionPane.WARNING_MESSAGE);
379
                                }
380

    
381
        }
382

    
383
        if (m_MapContext != null) {
384
            m_MapContext.endAtomicEvent();
385
        }
386

    
387
        if (bInvalidate) {
388
            m_MapContext.invalidate();
389
        }
390

    
391
        if (bShowAdditionalPanel && m_bShowResultsWindows) {
392
            AdditionalResults.showPanel();
393
        }
394

    
395
    }
396
}