Revision 45518

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.lib/org.gvsig.symbology.lib.impl/src/main/java/org/gvsig/symbology/fmap/mapcontext/rendering/symbol/marker/impl/AbstractMarkerSymbol.java
53 53

  
54 54
/**
55 55
 * Abstract class that any MARKER SYMBOL should extend.
56
 * @author 2005-2008 jaume dominguez faus - jaume.dominguez@iver.es
57
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
56
 *
57
 * @author gvSIG team
58 58
 */
59 59
public abstract class AbstractMarkerSymbol extends AbstractSymbol implements IMarkerSymbol {
60
    
61
    public static final String MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MarkerSymbol";    
62
    
60

  
61
    public static final String MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME = "MarkerSymbol";
62

  
63 63
    private static final String FIELD_COLOR = "color";
64

  
65 64
    private static final String FIELD_ROTATION = "rotation";
66

  
67 65
    private static final String FIELD_OFFSET = "offset";
68

  
69 66
    private static final String FIELD_SIZE = "size";
70
    
71 67
    private static final String FIELD_MASK = "mask";
72
    
73
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
	private Color color;
75
	private double rotation;
76
	private Point2D offset = new Point2D.Double();
77
	private double size = 4d;
78
	private IMask mask;
79 68

  
80
	public AbstractMarkerSymbol() {
81
		super();
82
		color =
83
				MapContextLocator.getSymbolManager()
84
						.getSymbolPreferences()
85
						.getDefaultSymbolFillColor();
86
	}
69
    private static final GeometryManager GEOMETRY_MANAGER = GeometryLocator.getGeometryManager();
70
    private Color color;
71
    private double rotation;
72
    private Point2D offset = new Point2D.Double();
73
    private double size = 4d;
74
    private IMask mask;
87 75

  
88
	public final int getSymbolType() {
89
		return Geometry.TYPES.POINT;
90
	}
76
    public AbstractMarkerSymbol() {
77
        super();
78
        color = MapContextLocator.getSymbolManager()
79
                .getSymbolPreferences()
80
                .getDefaultSymbolFillColor();
81
    }
91 82

  
92
	public double getRotation() {
93
		return rotation;
94
	}
83
    @Override
84
    public final int getSymbolType() {
85
        return Geometry.TYPES.POINT;
86
    }
95 87

  
96
	public void setRotation(double r) {
97
		this.rotation = r;
98
	}
88
    @Override
89
    public double getRotation() {
90
        return rotation;
91
    }
99 92

  
100
	public Point2D getOffset() {
101
		if (offset == null) {
102
			offset = new Point();
103
		}
104
		return offset;
105
	}
93
    @Override
94
    public void setRotation(double r) {
95
        this.rotation = r;
96
    }
106 97

  
107
	public void setOffset(Point2D offset) {
108
		this.offset = offset;
109
	}
98
    @Override
99
    public Point2D getOffset() {
100
        if (offset == null) {
101
            offset = new Point();
102
        }
103
        return offset;
104
    }
110 105

  
111
	public boolean isSuitableFor(Geometry geom) {
112
		return geom.getType() == Geometry.TYPES.POINT;
113
	}
106
    @Override
107
    public void setOffset(Point2D offset) {
108
        this.offset = offset;
109
    }
114 110

  
115
	public int getOnePointRgb() {
116
		return color.getRGB();
117
	}
111
    @Override
112
    public boolean isSuitableFor(Geometry geom) {
113
        return geom.getType() == Geometry.TYPES.POINT;
114
    }
118 115

  
119
	public double getSize() {
120
		return size;
121
	}
116
    @Override
117
    public int getOnePointRgb() {
118
        return color.getRGB();
119
    }
122 120

  
123
	public void setSize(double size) {
124
		this.size = size;
125
	}
121
    @Override
122
    public double getSize() {
123
        return size;
124
    }
126 125

  
127 126
    @Override
128
	public Color getColor() {
129
		return color;
130
	}
127
    public void setSize(double size) {
128
        this.size = size;
129
    }
131 130

  
132
	public void setColor(Color color) {
133
		this.color = color;
134
	}
131
    @Override
132
    public Color getColor() {
133
        return color;
134
    }
135 135

  
136
	public void setAlpha(int outlineAlpha) {
137
		Color color = getColor();
138
		setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(),
139
				outlineAlpha));
140
	}
136
    @Override
137
    public void setColor(Color color) {
138
        this.color = color;
139
    }
141 140

  
142
	public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
143
		float cs = (float) getCartographicSize(viewPort, dpi, geom);
144
		distances[0] = cs;
145
		distances[1] = cs;
146
	}
141
    @Override
142
    public void setAlpha(int outlineAlpha) {
143
        Color theColor = getColor();
144
        setColor(new Color(theColor.getRed(), theColor.getGreen(), theColor.getBlue(),
145
                outlineAlpha));
146
    }
147 147

  
148
    @Override
149
    public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
150
        float cs = (float) getCartographicSize(viewPort, dpi, geom);
151
        distances[0] = cs;
152
        distances[1] = cs;
153
    }
148 154

  
149
	public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
150
		double originalSize = getSize();
151
		double size=originalSize;
152
		// scale it to size
153
		int pq = properties.getPrintQuality();
154
		if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
155
			size *= (double) 300/72;
156
		}else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
157
			size *= (double) 600/72;
158
		}else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
159
			// size *= 72/72; // (which is the same than doing nothing)
160
		}
161
//		setSize(size);
162
		draw(g,at,geom, null, null);
155
    @Override
156
    public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
157
        double originalSize = getSize();
158
        double theSize = originalSize;
159
        // scale it to size
160
        int pq = properties.getPrintQuality();
161
        switch (pq) {
162
            case PrintAttributes.PRINT_QUALITY_NORMAL:
163
                theSize *= (double) 300 / 72;
164
                break;
165
            case PrintAttributes.PRINT_QUALITY_HIGH:
166
                theSize *= (double) 600 / 72;
167
                break;
168
            case PrintAttributes.PRINT_QUALITY_DRAFT:
169
                // size *= 72/72; // (which is the same than doing nothing)
170
                break;
171
            default:
172
                break;
173
        }
174
//		setSize(theSize);
175
        draw(g, at, geom, null, null);
163 176
//		setSize(originalSize);
164
	}
177
    }
165 178

  
166
	public final IMask getMask() {
167
		return mask;
168
	}
179
    @Override
180
    public final IMask getMask() {
181
        return mask;
182
    }
169 183

  
170
	public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
171
		try {
172
			if (properties==null)
173
				draw(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
174
			else{
175
				double originalSize = getSize();
176
				double size=originalSize;
177
				int pq = properties.getPrintQuality();
178
				if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
179
					size *= (double) 300/72;
180
				}else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
181
					size *= (double) 600/72;
182
				}else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
183
					// d *= 72/72; // (which is the same than doing nothing)
184
				}
185
				setSize(size);
186
				print(g, scaleInstance, geomManager.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
187
				setSize(originalSize);
188
			}
184
    @Override
185
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
186
        try {
187
            if (properties == null) {
188
                draw(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), null, null);
189
            } else {
190
                double originalSize = getSize();
191
                double theSize = originalSize;
192
                int pq = properties.getPrintQuality();
193
                switch (pq) {
194
                    case PrintAttributes.PRINT_QUALITY_NORMAL:
195
                        theSize *= (double) 300 / 72;
196
                        break;
197
                    case PrintAttributes.PRINT_QUALITY_HIGH:
198
                        theSize *= (double) 600 / 72;
199
                        break;
200
                    case PrintAttributes.PRINT_QUALITY_DRAFT:
201
                        // d *= 72/72; // (which is the same than doing nothing)
202
                        break;
203
                    default:
204
                        break;
205
                }
206
                setSize(theSize);
207
                print(g, scaleInstance, GEOMETRY_MANAGER.createPoint(r.getCenterX(), r.getCenterY(), SUBTYPES.GEOM2D), properties);
208
                setSize(originalSize);
209
            }
189 210

  
211
        } catch (CreateGeometryException e) {
212
            throw new SymbolDrawingException(TYPES.POINT);
213
        }
214
    }
190 215

  
216
    @Override
217
    public final void setMask(IMask mask) {
218
        this.mask = mask;
219
    }
191 220

  
192
		} catch (CreateGeometryException e) {
193
			throw new SymbolDrawingException(TYPES.POINT);
194
		}
195
	}
221
    @Override
222
    public void setCartographicSize(double cartographicSize, Geometry geom) {
223
        setSize(cartographicSize);
224
    }
196 225

  
197
	public final void setMask(IMask mask) {
198
		this.mask = mask;
199
	}
226
    @Override
227
    public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
228
        double oldSize = getSize();
229
        setCartographicSize(getCartographicSize(
230
                viewPort,
231
                dpi,
232
                geom),
233
                geom);
234
        return oldSize;
235
    }
200 236

  
237
    @Override
238
    public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
239
        return CartographicSupportToolkit.
240
                getCartographicLength(this,
241
                        getSize(),
242
                        viewPort,
243
                        dpi);
244
    }
201 245

  
246
    @Override
247
    public Object clone() throws CloneNotSupportedException {
248
        AbstractMarkerSymbol copy = (AbstractMarkerSymbol) super.clone();
202 249

  
203
	public void setCartographicSize(double cartographicSize, Geometry geom) {
204
		setSize(cartographicSize);
205
	}
250
        // Clone the offset
251
        if (offset != null) {
252
            copy.offset = (Point2D) offset.clone();
253
        }
206 254

  
207
	public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
208
		double oldSize = getSize();
209
		setCartographicSize(getCartographicSize(
210
								viewPort,
211
								dpi,
212
								geom),
213
							geom);
214
		return oldSize;
215
	}
255
        // clone the mask
256
        if (mask != null) {
257
            copy.mask = (IMask) mask.clone();
258
        }
259
        return copy;
260
    }
216 261

  
217
	public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
218
		return CartographicSupportToolkit.
219
					getCartographicLength(this,
220
										  getSize(),
221
										  viewPort,
222
										  dpi);
223
	}
224
	
225
	public Object clone() throws CloneNotSupportedException {
226
		AbstractMarkerSymbol copy  = (AbstractMarkerSymbol) super.clone();
227
		
228
		// Clone the offset
229
		if (offset != null) {
230
			copy.offset = (Point2D) offset.clone();
231
		}
232
		
233
		// clone the mask
234
		if (mask != null) {
235
			copy.mask = (IMask) mask.clone();
236
		}
237
		return copy;
238
	}
239
	
240
	public void loadFromState(PersistentState state)
241
			throws PersistenceException {
242
		// Set parent symbol properties
243
		super.loadFromState(state);
244
		// Set own properties
245
		setColor((Color) state.get(FIELD_COLOR));
246
		setMask((IMask) state.get(FIELD_MASK));
247
		setOffset((Point2D) state.get(FIELD_OFFSET));
248
		setRotation(state.getDouble(FIELD_ROTATION));
249
		setSize(state.getDouble(FIELD_SIZE));
250
	}
262
    @Override
263
    public void loadFromState(PersistentState state)
264
            throws PersistenceException {
265
        // Set parent symbol properties
266
        super.loadFromState(state);
267
        // Set own properties
268
        setColor((Color) state.get(FIELD_COLOR));
269
        setMask((IMask) state.get(FIELD_MASK));
270
        setOffset((Point2D) state.get(FIELD_OFFSET));
271
        setRotation(state.getDouble(FIELD_ROTATION));
272
        setSize(state.getDouble(FIELD_SIZE));
273
    }
251 274

  
252
	public void saveToState(PersistentState state) throws PersistenceException {
253
		// Save parent symbol properties
254
		super.saveToState(state);
255
		// Save own properties
256
		state.set(FIELD_COLOR, getColor());
257
		state.set(FIELD_MASK, getMask());
258
		state.set(FIELD_OFFSET, getOffset());
259
		state.set(FIELD_ROTATION, getRotation());
260
		state.set(FIELD_SIZE, getSize());
261
	}
275
    @Override
276
    public void saveToState(PersistentState state) throws PersistenceException {
277
        // Save parent symbol properties
278
        super.saveToState(state);
279
        // Save own properties
280
        state.set(FIELD_COLOR, getColor());
281
        state.set(FIELD_MASK, getMask());
282
        state.set(FIELD_OFFSET, getOffset());
283
        state.set(FIELD_ROTATION, getRotation());
284
        state.set(FIELD_SIZE, getSize());
285
    }
262 286

  
263
	public static class RegisterPersistence implements Callable {
287
    public static class RegisterPersistence implements Callable {
264 288

  
265
		public Object call() throws Exception {
266
			PersistenceManager manager = ToolsLocator.getPersistenceManager();
267
			if( manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
268
				DynStruct definition = manager.addDefinition(
269
						AbstractMarkerSymbol.class,
270
						MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
271
						MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
272
						null, 
273
						null
274
				);
289
        @Override
290
        public Object call() throws Exception {
291
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
292
            if (manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
293
                DynStruct definition = manager.addDefinition(
294
                        AbstractMarkerSymbol.class,
295
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
296
                        MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
297
                        null,
298
                        null
299
                );
275 300

  
276
				// Extend the Symbol base definition
277
				definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
301
                // Extend the Symbol base definition
302
                definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME));
278 303

  
279
				// Color
280
				definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
281
				// Mask
282
				definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
304
                // Color
305
                definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class);
306
                // Mask
307
                definition.addDynFieldObject(FIELD_MASK).setMandatory(false).setClassOfValue(IMask.class);
283 308

  
284
				// Offset
285
				definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
309
                // Offset
310
                definition.addDynFieldObject(FIELD_OFFSET).setMandatory(false).setClassOfValue(Point2D.class);
286 311

  
287
				// Rotation
288
				definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
312
                // Rotation
313
                definition.addDynFieldDouble(FIELD_ROTATION).setMandatory(true);
289 314

  
290
				// Size
291
				definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
292
			}
293
			return Boolean.TRUE;
294
		}
295
		
296
	}
315
                // Size
316
                definition.addDynFieldDouble(FIELD_SIZE).setMandatory(true);
317
            }
318
            return true;
319
        }
297 320

  
321
    }
322

  
298 323
}

Also available in: Unified diff