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:
033: package com.vividsolutions.jump.workbench.ui.plugin.wms;
034:
035: import java.util.ArrayList;
036: import java.util.Collections;
037: import java.util.Enumeration;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Vector;
041:
042: import javax.swing.tree.DefaultTreeModel;
043: import javax.swing.tree.TreeNode;
044:
045: import com.vividsolutions.wms.MapLayer;
046:
047: public class MapLayerTreeModel extends DefaultTreeModel {
048: private boolean sorted = false;
049:
050: public MapLayerTreeModel(MapLayer topLayer) {
051: super (new LayerNode(topLayer, null));
052: ((LayerNode) getRoot()).mapLayerTreeModel = this ;
053: }
054:
055: public void setSorted(boolean sorted) {
056: this .sorted = sorted;
057: reload();
058: }
059:
060: public static class LayerNode implements TreeNode, Comparable {
061: private MapLayer layer;
062: private MapLayerTreeModel mapLayerTreeModel;
063:
064: public LayerNode(MapLayer layer,
065: MapLayerTreeModel mapLayerTreeModel) {
066: this .layer = layer;
067: this .mapLayerTreeModel = mapLayerTreeModel;
068: }
069:
070: public boolean isContainer() {
071: return layer.getName() == null;
072: }
073:
074: public MapLayer getLayer() {
075: return layer;
076: }
077:
078: public TreeNode getChildAt(int childIndex) {
079: return (TreeNode) childList().get(childIndex);
080: }
081:
082: public int getChildCount() {
083: return childList().size();
084: }
085:
086: public TreeNode getParent() {
087: return new LayerNode(layer.getParent(), mapLayerTreeModel);
088: }
089:
090: public int getIndex(TreeNode node) {
091: return childList().indexOf(node);
092: }
093:
094: public boolean getAllowsChildren() {
095: return true;
096: }
097:
098: public boolean isLeaf() {
099: return getChildCount() == 0;
100: }
101:
102: public Enumeration children() {
103: return new Vector(childList()).elements();
104: }
105:
106: private List childList() {
107: ArrayList children = new ArrayList();
108:
109: for (Iterator i = layer.getSubLayerList().iterator(); i
110: .hasNext();) {
111: MapLayer layer = (MapLayer) i.next();
112: children.add(new LayerNode(layer, mapLayerTreeModel));
113: }
114:
115: if (mapLayerTreeModel.sorted) {
116: Collections.sort(children);
117: }
118:
119: return children;
120: }
121:
122: public boolean equals(Object o) {
123: //Needed for the #contains check in MapLayerPanel, as well as #getIndex. [Jon Aquino]
124: LayerNode other = (LayerNode) o;
125:
126: return layer == other.layer;
127: }
128:
129: public int compareTo(Object o) {
130: LayerNode other = (LayerNode) o;
131:
132: return layer.getTitle().compareTo(other.layer.getTitle());
133: }
134: }
135: }
|