001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui.renderer.style;
033:
034: import java.awt.Color;
035: import java.awt.Component;
036: import java.io.IOException;
037: import java.io.InputStream;
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Collections;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.StringTokenizer;
044: import java.util.TreeSet;
045:
046: import javax.swing.JList;
047: import javax.swing.JPanel;
048: import javax.swing.ListCellRenderer;
049:
050: import com.vividsolutions.jts.util.Assert;
051: import com.vividsolutions.jump.util.CollectionMap;
052: import com.vividsolutions.jump.util.FileUtil;
053: import com.vividsolutions.jump.util.OrderedMap;
054: import com.vividsolutions.jump.workbench.ui.GUIUtil;
055:
056: /**
057: * The colour schemes were taken from the following sources:
058: * <ul>
059: * <li>
060: * Visual Mining, Inc. "Charts--Color Palettes" 2003.
061: * Available from http://chartworks.com/resources/palettes.html.
062: * Internet; accessed 24 April 2003.
063: * <li>
064: * Brewer, Cindy and Harrower, Mark. "ColorBrewer".
065: * Available from http://www.personal.psu.edu/faculty/c/a/cab38/ColorBrewerBeta2.html.
066: * Internet; accessed 24 April 2003.
067: * </ul>
068: */
069: public class ColorScheme {
070: public static ColorScheme create(String name) {
071: Assert.isTrue(nameToColorsMap().keySet().contains(name));
072: return new ColorScheme(name, nameToColorsMap().getItems(name));
073: }
074:
075: private static ArrayList rangeColorSchemeNames;
076: private static ArrayList discreteColorSchemeNames;
077:
078: private static void load() {
079: try {
080: rangeColorSchemeNames = new ArrayList();
081: discreteColorSchemeNames = new ArrayList();
082: nameToColorsMap = new CollectionMap();
083: InputStream inputStream = ColorScheme.class
084: .getResourceAsStream("ColorScheme.txt");
085: try {
086: for (Iterator i = FileUtil.getContents(inputStream)
087: .iterator(); i.hasNext();) {
088: String line = (String) i.next();
089: add(line);
090: }
091: } finally {
092: inputStream.close();
093: }
094: } catch (IOException e) {
095: Assert.shouldNeverReachHere(e.toString());
096: }
097: }
098:
099: private static void add(String line) {
100: StringTokenizer tokenizer = new StringTokenizer(line, ",");
101: String name = tokenizer.nextToken();
102: boolean range = tokenizer.nextToken().equals("range");
103: (range ? rangeColorSchemeNames : discreteColorSchemeNames)
104: .add(name);
105: while (tokenizer.hasMoreTokens()) {
106: String hex = tokenizer.nextToken();
107: Assert.isTrue(hex.length() == 6, hex);
108: nameToColorsMap().addItem(name, Color.decode("#" + hex));
109: }
110: }
111:
112: private static CollectionMap nameToColorsMap() {
113: if (nameToColorsMap == null) {
114: load();
115: }
116: return nameToColorsMap;
117: }
118:
119: private static CollectionMap nameToColorsMap;
120:
121: public static Collection rangeColorSchemeNames() {
122: if (rangeColorSchemeNames == null) {
123: load();
124: }
125: return Collections.unmodifiableList(rangeColorSchemeNames);
126: }
127:
128: public static Collection discreteColorSchemeNames() {
129: if (discreteColorSchemeNames == null) {
130: load();
131: }
132: return Collections.unmodifiableList(discreteColorSchemeNames);
133: }
134:
135: private String name;
136:
137: public ColorScheme(String name, Collection colors) {
138: this .name = name;
139: this .colors = new ArrayList(colors);
140: }
141:
142: private int lastColorReturned = -1;
143:
144: public int getLastColorReturned() {
145: return lastColorReturned;
146: }
147:
148: public void setLastColorReturned(int lastColorReturned) {
149: this .lastColorReturned = lastColorReturned;
150: }
151:
152: private List colors;
153:
154: public Color next() {
155: lastColorReturned++;
156: if (lastColorReturned >= colors.size()) {
157: lastColorReturned = 0;
158: }
159: return (Color) colors.get(lastColorReturned);
160: }
161:
162: public List getColors() {
163: return Collections.unmodifiableList(colors);
164: }
165:
166: public String getName() {
167: return name;
168: }
169:
170: }
|