gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / roi / ROIDataModel.java @ 2125
History | View | Annotate | Download (11.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
package org.gvsig.raster.tools.app.basic.tool.roi; |
23 |
|
24 |
import java.awt.Image; |
25 |
import java.io.File; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.LinkedHashMap; |
29 |
import java.util.List; |
30 |
|
31 |
import javax.swing.JComboBox; |
32 |
import javax.swing.table.DefaultTableModel; |
33 |
|
34 |
import org.gvsig.andami.IconThemeHelper; |
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
37 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
38 |
import org.gvsig.app.project.documents.view.toolListeners.StatusBarListener; |
39 |
import org.gvsig.fmap.dal.coverage.exception.GridException; |
40 |
import org.gvsig.fmap.dal.coverage.exception.ROIException; |
41 |
import org.gvsig.fmap.dal.coverage.grid.Grid; |
42 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.geom.Geometry; |
47 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
48 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
52 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
53 |
import org.gvsig.fmap.mapcontrol.MapControl; |
54 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
55 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseMovementBehavior; |
56 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
57 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PolygonBehavior; |
58 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PolylineBehavior; |
59 |
import org.gvsig.gui.beans.table.TableContainer; |
60 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
61 |
import org.gvsig.gui.beans.table.models.IModel; |
62 |
import org.gvsig.metadata.exceptions.MetadataException; |
63 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
64 |
import org.gvsig.raster.roi.ROI; |
65 |
import org.gvsig.raster.roi.VectorialROI; |
66 |
import org.gvsig.raster.swing.roi.ROIPanel; |
67 |
import org.gvsig.raster.swing.roi.ROIPanelDataModel; |
68 |
import org.gvsig.raster.tools.app.basic.RasterToolsUtil; |
69 |
import org.gvsig.raster.tools.app.basic.tool.roi.ui.listener.DrawMouseViewListener; |
70 |
|
71 |
/**
|
72 |
* Data model for ROIs panel
|
73 |
*
|
74 |
* @author Nacho Brodin nachobrodin@gmail.com
|
75 |
*/
|
76 |
public class ROIDataModel implements ROIPanelDataModel { |
77 |
private FLayer fLayer = null; |
78 |
private MapControl mapControl = null; |
79 |
private AbstractViewPanel view = null; |
80 |
private GraphicLayer graphicLayer = null; |
81 |
private String previousTool = null; |
82 |
private MapContextManager mapContextManager = MapContextLocator.getMapContextManager();
|
83 |
private ROIFeatureManager roiFeatureManager = null; |
84 |
private Image curImage = null; |
85 |
private LinkedHashMap<String, ROI> rois = null; |
86 |
private ROIPanel panel = null; |
87 |
private Grid grid = null; |
88 |
private List<FLyrVect> fLyrsList = null; |
89 |
private File roiFile = null; |
90 |
|
91 |
public class LayerElement { |
92 |
private FLyrVect lyr = null; |
93 |
public LayerElement(FLyrVect lyr) {
|
94 |
this.lyr = lyr;
|
95 |
} |
96 |
|
97 |
public FLyrVect getLayer() {
|
98 |
return lyr;
|
99 |
} |
100 |
|
101 |
public String toString() { |
102 |
return lyr.getName();
|
103 |
} |
104 |
} |
105 |
|
106 |
public ROIDataModel(List<FLyrVect> vectLayers) { |
107 |
this.fLyrsList = vectLayers;
|
108 |
} |
109 |
|
110 |
public void setROIPanel(ROIPanel panel) { |
111 |
this.panel = panel;
|
112 |
JComboBox list = panel.getLayerList(ROIPanel.FLYRS_COMBO);
|
113 |
list.removeAllItems(); |
114 |
for (int i = 0; i < fLyrsList.size(); i++) { |
115 |
list.addItem(new LayerElement(fLyrsList.get(i)));
|
116 |
} |
117 |
} |
118 |
|
119 |
public ROIPanel getROIPanel() {
|
120 |
return panel;
|
121 |
} |
122 |
|
123 |
public RasterDataStore getRasterDataStore() {
|
124 |
return ((FLyrRaster)fLayer).getDataStore();
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Sets the layer
|
129 |
* @param layer
|
130 |
* @throws GridException
|
131 |
*/
|
132 |
public void setLayer(FLayer layer) throws GridException { |
133 |
fLayer = layer; |
134 |
if (view == null) { |
135 |
IWindow[] list = PluginServices.getMDIManager().getAllWindows();
|
136 |
for (int i = 0; i < list.length; i++) { |
137 |
if (list[i] instanceof AbstractViewPanel) |
138 |
view = (AbstractViewPanel) list[i]; |
139 |
} |
140 |
if (view == null) |
141 |
return;
|
142 |
mapControl = view.getMapControl(); |
143 |
graphicLayer = view.getMapControl().getMapContext().getGraphicsLayer(); |
144 |
|
145 |
/*
|
146 |
* Guardar la herramienta actual para recuperarla m?s tarde, por
|
147 |
* ejemplo al cerrar el contenedor del este panel.
|
148 |
*/
|
149 |
previousTool = mapControl.getCurrentTool(); |
150 |
|
151 |
// Listener de eventos de movimiento que pone las coordenadas del
|
152 |
// rat?n en la barra de estado
|
153 |
StatusBarListener sbl = new StatusBarListener(mapControl);
|
154 |
|
155 |
DrawMouseViewListener drawMouseViewListener = new DrawMouseViewListener(this); |
156 |
mapControl.addBehavior("drawPolygonROI", new Behavior[] { |
157 |
new PolygonBehavior(drawMouseViewListener),
|
158 |
new MouseMovementBehavior(sbl) });
|
159 |
mapControl.addBehavior("drawLineROI", new Behavior[] { |
160 |
new PolylineBehavior(drawMouseViewListener),
|
161 |
new MouseMovementBehavior(sbl) });
|
162 |
mapControl.addBehavior("drawPointROI", new Behavior[] { |
163 |
new PointBehavior(drawMouseViewListener),
|
164 |
new MouseMovementBehavior(sbl) });
|
165 |
} |
166 |
|
167 |
try {
|
168 |
roiFeatureManager = new ROIFeatureManager(fLayer.getMetadataID().toString());
|
169 |
} catch (MetadataException e) {
|
170 |
throw new GridException("Can't create ROI",e); |
171 |
} |
172 |
clearROIs(); |
173 |
loadROIs(); |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Elimina todas las filas de la tabla de rois as? como la ROI y objetos
|
178 |
* FGraphic asociados (borr?ndolos tambi?n de la vista) a cada una.
|
179 |
*
|
180 |
*/
|
181 |
public void clearROIs() { |
182 |
try {
|
183 |
panel.getTable().removeAllRows(); |
184 |
} catch (NotInitializeException e) {
|
185 |
RasterToolsUtil.messageBoxError("error_tabla_rois", this, e); |
186 |
} |
187 |
|
188 |
boolean repaint = roiFeatureManager.clear(graphicLayer);
|
189 |
|
190 |
if (repaint)
|
191 |
getMapControl().rePaintDirtyLayers(); |
192 |
getRois().clear(); |
193 |
} |
194 |
|
195 |
private LinkedHashMap<String, ROI> getRois() { |
196 |
if (rois == null) |
197 |
rois = new LinkedHashMap<String, ROI>(); |
198 |
return rois;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Cargar las ROIs asociadas a la capa, si las hay.
|
203 |
*
|
204 |
* @throws GridException
|
205 |
*
|
206 |
*/
|
207 |
private void loadROIs() throws GridException { |
208 |
List<ROI> roisArray = null; |
209 |
try {
|
210 |
roisArray = ((FLyrRaster) fLayer).getRois(); |
211 |
} catch (ROIException e) {
|
212 |
RasterToolsUtil.messageBoxError("problems_loading_rois", this, e); |
213 |
} |
214 |
loadROIs(roisArray); |
215 |
} |
216 |
|
217 |
@SuppressWarnings("deprecation") |
218 |
private void loadROIs(List<ROI> roisArray) throws GridException { |
219 |
if (roisArray != null) { |
220 |
ISymbol symbol = null;
|
221 |
int nPoints, nPolygons, nLines;
|
222 |
|
223 |
FeatureStore store = graphicLayer.getFeatureStore(); |
224 |
try {
|
225 |
store.edit(); |
226 |
|
227 |
for (Iterator<ROI> iter = roisArray.iterator(); iter.hasNext();) { |
228 |
ROI roi = (ROI) iter.next(); |
229 |
|
230 |
nPoints = 0;
|
231 |
nPolygons = 0;
|
232 |
nLines = 0;
|
233 |
|
234 |
Object row[] = ((IModel) panel.getTable().getModel()).getNewLine(); |
235 |
row[0] = roi.getName();
|
236 |
row[4] = roi.getColor();
|
237 |
|
238 |
addROI(roi); |
239 |
VectorialROI vectorialROI = (VectorialROI) roi; |
240 |
for (Iterator<Geometry> iterator = vectorialROI.getGeometries().iterator(); iterator.hasNext();) { |
241 |
Geometry geometry = (Geometry) iterator.next(); |
242 |
symbol = mapContextManager.getSymbolManager().createSymbol(geometry.getType(), roi.getColor()); |
243 |
switch (geometry.getType()) {
|
244 |
case Geometry.TYPES.POINT:
|
245 |
nPoints++; |
246 |
break;
|
247 |
case Geometry.TYPES.SURFACE:
|
248 |
nPolygons++; |
249 |
break;
|
250 |
case Geometry.TYPES.CURVE:
|
251 |
nLines++; |
252 |
break;
|
253 |
} |
254 |
EditableFeature feature = roiFeatureManager.addROIFeature(vectorialROI, graphicLayer, geometry, symbol); |
255 |
|
256 |
store.insert(feature); |
257 |
} |
258 |
row[1] = new Integer(nPolygons); |
259 |
row[2] = new Integer(nLines); |
260 |
row[3] = new Integer(nPoints); |
261 |
((DefaultTableModel)panel.getTable().getModel()).addRow(row);
|
262 |
} |
263 |
store.finishEditing(); |
264 |
} catch (DataException e) {
|
265 |
throw new GridException("Error loading ROIs from layer " + fLayer.getName(), e); |
266 |
} |
267 |
selectDrawRoiTool(); |
268 |
getMapControl().drawGraphics(); |
269 |
} |
270 |
} |
271 |
|
272 |
public void addROI(ROI roi) { |
273 |
getRois().put(roi.getName(), roi); |
274 |
} |
275 |
|
276 |
public void selectDrawRoiTool() { |
277 |
if (mapControl != null) |
278 |
if (panel.getButton(ROIPanel.POLYGON_BUTTON).isSelected()) {
|
279 |
Image img = IconThemeHelper.getImageIcon("polygon-cursor").getImage(); |
280 |
curImage = img; |
281 |
mapControl.setTool("drawPolygonROI");
|
282 |
} else if (panel.getButton(ROIPanel.LINE_BUTTON).isSelected()) { |
283 |
Image img = IconThemeHelper.getImageIcon("line-cursor").getImage(); |
284 |
curImage = img; |
285 |
mapControl.setTool("drawLineROI");
|
286 |
} else if (panel.getButton(ROIPanel.POINT_BUTTON).isSelected()) { |
287 |
Image img = IconThemeHelper.getImageIcon("point-cursor").getImage(); |
288 |
curImage = img; |
289 |
mapControl.setTool("drawPointROI");
|
290 |
} |
291 |
} |
292 |
|
293 |
public void changeRoiName(String currentName, String newName) |
294 |
throws DataException {
|
295 |
getROI(currentName).setName(newName); |
296 |
getRois().put(newName, getRois().remove(currentName)); |
297 |
roiFeatureManager.changeROIName(currentName, newName, graphicLayer); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Elimina la ROI correspondiente al nombre <code>roiName</code> as? como
|
302 |
* sus objetos FGraphic asociados (borr?ndolos tambi?n de la vista). (NO
|
303 |
* elimina la fila correspondiente en la tabla del panel).
|
304 |
*
|
305 |
* @param roiName
|
306 |
* Nombre de la ROI a eliminar.
|
307 |
*/
|
308 |
public void removeROI(String roiName) { |
309 |
getRois().remove(roiName); |
310 |
boolean repaint = roiFeatureManager.removeROI(roiName, graphicLayer);
|
311 |
if (repaint) {
|
312 |
getMapControl().rePaintDirtyLayers(); |
313 |
} |
314 |
} |
315 |
|
316 |
public Grid getGrid() {
|
317 |
if(grid == null) { |
318 |
FLyrRaster rasterLayer = (FLyrRaster) getFLayer(); |
319 |
try {
|
320 |
grid = rasterLayer.getReadOnlyFullGrid(false);
|
321 |
} catch (GridException e) {
|
322 |
return null; |
323 |
} catch (InterruptedException e) { |
324 |
return null; |
325 |
} |
326 |
} |
327 |
return grid;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* Borra de capa GraphicsLayer de mapContext los gr?ficos pertenecientes a
|
332 |
* las rois del panel. Si la GraphicsLayer se queda sin gr?ficos se eliminan
|
333 |
* tambien los s?mbolos de esta.
|
334 |
*/
|
335 |
public void clearRoiGraphics() { |
336 |
roiFeatureManager.clearFeaturesFromGraphicLayer(graphicLayer); |
337 |
} |
338 |
|
339 |
public MapControl getMapControl() {
|
340 |
return mapControl;
|
341 |
} |
342 |
|
343 |
public TableContainer getTable() {
|
344 |
return panel.getTable();
|
345 |
} |
346 |
|
347 |
public ROI getROI(String roiName) { |
348 |
return (ROI) getRois().get(roiName);
|
349 |
} |
350 |
|
351 |
public GraphicLayer getGraphicLayer() {
|
352 |
return graphicLayer;
|
353 |
} |
354 |
|
355 |
public void setPreviousTool() { |
356 |
if (previousTool != null) |
357 |
getMapControl().setTool(previousTool); |
358 |
} |
359 |
|
360 |
public List<ROI> getROIs() { |
361 |
return new ArrayList<ROI>(getRois().values()); |
362 |
} |
363 |
|
364 |
public void setROIs(List<ROI> roisArray) throws GridException { |
365 |
loadROIs(roisArray); |
366 |
} |
367 |
|
368 |
public FLayer getFLayer() {
|
369 |
return fLayer;
|
370 |
} |
371 |
|
372 |
public ROIFeatureManager getRoiFeatureManager() {
|
373 |
return roiFeatureManager;
|
374 |
} |
375 |
|
376 |
public Image getToolImage() { |
377 |
return curImage;
|
378 |
} |
379 |
|
380 |
public void setROIFile(File file) { |
381 |
this.roiFile = file;
|
382 |
} |
383 |
|
384 |
public File getROIFile() { |
385 |
return this.roiFile; |
386 |
} |
387 |
} |