001: package com.vividsolutions.jump.workbench.ui.renderer.style;
002:
003: import com.vividsolutions.jts.util.Assert;
004: import com.vividsolutions.jump.util.Blackboard;
005:
006: import java.awt.*;
007: import java.awt.geom.AffineTransform;
008: import java.awt.geom.Rectangle2D;
009: import java.awt.image.BufferedImage;
010: import java.awt.image.ColorModel;
011:
012: import java.util.Iterator;
013:
014: public abstract class BasicFillPattern implements Paint, Cloneable {
015: public static final String COLOR_KEY = "COLOR";
016:
017: //Lazily initialize paint -- it may be too slow to create paint for all fill patterns
018: //at once. [Jon Aquino]
019: private Paint paint;
020: private Blackboard properties;
021:
022: /**
023: * Parameterless constructor for Java2XML
024: */
025: public BasicFillPattern() {
026: }
027:
028: public BasicFillPattern(Blackboard properties) {
029: setProperties(properties);
030: }
031:
032: public boolean equals(Object obj) {
033: if (obj == null) {
034: return false;
035: }
036:
037: //Allow checking for equality. Otherwise, lose selected combo box value
038: //on re-entering Change Styles dialog. [Jon Aquino]
039: if (getClass() != obj.getClass()) {
040: return false;
041: }
042:
043: BasicFillPattern other = (BasicFillPattern) obj;
044:
045: if (getProperties().getProperties().size() != other
046: .getProperties().getProperties().size()) {
047: return false;
048: }
049:
050: for (Iterator i = getProperties().getProperties().keySet()
051: .iterator(); i.hasNext();) {
052: String key = (String) i.next();
053:
054: if (other.getProperties().getProperties().get(key) == null) {
055: return false;
056: }
057:
058: if (!getProperties().getProperties().get(key).equals(
059: other.getProperties().getProperties().get(key))) {
060: return false;
061: }
062: }
063:
064: return true;
065: }
066:
067: private Paint getPaint() {
068: if (paint == null) {
069: BufferedImage image = createImage(properties);
070: paint = new TexturePaint(image, new Rectangle2D.Double(0,
071: 0, image.getWidth(), image.getHeight()));
072: }
073:
074: return paint;
075: }
076:
077: public Blackboard getProperties() {
078: return properties;
079: }
080:
081: public BasicFillPattern setProperties(Blackboard properties) {
082: this .properties = properties;
083: paint = null;
084:
085: return this ;
086: }
087:
088: public abstract BufferedImage createImage(Blackboard properties);
089:
090: public PaintContext createContext(ColorModel cm,
091: Rectangle deviceBounds, Rectangle2D userBounds,
092: AffineTransform xform, RenderingHints hints) {
093: return getPaint().createContext(cm, deviceBounds, userBounds,
094: xform, hints);
095: }
096:
097: public int getTransparency() {
098: return getPaint().getTransparency();
099: }
100:
101: public BasicFillPattern setColor(Color color) {
102: setProperties(getProperties().put(COLOR_KEY, color));
103:
104: return this ;
105: }
106:
107: public Object clone() {
108: try {
109: return ((BasicFillPattern) getClass().newInstance())
110: .setProperties((Blackboard) (properties.clone()));
111: } catch (InstantiationException e) {
112: Assert.shouldNeverReachHere();
113: } catch (IllegalAccessException e) {
114: Assert.shouldNeverReachHere();
115: }
116: return null;
117: }
118: }
|