1 /**************** <auto-copyright.pl BEGIN do not edit this line> **************
2 *
3 * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
4 *
5 * Original Authors:
6 * Allen Bierbaum, Christopher Just,
7 * Patrick Hartling, Kevin Meinert,
8 * Carolina Cruz-Neira, Albert Baker
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 *
25 * -----------------------------------------------------------------
26 * File: PopupButton.java,v
27 * Date modified: 2005/01/02 01:50:06
28 * Version: 1.4
29 * -----------------------------------------------------------------
30 *
31 *************** <auto-copyright.pl END do not edit this line> ***************/
32
33 package org.vrjuggler.vrjconfig;
34
35 import java.awt.*;
36 import java.awt.event.*;
37 import javax.swing.*;
38 import javax.swing.event.*;
39
40 /***
41 * This component acts very much like a button with an extra "popup" button
42 * attached to its side. When the popup button is clicked, a popup menu is
43 * displayed allowing the user to choose between many different options. The
44 * main part of the button when clicked invokes the first item on the popup
45 * menu as a sort of shortcut.
46 */
47 public class PopupButton
48 extends JComponent
49 {
50 public PopupButton()
51 {
52 try
53 {
54 jbInit();
55 }
56 catch(Exception e)
57 {
58 e.printStackTrace();
59 }
60
61
62 ClassLoader loader = PopupButton.class.getClassLoader();
63 try
64 {
65 arrowBtn.setIcon(new ImageIcon(loader.getResource("org/vrjuggler/vrjconfig/images/popdown.gif")));
66 }
67 catch (NullPointerException npe)
68 {
69
70 }
71
72
73 JPopupMenu menu = new JPopupMenu();
74 menu.add(new JMenuItem(""));
75 setPopupMenu(menu);
76 }
77
78 /***
79 * Sets the popup menu associated with this button.
80 *
81 * @param menu the menu to pop up when the popup button is depressed
82 *
83 * @throws IllegalArgumentException if menu is null
84 */
85 public void setPopupMenu(JPopupMenu menu)
86 {
87
88 if (menu == null)
89 {
90 throw new IllegalArgumentException("menu must not be null");
91 }
92
93
94 if (popupMenu != null)
95 {
96 popupMenu.removePopupMenuListener(popupListener);
97 }
98 menu.setVisible(false);
99 popupMenu = menu;
100 popupMenu.addPopupMenuListener(popupListener);
101
102
103 JMenuItem first_item = (JMenuItem)popupMenu.getComponent(0);
104 iconBtn.setIcon(first_item.getIcon());
105 iconBtn.setText(first_item.getText());
106 }
107
108 public JPopupMenu getPopupMenu()
109 {
110 return popupMenu;
111 }
112
113 public Dimension getMinimumSize()
114 {
115 return baseBox.getMinimumSize();
116 }
117
118 public Dimension getMaximumSize()
119 {
120 return baseBox.getMaximumSize();
121 }
122
123 public Dimension getPreferredSize()
124 {
125 return baseBox.getPreferredSize();
126 }
127
128 /***
129 * Internal helper that makes sure the popup menu is visible.
130 */
131 protected void showPopupMenu()
132 {
133 if (! popupMenu.isVisible())
134 {
135 popupMenu.getSelectionModel().setSelectedIndex(0);
136 popupMenu.show(arrowBtn, 0, arrowBtn.getHeight());
137 }
138 }
139
140
141 public void setEnabled(boolean enabled) {
142 iconBtn.setEnabled(enabled);
143 arrowBtn.setEnabled(enabled);
144 }
145
146 /***
147 * JBuilder automatically generated UI initialization.
148 */
149 private void jbInit()
150 throws Exception
151 {
152 baseBox = Box.createHorizontalBox();
153 this.setLayout(baseLayout);
154 this.setBorder(iconBtn.getBorder());
155
156
157
158
159
160
161
162
163
164 Insets insets = iconBtn.getMargin();
165 iconBtn.setMargin(new Insets(insets.top, insets.left, insets.bottom, 0));
166 iconBtn.setBorderPainted(false);
167 iconBtn.setFocusPainted(false);
168 arrowBtn.setMargin(new Insets(0, 0, 0, insets.right));
169 arrowBtn.setBorderPainted(false);
170 arrowBtn.setFocusPainted(false);
171 arrowBtn.addChangeListener(new ChangeListener()
172 {
173 public void stateChanged(ChangeEvent evt)
174 {
175
176 if (arrowBtn.getModel().isArmed() &&
177 arrowBtn.getModel().isPressed() &&
178 ! arrowBtn.getModel().isSelected())
179 {
180 showPopupMenu();
181 }
182 }
183 });
184 iconBtn.addChangeListener(new ChangeListener()
185 {
186 public void stateChanged(ChangeEvent evt)
187 {
188 arrowBtn.setSelected(iconBtn.getModel().isPressed() &&
189 iconBtn.getModel().isArmed());
190 }
191 });
192 iconBtn.addActionListener(new ActionListener()
193 {
194 public void actionPerformed(ActionEvent evt)
195 {
196 JMenuItem first_item = (JMenuItem)popupMenu.getComponent(0);
197 first_item.doClick();
198 }
199 });
200 this.add(baseBox, BorderLayout.CENTER);
201 baseBox.add(iconBtn, null);
202 baseBox.add(arrowBtn, null);
203 }
204
205
206 private BorderLayout baseLayout = new BorderLayout();
207 private Box baseBox;
208 private JButton iconBtn = new JButton();
209 private JToggleButton arrowBtn = new JToggleButton()
210 {
211 public Dimension getMaximumSize()
212 {
213 Dimension size = super.getMaximumSize();
214 Dimension icon_size = iconBtn.getMaximumSize();
215 if (icon_size.height > size.height)
216 {
217 size.height = icon_size.height;
218 }
219 return size;
220 }
221
222 public Dimension getPreferredSize()
223 {
224 Dimension size = super.getPreferredSize();
225 Dimension icon_size = iconBtn.getPreferredSize();
226 if (icon_size.height > size.height)
227 {
228 size.height = icon_size.height;
229 }
230 return size;
231 }
232 };
233
234 /***
235 * The popup menu that is shown when the popup button is pressed.
236 */
237 private JPopupMenu popupMenu;
238
239 /***
240 * Listens for change to the popup manu.
241 */
242 private PopupListener popupListener = new PopupListener();
243
244 /***
245 * Specialized listener for the popup menu.
246 */
247 class PopupListener
248 implements PopupMenuListener
249 {
250 public void popupMenuCanceled(PopupMenuEvent evt)
251 {
252 arrowBtn.getModel().setSelected(false);
253 }
254
255 public void popupMenuWillBecomeInvisible(PopupMenuEvent evt)
256 {
257 arrowBtn.getModel().setSelected(false);
258 }
259
260 public void popupMenuWillBecomeVisible(PopupMenuEvent evt)
261 {
262 }
263 }
264 }