/[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 318 by alfonx, Wed Aug 26 13:33:32 2009 UTC revision 500 by alfonx, Sun Oct 25 16:56:21 2009 UTC
# 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.SimpleFeature;  import org.geotools.feature.FeatureCollection;
60    import org.geotools.feature.FeatureIterator;
61    import org.geotools.filter.FidFilterImpl;
62  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
63    import org.geotools.renderer.GTRenderer;
64    import org.geotools.renderer.label.LabelCacheImpl;
65    import org.geotools.renderer.lite.StreamingRenderer;
66  import org.geotools.styling.FeatureTypeStyle;  import org.geotools.styling.FeatureTypeStyle;
67  import org.geotools.styling.Style;  import org.geotools.styling.Style;
68    import org.geotools.styling.visitor.DuplicatingStyleVisitor;
69    import org.opengis.feature.simple.SimpleFeature;
70    import org.opengis.feature.simple.SimpleFeatureType;
71    import org.opengis.filter.Filter;
72    import org.opengis.filter.Not;
73  import org.opengis.filter.identity.FeatureId;  import org.opengis.filter.identity.FeatureId;
74    
75  import schmitzm.geotools.FilterUtil;  import schmitzm.geotools.FilterUtil;
76    import schmitzm.geotools.GTUtil;
77  import schmitzm.geotools.gui.JMapPane;  import schmitzm.geotools.gui.JMapPane;
78  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
79  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.JMapPaneEvent;
80  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
81  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
 import schmitzm.lang.LangUtil;  
82  import skrueger.geotools.MapPaneToolBar;  import skrueger.geotools.MapPaneToolBar;
83  import skrueger.geotools.StyledLayerInterface;  import skrueger.geotools.StyledFeaturesInterface;
84    
85  /**  /**
86   * This class keeps the selection of a (feature) {@link JTable} synchronized   * This class keeps the selection of a (feature) {@link JTable} synchronized
# Line 97  public class FeatureMapLayerSelectionSyn Line 109  public class FeatureMapLayerSelectionSyn
109           * model.           * model.
110           */           */
111          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
112          protected final StyledLayerInterface<?> styledLayer;          protected final StyledFeaturesInterface<?> styledLayer;
113          protected final JMapPane mapPane;          protected final JMapPane mapPane;
114          private final MapPaneToolBar toolBar;          private final MapPaneToolBar toolBar;
115            private final HashMap<Object, Object> defaultGTRenderingHints;
116    
117          /**          /**
118           * Creates a new synchronizer           * Creates a new synchronizer
# Line 113  public class FeatureMapLayerSelectionSyn Line 126  public class FeatureMapLayerSelectionSyn
126           */           */
127          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
128                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
129                          StyledLayerInterface<?> styledLayer, MapLayer mapLayer,                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,
130                          JMapPane mapPane, MapPaneToolBar toolBar) {                          JMapPane mapPane, MapPaneToolBar toolBar,
131                            HashMap<Object, Object> defaultGTRenderingHints) {
132    
133                  super(layerSelModel);                  super(layerSelModel);
134                  this.styledLayer = styledLayer;                  this.styledLayer = styledLayer;
# Line 122  public class FeatureMapLayerSelectionSyn Line 136  public class FeatureMapLayerSelectionSyn
136                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
137                  this.mapPane = mapPane;                  this.mapPane = mapPane;
138                  this.toolBar = toolBar;                  this.toolBar = toolBar;
139                    if (defaultGTRenderingHints != null)
140                            this.defaultGTRenderingHints = defaultGTRenderingHints;
141                    else
142                            this.defaultGTRenderingHints = new HashMap<Object, Object>();
143          }          }
144    
145          /**          /**
# Line 170  public class FeatureMapLayerSelectionSyn Line 188  public class FeatureMapLayerSelectionSyn
188           *            A {@link Vector} of SimpleFeature-IDs that are selected.           *            A {@link Vector} of SimpleFeature-IDs that are selected.
189           */           */
190          private void changeLayerStyle(final Vector<String> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
                 Style selectionMapStyle = null;  
191                  try {                  try {
192    
193                            Style originalStyle = mapLayer.getStyle();
194    
195                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
196    
197                                  selectionMapStyle = styledLayer.getStyle();                                  // Check if the Style contains a SELECTION FTS
198                                  // LOGGER.debug("NO SELECTION .. set to original style directly");  
199                                    FeatureTypeStyle[] clone = originalStyle.featureTypeStyles()
200                                                    .toArray(new FeatureTypeStyle[] {}).clone();
201    
202                                    for (FeatureTypeStyle fts : clone) {
203                                            if (fts.getName() != null
204                                                            && fts.getName().equals(SELECTION_STYLING)) {
205                                                    originalStyle.featureTypeStyles().remove(fts);
206    
207                                                    replaceRenderer();
208                                                    // mapPane.refresh();
209    
210                                                    return;
211                                            }
212                                    }
213    
214                          } else {                          } else {
215    
216                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
217    
218                                    // Saving a repaint if the selection didn't change
219                                    if (!selectionChanged(newSelection, originalStyle)) {
220                                            return;
221                                    }
222    
223                                  // We take Style from the MapLayer that is displayed at the                                  // We take Style from the MapLayer that is displayed at the
224                                  // moment. We do not use the styledLayer.getStyle, because in                                  // moment. We do not use the styledLayer.getStyle, because in
225                                  // the atlas, this always return the default style, but                                  // the atlas, this always return the default style, but
226                                  // additional styles might be selected.                                  // additional styles might be selected.
227                                  // Taking the style from the mapLayer indicated, that we have to                                  // Taking the style from the mapLayer indicated, that we have to
228                                  // remove any selection rules first.                                  // remove any selection rules first.
                                 Style originalStyle = mapLayer.getStyle();  
229    
230                                  // TODO The default style is not good here. We need                                  FeatureTypeStyle selectionFTStyle = StylingUtil
                                 // .createSelectionStyle(normalStyle, geoObj);  
                                 selectionMapStyle = StylingUtil  
231                                                  .createSelectionStyle(styledLayer.getGeoObject());                                                  .createSelectionStyle(styledLayer.getGeoObject());
                                 // selectionMapStyle = StylingUtil  
                                 // .createDefaultStyle(styledLayer.getGeoObject());  
232    
233                                  selectionMapStyle.getFeatureTypeStyles()[0]                                  selectionFTStyle.setName(SELECTION_STYLING);
                                                 .setName(SELECTION_STYLING);  
234    
235                                  /**                                  /**
236                                   *                                   *
237                                   * Add a Filter to the selectionMapStyle, so that it is only                                   * Add a Filter to the selectionMapStyle, so that it is only
238                                   * used on objects that are selected. <br/>                                   * used on objects that are selected. <br/>
239                                     * To optimize rendering speed, the filter checks whether more
240                                     * than half of the features are selected. If so, the filter is
241                                     * inverted to be shorter.
242                                   *                                   *
                                  * 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!  
243                                   */                                   */
244                                    final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer
245                                                    .getFeatureCollectionFiltered();
246                                    if (newSelection.size() > featureCollectionFiltered.size() / 2) {
247                                            FeatureIterator<SimpleFeature> iterator = featureCollectionFiltered
248                                                            .features();
249                                            Set<FeatureId> antiFids = new HashSet<FeatureId>();
250                                            try {
251                                                    while (iterator.hasNext()) {
252                                                            SimpleFeature next = iterator.next();
253                                                            if (!newSelection.contains(next.getID()))
254                                                                    antiFids.add(FilterUtil.FILTER_FAC2
255                                                                                    .featureId(next.getID()));
256                                                    }
257    
258                                                    selectionFTStyle.rules().get(0).setFilter(
259                                                                    FilterUtil.FILTER_FAC2
260                                                                                    .not(FilterUtil.FILTER_FAC2
261                                                                                                    .id(antiFids)));
262    
263                                            } finally {
264                                                    featureCollectionFiltered.close(iterator);
265                                            }
266                                    } else {
267                                            Set<FeatureId> fids = new HashSet<FeatureId>();
268                                            for (String fid : newSelection) {
269                                                    fids.add(FilterUtil.FILTER_FAC2.featureId(fid));
270                                            }
271    
272                                  Set<FeatureId> fids = new HashSet<FeatureId>();                                          selectionFTStyle.rules().get(0).setFilter(
273                                  for (String fid : newSelection) {                                                          FilterUtil.FILTER_FAC2.id(fids));
                                         fids.add(FilterUtil.FILTER_FAC2.featureId(fid));  
274                                  }                                  }
275    
276                                  selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]                                  // Maybe there has already been another selection
277                                                  .setFilter(FilterUtil.FILTER_FAC2.id(fids));                                  // FeatureTypeStyle... Let's replace it...
278                                    boolean foundAndReplaced = false;
279                                  FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle                                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
280                                                  .getFeatureTypeStyles();                                          if (fts.getName() != null
281                                  FeatureTypeStyle[] newFeatureTypes;                                                          && fts.getName().equals(SELECTION_STYLING)) {
282                                  if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]                                                  foundAndReplaced = true;
283                                                  .getName() != null                                                  fts.rules().clear();
284                                                  && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]                                                  fts.rules().addAll(selectionFTStyle.rules());
285                                                                  .getName().equals(SELECTION_STYLING)) {                                                  break;
286                                          newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,                                          }
                                                         originalFeatureTypeStyles.length - 1);  
                                 } else {  
                                         newFeatureTypes = originalFeatureTypeStyles;  
287                                  }                                  }
288                                    if (!foundAndReplaced) {
289                                            originalStyle.featureTypeStyles().add(selectionFTStyle);
290                                    }
291    
292                                    // Refresh the map
293                                    // mapPane.refresh();
294    
295                                  // The last FTS is the selection FTS                                  DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
296                                  newFeatureTypes = LangUtil.extendArray(newFeatureTypes,                                  dsv.visit(originalStyle);
297                                                  selectionMapStyle.getFeatureTypeStyles());                                  Style newStyle = (Style) dsv.getCopy();
298    
299                                    // SK-Debug
300                                    try {
301                                            //
302                                            StylingUtil.saveStyleToSLD(newStyle, new File(
303                                                            "/home/stefan/Desktop/selection.sld"));
304                                    } catch (Exception e) {
305                                    }
306    
307                                  selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);                                  // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
308                                    // dsv.visit(originalStyle);
309                                    // Style newStyle = originalStyle;
310    
311                                    // Style newStyle = StylingUtil.STYLE_BUILDER.createStyle();
312                                    // newStyle.featureTypeStyles().addAll(originalStyle.featureTypeStyles());
313                                    mapLayer.setStyle(newStyle);
314    
315                                    replaceRenderer();
316                          }                          }
317    
                         mapLayer.setStyle(selectionMapStyle);  
                         mapPane.refresh();  
318                  } catch (Exception e) {                  } catch (Exception e) {
319                          LOGGER.error("Error while trying to create a selection style", e);                          LOGGER.error("Error while trying to create a selection style", e);
320                  }                  }
321          }          }
322    
323          /**          /**
324             * Analyses whether the selection has changed in comparison to the selection
325             * stored in the mapLayer.Style
326             *
327             * @param newSelection
328             * @param originalStyle
329             * @return
330             */
331            private boolean selectionChanged(Vector<String> newSelection,
332                            Style originalStyle) {
333    
334                    boolean SELECTION_STYLING_foundInMapStyle = false;
335    
336                    /**
337                     * testing, whether the selection really changed. If not, we can save
338                     * one paint!
339                     */
340                    for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
341    
342                            if (fts.getName() != null
343                                            && fts.getName().equals(SELECTION_STYLING)) {
344    
345                                    SELECTION_STYLING_foundInMapStyle = true;
346    
347                                    Filter filter = fts.rules().get(0).getFilter();
348                                    if (filter instanceof Not) {
349                                            FidFilterImpl antiOrigFidsFilter = (FidFilterImpl) ((Not) filter)
350                                                            .getFilter();
351    
352                                            // Check one way
353                                            final Set<String> antiFids = antiOrigFidsFilter
354                                                            .getFidsSet();
355    
356                                            if (antiFids.isEmpty() && !newSelection.isEmpty())
357                                                    return true;
358    
359                                            for (String fid : antiFids) {
360                                                    if (newSelection.contains(fid)) {
361                                                            return true;
362                                                    }
363                                            }
364    
365                                            // Check the other way
366                                            for (String fid : newSelection) {
367                                                    if (antiFids.contains(fid)) {
368                                                            return true;
369                                                    }
370                                            }
371    
372                                    } else {
373                                            FidFilterImpl origFidsFilter = (FidFilterImpl) filter;
374    
375                                            // Check one way
376                                            final Set<String> fids = origFidsFilter.getFidsSet();
377    
378                                            if (fids.isEmpty() && newSelection.isEmpty())
379                                                    return false;
380                                            if (fids.size() != newSelection.size())
381                                                    return true;
382    
383                                            for (String fid : fids) {
384                                                    if (!newSelection.contains(fid)) {
385                                                            return true;
386                                                    }
387                                            }
388    
389                                            // Check the other way
390                                            for (String fid : newSelection) {
391                                                    if (!fids.contains(fid)) {
392                                                            return true;
393                                                    }
394                                            }
395    
396                                    }
397    
398                                    break;
399                            }
400                    }
401    
402                    if (!SELECTION_STYLING_foundInMapStyle && !newSelection.isEmpty())
403                            return true;
404    
405                    return false;
406            }
407    
408            private void replaceRenderer() {
409                    //
410                    // // Has to be done before we apply the new Renderer
411                    // mapLayer.setStyle(style);
412    
413                    GTRenderer oldRenderer = mapPane.getRenderer();
414    
415                    /**
416                     * Explicitly putting a new instance of LabelCacheDefault into the
417                     * renderer instance, so JMapPane doesn't reuse the old one. This is
418                     * very useful when changing the TextSymbolizer with AtlasStyler<br/>
419                     * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not
420                     * reuse the old Renderer - better to create a new one!
421                     */
422                    final GTRenderer newRenderer = GTUtil.createGTRenderer();
423    
424                    final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;
425                    rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,
426                                    new LabelCacheImpl());
427    
428                    newRenderer.setRendererHints(rendererHints);
429                    mapPane.setRenderer(newRenderer);
430    
431                    if (oldRenderer != null) {
432    
433                            RenderingHints java2DHints = oldRenderer.getJava2DHints();
434                            if (java2DHints != null) {
435                                    newRenderer.setJava2DHints(java2DHints);
436                            }
437    
438                            oldRenderer.setContext(null);
439                            oldRenderer = null;
440                    }
441    
442                    mapPane.refresh();
443    
444            }
445    
446            /**
447           * Used to synchronize {@link FeatureSelectedEvent}s with the           * Used to synchronize {@link FeatureSelectedEvent}s with the
448           * {@link StyledFeatureLayerSelectionModel}           * {@link StyledFeatureLayerSelectionModel}
449           */           */
# Line 280  public class FeatureMapLayerSelectionSyn Line 475  public class FeatureMapLayerSelectionSyn
475                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
476                          return;                          return;
477    
                 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;  
   
478                  /**                  /**
479                   * Checking, that the FeatureSelectedEvent actually contains features                   * Checking, that the FeatureSelectedEvent actually contains features
480                   * from this layer                   * from this layer
481                   */                   */
482                    FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
483                  final String sourceID = fse.getSourceLayer().getTitle();                  final String sourceID = fse.getSourceLayer().getTitle();
484                  final String syncForID = mapLayer.getTitle();                  final String syncForID = mapLayer.getTitle();
485                  if (sourceID != null && syncForID != null                  if (sourceID != null && syncForID != null
# Line 294  public class FeatureMapLayerSelectionSyn Line 488  public class FeatureMapLayerSelectionSyn
488                          return;                          return;
489                  }                  }
490    
491                  LOGGER.debug("do event " + fse);                  // LOGGER.debug("do event " + fse);
492    
493                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
494                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
495    
496                  Collection<SimpleFeature> selectionResult = (Collection<SimpleFeature>) fse                  final FeatureCollection<SimpleFeatureType, SimpleFeature> selectionResult = fse
497                                  .getSelectionResult();                                  .getSelectionResult();
498                    Iterator<SimpleFeature> fi = selectionResult.iterator();
499                    try {
500    
501                  // reset the selection of the DpLayerSelectionModel                          // reset the selection of the DpLayerSelectionModel
502                  // layerSelModel.setValueIsAdjusting(true);                          // layerSelModel.setValueIsAdjusting(true);
503    
504                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {                          if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
505                          layerSelModel.setValueIsAdjusting(true);                                  layerSelModel.setValueIsAdjusting(true);
                         for (SimpleFeature f : selectionResult) {  
                                 layerSelModel.addSelection(f.getID());  
                         }  
                         layerSelModel.setValueIsAdjusting(false);  
                 } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {  
                         layerSelModel.setValueIsAdjusting(true);  
                         layerSelModel.clearSelection();  
506    
507                          for (SimpleFeature f : selectionResult) {                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
508                                  layerSelModel.addSelection(f.getID());                                          SimpleFeature f = fi.next();
509                          }                                          layerSelModel.addSelection(f.getID());
510                                    }
511                                    layerSelModel.setValueIsAdjusting(false);
512                            } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
513                                    layerSelModel.setValueIsAdjusting(true);
514                                    layerSelModel.clearSelection();
515    
516                                    for (int fIdx = 0; fi.hasNext(); fIdx++) {
517                                            SimpleFeature f = fi.next();
518                                            layerSelModel.addSelection(f.getID());
519                                    }
520    
521                                    // LOGGER.debug("Setting selection to " + fi.());
522    
523                          LOGGER.debug("Setting selection to " + selectionResult.size());                                  layerSelModel.setValueIsAdjusting(false);
524                          layerSelModel.setValueIsAdjusting(false);                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
525                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {                                  layerSelModel.setValueIsAdjusting(true);
526                          layerSelModel.setValueIsAdjusting(true);                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
527                          for (SimpleFeature f : selectionResult) {                                          SimpleFeature f = fi.next();
528                                  layerSelModel.removeSelection(f.getID());                                          layerSelModel.removeSelection(f.getID());
529                                    }
530                                    layerSelModel.setValueIsAdjusting(false);
531                          }                          }
532                          layerSelModel.setValueIsAdjusting(false);  
533                    } finally {
534                            selectionResult.close(fi);
535                  }                  }
536    
537                  // 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.318  
changed lines
  Added in v.500

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26