/[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 419 by alfonx, Wed Sep 30 15:36:39 2009 UTC revision 492 by alfonx, Fri Oct 23 13:49:49 2009 UTC
# Line 45  package skrueger.geotools.selection; Line 45  package skrueger.geotools.selection;
45  import java.awt.RenderingHints;  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.io.File;
49  import java.util.HashMap;  import java.util.HashMap;
50  import java.util.HashSet;  import java.util.HashSet;
51  import java.util.Iterator;  import java.util.Iterator;
# Line 55  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.FeatureCollection;
60    import org.geotools.feature.FeatureIterator;
61  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
62  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
63  import org.geotools.renderer.label.LabelCacheImpl;  import org.geotools.renderer.label.LabelCacheImpl;
64  import org.geotools.renderer.lite.StreamingRenderer;  import org.geotools.renderer.lite.StreamingRenderer;
 import org.geotools.renderer.shape.ShapefileRenderer;  
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;  import org.geotools.styling.visitor.DuplicatingStyleVisitor;
68  import org.opengis.feature.simple.SimpleFeature;  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;
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 102  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;          private final HashMap<Object, Object> defaultGTRenderingHints;
# Line 119  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, HashMap<Object, Object> defaultGTRenderingHints) {                          JMapPane mapPane, MapPaneToolBar toolBar,
129                            HashMap<Object, Object> defaultGTRenderingHints) {
130    
131                  super(layerSelModel);                  super(layerSelModel);
132                  this.styledLayer = styledLayer;                  this.styledLayer = styledLayer;
# Line 130  public class FeatureMapLayerSelectionSyn Line 136  public class FeatureMapLayerSelectionSyn
136                  this.toolBar = toolBar;                  this.toolBar = toolBar;
137                  if (defaultGTRenderingHints != null)                  if (defaultGTRenderingHints != null)
138                          this.defaultGTRenderingHints = defaultGTRenderingHints;                          this.defaultGTRenderingHints = defaultGTRenderingHints;
139                  else                  else
140                          this.defaultGTRenderingHints = new HashMap<Object, Object>();                          this.defaultGTRenderingHints = new HashMap<Object, Object>();
141          }          }
142    
# Line 181  public class FeatureMapLayerSelectionSyn Line 187  public class FeatureMapLayerSelectionSyn
187           */           */
188          private void changeLayerStyle(final Vector<String> newSelection) {          private void changeLayerStyle(final Vector<String> newSelection) {
189                  try {                  try {
 //                      GTRenderer r = mapPane.getRenderer();  
 //                      if (r instanceof ShapefileRenderer) {  
 //                              ShapefileRenderer sfr = (ShapefileRenderer)r;  
 //                              sfr.setCaching(false);  
 //                      }  
 //                        
                           
190    
                           
191                          Style originalStyle = mapLayer.getStyle();                          Style originalStyle = mapLayer.getStyle();
192                          if (newSelection.isEmpty()) {                          if (newSelection.isEmpty()) {
193    
194                                  // Check if the Style contains a SELECTION FTS                                  // Check if the Style contains a SELECTION FTS
195                                    
196                                  FeatureTypeStyle[] clone = originalStyle.featureTypeStyles().toArray( new FeatureTypeStyle[] {} ).clone();                                  FeatureTypeStyle[] clone = originalStyle.featureTypeStyles()
197                                                                                    .toArray(new FeatureTypeStyle[] {}).clone();
198    
199                                  for (FeatureTypeStyle fts : clone) {                                  for (FeatureTypeStyle fts : clone) {
200                                          if (fts.getName() != null                                          if (fts.getName() != null
201                                                          && fts.getName().equals(SELECTION_STYLING)) {                                                          && fts.getName().equals(SELECTION_STYLING)) {
202                                                  originalStyle.featureTypeStyles().remove(fts);                                                  originalStyle.featureTypeStyles().remove(fts);
203                                                    
204                                                  replaceRenderer();                                                  replaceRenderer();
205  //                                              mapPane.refresh();                                                  // mapPane.refresh();
                                                   
206    
207                                                  return;                                                  return;
208                                          }                                          }
# Line 229  public class FeatureMapLayerSelectionSyn Line 227  public class FeatureMapLayerSelectionSyn
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                                  selectionFTStyle.rules().get(0).setFilter(                                          selectionFTStyle.rules().get(0).setFilter(
265                                                  FilterUtil.FILTER_FAC2.id(fids));                                                          FilterUtil.FILTER_FAC2.id(fids));
266                                    }
267    
268                                  // Maybe there has already been another selection                                  // Maybe there has already been another selection
269                                  // FeatureTypeStyle... Let's replace it...                                  // FeatureTypeStyle... Let's replace it...
# Line 263  public class FeatureMapLayerSelectionSyn Line 280  public class FeatureMapLayerSelectionSyn
280                                  if (!foundAndReplaced) {                                  if (!foundAndReplaced) {
281                                          originalStyle.featureTypeStyles().add(selectionFTStyle);                                          originalStyle.featureTypeStyles().add(selectionFTStyle);
282                                  }                                  }
                                   
                                   
283    
284                                  // Refresh the map                                  // Refresh the map
285  //                              mapPane.refresh();                                  // mapPane.refresh();
286                                    
287                                  DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();                                  DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
288                                  dsv.visit(originalStyle);                                  dsv.visit(originalStyle);
289                                  Style newStyle = (Style)dsv.getCopy();                                  Style newStyle = (Style) dsv.getCopy();
290                                    //
291  //                              Style newStyle = StylingUtil.STYLE_BUILDER.createStyle();  //                              try {
292  //                              newStyle.featureTypeStyles().addAll(originalStyle.featureTypeStyles());  ////                                    
293    ////                            StylingUtil.saveStyleToSLD(newStyle, new File(
294    ////                                            "/home/stefan/Desktop/selection.sld"));
295    //                              } catch (Exception e) {
296    //                              }
297    //
298    //                              // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor();
299                                    // dsv.visit(originalStyle);
300                                    // Style newStyle = originalStyle;
301    
302                                    // Style newStyle = StylingUtil.STYLE_BUILDER.createStyle();
303                                    // newStyle.featureTypeStyles().addAll(originalStyle.featureTypeStyles());
304                                  mapLayer.setStyle(newStyle);                                  mapLayer.setStyle(newStyle);
305                                    
306                                  replaceRenderer();                                  replaceRenderer();
307                          }                          }
 //                        
 //                      if (r instanceof ShapefileRenderer) {  
 //                              ShapefileRenderer sfr = (ShapefileRenderer)r;  
 //                              sfr.setCaching(true);  
 //                      }  
   
308    
309                  } catch (Exception e) {                  } catch (Exception e) {
310                          LOGGER.error("Error while trying to create a selection style", e);                          LOGGER.error("Error while trying to create a selection style", e);
# Line 292  public class FeatureMapLayerSelectionSyn Line 312  public class FeatureMapLayerSelectionSyn
312          }          }
313    
314          private void replaceRenderer() {          private void replaceRenderer() {
315  //                  //
316  //              // Has to be done before we apply the new Renderer                  // // Has to be done before we apply the new Renderer
317  //              mapLayer.setStyle(style);                  // mapLayer.setStyle(style);
318    
319                  GTRenderer oldRenderer = mapPane.getRenderer();                  GTRenderer oldRenderer = mapPane.getRenderer();
320    
# Line 305  public class FeatureMapLayerSelectionSyn Line 325  public class FeatureMapLayerSelectionSyn
325                   * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not                   * SK 9.7.09: It's not enought to user LabelCache.clear(). We can not
326                   * reuse the old Renderer - better to create a new one!                   * reuse the old Renderer - better to create a new one!
327                   */                   */
328                  final GTRenderer newRenderer = new ShapefileRenderer();                  final GTRenderer newRenderer = GTUtil.createGTRenderer();
329    
330                  final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;                  final HashMap<Object, Object> rendererHints = defaultGTRenderingHints;
331                  rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,                  rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY,
# Line 315  public class FeatureMapLayerSelectionSyn Line 335  public class FeatureMapLayerSelectionSyn
335                  mapPane.setRenderer(newRenderer);                  mapPane.setRenderer(newRenderer);
336    
337                  if (oldRenderer != null) {                  if (oldRenderer != null) {
338                            
339                          RenderingHints java2DHints = oldRenderer.getJava2DHints();                          RenderingHints java2DHints = oldRenderer.getJava2DHints();
340                          if (java2DHints != null) {                          if (java2DHints != null) {
341                                  newRenderer.setRendererHints(java2DHints);                                  newRenderer.setJava2DHints(java2DHints);
342                          }                          }
343                            
344                          oldRenderer.setContext(null);                          oldRenderer.setContext(null);
345                          oldRenderer = null;                          oldRenderer = null;
346                  }                  }
347                    
348                  mapPane.refresh();                  mapPane.refresh();
349    
350          }          }
# Line 361  public class FeatureMapLayerSelectionSyn Line 381  public class FeatureMapLayerSelectionSyn
381                  if (e.getSourceObject() != this.mapPane)                  if (e.getSourceObject() != this.mapPane)
382                          return;                          return;
383    
                 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;  
384    
385                  /**                  /**
386                   * Checking, that the FeatureSelectedEvent actually contains features                   * Checking, that the FeatureSelectedEvent actually contains features
387                   * from this layer                   * from this layer
388                   */                   */
389                    FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
390                  final String sourceID = fse.getSourceLayer().getTitle();                  final String sourceID = fse.getSourceLayer().getTitle();
391                  final String syncForID = mapLayer.getTitle();                  final String syncForID = mapLayer.getTitle();
392                  if (sourceID != null && syncForID != null                  if (sourceID != null && syncForID != null
# Line 375  public class FeatureMapLayerSelectionSyn Line 395  public class FeatureMapLayerSelectionSyn
395                          return;                          return;
396                  }                  }
397    
398                  LOGGER.debug("do event " + fse);                  // LOGGER.debug("do event " + fse);
399    
400                  // Avoid event circles in propertyChange(..)                  // Avoid event circles in propertyChange(..)
401                  selectionChangeCausedByMe = true;                  selectionChangeCausedByMe = true;
402    
403                  Iterator<SimpleFeature> fi = fse.getSelectionResult().iterator();                  final FeatureCollection<SimpleFeatureType, SimpleFeature> selectionResult = fse
404                                    .getSelectionResult();
405                    Iterator<SimpleFeature> fi = selectionResult.iterator();
406                    try {
407    
408                  // reset the selection of the DpLayerSelectionModel                          // reset the selection of the DpLayerSelectionModel
409                  // layerSelModel.setValueIsAdjusting(true);                          // layerSelModel.setValueIsAdjusting(true);
410    
411                  if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {                          if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
412                          layerSelModel.setValueIsAdjusting(true);                                  layerSelModel.setValueIsAdjusting(true);
413    
414                          for (int fIdx = 0; fi.hasNext(); fIdx++) {                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
415                                  SimpleFeature f = fi.next();                                          SimpleFeature f = fi.next();
416                                  layerSelModel.addSelection(f.getID());                                          layerSelModel.addSelection(f.getID());
417                          }                                  }
418                          layerSelModel.setValueIsAdjusting(false);                                  layerSelModel.setValueIsAdjusting(false);
419                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
420                          layerSelModel.setValueIsAdjusting(true);                                  layerSelModel.setValueIsAdjusting(true);
421                          layerSelModel.clearSelection();                                  layerSelModel.clearSelection();
422    
423                          for (int fIdx = 0; fi.hasNext(); fIdx++) {                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
424                                  SimpleFeature f = fi.next();                                          SimpleFeature f = fi.next();
425                                  layerSelModel.addSelection(f.getID());                                          layerSelModel.addSelection(f.getID());
426                          }                                  }
427    
428                          // LOGGER.debug("Setting selection to " + fi.());                                  // LOGGER.debug("Setting selection to " + fi.());
429    
430                          layerSelModel.setValueIsAdjusting(false);                                  layerSelModel.setValueIsAdjusting(false);
431                  } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {                          } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
432                          layerSelModel.setValueIsAdjusting(true);                                  layerSelModel.setValueIsAdjusting(true);
433                          for (int fIdx = 0; fi.hasNext(); fIdx++) {                                  for (int fIdx = 0; fi.hasNext(); fIdx++) {
434                                  SimpleFeature f = fi.next();                                          SimpleFeature f = fi.next();
435                                  layerSelModel.removeSelection(f.getID());                                          layerSelModel.removeSelection(f.getID());
436                                    }
437                                    layerSelModel.setValueIsAdjusting(false);
438                          }                          }
439                          layerSelModel.setValueIsAdjusting(false);  
440                    } finally {
441                            selectionResult.close(fi);
442                  }                  }
443    
444                  // 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.419  
changed lines
  Added in v.492

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26