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