/[schmitzm]/branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Diff of /branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

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

revision 97 by alfonx, Fri May 8 12:10:42 2009 UTC revision 158 by alfonx, Mon Jun 22 13:45:32 2009 UTC
# Line 15  package skrueger.geotools.selection; Line 15  package skrueger.geotools.selection;
15    
16  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
17  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
18    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;
25  import javax.swing.ListSelectionModel;  import javax.swing.ListSelectionModel;
 import javax.swing.event.ListSelectionEvent;  
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;
33  import org.geotools.styling.Style;  import org.geotools.styling.Style;
34    import org.opengis.filter.Filter;
35    import org.opengis.filter.FilterVisitor;
36    import org.opengis.filter.identity.FeatureId;
37    
38  import schmitzm.geotools.gui.FeatureCollectionTableModel;  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;
42  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
43  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
44  import skrueger.geotools.StyledFeatureCollectionInterface;  import schmitzm.lang.LangUtil;
45    import skrueger.geotools.MapPaneToolBar;
46  import skrueger.geotools.StyledMapInterface;  import skrueger.geotools.StyledMapInterface;
 import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;  
 import skrueger.geotools.selection.StyledLayerSelectionEvent;  
 import skrueger.geotools.selection.StyledLayerSelectionModel;  
47    
48  /**  /**
49   * This class keeps the selection of a (feature) {@link JTable} synchronized   * This class keeps the selection of a (feature) {@link JTable} synchronized
# Line 58  import skrueger.geotools.selection.Style Line 64  import skrueger.geotools.selection.Style
64   *         (University of Bonn/Germany)   *         (University of Bonn/Germany)
65   */   */
66  public class FeatureMapLayerSelectionSynchronizer extends  public class FeatureMapLayerSelectionSynchronizer extends
67                  StyledLayerSelectionModelSynchronizer<Feature> implements                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
68                  JMapPaneListener {                  implements JMapPaneListener {
69            public static final String SELECTION_STYLING = "SELECTION";
70          /**          /**
71           * Holds the {@link MapLayer} to keep synchronized with the layer selection           * Holds the {@link MapLayer} to keep synchronized with the layer selection
72           * model.           * model.
# Line 67  public class FeatureMapLayerSelectionSyn Line 74  public class FeatureMapLayerSelectionSyn
74          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
75          protected final StyledMapInterface<?> styledMapLayer;          protected final StyledMapInterface<?> styledMapLayer;
76          protected final JMapPane mapPane;          protected final JMapPane mapPane;
77            private final MapPaneToolBar toolBar;
78    
79          /**          /**
80           * Creates a new synchronizer           * Creates a new synchronizer
# Line 81  public class FeatureMapLayerSelectionSyn Line 89  public class FeatureMapLayerSelectionSyn
89          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
90                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
91                          StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,                          StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
92                          JMapPane mapPane) {                          JMapPane mapPane, MapPaneToolBar toolBar) {
93    
94                  super(layerSelModel);                  super(layerSelModel);
95                  this.styledMapLayer = styledMapLayer;                  this.styledMapLayer = styledMapLayer;
96    
97                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
98                  this.mapPane = mapPane;                  this.mapPane = mapPane;
99                    this.toolBar = toolBar;
100          }          }
101    
102          /**          /**
# Line 102  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 111  public class FeatureMapLayerSelectionSyn Line 124  public class FeatureMapLayerSelectionSyn
124                          return;                          return;
125                  // Apply new selection on table (except this event is one of                  // Apply new selection on table (except this event is one of
126                  // some more events)                  // some more events)
127                  Vector<Feature> newSelection = layerSelModel.getSelection();                  Vector<String> newSelection = layerSelModel.getSelection();
128                  if (newSelection == null)                  if (newSelection == null)
129                          return;                          return;
130    
# Line 124  public class FeatureMapLayerSelectionSyn Line 137  public class FeatureMapLayerSelectionSyn
137                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
138          }          }
139    
140          private void changeLayerStyle(Vector<Feature> newSelection) {          /**
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) {
148                  Style selectionMapStyle = null;                  Style selectionMapStyle = null;
149                  try {                  try {
150                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
151    
                                 LOGGER.debug("NO SELECTION");  
152                                  selectionMapStyle = styledMapLayer.getStyle();                                  selectionMapStyle = styledMapLayer.getStyle();
153                                    // LOGGER.debug("NO SELECTION .. set to original style directly");
154    
155                          } else {                          } else {
156                                  LOGGER.debug("SELECTION EXISTEnT");                                  LOGGER.debug("SELECTION .. change style");
157    
158                                    // We take Style from the MapLayer that is displayed at the
159                                    // moment. We do not use the styledMapLayer.getStyle, because in
160                                    // the atlas, this always return the default style, but
161                                    // additionaly styles might be selected.
162                                    // Taking the style from the mapLayer indicated, that we have to
163                                    // remove any selection rules first.
164                                    Style originalStyle = mapLayer.getStyle();
165    
166                                    // TODO The default style is not good here. We need
167                                    // .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]
174                                                    .setName(SELECTION_STYLING);
175    
176                                    /**
177                                     *
178                                     * Add a Filter to the selectionMapStyle, so that it is only
179                                     * used on objects that are selected. <br/>
180                                     *
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    
195                                    selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
196                                                    .setFilter(FilterUtil.FILTER_FAC2.id(fids));
197    
198                                    FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
199                                                    .getFeatureTypeStyles();
200                                    FeatureTypeStyle[] newFeatureTypes;
201                                    if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
202                                                    .getName() != null
203                                                    && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
204                                                                    .getName().equals(SELECTION_STYLING)) {
205                                            newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,
206                                                            originalFeatureTypeStyles.length - 1);
207                                    } else {
208                                            newFeatureTypes = originalFeatureTypeStyles;
209                                    }
210    
211                                    // The last FTS is the selection FTS
212                                    newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
213                                                    selectionMapStyle.getFeatureTypeStyles());
214    
215                                    selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
216    
217                          }                          }
218    
219                          mapLayer.setStyle(selectionMapStyle);                          mapLayer.setStyle(selectionMapStyle);
# Line 144  public class FeatureMapLayerSelectionSyn Line 223  public class FeatureMapLayerSelectionSyn
223                  }                  }
224          }          }
225    
226          //          /**
227          // /**           * Used to synchronize {@link FeatureSelectedEvent}s with the
228          // * Called when the table selection is changed by the user. When calling           * {@link StyledFeatureLayerSelectionModel}
229          // this           */
         // * method changes the selection of the {@link StyledLayerSelectionModel}.  
         // *  
         // * @param evt  
         // * an event  
         // */  
         // @Override  
         // public void valueChanged(ListSelectionEvent evt) {  
         //                
         // }  
   
230          @Override          @Override
231          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(JMapPaneEvent e) {
232                  if (!(e instanceof FeatureSelectedEvent))  
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 FeatureSelectedEvents if an appropriate tool is
244                     * selected.
245                     */
246                    final int selectedTool = toolBar.getSelectedTool();
247                    if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
248                                    && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
249                                    && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250                            return;
251                    }
252    
253                    // only listen to events directly coming from JMapPane
254                    // selection (ignore selections from FilterDialog)
255                    if (e.getSourceObject() != this.mapPane)
256                          return;                          return;
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);
273    
274                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
275                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
276    
277                    Collection<Feature> selectionResult = (Collection<Feature>) fse
278                                    .getSelectionResult();
279    
280                  // reset the selection of the DpLayerSelectionModel                  // reset the selection of the DpLayerSelectionModel
281                  // layerSelModel.setValueIsAdjusting(true);                  // layerSelModel.setValueIsAdjusting(true);
282    
283                  layerSelModel.addSelection((Collection<Feature>) fse                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
284                                  .getSelectionResult());                          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();
292    
293                            for (Feature f : selectionResult) {
294                                    layerSelModel.addSelection(f.getID());
295                            }
296    
297                  // for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {                          LOGGER.debug("Setting selection to " + selectionResult.size());
298                  // Feature changedFeature = featureTableModel.getFeature(featureTable                          layerSelModel.setValueIsAdjusting(false);
299                  // .convertRowIndexToModel(i));                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
300                  // if (featureTable.isRowSelected(i))                          layerSelModel.setValueIsAdjusting(true);
301                  // layerSelModel.addSelection(changedFeature);                          for (Feature f : selectionResult) {
302                  // else                                  layerSelModel.removeSelection(f.getID());
303                  // layerSelModel.removeSelection(changedFeature);                          }
304                  // }                          layerSelModel.setValueIsAdjusting(false);
305                  // layerSelModel.setValueIsAdjusting(false);                  }
306    
307                  // Show selected feature in the map, which is not automatically done by                  // Show selected features in the map, which is not automatically done by
308                  // the FeatureSelectedEvent                  // the origin: FeatureSelectedEvent
309                  changeLayerStyle(layerSelModel.getSelection());                  changeLayerStyle(layerSelModel.getSelection());
310                    
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.97  
changed lines
  Added in v.158

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26