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.util;
033:
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: import javax.swing.event.TreeModelEvent;
039: import javax.swing.event.TreeModelListener;
040: import javax.swing.tree.TreeModel;
041: import javax.swing.tree.TreePath;
042:
043: import com.vividsolutions.jts.util.Assert;
044:
045: public abstract class SimpleTreeModel implements TreeModel {
046:
047: public static abstract class Folder {
048: private Class childrenClass;
049: private String name;
050: private Object parent;
051:
052: public Folder(String name, Object parent, Class childrenClass) {
053: this .name = name;
054: this .parent = parent;
055: this .childrenClass = childrenClass;
056: }
057:
058: public abstract List getChildren();
059:
060: public String toString() {
061: return name;
062: }
063:
064: public int hashCode() {
065: //JTree puts nodes in a Hashtable. To keep things simple, just return 0,
066: //which will cause linear searches (fine for small trees). [Jon Aquino]
067: return 0;
068: }
069:
070: public boolean equals(Object other) {
071: //Folders are value objects. [Jon Aquino]
072: if (!(other instanceof Folder)) {
073: return false;
074: }
075: Folder otherFolder = (Folder) other;
076: return parent == otherFolder.parent
077: && name.equals(otherFolder.name);
078: }
079:
080: public Class getChildrenClass() {
081: return childrenClass;
082: }
083:
084: public Object getParent() {
085: return parent;
086: }
087:
088: }
089:
090: private Object root;
091:
092: public SimpleTreeModel(Object root) {
093: this .root = root;
094: }
095:
096: public Object getRoot() {
097: return root;
098: }
099:
100: public boolean isLeaf(Object node) {
101: return !(node instanceof Folder) && getChildCount(node) == 0;
102: }
103:
104: public void valueForPathChanged(TreePath path, Object newValue) {
105: }
106:
107: public int getIndexOfChild(Object parent, Object child) {
108: for (int i = 0; i < getChildCount(parent); i++) {
109: //Folders are value objects. [Jon Aquino]
110: if (child instanceof Folder
111: && getChild(parent, i) instanceof Folder
112: && getChild(parent, i).toString().equals(
113: child.toString())) {
114: return i;
115: }
116: if (getChild(parent, i) == child) {
117: return i;
118: }
119: }
120: Assert.shouldNeverReachHere(parent + ", " + child);
121: return -1;
122: }
123:
124: private ArrayList listeners = new ArrayList();
125: private boolean firingEvents = true;
126:
127: public void addTreeModelListener(TreeModelListener listener) {
128: listeners.add(listener);
129: }
130:
131: public void removeTreeModelListener(TreeModelListener listener) {
132: listeners.remove(listener);
133: }
134:
135: /**
136: * No need to handle Folders
137: * @param parent not a Folder
138: */
139: public abstract List getChildren(Object parent);
140:
141: public Object getChild(Object parent, int index) {
142: return children(parent).get(index);
143: }
144:
145: private List children(Object parent) {
146: return parent instanceof Folder ? ((Folder) parent)
147: .getChildren() : getChildren(parent);
148: }
149:
150: public int getChildCount(Object parent) {
151: return children(parent).size();
152: }
153:
154: public void fireTreeNodesChanged(TreeModelEvent e) {
155: if (!firingEvents) {
156: return;
157: }
158: for (Iterator i = listeners.iterator(); i.hasNext();) {
159: TreeModelListener l = (TreeModelListener) i.next();
160: l.treeNodesChanged(e);
161: }
162: }
163:
164: public void fireTreeNodesInserted(TreeModelEvent e) {
165: if (!firingEvents) {
166: return;
167: }
168: for (Iterator i = listeners.iterator(); i.hasNext();) {
169: TreeModelListener l = (TreeModelListener) i.next();
170: l.treeNodesInserted(e);
171: }
172: }
173:
174: public void fireTreeNodesRemoved(TreeModelEvent e) {
175: if (!firingEvents) {
176: return;
177: }
178: for (Iterator i = listeners.iterator(); i.hasNext();) {
179: TreeModelListener l = (TreeModelListener) i.next();
180: l.treeNodesRemoved(e);
181: }
182: }
183:
184: public void fireTreeStructureChanged(TreeModelEvent e) {
185: if (!firingEvents) {
186: return;
187: }
188: for (Iterator i = listeners.iterator(); i.hasNext();) {
189: TreeModelListener l = (TreeModelListener) i.next();
190: l.treeStructureChanged(e);
191: }
192: }
193:
194: public void setFiringEvents(boolean firingEvents) {
195: this.firingEvents = firingEvents;
196: }
197:
198: }
|