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;
034:
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Collections;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: import com.vividsolutions.jump.util.OrderedMap;
042: import com.vividsolutions.jump.workbench.model.Layer;
043:
044: public class InfoModel {
045: /**
046: * Releases references to the data, to facilitate garbage collection.
047: * Important for MDI apps like the JCS Workbench.
048: */
049: public void dispose() {
050: //new ArrayList to prevent ConcurrentModificationException [Jon Aquino]
051: for (Iterator i = new ArrayList(getLayers()).iterator(); i
052: .hasNext();) {
053: Layer layer = (Layer) i.next();
054: remove(layer);
055: }
056:
057: //It would be nice to use a model proxy so that there could be multiple
058: //views on the model, and when the last view calls #dispose on its proxy
059: //the real model would be garbage collected (see the pattern employed by
060: //LayerViewPanel). But each InfoTableModel has a reference to Features
061: // -- would each Feature would need a proxy? This smells ugly.
062: //So we'll go with an explicit #dispose method for now, and restrict
063: //the model to one view (when that view is closed, it will call #dispose).
064: //[Jon Aquino]
065: }
066:
067: private OrderedMap layerToTableModelMap = new OrderedMap();
068: private ArrayList listeners = new ArrayList();
069:
070: public InfoModel() {
071: }
072:
073: public Collection getLayerTableModels() {
074: return Collections.unmodifiableCollection(layerToTableModelMap
075: .values());
076: }
077:
078: public void add(Layer layer, Collection features) {
079: boolean layerNew = !layerToTableModelMap.containsKey(layer);
080: LayerTableModel layerTableModel = getTableModel(layer);
081: layerTableModel.addAll(features);
082:
083: //Notify the listeners *after* adding the features to the table-panel model:
084: //TablePanels need the data so they can properly size their columns. [Jon Aquino]
085: if (layerNew) {
086: for (Iterator i = listeners.iterator(); i.hasNext();) {
087: InfoModelListener listener = (InfoModelListener) i
088: .next();
089: listener.layerAdded(layerTableModel);
090: }
091: }
092: }
093:
094: public void remove(Layer layer) {
095: LayerTableModel layerTableModel = getTableModel(layer);
096:
097: for (Iterator i = listeners.iterator(); i.hasNext();) {
098: InfoModelListener listener = (InfoModelListener) i.next();
099: listener.layerRemoved(layerTableModel);
100: }
101:
102: ((LayerTableModel) layerToTableModelMap.get(layer)).dispose();
103: layerToTableModelMap.remove(layer);
104: }
105:
106: public void clear() {
107: ArrayList layers = new ArrayList(layerToTableModelMap.keySet());
108:
109: for (Iterator i = layers.iterator(); i.hasNext();) {
110: Layer layer = (Layer) i.next();
111: remove(layer);
112: }
113: }
114:
115: public LayerTableModel getTableModel(Layer layer) {
116: if (!layerToTableModelMap.containsKey(layer)) {
117: layerToTableModelMap.put(layer, new LayerTableModel(layer));
118: }
119:
120: return (LayerTableModel) layerToTableModelMap.get(layer);
121: }
122:
123: public List getLayers() {
124: return Collections.unmodifiableList(layerToTableModelMap
125: .keyList());
126: }
127:
128: public void addListener(InfoModelListener listener) {
129: listeners.add(listener);
130: }
131: }
|