// go over all visible ribbon tasks and create a toggle button
// for each one of them
List<RibbonTask> visibleTasks = this.getCurrentlyShownRibbonTasks();
final RibbonTask selectedTask = this.ribbon.getSelectedTask();
for (final RibbonTask task : visibleTasks) {
final JRibbonTaskToggleButton taskToggleButton = new JRibbonTaskToggleButton(
task);
taskToggleButton.setKeyTip(task.getKeyTip());
// wire listener to select the task when the button is
// selected
taskToggleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
scrollAndRevealTaskToggleButton(taskToggleButton);
ribbon.setSelectedTask(task);
// System.out.println("Button click on "
// + task.getTitle() + ", ribbon minimized? "
// + ribbon.isMinimized());
if (ribbon.isMinimized()) {
if (Boolean.TRUE.equals(ribbon
.getClientProperty(JUST_MINIMIZED))) {
ribbon.putClientProperty(JUST_MINIMIZED,
null);
return;
}
// special case - do we have this task currently
// shown in a popup?
List<PopupPanelManager.PopupInfo> popups = PopupPanelManager
.defaultManager().getShownPath();
if (popups.size() > 0) {
for (PopupPanelManager.PopupInfo popup : popups) {
if (popup.getPopupOriginator() == taskToggleButton) {
// hide all popups and return (hides
// the task popup and does not
// show any additional popup).
PopupPanelManager.defaultManager()
.hidePopups(null);
return;
}
}
}
PopupPanelManager.defaultManager().hidePopups(
null);
ribbon.remove(bandScrollablePanel);
int prefHeight = bandScrollablePanel.getView()
.getPreferredSize().height;
Insets ins = ribbon.getInsets();
prefHeight += ins.top + ins.bottom;
AbstractRibbonBand band = (ribbon
.getSelectedTask().getBandCount() > 0) ? ribbon
.getSelectedTask().getBand(0)
: null;
if (band != null) {
Insets bandIns = band.getInsets();
prefHeight += bandIns.top + bandIns.bottom;
}
// System.out.println(prefHeight
// + ":"
// + bandScrollablePanel.getView()
// .getComponentCount());
JPopupPanel popupPanel = new BandHostPopupPanel(
bandScrollablePanel, new Dimension(
ribbon.getWidth(), prefHeight));
int x = ribbon.getLocationOnScreen().x;
int y = ribbon.getLocationOnScreen().y
+ ribbon.getHeight();
// make sure that the popup stays in
// bounds
Rectangle scrBounds = ribbon
.getGraphicsConfiguration().getBounds();
int pw = popupPanel.getPreferredSize().width;
if ((x + pw) > (scrBounds.x + scrBounds.width)) {
x = scrBounds.x + scrBounds.width - pw;
}
int ph = popupPanel.getPreferredSize().height;
if ((y + ph) > (scrBounds.y + scrBounds.height)) {
y = scrBounds.y + scrBounds.height - ph;
}
// get the popup and show it
popupPanel.setPreferredSize(new Dimension(
ribbon.getWidth(), prefHeight));
Popup popup = PopupFactory.getSharedInstance()
.getPopup(taskToggleButton, popupPanel,
x, y);
PopupPanelManager.PopupListener tracker = new PopupPanelManager.PopupListener() {
@Override
public void popupShown(PopupEvent event) {
JComponent originator = event
.getPopupOriginator();
if (originator instanceof JRibbonTaskToggleButton) {
bandScrollablePanel.doLayout();
bandScrollablePanel.repaint();
}
}
@Override
public void popupHidden(PopupEvent event) {
JComponent originator = event
.getPopupOriginator();
if (originator instanceof JRibbonTaskToggleButton) {
ribbon.add(bandScrollablePanel);
PopupPanelManager.defaultManager()
.removePopupListener(this);
ribbon.revalidate();
ribbon.doLayout();
ribbon.repaint();
}
}
};
PopupPanelManager.defaultManager()
.addPopupListener(tracker);
PopupPanelManager.defaultManager().addPopup(
taskToggleButton, popup, popupPanel);
}
}
});
}
});
// wire listener to toggle ribbon minimization on double
// mouse click
taskToggleButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if ((ribbon.getSelectedTask() == task)
&& (e.getClickCount() == 2)) {
boolean wasMinimized = ribbon.isMinimized();
ribbon.setMinimized(!wasMinimized);
if (!wasMinimized) {
// fix for issue 69 - mark the ribbon as
// "just minimized" to prevent the action handler
// of the toggle button to show the ribbon in
// popup mode
ribbon.putClientProperty(JUST_MINIMIZED,
Boolean.TRUE);
}
}
}
});
// set the background hue color on the tab buttons
// of tasks in contextual groups
if (task.getContextualGroup() != null) {
taskToggleButton.setContextualGroupHueColor(task
.getContextualGroup().getHueColor());
}
taskToggleButton.putClientProperty(
BasicCommandButtonUI.DONT_DISPOSE_POPUPS, Boolean.TRUE);
this.taskToggleButtonGroup.add(taskToggleButton);
taskToggleButtonsHostPanel.add(taskToggleButton);
this.taskToggleButtons.put(task, taskToggleButton);
}
JRibbonTaskToggleButton toSelect = this.taskToggleButtons
.get(selectedTask);
if (toSelect != null) {
toSelect.getActionModel().setSelected(true);
}
for (int i = 0; i < this.ribbon.getTaskCount(); i++) {
RibbonTask task = this.ribbon.getTask(i);
for (AbstractRibbonBand band : task.getBands()) {