/[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

trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java revision 244 by alfonx, Wed Jul 29 09:33:33 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java revision 489 by alfonx, Tue Oct 20 20:04:38 2009 UTC
# Line 2  Line 2 
2   * Copyright (c) 2009 Martin O. J. Schmitz.   * Copyright (c) 2009 Martin O. J. Schmitz.
3   *   *
4   * This file is part of the SCHMITZM library - a collection of utility   * This file is part of the SCHMITZM library - a collection of utility
5   * classes based on Java 1.6, focussing (not only) on Java Swing   * classes based on Java 1.6, focusing (not only) on Java Swing
6   * and the Geotools library.   * and the Geotools library.
7   *   *
8   * The SCHMITZM project is hosted at:   * The SCHMITZM project is hosted at:
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  /**  /**
31   Copyright 2008 Stefan Alfons Krüger   Copyright 2008 Stefan Alfons Krüger
32    
33   atlas-framework - This file is part of the Atlas Framework   atlas-framework - This file is part of the Atlas Framework
34    
35   This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.   This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
# Line 42  Line 42 
42   **/   **/
43  package skrueger.geotools.selection;  package skrueger.geotools.selection;
44    
45    import java.awt.RenderingHints;
46  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
47  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
48  import java.util.Arrays;  import java.io.File;
49  import java.util.Collection;  import java.util.HashMap;
50  import java.util.HashSet;  import java.util.HashSet;
51    import java.util.Iterator;
52  import java.util.Set;  import java.util.Set;
53  import java.util.Vector;  import java.util.Vector;
54    
# Line 54  import javax.swing.JTable; Line 56  import javax.swing.JTable;
56  import javax.swing.ListSelectionModel;  import javax.swing.ListSelectionModel;
57  import javax.swing.event.ListSelectionListener;  import javax.swing.event.ListSelectionListener;
58    
59  import org.geotools.feature.Feature;  import org.geotools.feature.FeatureCollection;
60    import org.geotools.feature.FeatureIterator;
61  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
62    import org.geotools.renderer.GTRenderer;
63    import org.geotools.renderer.label.LabelCacheImpl;
64    import org.geotools.renderer.lite.StreamingRenderer;
65  import org.geotools.styling.FeatureTypeStyle;  import org.geotools.styling.FeatureTypeStyle;
66  import org.geotools.styling.Style;  import org.geotools.styling.Style;
67    import org.geotools.styling.visitor.DuplicatingStyleVisitor;
68    import org.opengis.feature.simple.SimpleFeature;
69    import org.opengis.feature.simple.SimpleFeatureType;
70  import org.opengis.filter.identity.FeatureId;  import org.opengis.filter.identity.FeatureId;
71    
72  import schmitzm.geotools.FilterUtil;  import schmitzm.geotools.FilterUtil;
73    import schmitzm.geotools.GTUtil;
74  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.JMapPane;
75  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
76  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
77  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
78  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
 import schmitzm.lang.LangUtil;  
79  import skrueger.geotools.MapPaneToolBar;  import skrueger.geotools.MapPaneToolBar;
80    import skrueger.geotools.StyledFeaturesInterface;
81  import skrueger.geotools.StyledLayerInterface;  import skrueger.geotools.StyledLayerInterface;
82    
83  /**  /**
# Line 97  public class FeatureMapLayerSelectionSyn Line 107  public class FeatureMapLayerSelectionSyn
107           * model.           * model.
108           */           */
109          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
110          protected final StyledLayerInterface<?> styledLayer;          protected final StyledFeaturesInterface<?> styledLayer;
111          protected final JMapPane mapPane;          protected final JMapPane mapPane;
112          private final MapPaneToolBar toolBar;          private final MapPaneToolBar toolBar;
113            private final HashMap<Object, Object> defaultGTRenderingHints;
114    
115          /**          /**
116           * Creates a new synchronizer           * Creates a new synchronizer
# Line 113  public class FeatureMapLayerSelectionSyn Line 124  public class FeatureMapLayerSelectionSyn
124           */           */
125          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
126                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
127                          StyledLayerInterface<?> styledLayer, MapLayer mapLayer,                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,
128                          JMapPane mapPane, MapPaneToolBar toolBar) {                          JMapPane mapPane, MapPaneToolBar toolBar,
129                            HashMap<Object, Object> defaultGTRenderingHints) {
130    
131                  super(layerSelModel);                  super(layerSelModel);
132                  this.styledLayer = styledLayer;                  this.styledLayer = styledLayer;
# Line 122  public class FeatureMapLayerSelectionSyn Line 134  public class FeatureMapLayerSelectionSyn
134                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
135                  this.mapPane = mapPane;                  this.mapPane = mapPane;
136                  this.toolBar = toolBar;                  this.toolBar = toolBar;
137                    if (defaultGTRenderingHints != null)
138                            this.defaultGTRenderingHints = defaultGTRenderingHints;
139                    else
140                            this.defaultGTRenderingHints = new HashMap<Object, Object>();
141          }          }
142    
143          /**          /**
# Line 167  public class FeatureMapLayerSelectionSyn Line 183  public class FeatureMapLayerSelectionSyn
183           * selection.           * selection.
184           *           *
185           * @param newSelection           * @param newSelection
186           *            A {@link Vector} of Feature-IDs that are selected.           *            A {@link Vector} of SimpleFeature-IDs that are selected.
187           */           */
188          private void changeLayerStyle(final Vector<String> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
                 Style selectionMapStyle = null;  
189                  try {                  try {
190    
191                            Style originalStyle = mapLayer.getStyle();
192                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
193    
194                                  selectionMapStyle = styledLayer.getStyle();                                  // Check if the Style contains a SELECTION FTS
195                                  // LOGGER.debug("NO SELECTION .. set to original style directly");  
196                                    FeatureTypeStyle[] clone = originalStyle.featureTypeStyles()
197                                                    .toArray(new FeatureTypeStyle[] {}).clone();
198    
199                                    for (FeatureTypeStyle fts : clone) {
200                                            if (fts.getName() != null
201                                                            && fts.getName().equals(SELECTION_STYLING)) {
202                                                    originalStyle.featureTypeStyles().remove(fts);
203    
204                                                    replaceRenderer();
205                                                    // mapPane.refresh();
206    
207                                                    return;
208                                            }
209                                    }
210    
211                          } else {                          } else {
212                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
# Line 186  public class FeatureMapLayerSelectionSyn Line 217  public class FeatureMapLayerSelectionSyn
217                                  // additional styles might be selected.                                  // additional styles might be selected.
218                                  // Taking the style from the mapLayer indicated, that we have to                                  // Taking the style from the mapLayer indicated, that we have to
219                                  // remove any selection rules first.                                  // remove any selection rules first.
                                 Style originalStyle = mapLayer.getStyle();  
220    
221                                  // TODO The default style is not good here. We need                                  FeatureTypeStyle selectionFTStyle = StylingUtil
                                 // .createSelectionStyle(normalStyle, geoObj);  
                                 selectionMapStyle = StylingUtil  
222                                                  .createSelectionStyle(styledLayer.getGeoObject());                                                  .createSelectionStyle(styledLayer.getGeoObject());
                                 // selectionMapStyle = StylingUtil  
                                 // .createDefaultStyle(styledLayer.getGeoObject());  
223    
224                                  selectionMapStyle.getFeatureTypeStyles()[0]                                  selectionFTStyle.setName(SELECTION_STYLING);
                                                 .setName(SELECTION_STYLING);  
225    
226                                  /**                                  /**
227                                   *                                   *
228                                   * Add a Filter to the selectionMapStyle, so that it is only                                   * Add a Filter to the selectionMapStyle, so that it is only
229                                   * used on objects that are selected. <br/>                                   * used on objects that are selected. <br/>
230                                     * To optimize rendering speed, the filter checks whether more
231                                     * than half of the features are selected. If so, the filter is
232                                     * inverted to be shorter.
233                                   *                                   *
                                  * Note 1:<br/>  
                                  * It is NEVER allowed to GeoTools extend Filter () { .. } (and write  
                                  * tests into the evaluate block). Especially for the  
                                  * ShapeFileRenderer, we may only use a geotools Filter.<br/>  
                                  *  
                                  * Note 2:<br/>  
                                  * The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects!  
234                                   */                                   */
235    
236                                  Set<FeatureId> fids = new HashSet<FeatureId>();                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer
237                                  for (String fid : newSelection) {                                                  .getFeatureCollectionFiltered();
238                                          fids.add(FilterUtil.FILTER_FAC2.featureId(fid));                                  if (newSelection.size() > featureCollectionFiltered.size() / 2) {
239                                  }                                          FeatureIterator<SimpleFeature> iterator = featureCollectionFiltered
240                                                            .features();
241                                            Set<FeatureId> antiFids = new HashSet<FeatureId>();
242                                            try {
243                                                    while (iterator.hasNext()) {
244                                                            SimpleFeature next = iterator.next();
245                                                            if (!newSelection.contains(next.getID()))
246                                                                    antiFids.add(FilterUtil.FILTER_FAC2
247                                                                                    .featureId(next.getID()));
248                                                    }
249    
250                                                    selectionFTStyle.rules().get(0).setFilter(
251                                                                    FilterUtil.FILTER_FAC2
252                                                                                    .not(FilterUtil.FILTER_FAC2
253                                                                                                    .id(antiFids)));
254    
255                                            } finally {
256                                                    featureCollectionFiltered.close(iterator);
257                                            }
258                                    } else {
259                                            Set<FeatureId> fids = new HashSet<FeatureId>();
260                                            for (String fid : newSelection) {
261                                                    fids.add(FilterUtil.FILTER_FAC2.featureId(fid));
262                                            }
263    
264                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]                                          selectionFTStyle.rules().get(0).setFilter(
265                                                  .setFilter(FilterUtil.FILTER_FAC2.id(fids));                                                          FilterUtil.FILTER_FAC2.id(fids));
266                                    }
267    
268                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle                                  // Maybe there has already been another selection
269                                                  .getFeatureTypeStyles();                                  // FeatureTypeStyle... Let's replace it...
270                                  FeatureTypeStyle[] newFeatureTypes;                                  boolean foundAndReplaced = false;
271                                  if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]                                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
272                                                  .getName() != null                                          if (fts.getName() != null
273                                                  && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]                                                          && fts.getName().equals(SELECTION_STYLING)) {
274                                                                  .getName().equals(SELECTION_STYLING)) {                                                  foundAndReplaced = true;
275                                          newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,                                                  fts.rules().clear();
276                                                          originalFeatureTypeStyles.length - 1);                                                  fts.rules().addAll(selectionFTStyle.rules());
277                                  } else {                                                  break;
278                                          newFeatureTypes = originalFeatureTypeStyles;                                          }
279                                    }
280                                    if (!foundAndReplaced) {
281                                            originalStyle.featureTypeStyles().add(selectionFTStyle);
282                                  }                                  }
283    
284                                  // The last FTS is the selection FTS                                  // Refresh the map
285                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,                                  // mapPane.refresh();
286                                                  selectionMapStyle.getFeatureTypeStyles());  
287                                    DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
288                                    dsv.visit(originalStyle);
289                                    Style newStyle = (Style) dsv.getCopy();
290    
291                                    StylingUtil.saveStyleToSLD(newStyle, new File(
292                                                    "/home/stefan/Desktop/selection.sld"));
293    
294                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);                                  // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
295                                    // dsv.visit(originalStyle);
296                                    // Style newStyle = originalStyle;
297    
298                                    // Style newStyle = StylingUtil.STYLE_BUILDER.createStyle();
299                                    // newStyle.featureTypeStyles().addAll(originalStyle.featureTypeStyles());
300                                    mapLayer.setStyle(newStyle);
301    
302                                    replaceRenderer();
303                          }                          }
304    
                         mapLayer.setStyle(selectionMapStyle);  
                         mapPane.refresh();  
305                  } catch (Exception e) {                  } catch (Exception e) {
306                          LOGGER.error("Error while trying to create a selection style", e);                          LOGGER.error("Error while trying to create a selection style", e);
307                  }                  }
308          }          }
309    
310            private void replaceRenderer() {
311                    //
312                    // // Has to be done before we apply the new Renderer
313                    // mapLayer.setStyle(style);
314    
315                    GTRenderer oldRenderer = mapPane.getRenderer();
316    
317                    /**
318                     * Explicitly putting a new instance of LabelCacheDefault into the
319                     * renderer instance, so JMapPane doesn't reuse the old one. This is
320                     * very useful when changing the TextSymbolizer with AtlasStyler<br/>
321                     * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not
322                     * reuse the old Renderer - better to create a new one!
323                     */
324                    final GTRenderer newRenderer = GTUtil.createGTRenderer();
325    
326                    final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;
327                    rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,
328                                    new LabelCacheImpl());
329    
330                    newRenderer.setRendererHints(rendererHints);
331                    mapPane.setRenderer(newRenderer);
332    
333                    if (oldRenderer != null) {
334    
335                            RenderingHints java2DHints = oldRenderer.getJava2DHints();
336                            if (java2DHints != null) {
337                                    newRenderer.setJava2DHints(java2DHints);
338                            }
339    
340                            oldRenderer.setContext(null);
341                            oldRenderer = null;
342                    }
343    
344                    mapPane.refresh();
345    
346            }
347    
348          /**          /**
349           * Used to synchronize {@link FeatureSelectedEvent}s with the           * Used to synchronize {@link FeatureSelectedEvent}s with the
350           * {@link StyledFeatureLayerSelectionModel}           * {@link StyledFeatureLayerSelectionModel}
# Line 294  public class FeatureMapLayerSelectionSyn Line 391  public class FeatureMapLayerSelectionSyn
391                          return;                          return;
392                  }                  }
393    
394                  LOGGER.debug("do event " + fse);                  // LOGGER.debug("do event " + fse);
395    
396                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
397                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
398    
399                  Collection<Feature> selectionResult = (Collection<Feature>) fse                  final FeatureCollection<SimpleFeatureType, SimpleFeature> selectionResult = fse
400                                  .getSelectionResult();                                  .getSelectionResult();
401                    Iterator<SimpleFeature> fi = selectionResult.iterator();
402                    try {
403    
404                  // reset the selection of the DpLayerSelectionModel                          // reset the selection of the DpLayerSelectionModel
405                  // layerSelModel.setValueIsAdjusting(true);                          // layerSelModel.setValueIsAdjusting(true);
406    
407                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {                          if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
408                          layerSelModel.setValueIsAdjusting(true);                                  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();  
409    
410                          for (Feature f : selectionResult) {                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
411                                  layerSelModel.addSelection(f.getID());                                          SimpleFeature f = fi.next();
412                          }                                          layerSelModel.addSelection(f.getID());
413                                    }
414                                    layerSelModel.setValueIsAdjusting(false);
415                            } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
416                                    layerSelModel.setValueIsAdjusting(true);
417                                    layerSelModel.clearSelection();
418    
419                                    for (int fIdx = 0; fi.hasNext(); fIdx++) {
420                                            SimpleFeature f = fi.next();
421                                            layerSelModel.addSelection(f.getID());
422                                    }
423    
424                                    // LOGGER.debug("Setting selection to " + fi.());
425    
426                          LOGGER.debug("Setting selection to " + selectionResult.size());                                  layerSelModel.setValueIsAdjusting(false);
427                          layerSelModel.setValueIsAdjusting(false);                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
428                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {                                  layerSelModel.setValueIsAdjusting(true);
429                          layerSelModel.setValueIsAdjusting(true);                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
430                          for (Feature f : selectionResult) {                                          SimpleFeature f = fi.next();
431                                  layerSelModel.removeSelection(f.getID());                                          layerSelModel.removeSelection(f.getID());
432                                    }
433                                    layerSelModel.setValueIsAdjusting(false);
434                          }                          }
435                          layerSelModel.setValueIsAdjusting(false);  
436                    } finally {
437                            selectionResult.close(fi);
438                  }                  }
439    
440                  // Show selected features in the map, which is not automatically done by                  // Show selected features in the map, which is not automatically done by

Legend:
Removed from v.244  
changed lines
  Added in v.489

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26