/[schmitzm]/branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Diff of /branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 491 by alfonx, Fri Oct 23 12:57:15 2009 UTC revision 509 by alfonx, Thu Nov 5 08:51: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 108  public class FeatureMapLayerSelectionSyn Line 104  public class FeatureMapLayerSelectionSyn
104           */           */
105          protected final MapLayer mapLayer;          protected final MapLayer mapLayer;
106          protected final StyledFeaturesInterface<?> styledLayer;          protected final StyledFeaturesInterface<?> styledLayer;
107          protected final JMapPane mapPane;          protected final SelectableXMapPane mapPane;
108          private final MapPaneToolBar toolBar;          private final MapPaneToolBar toolBar;
         private final HashMap<Object, Object> defaultGTRenderingHints;  
109    
110          /**          /**
111           * Creates a new synchronizer           * Creates a new synchronizer
# Line 125  public class FeatureMapLayerSelectionSyn Line 120  public class FeatureMapLayerSelectionSyn
120          public FeatureMapLayerSelectionSynchronizer(          public FeatureMapLayerSelectionSynchronizer(
121                          StyledFeatureLayerSelectionModel layerSelModel,                          StyledFeatureLayerSelectionModel layerSelModel,
122                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,                          StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer,
123                          JMapPane mapPane, MapPaneToolBar toolBar,                          SelectableXMapPane mapPane, MapPaneToolBar toolBar) {
                         HashMap<Object, Object> defaultGTRenderingHints) {  
124    
125                  super(layerSelModel);                  super(layerSelModel);
126                  this.styledLayer = styledLayer;                  this.styledLayer = styledLayer;
# Line 134  public class FeatureMapLayerSelectionSyn Line 128  public class FeatureMapLayerSelectionSyn
128                  this.mapLayer = mapLayer;                  this.mapLayer = mapLayer;
129                  this.mapPane = mapPane;                  this.mapPane = mapPane;
130                  this.toolBar = toolBar;                  this.toolBar = toolBar;
                 if (defaultGTRenderingHints != null)  
                         this.defaultGTRenderingHints = defaultGTRenderingHints;  
                 else  
                         this.defaultGTRenderingHints = new HashMap<Object, Object>();  
131          }          }
132    
133          /**          /**
# Line 189  public class FeatureMapLayerSelectionSyn Line 179  public class FeatureMapLayerSelectionSyn
179                  try {                  try {
180    
181                          Style originalStyle = mapLayer.getStyle();                          Style originalStyle = mapLayer.getStyle();
182    
183                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
184    
185                                  // Check if the Style contains a SELECTION FTS                                  // Check if the Style contains a SELECTION FTS
# Line 209  public class FeatureMapLayerSelectionSyn Line 200  public class FeatureMapLayerSelectionSyn
200                                  }                                  }
201    
202                          } else {                          } else {
203    
204                                  LOGGER.debug("SELECTION .. change style");                                  LOGGER.debug("SELECTION .. change style");
205    
206                                    // Saving a repaint if the selection didn't change
207                                    if (!selectionChanged(newSelection, originalStyle)) {
208                                            return;
209                                    }
210    
211                                  // We take Style from the MapLayer that is displayed at the                                  // We take Style from the MapLayer that is displayed at the
212                                  // moment. We do not use the styledLayer.getStyle, because in                                  // moment. We do not use the styledLayer.getStyle, because in
213                                  // the atlas, this always return the default style, but                                  // the atlas, this always return the default style, but
# Line 232  public class FeatureMapLayerSelectionSyn Line 229  public class FeatureMapLayerSelectionSyn
229                                   * inverted to be shorter.                                   * inverted to be shorter.
230                                   *                                   *
231                                   */                                   */
   
232                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer
233                                                  .getFeatureCollectionFiltered();                                                  .getFeatureCollectionFiltered();
234                                  if (newSelection.size() > featureCollectionFiltered.size() / 2) {                                  if (newSelection.size() > featureCollectionFiltered.size() / 2) {
# Line 287  public class FeatureMapLayerSelectionSyn Line 283  public class FeatureMapLayerSelectionSyn
283                                  DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();                                  DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
284                                  dsv.visit(originalStyle);                                  dsv.visit(originalStyle);
285                                  Style newStyle = (Style) dsv.getCopy();                                  Style newStyle = (Style) dsv.getCopy();
286  //  
287  //                              try {                                  // SK-Debug
288  ////                                                                      try {
289  ////                            StylingUtil.saveStyleToSLD(newStyle, new File(                                          //
290  ////                                            "/home/stefan/Desktop/selection.sld"));                                          StylingUtil.saveStyleToSLD(newStyle, new File(
291  //                              } catch (Exception e) {                                                          "/home/stefan/Desktop/selection.sld"));
292  //                              }                                  } catch (Exception e) {
293  //                                  }
294  //                              // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();  
295                                    // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
296                                  // dsv.visit(originalStyle);                                  // dsv.visit(originalStyle);
297                                  // Style newStyle = originalStyle;                                  // Style newStyle = originalStyle;
298    
# Line 311  public class FeatureMapLayerSelectionSyn Line 308  public class FeatureMapLayerSelectionSyn
308                  }                  }
309          }          }
310    
311          private void replaceRenderer() {          /**
312                  //           * Analyses whether the selection has changed in comparison to the selection
313                  // // Has to be done before we apply the new Renderer           * stored in the mapLayer.Style
314                  // mapLayer.setStyle(style);           *
315             * @param newSelection
316             * @param originalStyle
317             * @return
318             */
319            private boolean selectionChanged(Vector<String> newSelection,
320                            Style originalStyle) {
321    
322                  GTRenderer oldRenderer = mapPane.getRenderer();                  boolean SELECTION_STYLING_foundInMapStyle = false;
323    
324                  /**                  /**
325                   * Explicitly putting a new instance of LabelCacheDefault into the                   * testing, whether the selection really changed. If not, we can save
326                   * 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!  
327                   */                   */
328                  final GTRenderer newRenderer = GTUtil.createGTRenderer();                  for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) {
329    
330                  final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;                          if (fts.getName() != null
331                  rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,                                          && fts.getName().equals(SELECTION_STYLING)) {
                                 new LabelCacheImpl());  
332    
333                  newRenderer.setRendererHints(rendererHints);                                  SELECTION_STYLING_foundInMapStyle = true;
                 mapPane.setRenderer(newRenderer);  
334    
335                  if (oldRenderer != null) {                                  Filter filter = fts.rules().get(0).getFilter();
336                                    if (filter instanceof Not) {
337                                            FidFilterImpl antiOrigFidsFilter = (FidFilterImpl) ((Not) filter)
338                                                            .getFilter();
339    
340                          RenderingHints java2DHints = oldRenderer.getJava2DHints();                                          // Check one way
341                          if (java2DHints != null) {                                          final Set<String> antiFids = antiOrigFidsFilter
342                                  newRenderer.setJava2DHints(java2DHints);                                                          .getFidsSet();
343                          }  
344                                            if (antiFids.isEmpty() && !newSelection.isEmpty())
345                                                    return true;
346    
347                                            for (String fid : antiFids) {
348                                                    if (newSelection.contains(fid)) {
349                                                            return true;
350                                                    }
351                                            }
352    
353                                            // Check the other way
354                                            for (String fid : newSelection) {
355                                                    if (antiFids.contains(fid)) {
356                                                            return true;
357                                                    }
358                                            }
359    
360                                    } else {
361                                            FidFilterImpl origFidsFilter = (FidFilterImpl) filter;
362    
363                                            // Check one way
364                                            final Set<String> fids = origFidsFilter.getFidsSet();
365    
366                          oldRenderer.setContext(null);                                          if (fids.isEmpty() && newSelection.isEmpty())
367                          oldRenderer = null;                                                  return false;
368                                            if (fids.size() != newSelection.size())
369                                                    return true;
370    
371                                            for (String fid : fids) {
372                                                    if (!newSelection.contains(fid)) {
373                                                            return true;
374                                                    }
375                                            }
376    
377                                            // Check the other way
378                                            for (String fid : newSelection) {
379                                                    if (!fids.contains(fid)) {
380                                                            return true;
381                                                    }
382                                            }
383    
384                                    }
385    
386                                    break;
387                            }
388                  }                  }
389    
390                    if (!SELECTION_STYLING_foundInMapStyle && !newSelection.isEmpty())
391                            return true;
392    
393                    return false;
394            }
395    
396            /**
397             * Replaces the local renderer
398             */
399            private void replaceRenderer() {
400    //              //
401    //              // // Has to be done before we apply the new Renderer
402    //              // mapLayer.setStyle(style);
403    //
404    //              GTRenderer oldRenderer = mapPane.getLocalRenderer();
405    //
406    //              /**
407    //               * Explicitly putting a new instance of LabelCacheDefault into the
408    //               * renderer instance, so JMapPane doesn't reuse the old one. This is
409    //               * very useful when changing the TextSymbolizer with AtlasStyler<br/>
410    //               * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not
411    //               * reuse the old Renderer - better to create a new one!
412    //               */
413    //              final GTRenderer newRenderer = GTUtil.createGTRenderer();
414    //
415    //              final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;
416    //              rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,
417    //                              new LabelCacheImpl());
418    //
419    //              newRenderer.setRendererHints(rendererHints);
420    //              mapPane.setLocalRenderer(newRenderer);
421    //
422    //              if (oldRenderer != null) {
423    //
424    //                      RenderingHints java2DHints = oldRenderer.getJava2DHints();
425    //                      if (java2DHints != null) {
426    //                              newRenderer.setJava2DHints(java2DHints);
427    //                      }
428    //
429    //                      oldRenderer.setContext(null);
430    //                      oldRenderer = null;
431    //              }
432    
433                  mapPane.refresh();                  mapPane.refresh();
434    
435          }          }
# Line 354  public class FeatureMapLayerSelectionSyn Line 439  public class FeatureMapLayerSelectionSyn
439           * {@link StyledFeatureLayerSelectionModel}           * {@link StyledFeatureLayerSelectionModel}
440           */           */
441          @Override          @Override
442          public void performMapPaneEvent(JMapPaneEvent e) {          public void performMapPaneEvent(MapPaneEvent e) {
443    
444                  // 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.
445                  if (!isEnabled() || selectionChangeCausedByMe)                  if (!isEnabled() || selectionChangeCausedByMe)
# Line 381  public class FeatureMapLayerSelectionSyn Line 466  public class FeatureMapLayerSelectionSyn
466                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
467                          return;                          return;
468    
                 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;  
   
469                  /**                  /**
470                   * Checking, that the FeatureSelectedEvent actually contains features                   * Checking, that the FeatureSelectedEvent actually contains features
471                   * from this layer                   * from this layer
472                   */                   */
473                    FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
474                  final String sourceID = fse.getSourceLayer().getTitle();                  final String sourceID = fse.getSourceLayer().getTitle();
475                  final String syncForID = mapLayer.getTitle();                  final String syncForID = mapLayer.getTitle();
476                  if (sourceID != null && syncForID != null                  if (sourceID != null && syncForID != null

Legend:
Removed from v.491  
changed lines
  Added in v.509

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26