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.ribbon;
32
import java.awt.event.ActionListener;
35
import javax.swing.SwingConstants;
37
import org.pushingpixels.flamingo.api.common.*;
38
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
39
import org.pushingpixels.flamingo.api.common.popup.JCommandPopupMenu;
40
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
41
import org.pushingpixels.flamingo.internal.ui.ribbon.*;
44
* Ribbon band component. Can host three types of content:
47
* <li>Command buttons added with
48
* {@link #addCommandButton(AbstractCommandButton, RibbonElementPriority)}.</li>
49
* <li>Wrapped core / 3rd party components added with
50
* {@link #addRibbonComponent(JRibbonComponent)} or
51
* {@link #addRibbonComponent(JRibbonComponent, int)}.</li>
52
* <li>Ribbon galleries added with
53
* {@link #addRibbonGallery(String, List, Map, int, int, RibbonElementPriority)}
58
* Command buttons are added with associated {@link RibbonElementPriority}. The
59
* higher the priority, the longer the button "stays" in the
60
* {@link CommandButtonDisplayState#BIG} or
61
* {@link CommandButtonDisplayState#MEDIUM} state - depending on the available
66
* Wrapped components can span one or multiple rows. Use the
67
* {@link #addRibbonComponent(JRibbonComponent, int)} API to add a wrapped
68
* component that spans more than one row.
72
* Once a ribbon gallery is added with
73
* {@link #addRibbonGallery(String, List, Map, int, int, RibbonElementPriority)}
74
* , you can use the following APIs to configure the content and behavior of
79
* <li>{@link #addRibbonGalleryButtons(String, String, JCommandToggleButton...)}
81
* <li>{@link #removeRibbonGalleryButtons(String, JCommandToggleButton...)}</li>
82
* <li>{@link #setSelectedRibbonGalleryButton(String, JCommandToggleButton)}</li>
83
* <li>{@link #setRibbonGalleryExpandKeyTip(String, String)}</li>
85
* {@link #setRibbonGalleryPopupCallback(String, RibbonGalleryPopupCallback)}</li>
89
* A ribbon band can have multiple visual groups separated with vertical
90
* separator lines. To start a new unnamed group use the {@link #startGroup()}
91
* API. To start a new named group use the {@link #startGroup(String)} API.
92
* Unnamed groups will have three rows of controls. Named groups will have two
93
* rows of controls, with the top row showing the group title.
96
* @author Kirill Grouchnikov
98
public class JRibbonBand extends AbstractRibbonBand<JBandControlPanel> {
100
* This callback allows application code to place additional menu entries in
101
* the popup menu shown when the ribbon gallery expand button is clicked.
102
* Application code should use
103
* {@link JCommandPopupMenu#addMenuButton(JCommandMenuButton)} and
104
* {@link JCommandPopupMenu#addMenuSeparator()} APIs on the passed menu
107
* @author Kirill Grouchnikov
109
public static interface RibbonGalleryPopupCallback {
111
* Called just before the popup menu is about to be shown.
114
* The popup menu that will be shown.
116
public void popupToBeShown(JCommandPopupMenu menu);
120
* Big size with landscape orientation. Used for buttons in in-ribbon
123
public static final CommandButtonDisplayState BIG_FIXED_LANDSCAPE = new CommandButtonDisplayState(
124
"Big Fixed Landscape", 32) {
126
public CommandButtonLayoutManager createLayoutManager(
127
AbstractCommandButton button) {
128
return new CommandButtonLayoutManagerBigFixedLandscape();
133
* Big size with landscape orientation. Used for buttons in in-ribbon
136
public static final CommandButtonDisplayState BIG_FIXED = new CommandButtonDisplayState(
139
public CommandButtonLayoutManager createLayoutManager(
140
AbstractCommandButton button) {
141
return new CommandButtonLayoutManagerBigFixed();
146
* Constructs a <code>JRibbonBand</code> specifying the title and the icon
147
* when in the collapsed state.
149
* This is equivalent to <code>JRibbonBand(title, icon, null)</code>.
151
* @see #JRibbonBand(String, ResizableIcon, ActionListener)
155
* the icon displayed when the band collapses
157
public JRibbonBand(String title, ResizableIcon icon) {
158
this(title, icon, null);
162
* Constructs a <code>JRibbonBand</code> specifying the title, the icon when
163
* in the collapsed state, and the action listener for when the band is
169
* the icon displayed when the band collapses
170
* @param expandActionListener
171
* Expand action listener (can be <code>null</code>).
173
public JRibbonBand(String title, ResizableIcon icon,
174
ActionListener expandActionListener) {
175
super(title, icon, expandActionListener, new JBandControlPanel());
176
this.resizePolicies = Collections
177
.unmodifiableList(CoreRibbonResizePolicies
178
.getCorePoliciesPermissive(this));
183
* Adds the specified command button to <code>this</code> band.
185
* @param commandButton
186
* the command button to add
188
* priority of the button
190
public void addCommandButton(AbstractCommandButton commandButton,
191
RibbonElementPriority priority) {
192
commandButton.setHorizontalAlignment(SwingConstants.LEFT);
193
this.controlPanel.addCommandButton(commandButton, priority);
196
public void addRibbonGallery(String galleryName,
197
List<StringValuePair<List<JCommandToggleButton>>> buttons,
198
Map<RibbonElementPriority, Integer> preferredVisibleButtonCounts,
199
int preferredPopupMaxButtonColumns,
200
int preferredPopupMaxVisibleButtonRows,
201
RibbonElementPriority priority) {
202
this.addRibbonGallery(galleryName, buttons,
203
preferredVisibleButtonCounts, preferredPopupMaxButtonColumns,
204
preferredPopupMaxVisibleButtonRows,
205
JRibbonBand.BIG_FIXED_LANDSCAPE, priority);
209
* Adds a new ribbon gallery to <code>this</code> band.
215
* @param preferredVisibleButtonCounts
216
* Preferred count of visible buttons of the ribbon gallery under
218
* @param preferredPopupMaxButtonColumns
219
* Preferred maximum columns in the popup gallery associated with
220
* the ribbon gallery.
221
* @param preferredPopupMaxVisibleButtonRows
222
* Preferred maximum visible rows in the popup gallery associated
223
* with the ribbon gallery.
225
* The initial ribbon gallery priority.
226
* @see #addRibbonGalleryButtons(String, String, JCommandToggleButton...)
227
* @see #removeRibbonGalleryButtons(String, JCommandToggleButton...)
228
* @see #setSelectedRibbonGalleryButton(String, JCommandToggleButton)
230
public void addRibbonGallery(String galleryName,
231
List<StringValuePair<List<JCommandToggleButton>>> buttons,
232
Map<RibbonElementPriority, Integer> preferredVisibleButtonCounts,
233
int preferredPopupMaxButtonColumns,
234
int preferredPopupMaxVisibleButtonRows,
235
CommandButtonDisplayState ribbonButtonDisplayState,
236
RibbonElementPriority priority) {
237
JRibbonGallery gallery = new JRibbonGallery();
238
gallery.setButtonDisplayState(ribbonButtonDisplayState);
239
gallery.setName(galleryName);
240
for (Map.Entry<RibbonElementPriority, Integer> prefCountEntry : preferredVisibleButtonCounts
242
gallery.setPreferredVisibleButtonCount(prefCountEntry.getKey(),
243
prefCountEntry.getValue());
245
gallery.setGroupMapping(buttons);
246
gallery.setPreferredPopupPanelDimension(preferredPopupMaxButtonColumns,
247
preferredPopupMaxVisibleButtonRows);
249
// this.controlPanel.addRibbonGallery(gallery, priority);
250
addRibbonGallery(gallery, priority);
254
* Adds the specified command toggle buttons to a button group in the
255
* specified ribbon gallery.
258
* Ribbon gallery name.
259
* @param buttonGroupName
263
* @see #addRibbonGallery(String, List, Map, int, int,
264
* RibbonElementPriority)
265
* @see #removeRibbonGalleryButtons(String, JCommandToggleButton...)
266
* @see #setSelectedRibbonGalleryButton(String, JCommandToggleButton)
268
public void addRibbonGalleryButtons(String galleryName,
269
String buttonGroupName, JCommandToggleButton... buttons) {
270
JRibbonGallery gallery = this.controlPanel
271
.getRibbonGallery(galleryName);
274
gallery.addRibbonGalleryButtons(buttonGroupName, buttons);
278
* Adds the <code>gallery</code> to the ribbon band with a priority of
279
* {@link RibbonElementPriority#TOP}.
284
public void addRibbonGallery(JRibbonGallery gallery) {
285
controlPanel.addRibbonGallery(gallery, RibbonElementPriority.TOP);
289
* Adds the <code>gallery</code> to the ribbon band with the specified
290
* <code>priority</code>.
295
* the gallery priority
297
public void addRibbonGallery(JRibbonGallery gallery,
298
RibbonElementPriority priority) {
299
controlPanel.addRibbonGallery(gallery, priority);
303
* Removes command toggle buttons from the specified ribbon gallery.
306
* Ribbon gallery name.
309
* @see #addRibbonGallery(String, List, Map, int, int,
310
* RibbonElementPriority)
311
* @see #addRibbonGalleryButtons(String, String, JCommandToggleButton...)
312
* @see #setSelectedRibbonGalleryButton(String, JCommandToggleButton)
314
public void removeRibbonGalleryButtons(String galleryName,
315
JCommandToggleButton... buttons) {
316
JRibbonGallery gallery = this.controlPanel
317
.getRibbonGallery(galleryName);
320
gallery.removeRibbonGalleryButtons(buttons);
324
* Selects the specified command toggle button in the specified ribbon
328
* Ribbon gallery name.
329
* @param buttonToSelect
331
* @see #addRibbonGallery(String, List, Map, int, int,
332
* RibbonElementPriority)
333
* @see #addRibbonGalleryButtons(String, String, JCommandToggleButton...)
334
* @see #removeRibbonGalleryButtons(String, JCommandToggleButton...)
336
public void setSelectedRibbonGalleryButton(String galleryName,
337
JCommandToggleButton buttonToSelect) {
338
JRibbonGallery gallery = this.controlPanel
339
.getRibbonGallery(galleryName);
342
gallery.setSelectedButton(buttonToSelect);
346
* Sets the display state for the buttons of the specified ribbon gallery.
349
* Ribbon gallery name.
350
* @param displayState
351
* Display state for the buttons of the matching ribbon gallery.
353
public void setRibbonGalleryButtonDisplayState(String galleryName,
354
CommandButtonDisplayState displayState) {
355
JRibbonGallery gallery = this.controlPanel
356
.getRibbonGallery(galleryName);
359
gallery.setButtonDisplayState(displayState);
363
* Sets the application callback to place additional entries in the popup
364
* menu shown when the specified ribbon gallery is expanded.
368
* @param popupCallback
369
* Application callback.
370
* @see RibbonGalleryPopupCallback
372
public void setRibbonGalleryPopupCallback(String galleryName,
373
RibbonGalleryPopupCallback popupCallback) {
374
JRibbonGallery gallery = this.controlPanel
375
.getRibbonGallery(galleryName);
378
gallery.setPopupCallback(popupCallback);
382
* Sets the key tip on the expand button of the specified ribbon gallery.
386
* @param expandKeyTip
387
* The key tip on the expand button of the specified ribbon
390
public void setRibbonGalleryExpandKeyTip(String galleryName,
391
String expandKeyTip) {
392
JRibbonGallery gallery = this.controlPanel
393
.getRibbonGallery(galleryName);
396
gallery.setExpandKeyTip(expandKeyTip);
400
* Adds the specified ribbon component to this ribbon band.
403
* The ribbon component to add.
405
public void addRibbonComponent(JRibbonComponent comp) {
406
this.controlPanel.addRibbonComponent(comp);
410
* Adds the specified ribbon component to this ribbon band.
413
* The ribbon component to add.
415
* Row span of the ribbon component.
416
* @throws IllegalArgumentException
417
* if the row span is not legal. Legal row span is 1..3 for
418
* unnamed groups and 1..2 for named groups.
420
* @see #startGroup(String)
422
public void addRibbonComponent(JRibbonComponent comp, int rowSpan) {
423
int groupCount = this.controlPanel.getControlPanelGroupCount();
424
String groupTitle = (groupCount > 0) ? this.controlPanel
425
.getControlPanelGroupTitle(groupCount - 1) : null;
426
int availableRows = (groupTitle == null) ? 3 : 2;
427
if ((rowSpan <= 0) || (rowSpan > availableRows)) {
428
throw new IllegalArgumentException(
429
"Row span value not supported. Should be in 1.."
430
+ availableRows + " range");
432
this.controlPanel.addRibbonComponent(comp, rowSpan);
436
* Starts a new unnamed group.
438
* @return The index of the new group.
440
public int startGroup() {
441
return this.controlPanel.startGroup();
445
* Starts a new named group.
449
* @return The index of the new group.
451
public int startGroup(String groupTitle) {
452
return this.controlPanel.startGroup(groupTitle);
456
* Changes the title of the specified group.
461
* The new title for this group.
463
public void setGroupTitle(int groupIndex, String groupTitle) {
464
this.controlPanel.setGroupTitle(groupIndex, groupTitle);
467
public List<JRibbonComponent> getRibbonComponents(int groupIndex) {
468
return this.controlPanel.getRibbonComponents(groupIndex);
474
* @see org.jvnet.flamingo.ribbon.AbstractRibbonBand#cloneBand()
477
public AbstractRibbonBand<JBandControlPanel> cloneBand() {
478
AbstractRibbonBand<JBandControlPanel> result = new JRibbonBand(
479
this.getTitle(), this.getIcon(), this.getExpandActionListener());
480
result.applyComponentOrientation(this.getComponentOrientation());