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.ui;
033:
034: import java.awt.BorderLayout;
035: import java.awt.Color;
036: import java.awt.GridBagConstraints;
037: import java.awt.GridBagLayout;
038: import java.awt.Insets;
039: import java.awt.event.MouseAdapter;
040: import java.awt.event.MouseEvent;
041: import java.awt.geom.NoninvertibleTransformException;
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import javax.swing.BorderFactory;
047: import javax.swing.JPanel;
048: import javax.swing.event.ListSelectionEvent;
049: import javax.swing.event.ListSelectionListener;
050: import com.vividsolutions.jts.util.Assert;
051: import com.vividsolutions.jump.feature.BasicFeature;
052: import com.vividsolutions.jump.feature.Feature;
053: import com.vividsolutions.jump.feature.FeatureUtil;
054: import com.vividsolutions.jump.workbench.WorkbenchContext;
055: import com.vividsolutions.jump.workbench.model.Layer;
056: import com.vividsolutions.jump.workbench.model.LayerManagerProxy;
057: import com.vividsolutions.jump.workbench.ui.zoom.ZoomToSelectedItemsPlugIn;
058:
059: /**
060: * Implements an Attribute Panel.
061: */
062:
063: public class AttributePanel extends JPanel implements
064: InfoModelListener, AttributeTablePanelListener {
065: private SelectionManager selectionManager;
066: private BorderLayout borderLayout1 = new BorderLayout();
067: private GridBagLayout gridBagLayout1 = new GridBagLayout();
068: private HashMap layerToTablePanelMap = new HashMap();
069: private InfoModel model;
070: private WorkbenchContext workbenchContext;
071: private ZoomToSelectedItemsPlugIn zoomToSelectedItemsPlugIn = new ZoomToSelectedItemsPlugIn();
072: private Row nullRow = new Row() {
073: public boolean isFirstRow() {
074: return rowCount() == 0;
075: }
076:
077: public boolean isLastRow() {
078: return rowCount() == 0;
079: }
080:
081: public AttributeTablePanel getPanel() {
082: throw new UnsupportedOperationException();
083: }
084:
085: public int getIndex() {
086: throw new UnsupportedOperationException();
087: }
088:
089: public Row nextRow() {
090: return firstRow();
091: }
092:
093: public Row previousRow() {
094: return firstRow();
095: }
096:
097: private Row firstRow() {
098: return new BasicRow(getTablePanel((Layer) getModel()
099: .getLayers().get(0)), 0);
100: }
101:
102: public Feature getFeature() {
103: throw new UnsupportedOperationException();
104: }
105: };
106: private TaskFrame taskFrame;
107: private LayerManagerProxy layerManagerProxy;
108: private boolean addScrollPanesToChildren;
109:
110: /**
111: * @param layerManagerProxy Can't simply get LayerManager from TaskFrame
112: * because when that frame closes, it sets its LayerManager to null.
113: */
114: protected AttributePanel(InfoModel model,
115: WorkbenchContext workbenchContext, TaskFrame taskFrame,
116: LayerManagerProxy layerManagerProxy,
117: boolean addScrollPanesToChildren) {
118: this .addScrollPanesToChildren = addScrollPanesToChildren;
119: selectionManager = new SelectionManager(null, layerManagerProxy);
120: selectionManager.setPanelUpdatesEnabled(false);
121: this .taskFrame = taskFrame;
122: this .workbenchContext = workbenchContext;
123: this .layerManagerProxy = layerManagerProxy;
124: setModel(model);
125: try {
126: jbInit();
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: }
130: }
131:
132: public AttributeTablePanel getTablePanel(Layer layer) {
133: return (AttributeTablePanel) layerToTablePanelMap.get(layer);
134: }
135:
136: public InfoModel getModel() {
137: return model;
138: }
139:
140: public void setModel(InfoModel model) {
141: this .model = model;
142: model.addListener(this );
143: }
144:
145: public void layerAdded(LayerTableModel layerTableModel) {
146: addTablePanel(layerTableModel);
147: }
148:
149: public void layerRemoved(LayerTableModel layerTableModel) {
150: removeTablePanel(layerTableModel);
151: }
152:
153: void jbInit() throws Exception {
154: setLayout(gridBagLayout1);
155: }
156:
157: private void removeTablePanel(LayerTableModel layerTableModel) {
158: Assert.isTrue(layerToTablePanelMap.containsKey(layerTableModel
159: .getLayer()));
160: remove(getTablePanel(layerTableModel.getLayer()));
161: layerToTablePanelMap.remove(layerTableModel.getLayer());
162: revalidate();
163: repaint();
164: updateSelectionManager();
165: }
166:
167: private void addTablePanel(final LayerTableModel layerTableModel) {
168: Assert.isTrue(!layerToTablePanelMap.containsKey(layerTableModel
169: .getLayer()));
170: final AttributeTablePanel tablePanel = new AttributeTablePanel(
171: layerTableModel, addScrollPanesToChildren,
172: workbenchContext);
173: tablePanel.addListener(this );
174: layerToTablePanelMap
175: .put(layerTableModel.getLayer(), tablePanel);
176: add(tablePanel, new GridBagConstraints(0, getComponentCount(),
177: 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
178: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
179: revalidate();
180: repaint();
181: tablePanel.getTable().addMouseListener(new MouseAdapter() {
182: public void mouseClicked(MouseEvent e) {
183: try {
184: int row = tablePanel.getTable().rowAtPoint(
185: e.getPoint());
186: if (row == -1) {
187: return;
188: }
189: ArrayList features = new ArrayList();
190: features.add(layerTableModel.getFeature(row));
191: if (taskFrame.isVisible()) {
192: zoomToSelectedItemsPlugIn.flash(FeatureUtil
193: .toGeometries(features), taskFrame
194: .getLayerViewPanel());
195: }
196: } catch (Throwable t) {
197: workbenchContext.getErrorHandler().handleThrowable(
198: t);
199: }
200: }
201: });
202: tablePanel.getTable().getSelectionModel()
203: .addListSelectionListener(new ListSelectionListener() {
204: public void valueChanged(ListSelectionEvent e) {
205: updateSelectionManager();
206: }
207: });
208: updateSelectionManager();
209: }
210:
211: private void updateSelectionManager() {
212: selectionManager.clear();
213: for (Iterator i = layerToTablePanelMap.values().iterator(); i
214: .hasNext();) {
215: AttributeTablePanel tablePanel = (AttributeTablePanel) i
216: .next();
217: selectionManager.getFeatureSelection().selectItems(
218: tablePanel.getModel().getLayer(),
219: tablePanel.getSelectedFeatures());
220: }
221: }
222:
223: public int rowCount() {
224: int rowCount = 0;
225: for (Iterator i = layerToTablePanelMap.values().iterator(); i
226: .hasNext();) {
227: AttributeTablePanel tablePanel = (AttributeTablePanel) i
228: .next();
229: rowCount += tablePanel.getTable().getRowCount();
230: }
231: return rowCount;
232: }
233:
234: public void flashSelectedFeatures()
235: throws NoninvertibleTransformException {
236: zoomToSelectedItemsPlugIn.flash(FeatureUtil
237: .toGeometries(selectedFeatures()), taskFrame
238: .getLayerViewPanel());
239: }
240:
241: public void zoom(Collection features)
242: throws NoninvertibleTransformException {
243: zoomToSelectedItemsPlugIn.zoom(FeatureUtil
244: .toGeometries(features), taskFrame.getLayerViewPanel());
245: }
246:
247: public Collection selectedFeatures() {
248: ArrayList selectedFeatures = new ArrayList();
249: for (Iterator i = layerToTablePanelMap.values().iterator(); i
250: .hasNext();) {
251: AttributeTablePanel tablePanel = (AttributeTablePanel) i
252: .next();
253: if (tablePanel.getModel().getRowCount() == 0) {
254: return selectedFeatures;
255: }
256: int[] selectedRows = tablePanel.getTable()
257: .getSelectedRows();
258: for (int j = 0; j < selectedRows.length; j++) {
259: selectedFeatures.add(tablePanel.getModel().getFeature(
260: selectedRows[j]));
261: }
262: }
263: return selectedFeatures;
264: }
265:
266: public void selectInLayerViewPanel() {
267: taskFrame.getLayerViewPanel().getSelectionManager().clear();
268: for (Iterator i = layerToTablePanelMap.values().iterator(); i
269: .hasNext();) {
270: AttributeTablePanel tablePanel = (AttributeTablePanel) i
271: .next();
272: int[] selectedRows = tablePanel.getTable()
273: .getSelectedRows();
274: ArrayList selectedFeatures = new ArrayList();
275: for (int j = 0; j < selectedRows.length; j++) {
276: selectedFeatures.add(tablePanel.getModel().getFeature(
277: selectedRows[j]));
278: }
279: taskFrame.getLayerViewPanel().getSelectionManager()
280: .getFeatureSelection().selectItems(
281: tablePanel.getModel().getLayer(),
282: selectedFeatures);
283: }
284: }
285:
286: public Row topSelectedRow() {
287: for (Iterator i = layerToTablePanelMap.values().iterator(); i
288: .hasNext();) {
289: AttributeTablePanel panel = (AttributeTablePanel) i.next();
290: int selectedRow = panel.getTable().getSelectedRow();
291: if (selectedRow == -1) {
292: continue;
293: }
294: return new BasicRow(panel, selectedRow);
295: }
296: return nullRow;
297: }
298:
299: public void selectionReplaced(AttributeTablePanel panel) {
300: for (Iterator i = layerToTablePanelMap.values().iterator(); i
301: .hasNext();) {
302: AttributeTablePanel tablePanel = (AttributeTablePanel) i
303: .next();
304: if (tablePanel == panel) {
305: continue;
306: }
307: tablePanel.getTable().clearSelection();
308: }
309: }
310:
311: public void clearSelection() {
312: for (Iterator i = layerToTablePanelMap.values().iterator(); i
313: .hasNext();) {
314: AttributeTablePanel tablePanel = (AttributeTablePanel) i
315: .next();
316: tablePanel.getTable().clearSelection();
317: }
318: }
319:
320: public static interface Row {
321: public boolean isFirstRow();
322:
323: public boolean isLastRow();
324:
325: public AttributeTablePanel getPanel();
326:
327: public int getIndex();
328:
329: public Row nextRow();
330:
331: public Row previousRow();
332:
333: public Feature getFeature();
334: }
335:
336: private class BasicRow implements Row {
337: private AttributeTablePanel panel = null;
338: private int index;
339:
340: public BasicRow(AttributeTablePanel panel, int index) {
341: this .panel = panel;
342: this .index = index;
343: }
344:
345: public boolean isFirstRow() {
346: return (panel.getModel().getLayer() == getModel()
347: .getLayers().get(0))
348: && (index == 0);
349: }
350:
351: public boolean isLastRow() {
352: return (panel.getModel().getLayer() == getModel()
353: .getLayers().get(getModel().getLayers().size() - 1))
354: && (index == (panel.getTable().getRowCount() - 1));
355: }
356:
357: public AttributeTablePanel getPanel() {
358: return panel;
359: }
360:
361: public int getIndex() {
362: return index;
363: }
364:
365: public Row previousRow() {
366: if (isFirstRow()) {
367: return this ;
368: }
369: if (index > 0) {
370: return new BasicRow(panel, index - 1);
371: }
372: return new BasicRow(previousPanel(), previousPanel()
373: .getTable().getRowCount() - 1);
374: }
375:
376: public Row nextRow() {
377: if (isLastRow()) {
378: return this ;
379: }
380: if (index < (panel.getTable().getRowCount() - 1)) {
381: return new BasicRow(panel, index + 1);
382: }
383: return new BasicRow(nextPanel(), 0);
384: }
385:
386: private AttributeTablePanel previousPanel() {
387: return getTablePanel(previousLayer());
388: }
389:
390: private AttributeTablePanel nextPanel() {
391: return getTablePanel(nextLayer());
392: }
393:
394: private Layer previousLayer() {
395: return (Layer) getModel().getLayers().get(
396: getModel().getLayers().indexOf(
397: panel.getModel().getLayer()) - 1);
398: }
399:
400: private Layer nextLayer() {
401: return (Layer) getModel().getLayers().get(
402: getModel().getLayers().indexOf(
403: panel.getModel().getLayer()) + 1);
404: }
405:
406: public Feature getFeature() {
407: return panel.getModel().getFeature(index);
408: }
409: }
410:
411: public SelectionManager getSelectionManager() {
412: return selectionManager;
413: }
414: }
|