Bands embedded in Menus Not Visible
Closed this issue · 5 comments
Version of Radiance (latest release is 8.0-SNAPSHOT)
7.5.0
Sub-project (Common, Animation, Theming, Component, ...)
Ribbon
Version of Java (current minimum is 9)
21.0.1
Version of OS
Windows 11
The issue you're experiencing (expected vs actual, screenshot, stack trace etc)
I attempted to follow the code you pointed me towards in #478 to see if I could add a band in a command's popup menu. I am seeing odd behaviour which makes me think I have an issue with the instantiation of the band itself. As a quick test I did the following in the BasicCheckRibbon demo:
- In BasicRibbonBandUI.java I changed a few classes to public just so I could temporarily use them in
BasicCheckRibbon
. - I added a private
JRibbonBand basicsBand;
withinBasicCheckRibbon
- In getDocumentBand(), I added the following test code:
// MY TEST CODE
basicsBand = new JRibbonBand("Menu Band", Applications_office.factory());
basicsBand.setExpandButtonKeyTip("BY");
basicsBand.setCollapsedStateKeyTip("BA");
basicsBand.startGroup();
Command xCommand = Command.builder()
.setText("COMMAND")
.setIconFactory(Applications_office.factory())
.setAction(commandActionEvent -> {
System.out.println("COMMAND activated");
})
.build();
basicsBand.addRibbonCommand(
xCommand.project(CommandButtonPresentationModel.builder()
.setPopupKeyTip("X")
.setTextClick(CommandButtonPresentationModel.TextClick.ACTION)
.build()),
JRibbonBand.PresentationPriority.TOP);
List<RibbonBandResizePolicy> resizePolicies = new ArrayList<>();
resizePolicies.add(new CoreRibbonResizePolicies.Mirror(basicsBand));
resizePolicies.add(new CoreRibbonResizePolicies.Mid2Low(basicsBand));
basicsBand.setResizePolicies(resizePolicies);
BandCollapseCommand subBandCommand = BandCollapseCommand.builder()
.setText("TEST")
.setIconFactory(Applications_office.factory())
.setSecondaryContentModel(new BandCollapsePopupMenuContentModel())
.setSecondaryLifecycle(new BaseCommand.SecondaryLifecycle() {
@Override
public void onBeforeActivateSecondary(JPopupPanel popupPanel) {
popupPanel.add(basicsBand, BorderLayout.CENTER);
popupPanel.setPreferredSize(new Dimension(150,100));
}
@Override
public void onAfterActivateSecondary(JPopupPanel popupPanel) {
}
@Override
public void onBeforeDeactivateSecondary(JPopupPanel popupPanel) {
}
@Override
public void onAfterDeactivateSecondary(JPopupPanel popupPanel) {
popupPanel.removeAll();
}
})
.build();
JCommandButton testMenuJButton = new BandCollapseCommandButtonProjection(
subBandCommand,
BandCollapseCommandButtonPresentationModel.builder()
.setPresentationState(CommandButtonPresentationState.BIG)
// .setPopupKeyTip(this.miscBand.getCollapsedStateKeyTip())
.build())
.buildComponent();
result.addRibbonCommand(
testMenuJButton.getProjection(),
JRibbonBand.PresentationPriority.TOP);
// END MY TEST CODE
This creates basicsBand
and adds a single command. It then uses the BandCollapseCommand
and BandCollapseCommandButtonProjection
as per your example to configure a JCommandButton
that includes basicsBand
in its menu.
I thought that was all I should need. However, when I run the demo, the menu sized as per the popupPanel.setPreferredSize
call but it did not contain the band:
I then added the band to the Page Layout ribbon itself simply with this modification in configureRibbon
:
RibbonTask pageLayoutTask = new RibbonTask(
resourceBundle.getString("PageLayout.textTaskTitle"), clipboardBand,
quickStylesBand, fontBand, documentBand, findBand, basicsBand);
This caused the band to appear in the ribbon.
However, now when I open the menu associated with my new button, the band is present in the menu. The band then disappears from the ribbon.
From here on, the button's menu behaves correctly and the band never reappears on the ribbon. What am I doing wrong?
There is additional code in the ribbon that reparents the ribbon band to the popup panel when the popup is shown, and then removes it from the popup and adds it back to the ribbon hierarchy when the popup is hidden.
In this particular case, I would suggest first making your case work outside of the ribbon, just in a standalone command button and a custom popup, and then figuring out how to do this in the ribbon. In that followup, you can't put the same ribbon component in two places - ribbon band and the popup menu. You can try doing a similar reparenting, or you might need to create two instances of the same content. In the ribbon today reparenting works since the band content can't be displayed in two places at the same time.
There also are additional steps to how the ribbon band gets its initial size during the layout phase. All of these steps are internal implementation details, and I don't have any plans to expose helper APIs for a scenario like this - beyond what is needed internally to display the ribbon band in expanded state in a popup of an otherwise collapsed ribbon.
Just to be clear, I don't want the same component in two places. I only want it in the menu. I added it to the ribbon just to confirm that it was configured properly. It then appearing in the menu was just a side effect.
I would still suggest breaking this into more manageable steps. Instead of a whole band, make a simple JPanel
with red background and get it to display in the popup. From there, add some content to the panel to make sure it's still working. From there, make it work as a custom popup when that button is in a ribbon. And then, back to a standalone thing, with a ribbon band as your popup content, and then the final step is a ribbon band inside the popup displayed in the ribbon.
At any one of these steps, especially with ribbon bands, you might get into internal assumptions on where ribbon bands "expect" to find themselves. If that happens, your best fallback might be just a custom JPanel
in that popup.
For future questions like this, I just opened https://github.com/kirill-grouchnikov/radiance/discussions