001: package com.vividsolutions.jump.workbench.ui.renderer;
002:
003: //[sstein] : 30.07.2005 added variable maxFeatures with getters and setters
004:
005: import java.awt.Graphics2D;
006: import java.util.Collection;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
011:
012: public abstract class FeatureCollectionRenderer implements Renderer {
013:
014: private int maxFeatures = 100;
015:
016: private ImageCachingFeatureCollectionRenderer imageCachingFeatureCollectionRenderer;
017:
018: private SimpleFeatureCollectionRenderer simpleFeatureCollectionRenderer;
019:
020: private Renderer currentFeatureCollectionRenderer;
021:
022: public FeatureCollectionRenderer(Object contentID,
023: LayerViewPanel panel) {
024: this (contentID, panel,
025: new ImageCachingFeatureCollectionRenderer(contentID,
026: panel));
027: }
028:
029: public FeatureCollectionRenderer(
030: Object contentID,
031: LayerViewPanel panel,
032: ImageCachingFeatureCollectionRenderer imageCachingFeatureCollectionRenderer) {
033: this .imageCachingFeatureCollectionRenderer = imageCachingFeatureCollectionRenderer;
034: simpleFeatureCollectionRenderer = new SimpleFeatureCollectionRenderer(
035: contentID, panel);
036: currentFeatureCollectionRenderer = simpleFeatureCollectionRenderer;
037: }
038:
039: public void clearImageCache() {
040: imageCachingFeatureCollectionRenderer.clearImageCache();
041: simpleFeatureCollectionRenderer.clearImageCache();
042: }
043:
044: public boolean isRendering() {
045: return currentFeatureCollectionRenderer.isRendering();
046: }
047:
048: public Object getContentID() {
049: return currentFeatureCollectionRenderer.getContentID();
050: }
051:
052: public void copyTo(Graphics2D graphics) {
053: currentFeatureCollectionRenderer.copyTo(graphics);
054: }
055:
056: public Runnable createRunnable() {
057: Map layerToFeaturesMap = layerToFeaturesMap();
058: Collection styles = styles();
059: imageCachingFeatureCollectionRenderer
060: .setLayerToFeaturesMap(layerToFeaturesMap);
061: imageCachingFeatureCollectionRenderer.setStyles(styles);
062: simpleFeatureCollectionRenderer
063: .setLayerToFeaturesMap(layerToFeaturesMap);
064: simpleFeatureCollectionRenderer.setStyles(styles);
065: currentFeatureCollectionRenderer = useImageCaching(layerToFeaturesMap) ? (Renderer) imageCachingFeatureCollectionRenderer
066: : simpleFeatureCollectionRenderer;
067: return currentFeatureCollectionRenderer.createRunnable();
068: }
069:
070: protected boolean useImageCaching(Map layerToFeaturesMap) {
071: return featureCount(layerToFeaturesMap) >= this .maxFeatures;
072: }
073:
074: private int featureCount(Map layerToFeaturesMap) {
075: int count = 0;
076: for (Iterator i = layerToFeaturesMap.values().iterator(); i
077: .hasNext();) {
078: Collection features = (Collection) i.next();
079: count += features.size();
080: }
081:
082: return count;
083: }
084:
085: protected abstract Map layerToFeaturesMap();
086:
087: protected abstract Collection styles();
088:
089: public void cancel() {
090: imageCachingFeatureCollectionRenderer.cancel();
091: simpleFeatureCollectionRenderer.cancel();
092: }
093:
094: /**
095: * @return Returns the number of maxFeatures to render
096: * as vector graphic.
097: */
098: public int getMaxFeatures() {
099: return maxFeatures;
100: }
101:
102: /**
103: * @param maxFeatures The maximum number of Features to render
104: * as vector graphic.
105: */
106: public void setMaxFeatures(int maxFeatures) {
107: this .maxFeatures = maxFeatures;
108: }
109:
110: /**
111: * @return Returns the simpleFeatureCollectionRenderer.
112: */
113: public SimpleFeatureCollectionRenderer getSimpleFeatureCollectionRenderer() {
114: return simpleFeatureCollectionRenderer;
115: }
116:
117: }
|