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.FlowLayout;
036: import java.awt.GridLayout;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.util.ArrayList;
040: import java.util.HashMap;
041: import java.util.Iterator;
042:
043: import javax.swing.JButton;
044: import javax.swing.JPanel;
045:
046: import com.vividsolutions.jump.util.StringUtil;
047:
048: public class ButtonPanel extends JPanel {
049:
050: FlowLayout flowLayout1 = new FlowLayout();
051:
052: GridLayout gridLayout1 = new GridLayout();
053:
054: JPanel innerButtonPanel = new JPanel();
055:
056: public JButton getButton(String text) {
057: return (JButton) textToButtonMap.get(text);
058: }
059:
060: private ArrayList actionListeners = new ArrayList();
061:
062: // null if none selected
063: protected JButton selectedButton;
064:
065: private HashMap textToButtonMap = new HashMap();
066:
067: /**
068: * @param buttonText
069: * use ampersands to denote mnemonics
070: */
071: public ButtonPanel(String[] buttonText) {
072: innerButtonPanel.setLayout(gridLayout1);
073: this .setLayout(flowLayout1);
074: gridLayout1.setVgap(5);
075: gridLayout1.setHgap(5);
076: this .add(innerButtonPanel, null);
077: for (int i = 0; i < buttonText.length; i++) {
078: innerButtonPanel.add(createButton(buttonText[i]), null);
079: }
080: }
081:
082: private JButton createButton(String buttonText) {
083: final JButton button = new JButton(StringUtil.replaceAll(
084: buttonText, "&", ""));
085: button.setMnemonic(buttonText.indexOf("&") > -1 ? buttonText
086: .charAt(buttonText.indexOf("&") + 1) : buttonText
087: .charAt(0));
088: button.addActionListener(new ActionListener() {
089:
090: public void actionPerformed(ActionEvent e) {
091: selectedButton = button;
092: fireActionPerformed();
093: }
094: });
095: textToButtonMap.put(button.getText(), button);
096: return button;
097: }
098:
099: public void addActionListener(ActionListener l) {
100: this .actionListeners.add(l);
101: }
102:
103: public void removeActionListener(ActionListener l) {
104: this .actionListeners.remove(l);
105: }
106:
107: private void fireActionPerformed() {
108: for (Iterator i = actionListeners.iterator(); i.hasNext();) {
109: ActionListener l = (ActionListener) i.next();
110: l.actionPerformed(new ActionEvent(this , 0, null));
111: }
112: }
113:
114: /**
115: *
116: * @return null if no button selected
117: */
118: public JButton getSelectedButton() {
119: return selectedButton;
120: }
121:
122: public void setSelectedButton(JButton selectedButton) {
123: this.selectedButton = selectedButton;
124: }
125: }
|