/[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 158 by alfonx, Mon Jun 22 13:45:32 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 24  import javax.swing.ListSelectionModel; Line 26  import javax.swing.ListSelectionModel;
26  import javax.swing.event.ListSelectionListener;  import javax.swing.event.ListSelectionListener;
27    
28  import org.geotools.feature.Feature;  import org.geotools.feature.Feature;
29    import org.geotools.filter.FidFilter;
30    import org.geotools.filter.FidFilterImpl;
31  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
32  import org.geotools.styling.FeatureTypeStyle;  import org.geotools.styling.FeatureTypeStyle;
33  import org.geotools.styling.Style;  import org.geotools.styling.Style;
34  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
35  import org.opengis.filter.FilterVisitor;  import org.opengis.filter.FilterVisitor;
36    import org.opengis.filter.identity.FeatureId;
37    
38    import schmitzm.geotools.FilterUtil;
39  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.JMapPane;
40  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
41  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
# Line 105  public class FeatureMapLayerSelectionSyn Line 111  public class FeatureMapLayerSelectionSyn
111           */           */
112          @Override          @Override
113          public void propertyChange(PropertyChangeEvent evt) {          public void propertyChange(PropertyChangeEvent evt) {
114    
115                    if (!isEnabled())
116                            return;
117    
118                  if (!(evt instanceof StyledLayerSelectionEvent))                  if (!(evt instanceof StyledLayerSelectionEvent))
119                          return;                          return;
120                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
# Line 127  public class FeatureMapLayerSelectionSyn Line 137  public class FeatureMapLayerSelectionSyn
137                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
138          }          }
139    
140            /**
141             * Changes the Style of the {@link MapLayer} to reflect changes of the
142             * selection.
143             *
144             * @param newSelection
145             *            A {@link Vector} of Feature-IDs that are selected.
146             */
147          private void changeLayerStyle(final Vector<String> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
148                  Style selectionMapStyle = null;                  Style selectionMapStyle = null;
149                  try {                  try {
150                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
151    
152                                  selectionMapStyle = styledMapLayer.getStyle();                                  selectionMapStyle = styledMapLayer.getStyle();
153                                  LOGGER.debug("NO SELECTION .. set to original style directly");                                  // LOGGER.debug("NO SELECTION .. set to original style directly");
154    
155                          } else {                          } else {
156                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
# Line 149  public class FeatureMapLayerSelectionSyn Line 166  public class FeatureMapLayerSelectionSyn
166                                  // TODO The default style is not good here. We need                                  // TODO The default style is not good here. We need
167                                  // .createSelectionStyle(normalStyle, geoObj);                                  // .createSelectionStyle(normalStyle, geoObj);
168                                  selectionMapStyle = StylingUtil                                  selectionMapStyle = StylingUtil
169                                                  .createDefaultStyle(styledMapLayer.getGeoObject());                                                  .createSelectionStyle(styledMapLayer.getGeoObject());
170                                    // selectionMapStyle = StylingUtil
171                                    // .createDefaultStyle(styledMapLayer.getGeoObject());
172    
173                                  selectionMapStyle.getFeatureTypeStyles()[0]                                  selectionMapStyle.getFeatureTypeStyles()[0]
174                                                  .setName(SELECTION_STYLING);                                                  .setName(SELECTION_STYLING);
175    
176                                  // Symbolizer sss =                                  /**
177                                  // selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];                                   *
178                                  // if (sss instanceof PolygonSymbolizer) {                                   * Add a Filter to the selectionMapStyle, so that it is only
179                                  // PolygonSymbolizer ps = (PolygonSymbolizer) sss;                                   * used on objects that are selected. <br/>
180                                  // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));                                   *
181                                  // }                                   * Note 1:<br/>
182                                     * It is NEVER allowed to GeoTools extend Filter () { .. } (and write
183                                     * tests into the evaluate block). Especially for the
184                                     * ShapeFileRenderer, we may only use a geotools Filter.<br/>
185                                     *
186                                     * Note 2:<br/>
187                                     * The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects!
188                                     */
189    
190                                    Set<FeatureId> fids = new HashSet<FeatureId>();
191                                    for (String fid : newSelection) {
192                                            fids.add(FilterUtil.FILTER_FAC2.featureId(fid));
193                                    }
194    
                                 // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();  
195                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
196                                                  .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;  
                                                         }  
   
                                                 });  
197    
198                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
199                                                  .getFeatureTypeStyles();                                                  .getFeatureTypeStyles();
# Line 201  public class FeatureMapLayerSelectionSyn Line 207  public class FeatureMapLayerSelectionSyn
207                                  } else {                                  } else {
208                                          newFeatureTypes = originalFeatureTypeStyles;                                          newFeatureTypes = originalFeatureTypeStyles;
209                                  }                                  }
                                 //                                
                                 // // 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());  
                                 // }  
210    
211                                  // The last FTS is the selection FTS                                  // The last FTS is the selection FTS
212                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
# Line 219  public class FeatureMapLayerSelectionSyn Line 214  public class FeatureMapLayerSelectionSyn
214    
215                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
216    
                                 // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)  
   
                                 // Flat copy the style  
   
                                 // selectionMapStyle =  
                                 // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());  
   
217                          }                          }
218    
219                          mapLayer.setStyle(selectionMapStyle);                          mapLayer.setStyle(selectionMapStyle);
# Line 235  public class FeatureMapLayerSelectionSyn Line 223  public class FeatureMapLayerSelectionSyn
223                  }                  }
224          }          }
225    
226            /**
227             * Used to synchronize {@link FeatureSelectedEvent}s with the
228             * {@link StyledFeatureLayerSelectionModel}
229             */
230          @Override          @Override
231          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(JMapPaneEvent e) {
232    
233                    // Ignore event if it is caused by us or the synchronizer is disabled.
234                    if (!isEnabled() || selectionChangeCausedByMe)
235                            return;
236    
237                    if (!(e instanceof FeatureSelectedEvent)) {
238                            // LOGGER.debug("Ignoring event " + e);
239                            return;
240                    }
241    
242                  /**                  /**
243                   * Only listen to FeatureSelectionEvents if the appropriate tool is                   * Only listen to FeatureSelectedEvents if an appropriate tool is
244                   * selected.                   * selected.
245                   */                   */
246                  final int selectedTool = toolBar.getSelectedTool();                  final int selectedTool = toolBar.getSelectedTool();
247                  if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD                  if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
248                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
                                 && selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR  
249                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {                                  && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250                          return;                          return;
251                  }                  }
252    
                 if (!(e instanceof FeatureSelectedEvent)) {  
                         // LOGGER.debug("Ignoring event " + e);  
                         return;  
                 }  
253                  // only listen to events directly coming from JMapPane                  // only listen to events directly coming from JMapPane
254                  // selection (ignore selections from FilterDialog)                  // selection (ignore selections from FilterDialog)
255                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
# Line 261  public class FeatureMapLayerSelectionSyn Line 257  public class FeatureMapLayerSelectionSyn
257    
258                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
259    
260                  // ignore event if it is caused by the DpLayerVectorSelectionModel                  /**
261                  if (selectionChangeCausedByMe)                   * Checking, that the FeatureSelectedEvent actually contains features
262                     * from this layer
263                     */
264                    final String sourceID = fse.getSourceLayer().getTitle();
265                    final String syncForID = mapLayer.getTitle();
266                    if (sourceID != null && syncForID != null
267                                    && !sourceID.equals(syncForID)) {
268                            LOGGER.debug("Ignoring a FeatureSelectedEvent from " + sourceID);
269                          return;                          return;
270                    }
271    
272                  LOGGER.debug("Do event " + fse);                  LOGGER.debug("do event " + fse);
273    
274                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
275                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
276    
277                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) {                  Collection<Feature> selectionResult = (Collection<Feature>) fse
278                                    .getSelectionResult();
279    
280                    // reset the selection of the DpLayerSelectionModel
281                    // layerSelModel.setValueIsAdjusting(true);
282    
283                    if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
284                            layerSelModel.setValueIsAdjusting(true);
285                            for (Feature f : selectionResult) {
286                                    layerSelModel.addSelection(f.getID());
287                            }
288                            layerSelModel.setValueIsAdjusting(false);
289                    } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
290                            layerSelModel.setValueIsAdjusting(true);
291                          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();  
292    
293                                  for (Feature f : selectionResult) {                          for (Feature f : selectionResult) {
294                                          layerSelModel.addSelection(f.getID());                                  layerSelModel.addSelection(f.getID());
295                                  }                          }
296    
297                                  LOGGER.debug("Setting selection to " + selectionResult.size());                          LOGGER.debug("Setting selection to " + selectionResult.size());
298                                  layerSelModel.setValueIsAdjusting(false);                          layerSelModel.setValueIsAdjusting(false);
299                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
300                                  layerSelModel.setValueIsAdjusting(true);                          layerSelModel.setValueIsAdjusting(true);
301                                  for (Feature f : selectionResult) {                          for (Feature f : selectionResult) {
302                                          layerSelModel.removeSelection(f.getID());                                  layerSelModel.removeSelection(f.getID());
                                 }  
                                 layerSelModel.setValueIsAdjusting(false);  
303                          }                          }
304                  } // els it's not a Clear Selection command                          layerSelModel.setValueIsAdjusting(false);
305                    }
306    
307                  // Show selected features in the map, which is not automatically done by                  // Show selected features in the map, which is not automatically done by
308                  // the origin: FeatureSelectedEvent                  // the origin: FeatureSelectedEvent
# Line 311  public class FeatureMapLayerSelectionSyn Line 311  public class FeatureMapLayerSelectionSyn
311                  // Danger of event circles in propertyChange(..) banned                  // Danger of event circles in propertyChange(..) banned
312                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
313          }          }
314    
315  }  }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26