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 de.latlon.deejump.plugin.style;
033:
034: import java.util.Collections;
035: import java.util.Iterator;
036: import java.util.List;
037: import java.util.Map;
038: import java.util.TreeMap;
039: import java.util.TreeSet;
040:
041: import javax.swing.event.TableModelEvent;
042: import javax.swing.table.TableModel;
043:
044: import com.vividsolutions.jump.feature.FeatureSchema;
045: import com.vividsolutions.jump.util.Block;
046: import com.vividsolutions.jump.util.CollectionUtil;
047: import com.vividsolutions.jump.util.ImmutableFirstElementList;
048: import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle;
049: import com.vividsolutions.jump.workbench.ui.renderer.style.ColorScheme;
050: import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingTableModel;
051: import com.vividsolutions.jump.workbench.ui.renderer.style.VertexStyle;
052:
053: public class DeeColorThemingTableModel extends ColorThemingTableModel {
054:
055: public DeeColorThemingTableModel(BasicStyle defaultStyle,
056: String attributeName, Map attributeValueToBasicStyleMap,
057: Map attributeValueToLabelMap,
058: Map attributeValueToVertexStyleMap, FeatureSchema schema) {
059: //Value doesn't matter. [Jon Aquino]
060: super (defaultStyle, attributeName,
061: attributeValueToBasicStyleMap,
062: attributeValueToLabelMap, schema);
063: attributeMappings = new ImmutableFirstElementList(
064: new VerticedAttributeMapping(null, defaultStyle, null,
065: null));
066: this .attributeName = attributeName;
067: setMaps(attributeValueToBasicStyleMap,
068: attributeValueToLabelMap,
069: attributeValueToVertexStyleMap);
070: setColumns(createColumns(schema));
071: }
072:
073: /**
074: *
075: */
076: //public DeeColorThemingTableModel() {
077: // super(new BasicStyle(), );
078: // TODO Auto-generated constructor stub
079: //}
080: public static final int COLOR_COLUMN = 0;
081: public static final int ATTRIBUTE_COLUMN = 1;
082: public static final int LABEL_COLUMN = 2;
083:
084: public void setMaps(Map attributeValueToBasicStyleMap,
085: Map attributeValueToLabelMap,
086: Map attributeValueToVertexStyleMap) {
087: attributeMappings.clear();
088: for (Iterator i = attributeValueToBasicStyleMap.keySet()
089: .iterator(); i.hasNext();) {
090: Object attributeValue = i.next();
091: attributeMappings.add(new VerticedAttributeMapping(
092: attributeValue,
093: (BasicStyle) attributeValueToBasicStyleMap
094: .get(attributeValue),
095: (String) attributeValueToLabelMap
096: .get(attributeValue),
097: (VertexStyle) attributeValueToVertexStyleMap
098: .get(attributeValue)));
099:
100: }
101: fireTableChanged(new TableModelEvent(this ));
102: }
103:
104: protected static class VerticedAttributeMapping extends
105: AttributeMapping {
106: // private String label;
107: private VertexStyle vertexStyle;
108:
109: public VerticedAttributeMapping(Object attributeValue,
110: BasicStyle basicStyle, String label,
111: VertexStyle vertexStyle) {
112: super (attributeValue, basicStyle, label);
113: this .vertexStyle = vertexStyle;
114: }
115:
116: private void setVertexStyle(VertexStyle vertexSty) {
117: this .vertexStyle = vertexSty;
118: }
119:
120: public VertexStyle getVertexStyle() {
121: return this .vertexStyle;
122: }
123: }
124:
125: public void clear() {
126: attributeMappings.clear();
127: fireTableChanged(new TableModelEvent(this ));
128: }
129:
130: public boolean containsNullAttributeValues() {
131: for (Iterator i = nonDefaultAttributeMappings().iterator(); i
132: .hasNext();) {
133: AttributeMapping attributeMapping = (AttributeMapping) i
134: .next();
135: if (attributeMapping.getAttributeValue() == null) {
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: protected AttributeMapping attributeMapping(int i) {
143: return (AttributeMapping) attributeMappings.get(i);
144: }
145:
146: public BasicStyle getDefaultStyle() {
147: return attributeMapping(0).getBasicStyle();
148: }
149:
150: public Object findDuplicateAttributeValue() {
151: TreeSet set = new TreeSet();
152: for (Iterator i = nonDefaultAttributeMappings().iterator(); i
153: .hasNext();) {
154: AttributeMapping attributeMapping = (AttributeMapping) i
155: .next();
156: if (attributeMapping.getAttributeValue() == null) {
157: //Check nulls elsewhere. TreeSet won't accept nulls. [Jon Aquino]
158: continue;
159: }
160: if (set.contains(attributeMapping.getAttributeValue())) {
161: return attributeMapping.getAttributeValue();
162: }
163: set.add(attributeMapping.getAttributeValue());
164: }
165: return null;
166: }
167:
168: //Can't use TreeMap because attributes may not be unique (this is
169: //an invalid state of course and we won't let the user hit OK until he
170: //resolves it). Can't use HashMap because Geometry doesn't implement
171: //#hash. [Jon Aquino]
172: protected List attributeMappings;
173:
174: public static class AttributeValueTableModelEvent extends
175: TableModelEvent {
176: public AttributeValueTableModelEvent(TableModel source, int row) {
177: super (source, row);
178: }
179:
180: }
181:
182: //GH 2005.10.25
183: public void setVertexStyleAt(Object value, int rowIndex) {
184: ((VerticedAttributeMapping) attributeMapping(rowIndex))
185: .setVertexStyle((VertexStyle) value);
186: }
187:
188: public void apply(ColorScheme colorScheme,
189: boolean skipDefaultAttributeMapping) {
190: //Leave the first element out of the sort, because it's the "(All other values)"
191: //element. [Jon Aquino]
192: for (Iterator i = (skipDefaultAttributeMapping ? nonDefaultAttributeMappings()
193: : attributeMappings).iterator(); i.hasNext();) {
194: AttributeMapping attributeMapping = (AttributeMapping) i
195: .next();
196: attributeMapping.setBasicStyle(new BasicStyle(colorScheme
197: .next()));
198: }
199: fireTableChanged(new TableModelEvent(this ));
200: }
201:
202: public int getRowCount() {
203: return attributeMappings.size();
204: }
205:
206: public void setAttributeName(String attributeName) {
207: this .attributeName = attributeName;
208: }
209:
210: public Map getAttributeValueToBasicStyleMap() {
211: return attributeValueToObjectMap(new Block() {
212: public Object yield(Object attributeMapping) {
213: return ((AttributeMapping) attributeMapping)
214: .getBasicStyle();
215: }
216: });
217: }
218:
219: public Map getAttributeValueToVertexStyleMap() {
220: return attributeValueToObjectMap(new Block() {
221: public Object yield(Object attributeMapping) {
222: return ((VerticedAttributeMapping) attributeMapping)
223: .getVertexStyle();
224: }
225: });
226: }
227:
228: private Map attributeValueToObjectMap(Block getter) {
229: TreeMap attributeValueToObjectMap = new TreeMap();
230: //Skip the first element, which is the default style. [Jon Aquino]
231: for (Iterator i = nonDefaultAttributeMappings().iterator(); i
232: .hasNext();) {
233: AttributeMapping attributeMapping = (AttributeMapping) i
234: .next();
235: attributeValueToObjectMap.put(attributeMapping
236: .getAttributeValue(), getter
237: .yield(attributeMapping));
238:
239: }
240: return attributeValueToObjectMap;
241: }
242:
243: private boolean lastSortAscending = true;
244:
245: public boolean wasLastSortAscending() {
246: return lastSortAscending;
247: }
248:
249: public void sort() {
250: sort(!lastSortAscending);
251: }
252:
253: public void sort(boolean ascending) {
254: //Leave the first element out of the sort, because it's the "(All other values)"
255: //element. [Jon Aquino]
256: if (ascending) {
257: Collections.sort(nonDefaultAttributeMappings());
258: } else {
259: Collections.sort(nonDefaultAttributeMappings(), Collections
260: .reverseOrder());
261: }
262: lastSortAscending = ascending;
263: fireTableChanged(new TableModelEvent(this ));
264: }
265:
266: public void removeAttributeValues(int[] rows) {
267: for (Iterator i = CollectionUtil.reverseSortedSet(rows)
268: .iterator(); i.hasNext();) {
269: Integer row = (Integer) i.next();
270: attributeMappings.remove(row.intValue());
271: fireTableChanged(new TableModelEvent(this , row.intValue(),
272: row.intValue(), TableModelEvent.ALL_COLUMNS,
273: TableModelEvent.DELETE));
274: }
275: }
276:
277: /**
278: * @return row
279: */
280: public int insertAttributeValue(int row, ColorScheme colorScheme) {
281: attributeMappings.add(row, new AttributeMapping(null,
282: new BasicStyle(colorScheme.next()), ""));
283: fireTableChanged(new TableModelEvent(this , row, row,
284: TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
285: return row;
286: }
287:
288: protected String attributeName;
289:
290: public boolean isCellEditable(int rowIndex, int columnIndex) {
291: //Any cell except the one that says "(All other values)" [Jon Aquino]
292: if (rowIndex == 0 && columnIndex == ATTRIBUTE_COLUMN) {
293: return false;
294: }
295: if (rowIndex == 0 && columnIndex == LABEL_COLUMN) {
296: return false;
297: }
298: return true;
299: }
300:
301: protected List nonDefaultAttributeMappings() {
302: return attributeMappings.subList(1, attributeMappings.size());
303: }
304:
305: }
|