/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Diff of /branches/2.0-RC2/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 127 by mojays, Sat May 30 14:29:58 2009 UTC revision 162 by alfonx, Mon Jun 22 21:05:20 2009 UTC
# Line 17  import java.beans.PropertyChangeEvent; Line 17  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
18  import java.util.Arrays;  import java.util.Arrays;
19  import java.util.Collection;  import java.util.Collection;
20    import java.util.HashSet;
21    import java.util.Set;
22  import java.util.Vector;  import java.util.Vector;
23    
24  import javax.swing.JTable;  import javax.swing.JTable;
# Line 27  import org.geotools.feature.Feature; Line 29  import org.geotools.feature.Feature;
29  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
30  import org.geotools.styling.FeatureTypeStyle;  import org.geotools.styling.FeatureTypeStyle;
31  import org.geotools.styling.Style;  import org.geotools.styling.Style;
32  import org.opengis.filter.Filter;  import org.opengis.filter.identity.FeatureId;
 import org.opengis.filter.FilterVisitor;  
33    
34    import schmitzm.geotools.FilterUtil;
35  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.JMapPane;
36  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
37  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
# Line 105  public class FeatureMapLayerSelectionSyn Line 107  public class FeatureMapLayerSelectionSyn
107           */           */
108          @Override          @Override
109          public void propertyChange(PropertyChangeEvent evt) {          public void propertyChange(PropertyChangeEvent evt) {
110    
111                    if (!isEnabled())
112                            return;
113    
114                  if (!(evt instanceof StyledLayerSelectionEvent))                  if (!(evt instanceof StyledLayerSelectionEvent))
115                          return;                          return;
116                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
# Line 127  public class FeatureMapLayerSelectionSyn Line 133  public class FeatureMapLayerSelectionSyn
133                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
134          }          }
135    
136            /**
137             * Changes the Style of the {@link MapLayer} to reflect changes of the
138             * selection.
139             *
140             * @param newSelection
141             *            A {@link Vector} of Feature-IDs that are selected.
142             */
143          private void changeLayerStyle(final Vector<String> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
144                  Style selectionMapStyle = null;                  Style selectionMapStyle = null;
145                  try {                  try {
146                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
147    
148                                  selectionMapStyle = styledMapLayer.getStyle();                                  selectionMapStyle = styledMapLayer.getStyle();
149                                  LOGGER.debug("NO SELECTION .. set to original style directly");                                  // LOGGER.debug("NO SELECTION .. set to original style directly");
150    
151                          } else {                          } else {
152                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
# Line 149  public class FeatureMapLayerSelectionSyn Line 162  public class FeatureMapLayerSelectionSyn
162                                  // TODO The default style is not good here. We need                                  // TODO The default style is not good here. We need
163                                  // .createSelectionStyle(normalStyle, geoObj);                                  // .createSelectionStyle(normalStyle, geoObj);
164                                  selectionMapStyle = StylingUtil                                  selectionMapStyle = StylingUtil
165                                                  .createDefaultStyle(styledMapLayer.getGeoObject());                                                  .createSelectionStyle(styledMapLayer.getGeoObject());
166                                    // selectionMapStyle = StylingUtil
167                                    // .createDefaultStyle(styledMapLayer.getGeoObject());
168    
169                                  selectionMapStyle.getFeatureTypeStyles()[0]                                  selectionMapStyle.getFeatureTypeStyles()[0]
170                                                  .setName(SELECTION_STYLING);                                                  .setName(SELECTION_STYLING);
171    
172                                  // Symbolizer sss =                                  /**
173                                  // selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];                                   *
174                                  // if (sss instanceof PolygonSymbolizer) {                                   * Add a Filter to the selectionMapStyle, so that it is only
175                                  // PolygonSymbolizer ps = (PolygonSymbolizer) sss;                                   * used on objects that are selected. <br/>
176                                  // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));                                   *
177                                  // }                                   * Note 1:<br/>
178                                     * It is NEVER allowed to GeoTools extend Filter () { .. } (and write
179                                     * tests into the evaluate block). Especially for the
180                                     * ShapeFileRenderer, we may only use a geotools Filter.<br/>
181                                     *
182                                     * Note 2:<br/>
183                                     * The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects!
184                                     */
185    
186                                    Set<FeatureId> fids = new HashSet<FeatureId>();
187                                    for (String fid : newSelection) {
188                                            fids.add(FilterUtil.FILTER_FAC2.featureId(fid));
189                                    }
190    
                                 // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();  
191                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
192                                                  .setFilter(new Filter() {                                                  .setFilter(FilterUtil.FILTER_FAC2.id(fids));
   
                                                         @Override  
                                                         public Object accept(FilterVisitor visitor,  
                                                                         Object extraData) {  
                                                                 return null;  
                                                         }  
   
                                                         @Override  
                                                         public boolean evaluate(Object obj) {  
                                                                 if (obj instanceof Feature) {  
                                                                         Feature f = (Feature) obj;  
                                                                         // TODO BAD CODE! says Martin.. i am fine  
                                                                         // with it.. well.. not great...  
                                                                         for (String ffID : newSelection) {  
                                                                                 if (ffID.equals(f.getID()))  
                                                                                         return true;  
                                                                         }  
                                                                         return false;  
   
                                                                 }  
                                                                 return false;  
                                                         }  
   
                                                 });  
193    
194                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
195                                                  .getFeatureTypeStyles();                                                  .getFeatureTypeStyles();
# Line 201  public class FeatureMapLayerSelectionSyn Line 203  public class FeatureMapLayerSelectionSyn
203                                  } else {                                  } else {
204                                          newFeatureTypes = originalFeatureTypeStyles;                                          newFeatureTypes = originalFeatureTypeStyles;
205                                  }                                  }
                                 //                                
                                 // // REMOVE any Selection_Styling FTS (usually the last one)  
                                 //                                
                                 // for (FeatureTypeStyle fts : originalFeatureTypeStyles){  
                                 // if (fts.getName().equals(SELECTION_STYLING)) continue;  
                                 // newFeatureTypes =  
                                 //                                        
                                 // LangUtil.extendArray(  
                                 // originalFeatureTypeStyles, selectionMapStyle  
                                 // .getFeatureTypeStyles());  
                                 // }  
206    
207                                  // The last FTS is the selection FTS                                  // The last FTS is the selection FTS
208                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
# Line 219  public class FeatureMapLayerSelectionSyn Line 210  public class FeatureMapLayerSelectionSyn
210    
211                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
212    
                                 // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)  
   
                                 // Flat copy the style  
   
                                 // selectionMapStyle =  
                                 // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());  
   
213                          }                          }
214    
215                          mapLayer.setStyle(selectionMapStyle);                          mapLayer.setStyle(selectionMapStyle);
# Line 235  public class FeatureMapLayerSelectionSyn Line 219  public class FeatureMapLayerSelectionSyn
219                  }                  }
220          }          }
221    
222            /**
223             * Used to synchronize {@link FeatureSelectedEvent}s with the
224             * {@link StyledFeatureLayerSelectionModel}
225             */
226          @Override          @Override
227          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(JMapPaneEvent e) {
228    
229                    // Ignore event if it is caused by us or the synchronizer is disabled.
230                    if (!isEnabled() || selectionChangeCausedByMe)
231                            return;
232    
233                    if (!(e instanceof FeatureSelectedEvent)) {
234                            // LOGGER.debug("Ignoring event " + e);
235                            return;
236                    }
237    
238                  /**                  /**
239                   * Only listen to FeatureSelectionEvents if the appropriate tool is                   * Only listen to FeatureSelectedEvents if an appropriate tool is
240                   * selected.                   * selected.
241                   */                   */
242                  final int selectedTool = toolBar.getSelectedTool();                  final int selectedTool = toolBar.getSelectedTool();
243                  if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD                  if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
244                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
                                 && selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR  
245                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
246                          return;                          return;
247                  }                  }
248    
                 if (!(e instanceof FeatureSelectedEvent)) {  
                         // LOGGER.debug("Ignoring event " + e);  
                         return;  
                 }  
249                  // only listen to events directly coming from JMapPane                  // only listen to events directly coming from JMapPane
250                  // selection (ignore selections from FilterDialog)                  // selection (ignore selections from FilterDialog)
251                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
# Line 261  public class FeatureMapLayerSelectionSyn Line 253  public class FeatureMapLayerSelectionSyn
253    
254                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
255    
256                  // ignore event if it is caused by the DpLayerVectorSelectionModel                  /**
257                  if (selectionChangeCausedByMe)                   * Checking, that the FeatureSelectedEvent actually contains features
258                     * from this layer
259                     */
260                    final String sourceID = fse.getSourceLayer().getTitle();
261                    final String syncForID = mapLayer.getTitle();
262                    if (sourceID != null && syncForID != null
263                                    && !sourceID.equals(syncForID)) {
264                            LOGGER.debug("Ignoring a FeatureSelectedEvent from " + sourceID);
265                          return;                          return;
266                    }
267    
268                  LOGGER.debug("Do event " + fse);                  LOGGER.debug("do event " + fse);
269    
270                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
271                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
272    
273                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) {                  Collection<Feature> selectionResult = (Collection<Feature>) fse
274                                    .getSelectionResult();
275    
276                    // reset the selection of the DpLayerSelectionModel
277                    // layerSelModel.setValueIsAdjusting(true);
278    
279                    if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
280                            layerSelModel.setValueIsAdjusting(true);
281                            for (Feature f : selectionResult) {
282                                    layerSelModel.addSelection(f.getID());
283                            }
284                            layerSelModel.setValueIsAdjusting(false);
285                    } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
286                            layerSelModel.setValueIsAdjusting(true);
287                          layerSelModel.clearSelection();                          layerSelModel.clearSelection();
                 } else {  
                         Collection<Feature> selectionResult = (Collection<Feature>) fse  
                                         .getSelectionResult();  
   
                         // reset the selection of the DpLayerSelectionModel  
                         // layerSelModel.setValueIsAdjusting(true);  
   
                         if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {  
                                 layerSelModel.setValueIsAdjusting(true);  
                                 for (Feature f : selectionResult) {  
                                         layerSelModel.addSelection(f.getID());  
                                 }  
                                 layerSelModel.setValueIsAdjusting(false);  
                         } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {  
                                 layerSelModel.setValueIsAdjusting(true);  
                                 layerSelModel.clearSelection();  
288    
289                                  for (Feature f : selectionResult) {                          for (Feature f : selectionResult) {
290                                          layerSelModel.addSelection(f.getID());                                  layerSelModel.addSelection(f.getID());
291                                  }                          }
292    
293                                  LOGGER.debug("Setting selection to " + selectionResult.size());                          LOGGER.debug("Setting selection to " + selectionResult.size());
294                                  layerSelModel.setValueIsAdjusting(false);                          layerSelModel.setValueIsAdjusting(false);
295                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
296                                  layerSelModel.setValueIsAdjusting(true);                          layerSelModel.setValueIsAdjusting(true);
297                                  for (Feature f : selectionResult) {                          for (Feature f : selectionResult) {
298                                          layerSelModel.removeSelection(f.getID());                                  layerSelModel.removeSelection(f.getID());
                                 }  
                                 layerSelModel.setValueIsAdjusting(false);  
299                          }                          }
300                  } // els it's not a Clear Selection command                          layerSelModel.setValueIsAdjusting(false);
301                    }
302    
303                  // Show selected features in the map, which is not automatically done by                  // Show selected features in the map, which is not automatically done by
304                  // the origin: FeatureSelectedEvent                  // the origin: FeatureSelectedEvent
# Line 311  public class FeatureMapLayerSelectionSyn Line 307  public class FeatureMapLayerSelectionSyn
307                  // Danger of event circles in propertyChange(..) banned                  // Danger of event circles in propertyChange(..) banned
308                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
309          }          }
310    
311  }  }

Legend:
Removed from v.127  
changed lines
  Added in v.162

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26