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.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.MouseEvent;
038: import java.util.Enumeration;
039: import java.util.HashMap;
040:
041: import javax.swing.AbstractButton;
042: import javax.swing.ButtonGroup;
043: import javax.swing.Icon;
044: import javax.swing.JButton;
045: import javax.swing.JComponent;
046: import javax.swing.JToggleButton;
047:
048: import com.vividsolutions.jts.util.Assert;
049: import com.vividsolutions.jump.workbench.WorkbenchContext;
050: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
051: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
052: import com.vividsolutions.jump.workbench.plugin.PlugIn;
053: import com.vividsolutions.jump.workbench.ui.cursortool.CursorTool;
054: import com.vividsolutions.jump.workbench.ui.cursortool.QuasimodeTool;
055: import com.vividsolutions.jump.workbench.ui.task.TaskMonitorManager;
056:
057: /**
058: * Makes it easy to add CursorTools and PlugIns as toolbar buttons.
059: * An "EnableCheck" is used to specify whether to enable or disable the
060: * CursorTool buttons. Moreover, CursorTools are added as mutually exclusive
061: * toggle buttons (that is, JToggleButtons in a ButtonGroup). When a CursorTool
062: * button is pressed, the current CursorTool is unregistered and the new one
063: * is registered with the LayerViewPanel.
064: * <P>
065: * Set the cursor-tool-enable-check to use context-sensitive enabling of toolbar
066: * buttons.
067: * <P>
068: * Set the task-monitor-manager to report the progress of threaded plug-ins.
069: */
070: public class WorkbenchToolBar extends EnableableToolBar {
071: private HashMap cursorToolClassToButtonMap = new HashMap();
072:
073: private LayerViewPanelProxy layerViewPanelProxy;
074: private TaskMonitorManager taskMonitorManager = null;
075:
076: public AbstractButton getButton(Class cursorToolClass) {
077: Assert.isTrue(CursorTool.class
078: .isAssignableFrom(cursorToolClass));
079: return (AbstractButton) cursorToolClassToButtonMap
080: .get(cursorToolClass);
081: }
082:
083: // By default, CursorTool buttons are always enabled. [Jon Aquino]
084: private EnableCheck cursorToolEnableCheck = new EnableCheck() {
085: public String check(JComponent component) {
086: return null;
087: }
088: };
089:
090: private ButtonGroup cursorToolButtonGroup;
091:
092: public WorkbenchToolBar(LayerViewPanelProxy layerViewPanelProxy) {
093: this (layerViewPanelProxy, new ButtonGroup());
094: }
095:
096: public WorkbenchToolBar(LayerViewPanelProxy layerViewPanelProxy,
097: ButtonGroup cursorToolButtonGroup) {
098: this .cursorToolButtonGroup = cursorToolButtonGroup;
099: this .layerViewPanelProxy = layerViewPanelProxy;
100: }
101:
102: public void setCursorToolEnableCheck(
103: EnableCheck cursorToolEnableCheck) {
104: this .cursorToolEnableCheck = cursorToolEnableCheck;
105: }
106:
107: public void setTaskMonitorManager(
108: TaskMonitorManager taskMonitorManager) {
109: this .taskMonitorManager = taskMonitorManager;
110: }
111:
112: public ToolConfig addCursorTool(final CursorTool cursorTool) {
113: return addCursorTool(cursorTool.getName(), cursorTool,
114: new JToggleButton() {
115: public String getToolTipText(MouseEvent event) {
116: //Get tooltip text dynamically [Jon Aquino 11/13/2003]
117: return cursorTool.getName();
118: }
119: });
120: }
121:
122: public static class ToolConfig {
123: private JToggleButton button;
124: private QuasimodeTool quasimodeTool;
125:
126: public ToolConfig(JToggleButton button,
127: QuasimodeTool quasimodeTool) {
128: this .button = button;
129: this .quasimodeTool = quasimodeTool;
130: }
131:
132: public JToggleButton getButton() {
133: return button;
134: }
135:
136: public QuasimodeTool getQuasimodeTool() {
137: return quasimodeTool;
138: }
139:
140: }
141:
142: public ToolConfig addCursorTool(String tooltip,
143: final CursorTool cursorTool) {
144: JToggleButton button = new JToggleButton();
145: return addCursorTool(tooltip, cursorTool, button);
146: }
147:
148: private ToolConfig addCursorTool(String tooltip,
149: final CursorTool cursorTool, JToggleButton button) {
150: cursorToolButtonGroup.add(button);
151: cursorToolClassToButtonMap.put(cursorTool.getClass(), button);
152: final QuasimodeTool quasimodeTool = QuasimodeTool
153: .addStandardQuasimodes(cursorTool);
154: add(button, tooltip, cursorTool.getIcon(),
155: new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: //It's null when the Workbench starts up. [Jon Aquino]
158: //Or the active frame may not have a LayerViewPanel. [Jon Aquino]
159: if (layerViewPanelProxy.getLayerViewPanel() != null) {
160: layerViewPanelProxy
161: .getLayerViewPanel()
162: .setCurrentCursorTool(quasimodeTool);
163: }
164:
165: //<<TODO:DESIGN>> We really shouldn't create a new LeftClickFilter on each
166: //click of the tool button. Not a big deal though. [Jon Aquino]
167: }
168: }, cursorToolEnableCheck);
169: if (cursorToolButtonGroup.getButtonCount() == 1) {
170: cursorToolButtonGroup.setSelected(button.getModel(), true);
171: reClickSelectedCursorToolButton();
172: }
173: return new ToolConfig(button, quasimodeTool);
174: }
175:
176: public ButtonGroup getButtonGroup() {
177: return cursorToolButtonGroup;
178: }
179:
180: public JToggleButton getSelectedCursorToolButton() {
181: for (Enumeration e = cursorToolButtonGroup.getElements(); e
182: .hasMoreElements();) {
183: JToggleButton button = (JToggleButton) e.nextElement();
184:
185: if (button.getModel() == cursorToolButtonGroup
186: .getSelection()) {
187: return button;
188: }
189: }
190:
191: Assert.shouldNeverReachHere();
192:
193: return null;
194: }
195:
196: public void reClickSelectedCursorToolButton() {
197: if (cursorToolButtonGroup.getButtonCount() == 0) {
198: return;
199: }
200: getSelectedCursorToolButton().doClick();
201: }
202:
203: //<<TODO:REFACTOR>> This method duplicates code in FeatureInstaller, with the
204: //result that when the latter was updated (to handle ThreadedPlugIns), the
205: //changes were left out from the former. [Jon Aquino]
206: public JButton addPlugIn(Icon icon, final PlugIn plugIn,
207: EnableCheck enableCheck, WorkbenchContext workbenchContext) {
208: JButton button = new JButton();
209: add(button, plugIn.getName(), icon, AbstractPlugIn
210: .toActionListener(plugIn, workbenchContext,
211: taskMonitorManager), enableCheck);
212:
213: return button;
214: }
215: }
|