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.Dimension;
036: import java.awt.Insets;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.util.HashMap;
040: import java.util.Iterator;
041:
042: import javax.swing.AbstractButton;
043: import javax.swing.Icon;
044: import javax.swing.JComponent;
045: import javax.swing.JPanel;
046: import javax.swing.JToolBar;
047:
048: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
049:
050: /**
051: * Extends JToolBar to create an {@link JToolBar} with
052: * certain buttons enabled (for saving state).
053: */
054:
055: public class EnableableToolBar extends JToolBar {
056: protected HashMap buttonToEnableCheckMap = new HashMap();
057:
058: public EnableCheck getEnableCheck(AbstractButton button) {
059: return (EnableCheck) buttonToEnableCheckMap.get(button);
060: }
061:
062: public void setEnableCheck(AbstractButton button, EnableCheck check) {
063: buttonToEnableCheckMap.put(button, check);
064: }
065:
066: public EnableableToolBar() {
067: }
068:
069: public void updateEnabledState() {
070: for (Iterator i = buttonToEnableCheckMap.keySet().iterator(); i
071: .hasNext();) {
072: JComponent component = (JComponent) i.next();
073: EnableCheck enableCheck = (EnableCheck) buttonToEnableCheckMap
074: .get(component);
075: component.setEnabled(enableCheck.check(component) == null);
076: }
077: //Strange -- occasionally I've seen the depressed cursor tool enabled and
078: //all the other tools disabled. Maybe it's a bug in Java 1.3? [Jon Aquino]
079: }
080:
081: /**
082: * Unlike #addSeparator, works for vertical toolbars.
083: */
084: public void addSpacer() {
085: JPanel filler = new JPanel();
086: filler.setPreferredSize(new Dimension(5, 5));
087: filler.setMinimumSize(new Dimension(5, 5));
088: filler.setMaximumSize(new Dimension(5, 5));
089: add(filler);
090: }
091:
092: public void add(AbstractButton button, String tooltip, Icon icon,
093: ActionListener actionListener, EnableCheck enableCheck) {
094: if (enableCheck != null) {
095: buttonToEnableCheckMap.put(button, enableCheck);
096: }
097: button.setIcon(icon);
098: button.setMargin(new Insets(0, 0, 0, 0));
099: button.setToolTipText(tooltip);
100: button.addActionListener(actionListener);
101: button.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: updateEnabledState();
104: }
105: });
106: add(button);
107: }
108:
109: }
|