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.toolbox;
034:
035: import java.awt.BorderLayout;
036: import java.awt.GridLayout;
037: import java.awt.event.*;
038: import java.util.ArrayList;
039: import java.util.Iterator;
040:
041: import javax.swing.*;
042:
043: import com.vividsolutions.jump.workbench.WorkbenchContext;
044: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
045: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
046: import com.vividsolutions.jump.workbench.plugin.PlugIn;
047: import com.vividsolutions.jump.workbench.ui.GUIUtil;
048: import com.vividsolutions.jump.workbench.ui.WorkbenchToolBar;
049: import com.vividsolutions.jump.workbench.ui.cursortool.CursorTool;
050:
051: /**
052: * An always-on-top modeless dialog with a WorkbenchToolBar (for CursorTools,
053: * PlugIns, and other buttons). Takes care of unpressing the CursorTools (if
054: * necessary) when the dialog is closed (and pressing the first CursorTool on
055: * the main toolbar).
056: */
057:
058: //Must use modeless dialog rather than JInternalFrame; otherwise, if we
059: // implement
060: //it as an internal frame, when we click it, the original TaskFrame will
061: // deactivate,
062: //thus deactivating the current CursorTool. [Jon Aquino]
063: public class ToolboxDialog extends JDialog {
064: public AbstractButton getButton(Class cursorToolClass) {
065: for (Iterator i = toolBars.iterator(); i.hasNext();) {
066: WorkbenchToolBar toolBar = (WorkbenchToolBar) i.next();
067: AbstractButton button = toolBar.getButton(cursorToolClass);
068: if (button != null) {
069: return button;
070: }
071: }
072: return null;
073: }
074:
075: public WorkbenchToolBar getToolBar() {
076: if (toolBars.isEmpty()) {
077: addToolBar();
078: }
079: return (WorkbenchToolBar) toolBars.get(toolBars.size() - 1);
080: }
081:
082: public WorkbenchContext getContext() {
083: return context;
084: }
085:
086: public WorkbenchToolBar.ToolConfig add(CursorTool tool) {
087: return add(tool, null);
088: }
089:
090: /**
091: * @param enableCheck
092: * null to leave unspecified
093: */
094: public WorkbenchToolBar.ToolConfig add(CursorTool tool,
095: EnableCheck enableCheck) {
096: WorkbenchToolBar.ToolConfig config = getToolBar()
097: .addCursorTool(tool);
098: JToggleButton button = config.getButton();
099: getToolBar().setEnableCheck(
100: button,
101: enableCheck != null ? enableCheck
102: : new MultiEnableCheck());
103: registerButton(button, enableCheck);
104: return config;
105: }
106:
107: public void addPlugIn(PlugIn plugIn, EnableCheck enableCheck,
108: Icon icon) {
109: registerButton(getToolBar().addPlugIn(icon, plugIn,
110: enableCheck, context), enableCheck);
111: }
112:
113: private void registerButton(AbstractButton button,
114: EnableCheck enableCheck) {
115: buttons.add(button);
116: }
117:
118: private ArrayList buttons = new ArrayList();
119:
120: private ArrayList toolBars = new ArrayList();
121:
122: public void addToolBar() {
123: toolBars.add(new WorkbenchToolBar(context, context
124: .getWorkbench().getFrame().getToolBar()
125: .getButtonGroup()));
126: getToolBar().setBorder(null);
127: getToolBar().setFloatable(false);
128: gridLayout1.setRows(toolBars.size());
129: toolbarsPanel.add(getToolBar());
130: }
131:
132: public ToolboxDialog(final WorkbenchContext context) {
133: super (context.getWorkbench().getFrame(), "", false);
134: try {
135: jbInit();
136: } catch (Exception e) {
137: e.printStackTrace();
138: }
139: this .context = context;
140: setResizable(true);
141: setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
142: this .addComponentListener(new ComponentAdapter() {
143: public void componentHidden(ComponentEvent e) {
144: if (buttons.contains(context.getWorkbench().getFrame()
145: .getToolBar().getSelectedCursorToolButton())) {
146: ((AbstractButton) context.getWorkbench().getFrame()
147: .getToolBar().getButtonGroup()
148: .getElements().nextElement()).doClick();
149: }
150: }
151: });
152: }
153:
154: /**
155: * Call this method after all the CursorTools have been added.
156: */
157: public void finishAddingComponents() {
158: pack();
159: // #initializeLocation will be called again in #setVisible, but
160: // call it here just in case the window is realized by some other
161: // means than #setVisible (unlikely). [Jon Aquino 2005-03-14]
162: initializeLocation();
163: }
164:
165: public void setVisible(boolean visible) {
166: if (visible && !locationInitializedBeforeMakingDialogVisible) {
167: // #initializeLocation was called in #finishAddingComponents,
168: // but the Workbench may have moved since then, so call
169: // #initializeLocation again just before making the dialog
170: // visible. [Jon Aquino 2005-03-14]
171: initializeLocation();
172: locationInitializedBeforeMakingDialogVisible = true;
173: }
174: super .setVisible(visible);
175: }
176:
177: private boolean locationInitializedBeforeMakingDialogVisible = false;
178:
179: private void initializeLocation() {
180: GUIUtil.setLocation(this , initialLocation, context
181: .getWorkbench().getFrame().getDesktopPane());
182: }
183:
184: private GUIUtil.Location initialLocation = new GUIUtil.Location(0,
185: false, 0, false);
186:
187: private WorkbenchContext context;
188:
189: private BorderLayout borderLayout1 = new BorderLayout();
190:
191: private JPanel centerPanel = new JPanel();
192:
193: private BorderLayout borderLayout2 = new BorderLayout();
194:
195: private JPanel toolbarsPanel = new JPanel();
196:
197: private GridLayout gridLayout1 = new GridLayout();
198:
199: private void jbInit() throws Exception {
200: this .getContentPane().setLayout(borderLayout1);
201: centerPanel.setLayout(borderLayout2);
202: toolbarsPanel.setLayout(gridLayout1);
203: gridLayout1.setColumns(1);
204: this .getContentPane().add(centerPanel, BorderLayout.CENTER);
205: centerPanel.add(toolbarsPanel, BorderLayout.NORTH);
206: }
207:
208: public JPanel getCenterPanel() {
209: return centerPanel;
210: }
211:
212: public void updateEnabledState() {
213: for (Iterator i = toolBars.iterator(); i.hasNext();) {
214: WorkbenchToolBar toolBar = (WorkbenchToolBar) i.next();
215: toolBar.updateEnabledState();
216: }
217: }
218:
219: public void setInitialLocation(GUIUtil.Location location) {
220: initialLocation = location;
221: }
222:
223: }
|