/[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 105 by alfonx, Fri May 8 15:02:28 2009 UTC revision 127 by mojays, Sat May 30 14:29:58 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.Rule;  import org.geotools.styling.FeatureTypeStyle;
 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.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 67  import skrueger.geotools.selection.Style Line 58  import skrueger.geotools.selection.Style
58   *         (University of Bonn/Germany)   *         (University of Bonn/Germany)
59   */   */
60  public class FeatureMapLayerSelectionSynchronizer extends  public class FeatureMapLayerSelectionSynchronizer extends
61                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> implements                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
62                  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 76  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 90  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 120  public class FeatureMapLayerSelectionSyn Line 114  public class FeatureMapLayerSelectionSyn
114                          return;                          return;
115                  // Apply new selection on table (except this event is one of                  // Apply new selection on table (except this event is one of
116                  // some more events)                  // some more events)
117                  Vector<Feature> newSelection = layerSelModel.getSelection();                  Vector<String> newSelection = layerSelModel.getSelection();
118                  if (newSelection == null)                  if (newSelection == null)
119                          return;                          return;
120    
# Line 133  public class FeatureMapLayerSelectionSyn Line 127  public class FeatureMapLayerSelectionSyn
127                  selectionChangeCausedByMe = false;                  selectionChangeCausedByMe = false;
128          }          }
129    
130          private void changeLayerStyle(final Vector<Feature> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
131                  Style selectionMapStyle = null;                  Style selectionMapStyle = null;
132                  try {                  try {
133                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
134    
135                                  selectionMapStyle = styledMapLayer.getStyle();                                  selectionMapStyle = styledMapLayer.getStyle();
136                                  LOGGER.debug("NO SELECTION .. reset original style");                                  LOGGER.debug("NO SELECTION .. set to original style directly");
137                                    
138                          } else {                          } else {
139                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
140                                    
141                                  Style originalStyle = styledMapLayer.getStyle();                                  // We take Style from the MapLayer that is displayed at the
142                                                                    // moment. We do not use the styledMapLayer.getStyle, because in
143                                  // TODO This clone is a deep clone and it is slow..                                  // the atlas, this always return the default style, but
144                                  selectionMapStyle =  StylingUtil.clone(originalStyle);                                  // additionaly styles might be selected.
145                                                                    // Taking the style from the mapLayer indicated, that we have to
146                                                                    // remove any selection rules first.
147                                  Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();                                  Style originalStyle = mapLayer.getStyle();
148                                  selectedRule.setFilter(new Filter() {  
149                                    // TODO The default style is not good here. We need
150                                          @Override                                  // .createSelectionStyle(normalStyle, geoObj);
151                                          public Object accept(FilterVisitor visitor, Object extraData) {                                  selectionMapStyle = StylingUtil
152                                                  return null;                                                  .createDefaultStyle(styledMapLayer.getGeoObject());
153                                          }  
154                                    selectionMapStyle.getFeatureTypeStyles()[0]
155                                          @Override                                                  .setName(SELECTION_STYLING);
156                                          public boolean evaluate(Object obj) {  
157                                                  if (obj instanceof Feature) {                                  // Symbolizer sss =
158                                                          Feature f = (Feature)obj;                                  // selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];
159                                                          LOGGER.info("treffer");                                  // if (sss instanceof PolygonSymbolizer) {
160                                                          // TODO BAD CODE                                  // PolygonSymbolizer ps = (PolygonSymbolizer) sss;
161                                                          for (Feature ff : newSelection){                                  // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));
162                                                                  if (ff.getID().equals(f.getID())) return true;                                  // }
163    
164                                    // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
165                                    selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
166                                                    .setFilter(new Filter() {
167    
168                                                            @Override
169                                                            public Object accept(FilterVisitor visitor,
170                                                                            Object extraData) {
171                                                                    return null;
172                                                            }
173    
174                                                            @Override
175                                                            public boolean evaluate(Object obj) {
176                                                                    if (obj instanceof Feature) {
177                                                                            Feature f = (Feature) obj;
178                                                                            // TODO BAD CODE! says Martin.. i am fine
179                                                                            // with it.. well.. not great...
180                                                                            for (String ffID : newSelection) {
181                                                                                    if (ffID.equals(f.getID()))
182                                                                                            return true;
183                                                                            }
184                                                                            return false;
185    
186                                                                    }
187                                                                    return false;
188                                                          }                                                          }
189                                                          return false;  
190                                                                                                            });
191                                                  }  
192                                                  LOGGER.info("kein treffer");                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
193                                                  return false;                                                  .getFeatureTypeStyles();
194                                          }                                  FeatureTypeStyle[] newFeatureTypes;
195                                                                            if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
196                                  });                                                  .getName() != null
197                                                                                    && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
198                                  Style defaultStyle = StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());                                                                  .getName().equals(SELECTION_STYLING)) {
199                                  selectedRule.setSymbolizers(new Symbolizer[] {defaultStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0]} );                                          newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,
200                                                                                            originalFeatureTypeStyles.length - 1);
201                                  Rule[] rules = selectionMapStyle.getFeatureTypeStyles()[0].getRules();                                  } else {
202                                  rules = LangUtil.extendArray(rules, selectedRule);                                          newFeatureTypes = originalFeatureTypeStyles;
203                                                                    }
204                                  selectionMapStyle.getFeatureTypeStyles()[0].setRules(rules);                                  //                              
205                                                                    // // REMOVE any Selection_Styling FTS (usually the last one)
206  //                              selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)                                  //                              
207                                                                    // for (FeatureTypeStyle fts : originalFeatureTypeStyles){
208                                    // if (fts.getName().equals(SELECTION_STYLING)) continue;
209                                    // newFeatureTypes =
210                                    //                                      
211                                    // LangUtil.extendArray(
212                                    // originalFeatureTypeStyles, selectionMapStyle
213                                    // .getFeatureTypeStyles());
214                                    // }
215    
216                                    // The last FTS is the selection FTS
217                                    newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
218                                                    selectionMapStyle.getFeatureTypeStyles());
219    
220                                    selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
221    
222                                    // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)
223    
224                                  // Flat copy the style                                  // Flat copy the style
225                                    
226  //                              selectionMapStyle = StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());                                  // selectionMapStyle =
227                                                                    // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());
228                                    
229                          }                          }
230    
231                          mapLayer.setStyle(selectionMapStyle);                          mapLayer.setStyle(selectionMapStyle);
# Line 200  public class FeatureMapLayerSelectionSyn Line 235  public class FeatureMapLayerSelectionSyn
235                  }                  }
236          }          }
237    
         //  
         // /**  
         // * Called when the table selection is changed by the user. When calling  
         // this  
         // * method changes the selection of the {@link StyledLayerSelectionModel}.  
         // *  
         // * @param evt  
         // * an event  
         // */  
         // @Override  
         // public void valueChanged(ListSelectionEvent evt) {  
         //                
         // }  
   
238          @Override          @Override
239          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(JMapPaneEvent e) {
240                    
241                    /**
242                     * Only listen to FeatureSelectionEvents if the appropriate tool is
243                     * selected.
244                     */
245                    final int selectedTool = toolBar.getSelectedTool();
246                    if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
247                                    && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
248                                    && selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR
249                                    && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250                            return;
251                    }
252    
253                  if (!(e instanceof FeatureSelectedEvent)) {                  if (!(e instanceof FeatureSelectedEvent)) {
254                          LOGGER.debug("Ignoring event "+e);                          // LOGGER.debug("Ignoring event " + e);
255                          return;                          return;
256                  }                  }
257                    // only listen to events directly coming from JMapPane
258                    // selection (ignore selections from FilterDialog)
259                    if (e.getSourceObject() != this.mapPane)
260                            return;
261    
262                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;                  FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
                 LOGGER.debug("Do event "+fse);  
                 LOGGER.debug("Do event "+fse.getSelectionResult());  
263    
264                  // ignore event if it is caused by the DpLayerVectorSelectionModel                  // ignore event if it is caused by the DpLayerVectorSelectionModel
265                  if (selectionChangeCausedByMe)                  if (selectionChangeCausedByMe)
266                          return;                          return;
267    
268                    LOGGER.debug("Do event " + fse);
269    
270                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
271                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
272    
273                  // reset the selection of the DpLayerSelectionModel                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) {
274                  // layerSelModel.setValueIsAdjusting(true);                          layerSelModel.clearSelection();
275                    } else {
276                  layerSelModel.addSelection((Collection<Feature>) fse                          Collection<Feature> selectionResult = (Collection<Feature>) fse
277                                  .getSelectionResult());                                          .getSelectionResult();
278    
279                  // for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {                          // reset the selection of the DpLayerSelectionModel
280                  // Feature changedFeature = featureTableModel.getFeature(featureTable                          // layerSelModel.setValueIsAdjusting(true);
281                  // .convertRowIndexToModel(i));  
282                  // if (featureTable.isRowSelected(i))                          if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
283                  // layerSelModel.addSelection(changedFeature);                                  layerSelModel.setValueIsAdjusting(true);
284                  // else                                  for (Feature f : selectionResult) {
285                  // layerSelModel.removeSelection(changedFeature);                                          layerSelModel.addSelection(f.getID());
286                  // }                                  }
287                  // layerSelModel.setValueIsAdjusting(false);                                  layerSelModel.setValueIsAdjusting(false);
288                            } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
289                                    layerSelModel.setValueIsAdjusting(true);
290                                    layerSelModel.clearSelection();
291    
292                                    for (Feature f : selectionResult) {
293                                            layerSelModel.addSelection(f.getID());
294                                    }
295    
296                                    LOGGER.debug("Setting selection to " + selectionResult.size());
297                                    layerSelModel.setValueIsAdjusting(false);
298                            } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
299                                    layerSelModel.setValueIsAdjusting(true);
300                                    for (Feature f : selectionResult) {
301                                            layerSelModel.removeSelection(f.getID());
302                                    }
303                                    layerSelModel.setValueIsAdjusting(false);
304                            }
305                    } // els it's not a Clear Selection command
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          }          }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26