gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.app / org.gvsig.geoprocess.app.mainplugin / src / main / java / org / gvsig / geoprocess / gui / gvPostProcessTask.java @ 175
History | View | Annotate | Download (16 KB)
1 |
package org.gvsig.geoprocess.gui; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.util.List; |
5 |
|
6 |
import javax.swing.BorderFactory; |
7 |
import javax.swing.JOptionPane; |
8 |
import javax.swing.JScrollPane; |
9 |
import javax.swing.JTextPane; |
10 |
import javax.swing.ScrollPaneConstants; |
11 |
import javax.swing.SwingUtilities; |
12 |
import javax.swing.border.BevelBorder; |
13 |
|
14 |
import es.unex.sextante.core.GeoAlgorithm; |
15 |
import es.unex.sextante.core.ObjectAndDescription; |
16 |
import es.unex.sextante.core.OutputObjectsSet; |
17 |
import es.unex.sextante.core.ParametersSet; |
18 |
import es.unex.sextante.core.Sextante; |
19 |
import es.unex.sextante.dataObjects.ILayer; |
20 |
import es.unex.sextante.dataObjects.IRasterLayer; |
21 |
import es.unex.sextante.dataObjects.ITable; |
22 |
import es.unex.sextante.dataObjects.IVectorLayer; |
23 |
import es.unex.sextante.gui.additionalResults.AdditionalResults; |
24 |
import es.unex.sextante.gui.additionalResults.TableTools; |
25 |
import es.unex.sextante.gui.algorithm.iterative.SingleFeatureVectorLayer; |
26 |
import es.unex.sextante.gui.core.SextanteGUI; |
27 |
import es.unex.sextante.gui.settings.SextanteGeneralSettings; |
28 |
import es.unex.sextante.outputs.FileOutputChannel; |
29 |
import es.unex.sextante.outputs.IOutputChannel; |
30 |
import es.unex.sextante.outputs.NullOutputChannel; |
31 |
import es.unex.sextante.outputs.Output; |
32 |
import es.unex.sextante.outputs.Output3DRasterLayer; |
33 |
import es.unex.sextante.outputs.OutputRasterLayer; |
34 |
import es.unex.sextante.outputs.OutputTable; |
35 |
import es.unex.sextante.outputs.OutputText; |
36 |
import es.unex.sextante.outputs.OutputVectorLayer; |
37 |
import es.unex.sextante.outputs.OverwriteOutputChannel; |
38 |
import es.unex.sextante.parameters.Parameter; |
39 |
import es.unex.sextante.parameters.RasterLayerAndBand; |
40 |
|
41 |
import org.cresques.cts.IProjection; |
42 |
|
43 |
import org.gvsig.andami.PluginServices; |
44 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
45 |
import org.gvsig.app.extension.ProjectExtension; |
46 |
import org.gvsig.app.project.Project; |
47 |
import org.gvsig.app.project.ProjectManager; |
48 |
import org.gvsig.app.project.documents.Document; |
49 |
import org.gvsig.app.project.documents.table.TableDocument; |
50 |
import org.gvsig.app.project.documents.view.ViewDocument; |
51 |
import org.gvsig.app.project.documents.view.ViewManager; |
52 |
import org.gvsig.app.project.documents.view.gui.IView; |
53 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
54 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
55 |
import org.gvsig.fmap.mapcontext.MapContext; |
56 |
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException; |
57 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
58 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
59 |
import org.gvsig.fmap.mapcontext.layers.LayersIterator; |
60 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
61 |
import org.gvsig.geoprocess.core.FileTools; |
62 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
63 |
|
64 |
public class gvPostProcessTask implements Runnable { |
65 |
|
66 |
private MapContext m_MapContext;
|
67 |
private final GeoAlgorithm m_Algorithm; |
68 |
private final OutputObjectsSet m_Output; |
69 |
private final boolean m_bShowResultsWindows; |
70 |
|
71 |
public gvPostProcessTask(final GeoAlgorithm algorithm, |
72 |
final boolean bShowResultsWindow) { |
73 |
|
74 |
m_Output = algorithm.getOutputObjects(); |
75 |
m_Algorithm = algorithm; |
76 |
m_bShowResultsWindows = bShowResultsWindow; |
77 |
|
78 |
} |
79 |
|
80 |
public void run() { |
81 |
|
82 |
if (m_Output.hasLayers()) {
|
83 |
setOutputView(); |
84 |
} |
85 |
|
86 |
addResults(); |
87 |
|
88 |
} |
89 |
|
90 |
private void setOutputView() { |
91 |
|
92 |
ViewDocument view = null;
|
93 |
final ParametersSet parameters = m_Algorithm.getParameters();
|
94 |
for (int i = 0; i < parameters.getNumberOfParameters(); i++) { |
95 |
final Parameter param = parameters.getParameter(i);
|
96 |
final Object object = param.getParameterValueAsObject(); |
97 |
if (object instanceof ILayer) { |
98 |
view = getViewFromLayer((ILayer) object); |
99 |
m_MapContext = view.getMapContext(); |
100 |
return;
|
101 |
} else
|
102 |
if (object instanceof List) { |
103 |
final List list = (List) object; |
104 |
for (int j = 0; j < list.size(); j++) { |
105 |
final Object obj = list.get(j); |
106 |
if (obj instanceof ILayer) { |
107 |
view = getViewFromLayer((ILayer) obj); |
108 |
m_MapContext = view.getMapContext(); |
109 |
return;
|
110 |
} else
|
111 |
if (obj instanceof RasterLayerAndBand) { |
112 |
final RasterLayerAndBand rlab =
|
113 |
(RasterLayerAndBand) obj; |
114 |
view = getViewFromLayer(rlab.getRasterLayer()); |
115 |
m_MapContext = view.getMapContext(); |
116 |
return;
|
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
// if there is no input view, ask the user
|
123 |
final Project project =
|
124 |
((ProjectExtension) PluginServices |
125 |
.getExtension(ProjectExtension.class)).getProject(); |
126 |
final List<Document> views = project.getDocuments(ViewManager.TYPENAME); |
127 |
final Object[] options = new Object[views.size() + 1]; |
128 |
options[0] = Sextante.getText("Create_new_view"); |
129 |
for (int i = 0; i < views.size(); i++) { |
130 |
options[i + 1] = views.get(i);
|
131 |
} |
132 |
|
133 |
final Object selectedObject = |
134 |
JOptionPane.showInputDialog(null, |
135 |
Sextante.getText("Select_output_view"),
|
136 |
Sextante.getText("Output_view"), JOptionPane.PLAIN_MESSAGE, |
137 |
null, options, null); |
138 |
|
139 |
if (selectedObject instanceof IView) { |
140 |
m_MapContext = |
141 |
((IView) selectedObject).getMapControl().getMapContext(); |
142 |
} else {
|
143 |
final Document newView = |
144 |
ProjectManager.getInstance().createDocument( |
145 |
ViewManager.TYPENAME, |
146 |
"SEXTANTE (" + m_Algorithm.getName() + ")"); |
147 |
((ProjectExtension) PluginServices |
148 |
.getExtension(ProjectExtension.class)).getProject() |
149 |
.add(newView); |
150 |
final IWindow window = newView.getFactory().getMainWindow(newView);
|
151 |
final Runnable doWorkRunnable = new Runnable() { |
152 |
|
153 |
public void run() { |
154 |
PluginServices.getMDIManager().addWindow(window); |
155 |
m_MapContext = ((ViewDocument) newView).getMapContext(); |
156 |
} |
157 |
}; |
158 |
try {
|
159 |
SwingUtilities.invokeAndWait(doWorkRunnable);
|
160 |
} catch (final Exception e) { |
161 |
Sextante.addErrorToLog(e); |
162 |
} |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
private ViewDocument getViewFromLayer(ILayer layer) {
|
168 |
|
169 |
if (layer instanceof SingleFeatureVectorLayer) { |
170 |
layer = ((SingleFeatureVectorLayer) layer).getOriginalLayer(); |
171 |
} |
172 |
|
173 |
final FLayer gvSIGBaseLayer = (FLayer) layer.getBaseDataObject();
|
174 |
final Project project =
|
175 |
((ProjectExtension) PluginServices |
176 |
.getExtension(ProjectExtension.class)).getProject(); |
177 |
final List<Document> docs = project.getDocuments(ViewManager.TYPENAME); |
178 |
|
179 |
for (int i = 0; i < docs.size(); i++) { |
180 |
final ViewDocument viewDoc = (ViewDocument) docs.get(i);
|
181 |
final FLayers layers = viewDoc.getMapContext().getLayers();
|
182 |
final LayersIterator iter = new LayersIterator(layers); |
183 |
while (iter.hasNext()) {
|
184 |
final FLayer gvSIGLayer = iter.nextLayer();
|
185 |
if (gvSIGLayer.equals(gvSIGBaseLayer)) {
|
186 |
return viewDoc;
|
187 |
} |
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
return null; |
193 |
|
194 |
} |
195 |
|
196 |
private void addResults() { |
197 |
|
198 |
final boolean bUseInternalNames = |
199 |
new Boolean( |
200 |
SextanteGUI |
201 |
.getSettingParameterValue(SextanteGeneralSettings.USE_INTERNAL_NAMES)) |
202 |
.booleanValue(); |
203 |
final boolean bModiFyResultsNames = |
204 |
new Boolean( |
205 |
SextanteGUI |
206 |
.getSettingParameterValue(SextanteGeneralSettings.MODIFY_NAMES)) |
207 |
.booleanValue(); |
208 |
|
209 |
String sDescription;
|
210 |
boolean bInvalidate = false; |
211 |
boolean bShowAdditionalPanel = false; |
212 |
|
213 |
if (m_MapContext != null) { |
214 |
m_MapContext.beginAtomicEvent(); |
215 |
} |
216 |
|
217 |
for (int i = 0; i < m_Output.getOutputObjectsCount(); i++) { |
218 |
|
219 |
final Output out = m_Output.getOutput(i);
|
220 |
sDescription = out.getDescription(); |
221 |
final IOutputChannel channel = out.getOutputChannel();
|
222 |
final Object object = out.getOutputObject(); |
223 |
|
224 |
if ((out instanceof OutputRasterLayer) |
225 |
|| (out instanceof Output3DRasterLayer)
|
226 |
|| (out instanceof OutputTable)
|
227 |
|| (out instanceof OutputVectorLayer)) {
|
228 |
if (bUseInternalNames) {
|
229 |
sDescription = out.getName(); |
230 |
} else
|
231 |
if (bModiFyResultsNames) {
|
232 |
sDescription = |
233 |
SextanteGUI.modifyResultName(sDescription); |
234 |
} |
235 |
if ((channel instanceof NullOutputChannel) || (channel == null)) { |
236 |
continue;
|
237 |
} |
238 |
} |
239 |
if (out instanceof OutputVectorLayer) { |
240 |
String sFilename = null; |
241 |
if (channel instanceof FileOutputChannel) { |
242 |
sFilename = ((FileOutputChannel) channel).getFilename(); |
243 |
final FLyrVect flayer =
|
244 |
(FLyrVect) FileTools.openLayer(sFilename, sDescription, |
245 |
(IProjection) m_Algorithm.getOutputCRS()); |
246 |
if (flayer != null) { |
247 |
flayer.setName(sDescription); |
248 |
m_MapContext.getLayers().addLayer(flayer); |
249 |
bInvalidate = true;
|
250 |
} |
251 |
|
252 |
} else
|
253 |
if (channel instanceof OverwriteOutputChannel) { |
254 |
// TODO:add support for non file based layer
|
255 |
final FLyrVect flayer =
|
256 |
(FLyrVect) ((OverwriteOutputChannel) channel) |
257 |
.getLayer().getBaseDataObject(); |
258 |
try {
|
259 |
flayer.reload(); |
260 |
bInvalidate = true;
|
261 |
} catch (final ReloadLayerException e) { |
262 |
|
263 |
} |
264 |
} |
265 |
|
266 |
if (object != null) { |
267 |
((IVectorLayer) object).close(); |
268 |
} |
269 |
|
270 |
} else
|
271 |
if (out instanceof OutputTable) { |
272 |
try {
|
273 |
final String sFilename = |
274 |
((FileOutputChannel) channel).getFilename(); |
275 |
final TableDocument table =
|
276 |
FileTools.openTable(sFilename, sDescription); |
277 |
if (table != null) { |
278 |
table.setName(sDescription); |
279 |
((ProjectExtension) PluginServices |
280 |
.getExtension(ProjectExtension.class)) |
281 |
.getProject().add(table); |
282 |
final JScrollPane jScrollPane = |
283 |
TableTools |
284 |
.getScrollableTablePanelFromITable((ITable) object); |
285 |
AdditionalResults |
286 |
.addComponent(new ObjectAndDescription(
|
287 |
sDescription, jScrollPane)); |
288 |
bShowAdditionalPanel = true;
|
289 |
} |
290 |
} catch (final Exception e) { |
291 |
Sextante.addErrorToLog(e); |
292 |
} |
293 |
} else
|
294 |
if (out instanceof OutputRasterLayer) { |
295 |
final IRasterLayer rasterLayer = (IRasterLayer) object;
|
296 |
if (channel instanceof FileOutputChannel) { |
297 |
final String sFilename = |
298 |
((FileOutputChannel) channel).getFilename(); |
299 |
final FLyrRaster flayer =
|
300 |
(FLyrRaster) FileTools.openLayer(sFilename, |
301 |
sDescription, |
302 |
(IProjection) m_Algorithm.getOutputCRS()); |
303 |
if (flayer != null) { |
304 |
if (rasterLayer != null) { |
305 |
try {
|
306 |
|
307 |
NoData nodata = |
308 |
RasterLocator |
309 |
.getManager() |
310 |
.getDataStructFactory() |
311 |
.createNoData( |
312 |
rasterLayer |
313 |
.getNoDataValue(), |
314 |
null, sFilename);
|
315 |
|
316 |
flayer.setNoDataValue(nodata); |
317 |
nodata.save(); |
318 |
|
319 |
rasterLayer.close(); |
320 |
} catch (final Exception e) { |
321 |
Sextante.addErrorToLog(e); |
322 |
} |
323 |
} |
324 |
flayer.setName(sDescription); |
325 |
m_MapContext.getLayers().addLayer(flayer); |
326 |
bInvalidate = true;
|
327 |
|
328 |
} |
329 |
} |
330 |
} else
|
331 |
if (out instanceof OutputText) { |
332 |
JTextPane jTextPane;
|
333 |
JScrollPane jScrollPane;
|
334 |
jTextPane = new JTextPane(); |
335 |
jTextPane.setEditable(false);
|
336 |
jTextPane.setContentType("text/html");
|
337 |
jTextPane.setText((String) object);
|
338 |
jScrollPane = new JScrollPane(); |
339 |
jScrollPane.setViewportView(jTextPane); |
340 |
jScrollPane |
341 |
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
|
342 |
jTextPane.setBorder(BorderFactory
|
343 |
.createEtchedBorder(BevelBorder.LOWERED));
|
344 |
AdditionalResults |
345 |
.addComponent(new ObjectAndDescription(
|
346 |
sDescription, jScrollPane)); |
347 |
bShowAdditionalPanel = true;
|
348 |
} else
|
349 |
if (object instanceof Component) { |
350 |
AdditionalResults |
351 |
.addComponent(new ObjectAndDescription(
|
352 |
sDescription, object)); |
353 |
bShowAdditionalPanel = true;
|
354 |
} else
|
355 |
if (out instanceof Output3DRasterLayer) { |
356 |
JOptionPane.showMessageDialog(
|
357 |
SextanteGUI.getMainFrame(), |
358 |
Sextante.getText("3d_not_supported"),
|
359 |
Sextante.getText("Warning"),
|
360 |
JOptionPane.WARNING_MESSAGE);
|
361 |
} |
362 |
|
363 |
} |
364 |
|
365 |
if (m_MapContext != null) { |
366 |
m_MapContext.endAtomicEvent(); |
367 |
} |
368 |
|
369 |
if (bInvalidate) {
|
370 |
m_MapContext.invalidate(); |
371 |
} |
372 |
|
373 |
if (bShowAdditionalPanel && m_bShowResultsWindows) {
|
374 |
AdditionalResults.showPanel(); |
375 |
} |
376 |
|
377 |
} |
378 |
} |