/[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 493 by mojays, Fri Oct 23 14:09:27 2009 UTC revision 516 by alfonx, Wed Nov 11 21:17:33 2009 UTC
# Line 42  Line 42 
42   **/   **/
43  package skrueger.geotools.selection;  package skrueger.geotools.selection;
44    
 import java.awt.RenderingHints;  
45  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
46  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
47  import java.io.File;  import java.io.File;
 import java.util.HashMap;  
48  import java.util.HashSet;  import java.util.HashSet;
49  import java.util.Iterator;  import java.util.Iterator;
50  import java.util.Set;  import java.util.Set;
# Line 58  import javax.swing.event.ListSelectionLi Line 56  import javax.swing.event.ListSelectionLi
56    
57  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
58  import org.geotools.feature.FeatureIterator;  import org.geotools.feature.FeatureIterator;
59    import org.geotools.filter.FidFilterImpl;
60  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
 import org.geotools.renderer.GTRenderer;  
 import org.geotools.renderer.label.LabelCacheImpl;  
 import org.geotools.renderer.lite.StreamingRenderer;  
61  import org.geotools.styling.FeatureTypeStyle;  import org.geotools.styling.FeatureTypeStyle;
62  import org.geotools.styling.Style;  import org.geotools.styling.Style;
63  import org.geotools.styling.visitor.DuplicatingStyleVisitor;  import org.geotools.styling.visitor.DuplicatingStyleVisitor;
64  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
65  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
66    import org.opengis.filter.Filter;
67    import org.opengis.filter.Not;
68  import org.opengis.filter.identity.FeatureId;  import org.opengis.filter.identity.FeatureId;
69    
70  import schmitzm.geotools.FilterUtil;  import schmitzm.geotools.FilterUtil;
71  import schmitzm.geotools.GTUtil;  import schmitzm.geotools.gui.SelectableXMapPane;
 import schmitzm.geotools.gui.JMapPane;  
72  import schmitzm.geotools.map.event.FeatureSelectedEvent;  import schmitzm.geotools.map.event.FeatureSelectedEvent;
73  import schmitzm.geotools.map.event.JMapPaneEvent;  import schmitzm.geotools.map.event.MapPaneEvent;
74  import schmitzm.geotools.map.event.JMapPaneListener;  import schmitzm.geotools.map.event.JMapPaneListener;
75  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
76  import skrueger.geotools.MapPaneToolBar;  import skrueger.geotools.MapPaneToolBar;
77  import skrueger.geotools.StyledFeaturesInterface;  import skrueger.geotools.StyledFeaturesInterface;
 import skrueger.geotools.StyledLayerInterface;  
78    
79  /**  /**
80   * This class keeps the selection of a (feature) {@link JTable} synchronized   * This class keeps the selection of a (feature) {@link JTable} synchronized
# Line 101  import skrueger.geotools.StyledLayerInte Line 97  import skrueger.geotools.StyledLayerInte
97  public class FeatureMapLayerSelectionSynchronizer extends  public class FeatureMapLayerSelectionSynchronizer extends
98                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
99                  implements JMapPaneListener {                  implements JMapPaneListener {
100          public static final String SELECTION_STYLING = "SELECTION";  
101            /**
102             * This constant is set as the {@link FeatureTypeStyle#getName()} attribute
103             * in the {@link FeatureTypeStyle}s that only exist to present the selected
104             * features
105             **/
106            public static final String SELECTION_STYLING_FTS_NAME = "SELECTION";
107    
108          /**          /**
109           * Holds the {@link MapLayer} to keep synchronized with the layer selection           * Holds the {@link MapLayer} to keep synchronized with the layer selection
110           * model.           * model.
111           */           */
112          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
113          protected final StyledFeaturesInterface<?> styledLayer;          protected final StyledFeaturesInterface<?> styledLayer;
114          protected final JMapPane mapPane;          protected final SelectableXMapPane mapPane;
115          private final MapPaneToolBar toolBar;          private final MapPaneToolBar toolBar;
         private final HashMap<Object, Object> defaultGTRenderingHints;  
116    
117          /**          /**
118           * Creates a new synchronizer           * Creates a new synchronizer
# Line 125  public class FeatureMapLayerSelectionSyn Line 127  public class FeatureMapLayerSelectionSyn
127          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
128                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
129                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,
130                          JMapPane mapPane, MapPaneToolBar toolBar,                          SelectableXMapPane mapPane, MapPaneToolBar toolBar) {
                         HashMap<Object, Object> defaultGTRenderingHints) {  
131    
132                  super(layerSelModel);                  super(layerSelModel);
133                  this.styledLayer = styledLayer;                  this.styledLayer = styledLayer;
# Line 134  public class FeatureMapLayerSelectionSyn Line 135  public class FeatureMapLayerSelectionSyn
135                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
136                  this.mapPane = mapPane;                  this.mapPane = mapPane;
137                  this.toolBar = toolBar;                  this.toolBar = toolBar;
                 if (defaultGTRenderingHints != null)  
                         this.defaultGTRenderingHints = defaultGTRenderingHints;  
                 else  
                         this.defaultGTRenderingHints = new HashMap<Object, Object>();  
138          }          }
139    
140          /**          /**
# Line 189  public class FeatureMapLayerSelectionSyn Line 186  public class FeatureMapLayerSelectionSyn
186                  try {                  try {
187    
188                          Style originalStyle = mapLayer.getStyle();                          Style originalStyle = mapLayer.getStyle();
189    
190                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
191    
192                                  // Check if the Style contains a SELECTION FTS                                  // Check if the Style contains a SELECTION FTS
# Line 198  public class FeatureMapLayerSelectionSyn Line 196  public class FeatureMapLayerSelectionSyn
196    
197                                  for (FeatureTypeStyle fts : clone) {                                  for (FeatureTypeStyle fts : clone) {
198                                          if (fts.getName() != null                                          if (fts.getName() != null
199                                                          && fts.getName().equals(SELECTION_STYLING)) {                                                          && fts.getName().equals(SELECTION_STYLING_FTS_NAME)) {
200                                                  originalStyle.featureTypeStyles().remove(fts);                                                  originalStyle.featureTypeStyles().remove(fts);
201    
202                                                  replaceRenderer();                                                  replaceRenderer();
# Line 209  public class FeatureMapLayerSelectionSyn Line 207  public class FeatureMapLayerSelectionSyn
207                                  }                                  }
208    
209                          } else {                          } else {
210    
211                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
212    
213                                    // Saving a repaint if the selection didn't change
214                                    if (!selectionChanged(newSelection, originalStyle)) {
215                                            return;
216                                    }
217    
218                                  // We take Style from the MapLayer that is displayed at the                                  // We take Style from the MapLayer that is displayed at the
219                                  // moment. We do not use the styledLayer.getStyle, because in                                  // moment. We do not use the styledLayer.getStyle, because in
220                                  // the atlas, this always return the default style, but                                  // the atlas, this always return the default style, but
# Line 221  public class FeatureMapLayerSelectionSyn Line 225  public class FeatureMapLayerSelectionSyn
225                                  FeatureTypeStyle selectionFTStyle = StylingUtil                                  FeatureTypeStyle selectionFTStyle = StylingUtil
226                                                  .createSelectionStyle(styledLayer.getGeoObject());                                                  .createSelectionStyle(styledLayer.getGeoObject());
227    
228                                  selectionFTStyle.setName(SELECTION_STYLING);                                  selectionFTStyle.setName(SELECTION_STYLING_FTS_NAME);
229    
230                                  /**                                  /**
231                                   *                                   *
# Line 232  public class FeatureMapLayerSelectionSyn Line 236  public class FeatureMapLayerSelectionSyn
236                                   * inverted to be shorter.                                   * inverted to be shorter.
237                                   *                                   *
238                                   */                                   */
   
239                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer
240                                                  .getFeatureCollectionFiltered();                                                  .getFeatureCollectionFiltered();
241                                  if (newSelection.size() > featureCollectionFiltered.size() / 2) {                                  if (newSelection.size() > featureCollectionFiltered.size() / 2) {
# Line 270  public class FeatureMapLayerSelectionSyn Line 273  public class FeatureMapLayerSelectionSyn
273                                  boolean foundAndReplaced = false;                                  boolean foundAndReplaced = false;
274                                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {                                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
275                                          if (fts.getName() != null                                          if (fts.getName() != null
276                                                          && fts.getName().equals(SELECTION_STYLING)) {                                                          && fts.getName().equals(SELECTION_STYLING_FTS_NAME)) {
277                                                  foundAndReplaced = true;                                                  foundAndReplaced = true;
278                                                  fts.rules().clear();                                                  fts.rules().clear();
279                                                  fts.rules().addAll(selectionFTStyle.rules());                                                  fts.rules().addAll(selectionFTStyle.rules());
# Line 288  public class FeatureMapLayerSelectionSyn Line 291  public class FeatureMapLayerSelectionSyn
291                                  dsv.visit(originalStyle);                                  dsv.visit(originalStyle);
292                                  Style newStyle = (Style) dsv.getCopy();                                  Style newStyle = (Style) dsv.getCopy();
293    
294  //SK-Debug                                  // SK-Debug
295  //                              try {                                  try {
296  ////                                                                              //
297  ////                            StylingUtil.saveStyleToSLD(newStyle, new File(                                          StylingUtil.saveStyleToSLD(newStyle, new File(
298  ////                                            "/home/stefan/Desktop/selection.sld"));                                                          "/home/stefan/Desktop/selection.sld"));
299  //                              } catch (Exception e) {                                  } catch (Exception e) {
300  //                              }                                  }
301    
302                                  // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();                                  // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
303                                  // dsv.visit(originalStyle);                                  // dsv.visit(originalStyle);
# Line 312  public class FeatureMapLayerSelectionSyn Line 315  public class FeatureMapLayerSelectionSyn
315                  }                  }
316          }          }
317    
318          private void replaceRenderer() {          /**
319                  //           * Analyses whether the selection has changed in comparison to the selection
320                  // // Has to be done before we apply the new Renderer           * stored in the mapLayer.Style
321                  // mapLayer.setStyle(style);           *
322             * @param newSelection
323             * @param originalStyle
324             * @return
325             */
326            private boolean selectionChanged(Vector<String> newSelection,
327                            Style originalStyle) {
328    
329                  GTRenderer oldRenderer = mapPane.getRenderer();                  boolean SELECTION_STYLING_foundInMapStyle = false;
330    
331                  /**                  /**
332                   * Explicitly putting a new instance of LabelCacheDefault into the                   * testing, whether the selection really changed. If not, we can save
333                   * renderer instance, so JMapPane doesn't reuse the old one. This is                   * one paint!
                  * very useful when changing the TextSymbolizer with AtlasStyler<br/>  
                  * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not  
                  * reuse the old Renderer - better to create a new one!  
334                   */                   */
335                  final GTRenderer newRenderer = GTUtil.createGTRenderer();                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
336    
337                  final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;                          if (fts.getName() != null
338                  rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,                                          && fts.getName().equals(SELECTION_STYLING_FTS_NAME)) {
                                 new LabelCacheImpl());  
339    
340                  newRenderer.setRendererHints(rendererHints);                                  SELECTION_STYLING_foundInMapStyle = true;
                 mapPane.setRenderer(newRenderer);  
341    
342                  if (oldRenderer != null) {                                  Filter filter = fts.rules().get(0).getFilter();
343                                    if (filter instanceof Not) {
344                                            FidFilterImpl antiOrigFidsFilter = (FidFilterImpl) ((Not) filter)
345                                                            .getFilter();
346    
347                          RenderingHints java2DHints = oldRenderer.getJava2DHints();                                          // Check one way
348                          if (java2DHints != null) {                                          final Set<String> antiFids = antiOrigFidsFilter
349                                  newRenderer.setJava2DHints(java2DHints);                                                          .getFidsSet();
350                          }  
351                                            if (antiFids.isEmpty() && !newSelection.isEmpty())
352                                                    return true;
353    
354                                            for (String fid : antiFids) {
355                                                    if (newSelection.contains(fid)) {
356                                                            return true;
357                                                    }
358                                            }
359    
360                                            // Check the other way
361                                            for (String fid : newSelection) {
362                                                    if (antiFids.contains(fid)) {
363                                                            return true;
364                                                    }
365                                            }
366    
367                                    } else {
368                                            FidFilterImpl origFidsFilter = (FidFilterImpl) filter;
369    
370                          oldRenderer.setContext(null);                                          // Check one way
371                          oldRenderer = null;                                          final Set<String> fids = origFidsFilter.getFidsSet();
372    
373                                            if (fids.isEmpty() && newSelection.isEmpty())
374                                                    return false;
375                                            if (fids.size() != newSelection.size())
376                                                    return true;
377    
378                                            for (String fid : fids) {
379                                                    if (!newSelection.contains(fid)) {
380                                                            return true;
381                                                    }
382                                            }
383    
384                                            // Check the other way
385                                            for (String fid : newSelection) {
386                                                    if (!fids.contains(fid)) {
387                                                            return true;
388                                                    }
389                                            }
390    
391                                    }
392    
393                                    break;
394                            }
395                  }                  }
396    
397                    if (!SELECTION_STYLING_foundInMapStyle && !newSelection.isEmpty())
398                            return true;
399    
400                    return false;
401            }
402    
403            /**
404             * Replaces the local renderer
405             */
406            private void replaceRenderer() {
407                    // //
408                    // // // Has to be done before we apply the new Renderer
409                    // // mapLayer.setStyle(style);
410                    //
411                    // GTRenderer oldRenderer = mapPane.getLocalRenderer();
412                    //
413                    // /**
414                    // * Explicitly putting a new instance of LabelCacheDefault into the
415                    // * renderer instance, so JMapPane doesn't reuse the old one. This is
416                    // * very useful when changing the TextSymbolizer with AtlasStyler<br/>
417                    // * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not
418                    // * reuse the old Renderer - better to create a new one!
419                    // */
420                    // final GTRenderer newRenderer = GTUtil.createGTRenderer();
421                    //
422                    // final HashMap<Object, Object> rendererHints =
423                    // defaultGTRenderingHints;
424                    // rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,
425                    // new LabelCacheImpl());
426                    //
427                    // newRenderer.setRendererHints(rendererHints);
428                    // mapPane.setLocalRenderer(newRenderer);
429                    //
430                    // if (oldRenderer != null) {
431                    //
432                    // RenderingHints java2DHints = oldRenderer.getJava2DHints();
433                    // if (java2DHints != null) {
434                    // newRenderer.setJava2DHints(java2DHints);
435                    // }
436                    //
437                    // oldRenderer.setContext(null);
438                    // oldRenderer = null;
439                    // }
440    
441                  mapPane.refresh();                  mapPane.refresh();
442    
443          }          }
# Line 355  public class FeatureMapLayerSelectionSyn Line 447  public class FeatureMapLayerSelectionSyn
447           * {@link StyledFeatureLayerSelectionModel}           * {@link StyledFeatureLayerSelectionModel}
448           */           */
449          @Override          @Override
450          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(MapPaneEvent e) {
451    
452                  // Ignore event if it is caused by us or the synchronizer is disabled.                  // Ignore event if it is caused by us or the synchronizer is disabled.
453                  if (!isEnabled() || selectionChangeCausedByMe)                  if (!isEnabled() || selectionChangeCausedByMe)
# Line 382  public class FeatureMapLayerSelectionSyn Line 474  public class FeatureMapLayerSelectionSyn
474                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
475                          return;                          return;
476    
   
477                  /**                  /**
478                   * Checking, that the FeatureSelectedEvent actually contains features                   * Checking, that the FeatureSelectedEvent actually contains features
479                   * from this layer                   * from this layer

Legend:
Removed from v.493  
changed lines
  Added in v.516

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26