001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.styling;
018:
019: import org.geotools.event.AbstractGTComponent;
020: import org.geotools.event.GTList;
021: import org.geotools.resources.Utilities;
022: import org.opengis.util.Cloneable;
023:
024: // J2SE dependencies
025: import java.util.Arrays;
026: import java.util.List;
027: import java.util.logging.Level;
028: import java.util.logging.LogRecord;
029: import java.util.logging.Logger;
030:
031: /**
032: * DOCUMENT ME!
033: *
034: * @author James Macgill, CCG
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/styling/StyleImpl.java $
036: * @version $Id: StyleImpl.java 27862 2007-11-12 19:51:19Z desruisseaux $
037: */
038: public class StyleImpl extends AbstractGTComponent implements
039: org.geotools.styling.Style, Cloneable {
040: /** The logger for the default core module. */
041: private static final Logger LOGGER = org.geotools.util.logging.Logging
042: .getLogger("org.geotools.styling");
043: private List featureTypeStyles = new GTList(this ,
044: "featureTypeStyles");
045: private String abstractText = "";
046: private String name = "Default Styler";
047: private String title = "Default Styler";
048: private boolean defaultB = false;
049:
050: /**
051: * Creates a new instance of StyleImpl
052: */
053: protected StyleImpl() {
054: }
055:
056: public String getAbstract() {
057: return abstractText;
058: }
059:
060: public FeatureTypeStyle[] getFeatureTypeStyles() {
061: FeatureTypeStyle[] ret = new FeatureTypeStyleImpl[] { new FeatureTypeStyleImpl() };
062:
063: if ((featureTypeStyles != null)
064: && (featureTypeStyles.size() != 0)) {
065: if (LOGGER.isLoggable(Level.FINE))
066: LOGGER.fine("number of fts set "
067: + featureTypeStyles.size());
068:
069: ret = (FeatureTypeStyle[]) featureTypeStyles
070: .toArray(new FeatureTypeStyle[] {
071:
072: });
073: }
074:
075: return ret;
076: }
077:
078: public void setFeatureTypeStyles(FeatureTypeStyle[] styles) {
079: List newStyles = Arrays.asList(styles);
080:
081: this .featureTypeStyles.clear();
082: this .featureTypeStyles.addAll(newStyles);
083:
084: LOGGER.fine("StyleImpl added " + featureTypeStyles.size()
085: + " feature types");
086: }
087:
088: public void addFeatureTypeStyle(FeatureTypeStyle type) {
089: featureTypeStyles.add(type);
090: }
091:
092: public String getName() {
093: return name;
094: }
095:
096: public String getTitle() {
097: return title;
098: }
099:
100: public boolean isDefault() {
101: return defaultB;
102: }
103:
104: public void setAbstract(String abstractStr) {
105: abstractText = abstractStr;
106: fireChanged();
107: }
108:
109: public void setDefault(boolean isDefault) {
110: defaultB = isDefault;
111: fireChanged();
112: }
113:
114: public void setName(String name) {
115: this .name = name;
116: fireChanged();
117: }
118:
119: public void setTitle(String title) {
120: this .title = title;
121: fireChanged();
122: }
123:
124: public void accept(StyleVisitor visitor) {
125: visitor.visit(this );
126: }
127:
128: /**
129: * Clones the Style. Creates deep copy clone of the style.
130: *
131: * @return the Clone of the style.
132: *
133: * @throws RuntimeException DOCUMENT ME!
134: *
135: * @see org.geotools.styling.Style#clone()
136: */
137: public Object clone() {
138: Style clone;
139:
140: try {
141: clone = (Style) super .clone();
142: } catch (CloneNotSupportedException e) {
143: throw new RuntimeException(e); // this should never happen since we implement Cloneable
144: }
145:
146: FeatureTypeStyle[] ftsArray = new FeatureTypeStyle[featureTypeStyles
147: .size()];
148:
149: for (int i = 0; i < ftsArray.length; i++) {
150: FeatureTypeStyle fts = (FeatureTypeStyle) featureTypeStyles
151: .get(i);
152: ftsArray[i] = (FeatureTypeStyle) ((Cloneable) fts).clone();
153: }
154:
155: clone.setFeatureTypeStyles(ftsArray);
156:
157: return clone;
158: }
159:
160: /**
161: * Overrides hashcode.
162: *
163: * @return The hash code.
164: */
165: public int hashCode() {
166: final int PRIME = 1000003;
167: int result = 0;
168:
169: if (featureTypeStyles != null) {
170: result = (PRIME * result) + featureTypeStyles.hashCode();
171: }
172:
173: if (abstractText != null) {
174: result = (PRIME * result) + abstractText.hashCode();
175: }
176:
177: if (name != null) {
178: result = (PRIME * result) + name.hashCode();
179: }
180:
181: if (title != null) {
182: result = (PRIME * result) + title.hashCode();
183: }
184:
185: result = (PRIME * result) + (defaultB ? 1 : 0);
186:
187: return result;
188: }
189:
190: /**
191: * Compares this Style with another.
192: *
193: * <p>
194: * Two StyleImpl are equal if they have the same properties and the same
195: * list of FeatureTypeStyles.
196: * </p>
197: *
198: * @param oth The object to compare with this for equality.
199: *
200: * @return True if this and oth are equal.
201: */
202: public boolean equals(Object oth) {
203: if (this == oth) {
204: return true;
205: }
206:
207: if (oth instanceof StyleImpl) {
208: StyleImpl other = (StyleImpl) oth;
209:
210: return Utilities.equals(name, other.name)
211: && Utilities.equals(title, other.title)
212: && Utilities.equals(abstractText,
213: other.abstractText)
214: && Utilities.equals(featureTypeStyles,
215: other.featureTypeStyles);
216: }
217:
218: return false;
219: }
220:
221: public String toString() {
222: StringBuffer buf = new StringBuffer();
223: buf.append("StyleImpl<");
224: buf.append(notification);
225: buf.append(">");
226: buf.append("[");
227: if (name != null) {
228: buf.append(" name=");
229: buf.append(name);
230: } else {
231: buf.append(" UNNAMED");
232: }
233: if (defaultB) {
234: buf.append(", DEFAULT");
235: }
236: // if( title != null && title.length() != 0 ){
237: // buf.append(", title=");
238: // buf.append( title );
239: // }
240: buf.append("]");
241: return buf.toString();
242: }
243: }
|