001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.base;
019:
020: import java.awt.Insets;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.util.Iterator;
026: import java.util.Vector;
027:
028: import javax.swing.ButtonGroup;
029: import javax.swing.JButton;
030: import javax.swing.JPopupMenu;
031: import javax.swing.JRadioButtonMenuItem;
032: import javax.swing.SwingConstants;
033:
034: /**
035: * Resembles a JComboBox, using a JButton and a JMenu. This has the advantage
036: * that the user immediately sees all available items.
037: * <p>
038: * Use ItemListener to get notified of selection changes.
039: * <p>
040: * TODO (@author fdietz): use JComboBox button layout/ui
041: *
042: * @author fdietz
043: */
044:
045: public class ComboMenu extends JButton implements ActionListener {
046:
047: protected JPopupMenu popupMenu;
048:
049: protected Vector listeners;
050:
051: private ButtonGroup group;
052:
053: public ComboMenu() {
054: super ();
055:
056: //setIcon(ImageLoader.getImageIcon("stock_down-16.png"));
057: setIcon(new AscendingIcon());
058: setMargin(new Insets(1, 3, 1, 3));
059: setIconTextGap(12);
060:
061: setHorizontalTextPosition(SwingConstants.LEFT);
062:
063: listeners = new Vector();
064:
065: group = new ButtonGroup();
066:
067: popupMenu = new JPopupMenu();
068:
069: addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent evt) {
071: popupMenu.show(ComboMenu.this , 0, getHeight() - 2);
072: }
073: });
074: }
075:
076: /**
077: * @deprecated use default constructor instead and <code>addMenuItem</code>.
078: * this way the menu entries can be localized correctly
079: */
080: public ComboMenu(String[] list) {
081: this ();
082:
083: for (int i = 0; i < list.length; i++) {
084:
085: if (i == 0)
086: setText(list[i]);
087:
088: if (list[i].equalsIgnoreCase("separator")) {
089: popupMenu.addSeparator();
090: } else {
091: addMenuItem(list[i], list[i]);
092: }
093: }
094:
095: }
096:
097: public JRadioButtonMenuItem addMenuItem(String name,
098: String localizedName) {
099:
100: JRadioButtonMenuItem m = new JRadioButtonMenuItem(localizedName);
101:
102: m.setActionCommand(name);
103:
104: m.addActionListener(this );
105:
106: group.add(m);
107:
108: popupMenu.add(m);
109:
110: if (popupMenu.getComponentCount() == 1) {
111: setText(localizedName);
112: m.setSelected(true);
113: }
114:
115: return m;
116: }
117:
118: public void addSeparator() {
119: popupMenu.addSeparator();
120: }
121:
122: /**
123: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
124: */
125: public void actionPerformed(ActionEvent arg0) {
126: String action = arg0.getActionCommand();
127: JRadioButtonMenuItem m = (JRadioButtonMenuItem) arg0
128: .getSource();
129:
130: setText(m.getText());
131:
132: fireItemStateChanged(new ItemEvent(this , 0, action,
133: ItemEvent.SELECTED));
134:
135: }
136:
137: public void addItemListener(ItemListener l) {
138: listeners.add(l);
139: }
140:
141: public void setSelectedItem(int nr) {
142: JRadioButtonMenuItem item = (JRadioButtonMenuItem) popupMenu
143: .getComponent(0);
144:
145: setText(item.getText());
146: fireItemStateChanged(new ItemEvent(this , 0, item
147: .getActionCommand(), ItemEvent.SELECTED));
148: }
149:
150: protected void fireItemStateChanged(ItemEvent event) {
151: Iterator it = listeners.iterator();
152: while (it.hasNext()) {
153: ItemListener l = (ItemListener) it.next();
154: l.itemStateChanged(event);
155: }
156: }
157: }
|