2
* Copyright (c) 2005-2010 Flamingo Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Flamingo Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.flamingo.api.common.popup;
32
import java.awt.Component;
33
import java.util.ArrayList;
34
import java.util.Collections;
37
import javax.swing.JPopupMenu.Separator;
38
import javax.swing.event.ChangeEvent;
39
import javax.swing.event.ChangeListener;
41
import org.pushingpixels.flamingo.api.common.*;
42
import org.pushingpixels.flamingo.internal.ui.common.popup.BasicCommandPopupMenuUI;
43
import org.pushingpixels.flamingo.internal.ui.common.popup.PopupPanelUI;
46
* Popup menu. Can host any number of command menu buttons added with
47
* {@link #addMenuButton(JCommandMenuButton)} separated with optional
48
* {@link #addMenuSeparator()}. The
49
* {@link #JCommandPopupMenu(JCommandButtonPanel, int, int)} constructor allows
50
* placing a scrollable command button panel in the top part of the popup menu.
52
* @author Kirill Grouchnikov
54
public class JCommandPopupMenu extends JPopupPanel {
58
public static final String uiClassID = "CommandPopupMenuUI";
61
* The main button panel. Can be <code>null</code> if this command popup
62
* menu was created with the {@link #JCommandPopupMenu()} constructor.
64
* @see #JCommandPopupMenu(JCommandButtonPanel, int, int)
65
* @see #hasCommandButtonPanel()
66
* @see #getMainButtonPanel()
68
protected JCommandButtonPanel mainButtonPanel;
71
* Menu components. This list holds:
73
* <li>{@link JCommandMenuButton}s added with
74
* {@link #addMenuButton(JCommandMenuButton)}</li>
75
* <li>{@link JCommandToggleMenuButton}s added with
76
* {@link #addMenuButton(JCommandToggleMenuButton)}</li>
77
* <li>{@link Separator}s added with {@link #addMenuSeparator()}</li>
78
* <li>{@link JPanel}s added by the subclasses with
79
* {@link #addMenuPanel(JPanel)}</li>
82
* @see #addMenuButton(JCommandMenuButton)
83
* @see #addMenuButton(JCommandToggleMenuButton)
84
* @see #addMenuSeparator()
85
* @see #addMenuPanel(JPanel)
86
* @see #getMenuComponents()
88
protected java.util.List<Component> menuComponents;
91
* Maximum number of button columns visible in the {@link #mainButtonPanel}.
93
* @see #JCommandPopupMenu(JCommandButtonPanel, int, int)
94
* @see #getMaxButtonColumns()
96
protected int maxButtonColumns;
99
* Maximum number of button rows visible in the {@link #mainButtonPanel}.
101
* @see #JCommandPopupMenu(JCommandButtonPanel, int, int)
102
* @see #getMaxVisibleButtonRows()
104
protected int maxVisibleButtonRows;
107
* Maximum number of menu items visible in this menu. If more buttons are
108
* added with the {@link #addMenuButton(JCommandMenuButton)} and
109
* {@link #addMenuButton(JCommandToggleMenuButton)} APIs, the menu part will
110
* show scroller buttons above the first and below the last menu button. If
111
* the value is negative, there is no limitation on how many menu buttons
112
* are shown, and the entire popup menu can overflow the monitor edges.
114
protected int maxVisibleMenuButtons;
116
private boolean toDismissOnChildClick;
119
* Creates an empty popup menu with no button panel.
121
public JCommandPopupMenu() {
122
this.menuComponents = new ArrayList<Component>();
124
this.maxVisibleMenuButtons = -1;
125
this.toDismissOnChildClick = true;
129
* Creates a popup menu hosting the specified button panel.
132
* Fully constructed button panel.
133
* @param maxButtonColumns
134
* Maximum number of button columns visible in
135
* <code>buttonPanel</code>.
136
* @param maxVisibleButtonRows
137
* Maximum number of button rows visible in
138
* <code>buttonPanel</code>.
140
public JCommandPopupMenu(JCommandButtonPanel buttonPanel,
141
int maxButtonColumns, int maxVisibleButtonRows) {
144
this.mainButtonPanel = buttonPanel;
145
this.maxButtonColumns = maxButtonColumns;
146
this.maxVisibleButtonRows = maxVisibleButtonRows;
152
* Adds the specified menu button to this menu.
155
* Menu button to add.
157
public void addMenuButton(JCommandMenuButton menuButton) {
158
menuButton.setHorizontalAlignment(SwingUtilities.LEFT);
159
this.menuComponents.add(menuButton);
160
this.fireStateChanged();
164
* Adds the specified toggle menu button to this menu.
167
* Menu button to add.
169
public void addMenuButton(JCommandToggleMenuButton menuButton) {
170
menuButton.setHorizontalAlignment(SwingUtilities.LEFT);
171
this.menuComponents.add(menuButton);
172
this.fireStateChanged();
176
* Adds a menu separator to this menu.
178
public void addMenuSeparator() {
179
this.menuComponents.add(new JPopupMenu.Separator());
180
this.fireStateChanged();
184
* Adds a menu panel to this menu.
189
protected void addMenuPanel(JPanel menuPanel) {
190
if (this.maxVisibleMenuButtons > 0) {
191
throw new IllegalStateException(
192
"This method is not supported on menu that contains a command button panel");
194
this.menuComponents.add(menuPanel);
195
this.fireStateChanged();
199
* Returns indication whether this menu has a command button panel.
201
* @return <code>true</code> if this menu has a command button panel,
202
* <code>false</code> otherwise.
203
* @see #getMainButtonPanel()
205
public boolean hasCommandButtonPanel() {
206
return (this.mainButtonPanel != null);
210
* Returns the command button panel of this menu. Can return
213
* @return The command button panel of this menu.
214
* @see #hasCommandButtonPanel()
216
public JCommandButtonPanel getMainButtonPanel() {
217
return this.mainButtonPanel;
221
* Returns an unmodifiable list of all the menu components. Can return
224
* @return An unmodifiable list of all the menu components
226
public java.util.List<Component> getMenuComponents() {
227
if (this.menuComponents == null)
229
return Collections.unmodifiableList(this.menuComponents);
233
* Returns the maximum number of button columns visible in the command
234
* button panel of this menu. If this menu has been created with the
235
* {@link #JCommandPopupMenu()} constructor, zero is returned.
237
* @return The maximum number of button columns visible in the command
238
* button panel of this menu.
239
* @see #JCommandPopupMenu(JCommandButtonPanel, int, int)
240
* @see #getMaxVisibleButtonRows()
242
public int getMaxButtonColumns() {
243
return this.maxButtonColumns;
247
* Returns the maximum number of button rows visible in the command button
248
* panel of this menu. If this menu has been created with the
249
* {@link #JCommandPopupMenu()} constructor, zero is returned.
251
* @return The maximum number of button rows visible in the command button
252
* panel of this menu.
253
* @see #JCommandPopupMenu(JCommandButtonPanel, int, int)
254
* @see #getMaxButtonColumns()
256
public int getMaxVisibleButtonRows() {
257
return this.maxVisibleButtonRows;
261
* Returns the maximum number of menu items visible in this menu.
263
* @return The maximum number of menu items visible in this menu. If the
264
* value is negative, there is no limitation on how many menu
265
* buttons are shown, and the entire popup menu can overflow the
268
public int getMaxVisibleMenuButtons() {
269
return this.maxVisibleMenuButtons;
273
* Sets the maximum number of menu items visible in this menu. If the value
274
* is negative, there is no limitation on how many menu buttons are shown,
275
* and the entire popup menu can overflow the monitor edges.
277
* @param maxVisibleMenuButtons
278
* The new value for the maximum number of menu items visible in
281
public void setMaxVisibleMenuButtons(int maxVisibleMenuButtons) {
282
for (Component menuComp : this.menuComponents) {
283
if (menuComp instanceof JPanel) {
284
throw new IllegalStateException(
285
"This method is not supported on menus with panels");
289
int old = this.maxVisibleMenuButtons;
290
this.maxVisibleMenuButtons = maxVisibleMenuButtons;
292
if (old != this.maxVisibleMenuButtons) {
293
this.firePropertyChange("maxVisibleMenuButtons", old,
294
this.maxVisibleMenuButtons);
299
public String getUIClassID() {
304
public void updateUI() {
305
if (UIManager.get(getUIClassID()) != null) {
306
setUI((PopupPanelUI) UIManager.getUI(this));
308
setUI(BasicCommandPopupMenuUI.createUI(this));
313
* Adds the specified change listener to track changes to this popup menu.
316
* Change listener to add.
317
* @see #removeChangeListener(ChangeListener)
319
public void addChangeListener(ChangeListener l) {
320
this.listenerList.add(ChangeListener.class, l);
324
* Removes the specified change listener from tracking changes to this popup
328
* Change listener to remove.
329
* @see #addChangeListener(ChangeListener)
331
public void removeChangeListener(ChangeListener l) {
332
this.listenerList.remove(ChangeListener.class, l);
336
* Notifies all registered listener that the state of this popup menu has
339
protected void fireStateChanged() {
340
// Guaranteed to return a non-null array
341
Object[] listeners = this.listenerList.getListenerList();
342
// Process the listeners last to first, notifying
343
// those that are interested in this event
344
ChangeEvent event = new ChangeEvent(this);
345
for (int i = listeners.length - 2; i >= 0; i -= 2) {
346
if (listeners[i] == ChangeListener.class) {
347
((ChangeListener) listeners[i + 1]).stateChanged(event);
352
public boolean isToDismissOnChildClick() {
353
return toDismissOnChildClick;
356
public void setToDismissOnChildClick(boolean toDismissOnChildClick) {
357
this.toDismissOnChildClick = toDismissOnChildClick;