gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / roi / ROIFeatureManager.java @ 1309
History | View | Annotate | Download (8.03 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 |
|
25 |
import java.awt.Color; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import org.gvsig.fmap.dal.coverage.grid.ROI; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
38 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
43 |
import org.gvsig.tools.dispose.DisposableIterator; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
/**
|
48 |
* Manages a table of ROI with its related list of Features.
|
49 |
*
|
50 |
* @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
|
51 |
*/
|
52 |
public class ROIFeatureManager { |
53 |
protected static final Logger logger = LoggerFactory.getLogger(ROIFeatureManager.class); |
54 |
private Map<String, List<FeatureReference>> roiNameToFeatureList = Collections |
55 |
.synchronizedMap(new HashMap<String, List<FeatureReference>>()); |
56 |
|
57 |
private final String layerId; |
58 |
|
59 |
public ROIFeatureManager(String layerId) { |
60 |
this.layerId = layerId;
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Returns a read-only {@link Map} of ROI names to {@link FeatureReference}
|
65 |
* {@link List} objects.
|
66 |
*
|
67 |
* @return the roiNameToFeatureList the table of {@link FeatureReference}
|
68 |
* objects per {@link ROI}.
|
69 |
*/
|
70 |
public Map<String, List<FeatureReference>> getRoiNameToFeatureList() { |
71 |
return Collections.unmodifiableMap(roiNameToFeatureList); |
72 |
} |
73 |
|
74 |
public EditableFeature addROIFeature(ROI roi, GraphicLayer graphicLayer,
|
75 |
Geometry geometry, ISymbol symbol) throws DataException {
|
76 |
int symbolPos = graphicLayer.addSymbol(symbol);
|
77 |
EditableFeature feature = createEditableFeature( |
78 |
graphicLayer.getFeatureStore(), |
79 |
roi, |
80 |
geometry, |
81 |
symbolPos); |
82 |
|
83 |
addROIFeature(roi, feature.getReference()); |
84 |
|
85 |
return feature;
|
86 |
} |
87 |
|
88 |
public void addROIFeature(ROI roi, FeatureReference feature) { |
89 |
List<FeatureReference> featureList = roiNameToFeatureList.get(roi.getName());
|
90 |
|
91 |
if (featureList == null) { |
92 |
featureList = new ArrayList<FeatureReference>(); |
93 |
roiNameToFeatureList.put(roi.getName(), featureList); |
94 |
} |
95 |
|
96 |
featureList.add(feature); |
97 |
} |
98 |
|
99 |
public boolean clear(GraphicLayer layer) { |
100 |
return clear(layer, false); |
101 |
} |
102 |
|
103 |
public void clearFeaturesFromGraphicLayer(GraphicLayer layer) { |
104 |
clear(layer, true);
|
105 |
} |
106 |
|
107 |
private boolean clear(GraphicLayer layer, boolean onlyRemoveFromLayer) { |
108 |
boolean anythingCleared = false; |
109 |
//Evita un ConcurrentModificationException ya que no se puede leer y borrar a la vez de roiNameToFeatureList
|
110 |
List<String> roiNameList = new ArrayList<String>(); |
111 |
|
112 |
for (Iterator<String> iterator = roiNameToFeatureList.keySet().iterator(); |
113 |
iterator.hasNext();) { |
114 |
String roiName = iterator.next();
|
115 |
roiNameList.add(roiName); |
116 |
} |
117 |
|
118 |
for (int i = 0; i < roiNameList.size(); i++) { |
119 |
String roiName = roiNameList.get(i);
|
120 |
anythingCleared = anythingCleared || removeROI(roiName, layer, onlyRemoveFromLayer); |
121 |
} |
122 |
|
123 |
return anythingCleared;
|
124 |
} |
125 |
|
126 |
public boolean removeROI(String roiName, GraphicLayer layer) { |
127 |
return removeROI(roiName, layer, false); |
128 |
} |
129 |
|
130 |
private synchronized boolean removeROI(String roiName, GraphicLayer layer, |
131 |
boolean onlyRemoveFromLayer) {
|
132 |
//String roiId = getROIId(roiName);
|
133 |
//layer.removeGraphics(roiId);
|
134 |
removeGraphics(layer, getROIId(roiName)); |
135 |
synchronized (roiNameToFeatureList) {
|
136 |
return onlyRemoveFromLayer ? true : roiNameToFeatureList.remove(roiName) != null; |
137 |
} |
138 |
} |
139 |
|
140 |
public void changeColor(GraphicLayer lyr, String roiName, Color color) { |
141 |
DisposableIterator iterator = null;
|
142 |
FeatureSet featureSet = null;
|
143 |
FeatureStore store = lyr.getFeatureStore(); |
144 |
try{
|
145 |
store.beginComplexNotification(); |
146 |
if (!store.isEditing()){
|
147 |
store.edit(); |
148 |
} |
149 |
featureSet = store.getFeatureSet(); |
150 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
151 |
Feature feature = (Feature) iterator.next(); |
152 |
Object obj = feature.get(GraphicLayer.FEATURE_ATTR_GROUPID);
|
153 |
if (obj instanceof String && ((String)obj).equals(getROIId(roiName))) { |
154 |
int symbolPos = feature.getInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL);
|
155 |
ISymbol symbol = lyr.getSymbol(symbolPos); |
156 |
symbol.setColor(color); |
157 |
} |
158 |
} |
159 |
store.finishEditing(); |
160 |
store.endComplexNotification(); |
161 |
} catch (DataException e) {
|
162 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
163 |
} finally {
|
164 |
if (featureSet != null) { |
165 |
featureSet.dispose(); |
166 |
} |
167 |
if (iterator != null) { |
168 |
iterator.dispose(); |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
public void removeGraphics(GraphicLayer lyr, String prefix) { |
174 |
DisposableIterator iterator = null;
|
175 |
FeatureSet featureSet = null;
|
176 |
FeatureStore store = lyr.getFeatureStore(); |
177 |
try{
|
178 |
List<Feature> list = new ArrayList<Feature>(); |
179 |
store.beginComplexNotification(); |
180 |
if (!store.isEditing()){
|
181 |
store.edit(); |
182 |
} |
183 |
featureSet = store.getFeatureSet(); |
184 |
for (iterator = featureSet.iterator(); iterator.hasNext();) {
|
185 |
Feature feature = (Feature) iterator.next(); |
186 |
Object obj = feature.get(GraphicLayer.FEATURE_ATTR_GROUPID);
|
187 |
if (obj instanceof String && ((String)obj).startsWith(prefix)) { |
188 |
list.add(feature); |
189 |
//featureSet.delete(feature); //ConcurrentModificationException!!! Lo a?ado a una lista para eliminarlo posteriormente
|
190 |
} |
191 |
} |
192 |
for (int i = 0; i < list.size(); i++) { |
193 |
store.delete(list.get(i)); |
194 |
} |
195 |
store.finishEditing(); |
196 |
store.endComplexNotification(); |
197 |
} catch (DataException e) {
|
198 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
199 |
} finally {
|
200 |
if (featureSet != null) { |
201 |
featureSet.dispose(); |
202 |
} |
203 |
if (iterator != null) { |
204 |
iterator.dispose(); |
205 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
public String getROIId(String roiName) { |
210 |
return getROIPrefix().concat(roiName);
|
211 |
} |
212 |
|
213 |
private String getROIPrefix() { |
214 |
return layerId.concat("-"); |
215 |
} |
216 |
|
217 |
public void changeROIName(String oldName, String newName, GraphicLayer layer) |
218 |
throws DataException {
|
219 |
List<FeatureReference> featureList = roiNameToFeatureList.remove(oldName);
|
220 |
if (featureList != null) { |
221 |
roiNameToFeatureList.put(newName, featureList); |
222 |
String newroiid = getROIId(newName);
|
223 |
FeatureStore store = layer.getFeatureStore(); |
224 |
store.edit(); |
225 |
for (int i = 0; i < featureList.size(); i++) { |
226 |
FeatureReference ref = featureList.get(i); |
227 |
EditableFeature editable = ref.getFeature().getEditable(); |
228 |
editable.set(GraphicLayer.FEATURE_ATTR_GROUPID, newroiid); |
229 |
store.update(editable); |
230 |
} |
231 |
store.finishEditing(); |
232 |
} |
233 |
} |
234 |
|
235 |
public List<FeatureReference> getROIFeatures(String roiName) { |
236 |
return Collections.unmodifiableList(roiNameToFeatureList.get(roiName)); |
237 |
} |
238 |
|
239 |
private EditableFeature createEditableFeature(FeatureStore store, ROI roi,
|
240 |
Geometry geometry, int symbolPos) throws DataException { |
241 |
|
242 |
EditableFeature feature = store.createNewFeature(); |
243 |
feature.set(GraphicLayer.FEATURE_ATTR_GROUPID, getROIId(roi.getName())); |
244 |
feature.setGeometry(GraphicLayer.FEATURE_ATTR_GEOMETRY, geometry); |
245 |
feature.setInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL, symbolPos); |
246 |
|
247 |
return feature;
|
248 |
} |
249 |
} |