/[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 107 by alfonx, Mon May 11 13:14:20 2009 UTC revision 151 by alfonx, Fri Jun 19 16:22:29 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.Vector;  import java.util.Vector;
21    
22  import javax.swing.JTable;  import javax.swing.JTable;
23  import javax.swing.ListSelectionModel;  import javax.swing.ListSelectionModel;
 import javax.swing.event.ListSelectionEvent;  
24  import javax.swing.event.ListSelectionListener;  import javax.swing.event.ListSelectionListener;
 import javax.swing.text.Utilities;  
25    
26  import org.geotools.feature.Feature;  import org.geotools.feature.Feature;
27  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
28  import org.geotools.styling.PolygonSymbolizer;  import org.geotools.styling.FeatureTypeStyle;
 import org.geotools.styling.Rule;  
 import org.geotools.styling.RuleImpl;  
29  import org.geotools.styling.Style;  import org.geotools.styling.Style;
 import org.geotools.styling.StyleImpl;  
 import org.geotools.styling.Symbolizer;  
30  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
31  import org.opengis.filter.FilterVisitor;  import org.opengis.filter.FilterVisitor;
 import org.opengis.layer.StyleURL;  
32    
 import schmitzm.geotools.FilterUtil;  
 import schmitzm.geotools.gui.FeatureCollectionTableModel;  
33  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.JMapPane;
34  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
35  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
36  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
37  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
38  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
39  import skrueger.geotools.StyledFeatureCollectionInterface;  import skrueger.geotools.MapPaneToolBar;
40  import skrueger.geotools.StyledMapInterface;  import skrueger.geotools.StyledMapInterface;
 import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;  
 import skrueger.geotools.selection.StyledLayerSelectionEvent;  
 import skrueger.geotools.selection.StyledLayerSelectionModel;  
41    
42  /**  /**
43   * This class keeps the selection of a (feature) {@link JTable} synchronized   * This class keeps the selection of a (feature) {@link JTable} synchronized
# Line 71  import skrueger.geotools.selection.Style Line 60  import skrueger.geotools.selection.Style
60  public class FeatureMapLayerSelectionSynchronizer extends  public class FeatureMapLayerSelectionSynchronizer extends
61                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
62                  implements JMapPaneListener {                  implements JMapPaneListener {
63            public static final String SELECTION_STYLING = "SELECTION";
64          /**          /**
65           * Holds the {@link MapLayer} to keep synchronized with the layer selection           * Holds the {@link MapLayer} to keep synchronized with the layer selection
66           * model.           * model.
# Line 78  public class FeatureMapLayerSelectionSyn Line 68  public class FeatureMapLayerSelectionSyn
68          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
69          protected final StyledMapInterface<?> styledMapLayer;          protected final StyledMapInterface<?> styledMapLayer;
70          protected final JMapPane mapPane;          protected final JMapPane mapPane;
71            private final MapPaneToolBar toolBar;
72    
73          /**          /**
74           * Creates a new synchronizer           * Creates a new synchronizer
# Line 92  public class FeatureMapLayerSelectionSyn Line 83  public class FeatureMapLayerSelectionSyn
83          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
84                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
85                          StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,                          StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
86                          JMapPane mapPane) {                          JMapPane mapPane, MapPaneToolBar toolBar) {
87    
88                  super(layerSelModel);                  super(layerSelModel);
89                  this.styledMapLayer = styledMapLayer;                  this.styledMapLayer = styledMapLayer;
90    
91                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
92                  this.mapPane = mapPane;                  this.mapPane = mapPane;
93                    this.toolBar = toolBar;
94          }          }
95    
96          /**          /**
# Line 113  public class FeatureMapLayerSelectionSyn Line 105  public class FeatureMapLayerSelectionSyn
105           */           */
106          @Override          @Override
107          public void propertyChange(PropertyChangeEvent evt) {          public void propertyChange(PropertyChangeEvent evt) {
108    
109                    if (!isEnabled())
110                            return;
111    
112                  if (!(evt instanceof StyledLayerSelectionEvent))                  if (!(evt instanceof StyledLayerSelectionEvent))
113                          return;                          return;
114                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
# Line 122  public class FeatureMapLayerSelectionSyn Line 118  public class FeatureMapLayerSelectionSyn
118                          return;                          return;
119                  // Apply new selection on table (except this event is one of                  // Apply new selection on table (except this event is one of
120                  // some more events)                  // some more events)
121                  Vector<Feature> newSelection = layerSelModel.getSelection();                  Vector<String> newSelection = layerSelModel.getSelection();
122                  if (newSelection == null)                  if (newSelection == null)
123                          return;                          return;
124    
# Line 135  public class FeatureMapLayerSelectionSyn Line 131  public class FeatureMapLayerSelectionSyn
131                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
132          }          }
133    
134          private void changeLayerStyle(final Vector<Feature> newSelection) {          /**
135             * Changes the Style of the {@link MapLayer} to reflect changes of the
136             * selection.
137             *
138             * @param newSelection
139             *            A {@link Vector} of Feature-IDs that are selected.
140             */
141            private void changeLayerStyle(final Vector<String> newSelection) {
142                  Style selectionMapStyle = null;                  Style selectionMapStyle = null;
143                  try {                  try {
144                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
145    
146                                  selectionMapStyle = styledMapLayer.getStyle();                                  selectionMapStyle = styledMapLayer.getStyle();
147                                  LOGGER.debug("NO SELECTION .. reset original style");                                  // LOGGER.debug("NO SELECTION .. set to original style directly");
148    
149                          } else {                          } else {
150                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
151    
152                                  Style originalStyle = styledMapLayer.getStyle();                                  // We take Style from the MapLayer that is displayed at the
153                                    // moment. We do not use the styledMapLayer.getStyle, because in
154                                    // the atlas, this always return the default style, but
155                                    // additionaly styles might be selected.
156                                    // Taking the style from the mapLayer indicated, that we have to
157                                    // remove any selection rules first.
158                                    Style originalStyle = mapLayer.getStyle();
159    
160                                  // TODO This clone is a deep clone and it is slow..                                  // TODO The default style is not good here. We need
161                                    // .createSelectionStyle(normalStyle, geoObj);
162                                  selectionMapStyle = StylingUtil                                  selectionMapStyle = StylingUtil
163                                                  .createDefaultStyle(styledMapLayer.getGeoObject());                                                  .createSelectionStyle(styledMapLayer.getGeoObject());
164                                                                    // selectionMapStyle = StylingUtil
165  //                              Symbolizer sss = selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];                                  // .createDefaultStyle(styledMapLayer.getGeoObject());
166  //                              if (sss instanceof PolygonSymbolizer) {  
167  //                                      PolygonSymbolizer ps = (PolygonSymbolizer) sss;                                  selectionMapStyle.getFeatureTypeStyles()[0]
168  //                                      ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));                                                  .setName(SELECTION_STYLING);
 //                              }  
169    
170                                  // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();                                  // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
171                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
# Line 172  public class FeatureMapLayerSelectionSyn Line 181  public class FeatureMapLayerSelectionSyn
181                                                          public boolean evaluate(Object obj) {                                                          public boolean evaluate(Object obj) {
182                                                                  if (obj instanceof Feature) {                                                                  if (obj instanceof Feature) {
183                                                                          Feature f = (Feature) obj;                                                                          Feature f = (Feature) obj;
184                                                                          // TODO BAD CODE                                                                          // TODO BAD CODE! says Martin.. i am fine
185                                                                          for (Feature ff : newSelection) {                                                                          // with it.. well.. not great...
186                                                                                  if (ff.getID().equals(f.getID()))                                                                          for (String ffID : newSelection) {
187                                                                                    if (ffID.equals(f.getID()))
188                                                                                          return true;                                                                                          return true;
189                                                                          }                                                                          }
190                                                                          return false;                                                                          return false;
# Line 185  public class FeatureMapLayerSelectionSyn Line 195  public class FeatureMapLayerSelectionSyn
195    
196                                                  });                                                  });
197    
198                                  selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray(                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
199                                                  originalStyle                                                  .getFeatureTypeStyles();
200                                                                  .getFeatureTypeStyles(), selectionMapStyle.getFeatureTypeStyles()));                                  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(originalStyle.getF)                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
   
                                 // Flat copy the style  
   
                                 // selectionMapStyle =  
                                 // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());  
216    
217                          }                          }
218    
# Line 205  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    
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)) {                  if (!(e instanceof FeatureSelectedEvent)) {
238  //                      LOGGER.debug("Ignoring event " + e);                          // LOGGER.debug("Ignoring event " + e);
239                          return;                          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;
257    
258                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
 //              LOGGER.debug("Do event " + fse);  
 //              LOGGER.debug("Do event " + fse.getSelectionResult());  
259    
260                  // ignore event if it is caused by the DpLayerVectorSelectionModel                  /**
261                  if (selectionChangeCausedByMe)                   * Checking, that the FeatureSelectedEvent actually contains features from this layer
262                     */
263                    final String sourceID = fse.getSourceLayer().getTitle();
264                    final String syncForID = mapLayer.getTitle();
265                    if (sourceID != null && syncForID != null
266                                    && !sourceID.equals(syncForID)){
267                            LOGGER.debug("Ignoring a FeatureSelectedEvent from "+sourceID);
268                          return;                          return;
269                    }
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 (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {                          for (Feature f : selectionResult) {
294                  // Feature changedFeature = featureTableModel.getFeature(featureTable                                  layerSelModel.addSelection(f.getID());
295                  // .convertRowIndexToModel(i));                          }
                 // if (featureTable.isRowSelected(i))  
                 // layerSelModel.addSelection(changedFeature);  
                 // else  
                 // layerSelModel.removeSelection(changedFeature);  
                 // }  
                 // layerSelModel.setValueIsAdjusting(false);  
296    
297                  // Show selected feature in the map, which is not automatically done by                          LOGGER.debug("Setting selection to " + selectionResult.size());
298                  // the FeatureSelectedEvent                          layerSelModel.setValueIsAdjusting(false);
299                    } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
300                            layerSelModel.setValueIsAdjusting(true);
301                            for (Feature f : selectionResult) {
302                                    layerSelModel.removeSelection(f.getID());
303                            }
304                            layerSelModel.setValueIsAdjusting(false);
305                    }
306    
307                    // Show selected features in the map, which is not automatically done by
308                    // 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.107  
changed lines
  Added in v.151

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26