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.model;
033:
034: import java.awt.Color;
035: import java.util.ArrayList;
036: import java.util.Collections;
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041:
042: import javax.swing.tree.TreePath;
043:
044: import com.vividsolutions.jts.util.Assert;
045: import com.vividsolutions.jump.util.LangUtil;
046: import com.vividsolutions.jump.util.SimpleTreeModel;
047: import com.vividsolutions.jump.util.SimpleTreeModel.Folder;
048: import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle;
049: import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingStyle;
050:
051: /**
052: * JTree model for displaying the Layers, WMSLayers, and other Layerables
053: * contained in a LayerManager.
054: */
055: public class LayerTreeModel extends SimpleTreeModel {
056:
057: public static class Root {
058: private Root() {
059: }
060: }
061:
062: private LayerManagerProxy layerManagerProxy;
063:
064: public LayerTreeModel(LayerManagerProxy layerManagerProxy) {
065: super (new Root());
066: this .layerManagerProxy = layerManagerProxy;
067: }
068:
069: public static class ColorThemingValue {
070: private Object value;
071: private BasicStyle style;
072: private String label;
073:
074: public ColorThemingValue(Object value, BasicStyle style,
075: String label) {
076: this .value = value;
077: this .style = style;
078: Assert.isTrue(label != null);
079: this .label = label;
080: }
081:
082: public String toString() {
083: return label;
084: }
085:
086: public boolean equals(Object other) {
087: return other instanceof ColorThemingValue
088: && LangUtil.bothNullOrEqual(value,
089: ((ColorThemingValue) other).value)
090: && style == ((ColorThemingValue) other).style;
091: }
092:
093: public BasicStyle getStyle() {
094: return style;
095: }
096: }
097:
098: public int getIndexOfChild(Object parent, Object child) {
099: for (int i = 0; i < getChildCount(parent); i++) {
100: // ColorThemingValue are value objects. [Jon Aquino]
101: if (child instanceof ColorThemingValue
102: && getChild(parent, i) instanceof ColorThemingValue
103: && getChild(parent, i).equals(child)) {
104: return i;
105: }
106: }
107: return super .getIndexOfChild(parent, child);
108: }
109:
110: public List getChildren(Object parent) {
111: if (parent == getRoot()) {
112: return layerManagerProxy.getLayerManager().getCategories();
113: }
114: if (parent instanceof Category) {
115: return ((Category) parent).getLayerables();
116: }
117: if (parent instanceof Layer
118: && ColorThemingStyle.get((Layer) parent).isEnabled()) {
119: Map attributeValueToBasicStyleMap = ColorThemingStyle.get(
120: (Layer) parent).getAttributeValueToBasicStyleMap();
121: Map attributeValueToLabelMap = ColorThemingStyle.get(
122: (Layer) parent).getAttributeValueToLabelMap();
123: List colorThemingValues = new ArrayList();
124: for (Iterator i = attributeValueToBasicStyleMap.keySet()
125: .iterator(); i.hasNext();) {
126: Object value = (Object) i.next();
127: colorThemingValues.add(new ColorThemingValue(value,
128: (BasicStyle) attributeValueToBasicStyleMap
129: .get(value),
130: (String) attributeValueToLabelMap.get(value)));
131: }
132: return colorThemingValues;
133: }
134: if (parent instanceof ColorThemingValue) {
135: return Collections.EMPTY_LIST;
136: }
137: if (parent instanceof Layerable) {
138: return new ArrayList();
139: }
140: Assert.shouldNeverReachHere(parent.getClass().getName());
141: return null;
142: }
143:
144: public void valueForPathChanged(TreePath path, Object newValue) {
145: if (path.getLastPathComponent() instanceof Layerable) {
146: ((Layerable) path.getLastPathComponent())
147: .setName((String) newValue);
148: } else if (path.getLastPathComponent() instanceof Category) {
149: ((Category) path.getLastPathComponent())
150: .setName((String) newValue);
151: } else {
152: Assert.shouldNeverReachHere();
153: }
154: }
155:
156: }
|