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 @ 1316
History | View | Annotate | Download (7.97 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 = removeGraphics(layer, getROIId(roiName)) ? true : anythingCleared;
|
121 |
if(onlyRemoveFromLayer)
|
122 |
roiNameToFeatureList.remove(roiName); |
123 |
} |
124 |
|
125 |
return anythingCleared;
|
126 |
} |
127 |
|
128 |
public boolean removeROI(String roiName, GraphicLayer layer) { |
129 |
removeGraphics(layer, getROIId(roiName)); |
130 |
return roiNameToFeatureList.remove(roiName) != null; |
131 |
} |
132 |
|
133 |
public void changeColor(GraphicLayer lyr, String roiName, Color color) { |
134 |
DisposableIterator iterator = null;
|
135 |
FeatureSet featureSet = null;
|
136 |
FeatureStore store = lyr.getFeatureStore(); |
137 |
try{
|
138 |
store.beginComplexNotification(); |
139 |
if (!store.isEditing()){
|
140 |
store.edit(); |
141 |
} |
142 |
featureSet = store.getFeatureSet(); |
143 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
144 |
Feature feature = (Feature) iterator.next(); |
145 |
Object obj = feature.get(GraphicLayer.FEATURE_ATTR_GROUPID);
|
146 |
if (obj instanceof String && ((String)obj).equals(getROIId(roiName))) { |
147 |
int symbolPos = feature.getInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL);
|
148 |
ISymbol symbol = lyr.getSymbol(symbolPos); |
149 |
symbol.setColor(color); |
150 |
} |
151 |
} |
152 |
store.finishEditing(); |
153 |
store.endComplexNotification(); |
154 |
} catch (DataException e) {
|
155 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
156 |
} finally {
|
157 |
if (featureSet != null) { |
158 |
featureSet.dispose(); |
159 |
} |
160 |
if (iterator != null) { |
161 |
iterator.dispose(); |
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
public boolean removeGraphics(GraphicLayer lyr, String prefix) { |
167 |
boolean anythingCleared = false; |
168 |
DisposableIterator iterator = null;
|
169 |
FeatureSet featureSet = null;
|
170 |
FeatureStore store = lyr.getFeatureStore(); |
171 |
try{
|
172 |
List<Feature> list = new ArrayList<Feature>(); |
173 |
store.beginComplexNotification(); |
174 |
if (!store.isEditing()){
|
175 |
store.edit(); |
176 |
} |
177 |
featureSet = store.getFeatureSet(); |
178 |
for (iterator = featureSet.fastIterator(); iterator.hasNext();) {
|
179 |
Feature feature = (Feature) iterator.next(); |
180 |
Object obj = feature.get(GraphicLayer.FEATURE_ATTR_GROUPID);
|
181 |
if (obj instanceof String && ((String)obj).startsWith(prefix)) { |
182 |
//list.add(feature.getCopy());
|
183 |
anythingCleared = true;
|
184 |
iterator.remove(); |
185 |
//featureSet.delete(feature); //ConcurrentModificationException!!! Lo a?ado a una lista para eliminarlo posteriormente
|
186 |
} |
187 |
} |
188 |
/*anythingCleared = list.size() > 0;
|
189 |
for (int i = 0; i < list.size(); i++) {
|
190 |
store.delete(list.get(i));
|
191 |
}*/
|
192 |
store.finishEditing(); |
193 |
store.endComplexNotification(); |
194 |
} catch (DataException e) {
|
195 |
logger.error("Error clearing all the geometry of the graphic layer", e);
|
196 |
} finally {
|
197 |
if (featureSet != null) { |
198 |
featureSet.dispose(); |
199 |
} |
200 |
if (iterator != null) { |
201 |
iterator.dispose(); |
202 |
} |
203 |
} |
204 |
return anythingCleared;
|
205 |
} |
206 |
|
207 |
public String getROIId(String roiName) { |
208 |
return getROIPrefix().concat(roiName);
|
209 |
} |
210 |
|
211 |
private String getROIPrefix() { |
212 |
return layerId.concat("-"); |
213 |
} |
214 |
|
215 |
public void changeROIName(String oldName, String newName, GraphicLayer layer) |
216 |
throws DataException {
|
217 |
List<FeatureReference> featureList = roiNameToFeatureList.remove(oldName);
|
218 |
if (featureList != null) { |
219 |
roiNameToFeatureList.put(newName, featureList); |
220 |
String newroiid = getROIId(newName);
|
221 |
FeatureStore store = layer.getFeatureStore(); |
222 |
store.edit(); |
223 |
for (int i = 0; i < featureList.size(); i++) { |
224 |
FeatureReference ref = featureList.get(i); |
225 |
EditableFeature editable = ref.getFeature().getEditable(); |
226 |
editable.set(GraphicLayer.FEATURE_ATTR_GROUPID, newroiid); |
227 |
store.update(editable); |
228 |
} |
229 |
store.finishEditing(); |
230 |
} |
231 |
} |
232 |
|
233 |
public List<FeatureReference> getROIFeatures(String roiName) { |
234 |
return Collections.unmodifiableList(roiNameToFeatureList.get(roiName)); |
235 |
} |
236 |
|
237 |
private EditableFeature createEditableFeature(FeatureStore store, ROI roi,
|
238 |
Geometry geometry, int symbolPos) throws DataException { |
239 |
|
240 |
EditableFeature feature = store.createNewFeature(); |
241 |
feature.set(GraphicLayer.FEATURE_ATTR_GROUPID, getROIId(roi.getName())); |
242 |
feature.setGeometry(GraphicLayer.FEATURE_ATTR_GEOMETRY, geometry); |
243 |
feature.setInt(GraphicLayer.FEATURE_ATTR_IDSYMBOL, symbolPos); |
244 |
|
245 |
return feature;
|
246 |
} |
247 |
} |