001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.ui.palette;
010:
011: import java.awt.Color;
012: import java.io.IOException;
013: import java.net.URL;
014: import java.util.ArrayList;
015: import java.util.Arrays;
016: import java.util.EnumSet;
017: import java.util.List;
018: import java.util.Properties;
019:
020: import org.geotools.util.SimpleInternationalString;
021:
022: /**
023: * Represent palette information, with conveince methods for SLD.
024: * <p>
025: * This has been inspired by the work of ColorBrewer, indeed this is used to capture some of the
026: * information produced by that project.
027: * </p>
028: * @deprecated use org.geotools.brewer.color.BrewerPalette
029: */
030: public class Palette {
031: /** <code>name</code> field */
032: final SimpleInternationalString name;
033: /** <code>contents</code> field */
034:
035: final public List<Scheme> contents;
036:
037: /** <code>type</code> field */
038: final public SchemeType type;
039:
040: /** Friendly indicators */
041: static final int GOOD = 1;
042: static final int DOUBTFUL = 0;
043: static final int BAD = -1;
044:
045: /**
046: * Construct <code>Palette</code>.
047: */
048: public Palette(String name, Scheme list[]) {
049: this .name = new SimpleInternationalString(name);
050: this .contents = Arrays.asList(list);
051: this .type = SchemeType.QUALITATIVE;
052: }
053:
054: /**
055: * Construct <code>Palette</code>.
056: *
057: * @param bundle
058: * @throws IOException
059: */
060: public Palette(String pal) throws IOException {
061: this (load(pal));
062: }
063:
064: static final Properties load(String pal) throws IOException {
065: URL find = Palette.class.getResource(pal);
066: Properties load = new Properties();
067: load.load(find.openStream());
068:
069: return load;
070: }
071:
072: /**
073: * Construct list from properties
074: * <p>
075: * Parse in a Palette from a properties file:<pre><code>
076: * palette.key=example
077: * example.display=Example
078: * palette.min=2
079: * palette.max=3
080: * palette.type=SEQUENTIAL
081: * example2={158, 202, 225},{ 49, 130, 189}
082: * example2.good=colorBlind, photoCopy, projector, lcd, crt, print
083: * example2.doubtful=projector
084: * example3={222, 235, 247},{158, 202, 225},{ 49, 130, 189}
085: * example3.good=lcd, crt
086: * </code></pre>
087: * </p>
088: * @param properties
089: */
090: public Palette(Properties properties) {
091: String KEY = properties.getProperty("palette.key"); //$NON-NLS-1$
092: name = new SimpleInternationalString(properties
093: .getProperty("palette.display")); //$NON-NLS-1$
094:
095: type = Enum.valueOf(SchemeType.class, properties.getProperty(
096: "palette.type").trim()); //$NON-NLS-1$
097:
098: int min = Integer.parseInt(properties
099: .getProperty("palette.min")); //$NON-NLS-1$
100: int max = Integer.parseInt(properties
101: .getProperty("palette.max")); //$NON-NLS-1$
102: contents = new ArrayList<Scheme>();
103:
104: for (int i = min; i <= max; i++) {
105: //String key = KEY+i;
106: String defn = properties.getProperty(KEY + i);
107: // ie. {158, 202, 225},{ 49, 130, 189}
108: List<Color> colours = new ArrayList<Color>();
109: //StringBuffer hack = new StringBuffer( defn );
110: defn = defn.replace("},", "|"); //$NON-NLS-1$ //$NON-NLS-2$
111: defn = defn.replace('}', ' ');
112: defn = defn.replace('{', ' ');
113: defn = noWhitespace(defn);
114: for (String c : defn.split("\\|")) { //$NON-NLS-1$
115: if (c.length() == 0)
116: continue;
117: String rgb[] = c.trim().split(","); //$NON-NLS-1$
118: int r = Integer.parseInt(rgb[0].trim());
119: int g = Integer.parseInt(rgb[1].trim());
120: int b = Integer.parseInt(rgb[2].trim());
121: colours.add(new Color(r, g, b));
122: }
123: String goodDefn = noWhitespace(properties.getProperty(KEY
124: + i + ".good")); //$NON-NLS-1$
125: EnumSet<Friendly> good = Friendly.parse(goodDefn);
126:
127: String doubtfulDefn = noWhitespace(properties
128: .getProperty(KEY + i + ".doubtful")); //$NON-NLS-1$
129: EnumSet<Friendly> doubtful = Friendly.parse(doubtfulDefn);
130: Scheme scheme = new Scheme(colours, good, doubtful);
131: //System.out.println( i+": "+scheme );
132: contents.add(scheme);
133: }
134: }
135:
136: static final String noWhitespace(String str) {
137: if (str == null)
138: return null;
139: StringBuffer buf = new StringBuffer();
140: for (int i = 0; i < str.length(); i++) {
141: char c = str.charAt(i);
142: if (Character.isWhitespace(c))
143: continue;
144: buf.append(c);
145: }
146: return buf.toString();
147: }
148: }
|