KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > ComboMenu


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.base;
19
20 import java.awt.Insets JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.awt.event.ItemListener JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JPopupMenu JavaDoc;
31 import javax.swing.JRadioButtonMenuItem JavaDoc;
32 import javax.swing.SwingConstants JavaDoc;
33
34 /**
35  * Resembles a JComboBox, using a JButton and a JMenu. This has the advantage
36  * that the user immediately sees all available items.
37  * <p>
38  * Use ItemListener to get notified of selection changes.
39  * <p>
40  * TODO (@author fdietz): use JComboBox button layout/ui
41  *
42  * @author fdietz
43  */

44
45 public class ComboMenu extends JButton JavaDoc implements ActionListener JavaDoc {
46
47     protected JPopupMenu JavaDoc popupMenu;
48
49     protected Vector JavaDoc listeners;
50
51     private ButtonGroup JavaDoc group;
52
53     public ComboMenu() {
54         super();
55
56         //setIcon(ImageLoader.getImageIcon("stock_down-16.png"));
57
setIcon(new AscendingIcon());
58         setMargin(new Insets JavaDoc(1, 3, 1, 3));
59         setIconTextGap(12);
60
61         setHorizontalTextPosition(SwingConstants.LEFT);
62
63         listeners = new Vector JavaDoc();
64
65         group = new ButtonGroup JavaDoc();
66
67         popupMenu = new JPopupMenu JavaDoc();
68
69         addActionListener(new ActionListener JavaDoc() {
70             public void actionPerformed(ActionEvent JavaDoc evt) {
71                 popupMenu.show(ComboMenu.this, 0, getHeight() - 2);
72             }
73         });
74     }
75
76     /**
77      * @deprecated use default constructor instead and <code>addMenuItem</code>.
78      * this way the menu entries can be localized correctly
79      */

80     public ComboMenu(String JavaDoc[] list) {
81         this();
82
83         for (int i = 0; i < list.length; i++) {
84
85             if (i == 0)
86                 setText(list[i]);
87
88             if (list[i].equalsIgnoreCase("separator")) {
89                 popupMenu.addSeparator();
90             } else {
91                 addMenuItem(list[i], list[i]);
92             }
93         }
94
95     }
96
97     public JRadioButtonMenuItem JavaDoc addMenuItem(String JavaDoc name, String JavaDoc localizedName) {
98
99         JRadioButtonMenuItem JavaDoc m = new JRadioButtonMenuItem JavaDoc(localizedName);
100
101         m.setActionCommand(name);
102
103         m.addActionListener(this);
104
105         group.add(m);
106
107         popupMenu.add(m);
108
109         if (popupMenu.getComponentCount() == 1) {
110             setText(localizedName);
111             m.setSelected(true);
112         }
113         
114         return m;
115     }
116
117     public void addSeparator() {
118         popupMenu.addSeparator();
119     }
120
121     /**
122      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
123      */

124     public void actionPerformed(ActionEvent JavaDoc arg0) {
125         String JavaDoc action = arg0.getActionCommand();
126         JRadioButtonMenuItem JavaDoc m = (JRadioButtonMenuItem JavaDoc) arg0.getSource();
127
128         setText(m.getText());
129
130         fireItemStateChanged(new ItemEvent JavaDoc(this, 0, action, ItemEvent.SELECTED));
131
132     }
133
134     public void addItemListener(ItemListener JavaDoc l) {
135         listeners.add(l);
136     }
137     
138     public void setSelectedItem( int nr ) {
139         JRadioButtonMenuItem JavaDoc item = (JRadioButtonMenuItem JavaDoc)popupMenu.getComponent(0);
140         
141         setText(item.getText());
142         fireItemStateChanged(new ItemEvent JavaDoc(this, 0, item.getActionCommand(), ItemEvent.SELECTED));
143     }
144
145     protected void fireItemStateChanged(ItemEvent JavaDoc event) {
146         Iterator JavaDoc it = listeners.iterator();
147         while (it.hasNext()) {
148             ItemListener JavaDoc l = (ItemListener JavaDoc) it.next();
149             l.itemStateChanged(event);
150         }
151     }
152 }
Popular Tags