001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on April 6, 2004, 4:11 PM
017: */
018:
019: package org.geotools.renderer.lite;
020:
021: import java.util.Vector;
022:
023: import org.geotools.filter.Expression;
024: import org.geotools.filter.FilterFactory;
025: import org.geotools.filter.FilterFactoryFinder;
026:
027: /**
028: *
029: * @author jfc173
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/lite/GlyphPropertiesList.java $
031: */
032: public class GlyphPropertiesList {
033:
034: private Vector list = new Vector();
035: private Vector names = new Vector();
036: private FilterFactory factory = FilterFactoryFinder
037: .createFilterFactory();
038:
039: /** Creates a new instance of GlyphPropertiesList */
040: public GlyphPropertiesList() {
041: }
042:
043: public void addProperty(String name, Class type, Object value) {
044: if (type.isAssignableFrom(value.getClass())) {
045: list.add(new GlyphProperty(name, type, value));
046: names.add(name);
047: } else {
048: throw new RuntimeException(
049: "Wrong class for setting variable " + name
050: + ". Expected a " + type
051: + " but received a " + value.getClass()
052: + ".");
053: }
054: }
055:
056: /**
057: * the index i starts counting at 0, not 1. A list with two properties has property 0 and property 1.
058: */
059: public String getPropertyName(int i) {
060: return ((GlyphProperty) list.get(i)).getName();
061: }
062:
063: public int getPropertyIndex(String name) {
064: return names.indexOf(name);
065: }
066:
067: /**
068: * the index i starts counting at 0, not 1. A list with two properties has property 0 and property 1.
069: */
070: public Class getPropertyType(int i) {
071: return ((GlyphProperty) list.get(i)).getType();
072: }
073:
074: public Class getPropertyType(String name) {
075: int index = names.indexOf(name);
076: if (index != -1) {
077: return getPropertyType(index);
078: } else {
079: throw new RuntimeException(
080: "Tried to get the class of a non-existent property: "
081: + name);
082: }
083: }
084:
085: public boolean hasProperty(String name) {
086: return names.contains(name);
087: }
088:
089: /**
090: * the index i starts counting at 0, not 1. A list with two properties has property 0 and property 1.
091: */
092: public Object getPropertyValue(int i) {
093: return ((GlyphProperty) list.get(i)).getValue();
094: }
095:
096: public Object getPropertyValue(String name) {
097: int index = names.indexOf(name);
098: if (index != -1) {
099: return getPropertyValue(index);
100: } else {
101: throw new RuntimeException(
102: "Tried to get the class of a non-existent property: "
103: + name);
104: }
105: }
106:
107: private Expression stringToLiteral(String s) {
108: return factory.createLiteralExpression(s);
109: }
110:
111: private Expression numberToLiteral(Double d) {
112: return factory.createLiteralExpression(d.doubleValue());
113: }
114:
115: private Expression numberToLiteral(Integer i) {
116: return factory.createLiteralExpression(i.intValue());
117: }
118:
119: public void setPropertyValue(String name, int value) {
120: setPropertyValue(name, new Integer(value));
121: }
122:
123: public void setPropertyValue(String name, double value) {
124: setPropertyValue(name, new Double(value));
125: }
126:
127: public void setPropertyValue(String name, Object value) {
128: int index = names.indexOf(name);
129: if (index != -1) {
130: GlyphProperty prop = (GlyphProperty) list.get(index);
131: if (value instanceof String) {
132: value = stringToLiteral((String) value);
133: }
134: if (value instanceof Integer) {
135: value = numberToLiteral((Integer) value);
136: }
137: if (value instanceof Double) {
138: value = numberToLiteral((Double) value);
139: }
140: if (prop.getType().isAssignableFrom(value.getClass())) {
141: prop.setValue(value);
142: } else {
143: throw new RuntimeException(
144: "Wrong class for setting variable " + name
145: + ". Expected a " + prop.getType()
146: + " but received a " + value.getClass()
147: + ".");
148: }
149: } else {
150: throw new RuntimeException(
151: "Tried to set the value of a non-existent property: "
152: + name);
153: }
154: }
155: }
|