Skip to content

Commit

Permalink
Strong reference the popup listener registered on popup ribbon tasks
Browse files Browse the repository at this point in the history
Fixes #472
  • Loading branch information
kirill-grouchnikov committed Jun 22, 2024
1 parent 07bc3c3 commit 8d9f079
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public abstract class BasicRibbonUI extends RibbonUI {
*/
protected Map<RibbonTask, JRibbonTaskToggleButton> taskToggleButtons;

// Strong references to popup listeners registered when a task is displayed in a popup (when
// the ribbon is minimized). Not having strong references can lead to the locally defined listener
// to be garbage collected and not notified when the ribbon is maximized, and the end result
// is the ribbon task content not getting re-parented back to the ribbon.
private Map<RibbonTask, PopupPanelManager.PopupListener> taskPopupListeners;

/**
* Group model for task toggle buttons.
*/
Expand All @@ -113,6 +119,7 @@ public abstract class BasicRibbonUI extends RibbonUI {
*/
protected BasicRibbonUI() {
this.taskToggleButtons = new HashMap<>();
this.taskPopupListeners = new HashMap<>();
this.taskToggleGroupModel = new CommandToggleGroupModel();
this.taskToggleGroupModel.setAllowsClearingSelection(false);
}
Expand Down Expand Up @@ -163,6 +170,7 @@ protected void installListeners() {
ribbon.repaint();
}
if ("minimized".equals(propertyChangeEvent.getPropertyName())) {
System.out.println("Minimizing ribbon state change -> hiding popups");
PopupPanelManager.defaultManager().hidePopups(null);
RichTooltipManager.sharedInstance().hideCurrentlyShowingTipIfNecessary();
ribbon.revalidate();
Expand Down Expand Up @@ -195,6 +203,11 @@ protected void uninstallListeners() {

this.ribbon.removeComponentListener(this.ribbonComponentListener);
this.ribbonComponentListener = null;

for (PopupPanelManager.PopupListener taskPopupListener: this.taskPopupListeners.values()) {
PopupPanelManager.defaultManager().removePopupListener(taskPopupListener);
}
this.taskPopupListeners.clear();
}

/**
Expand Down Expand Up @@ -1282,6 +1295,7 @@ public void popupHidden(PopupEvent event) {
if (originator instanceof JRibbonTaskToggleButton) {
ribbon.add(bandScrollablePanel);
PopupPanelManager.defaultManager().removePopupListener(this);
taskPopupListeners.remove(task);
ribbon.revalidate();
ribbon.doLayout();
ribbon.repaint();
Expand All @@ -1290,6 +1304,7 @@ public void popupHidden(PopupEvent event) {
};
PopupPanelManager.defaultManager().addPopupListener(tracker);
PopupPanelManager.defaultManager().showPopup(taskToggleButton, popupPanel, x, y);
taskPopupListeners.put(task, tracker);
}
}

Expand Down

0 comments on commit 8d9f079

Please sign in to comment.