Skip to content

Commit

Permalink
Add dynamic update to one of the anchored commands in the ribbon demo
Browse files Browse the repository at this point in the history
For #481
  • Loading branch information
kirill-grouchnikov committed Sep 11, 2024
1 parent 6722b0b commit 4c3aace
Showing 1 changed file with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public class BasicCheckRibbon extends JRibbonFrame {
private Command amEntryExit;
private Command amFooterProps;

private Command anchoredShareCommand;

private RibbonComboBoxContentModel<String> fontComboBoxModel;
private RibbonCheckBoxContentModel rulerCheckBoxModel;
private RibbonSpinnerNumberContentModel indentLeftSpinnerModel;
Expand Down Expand Up @@ -1590,6 +1592,29 @@ private void createCommands() {
.setSelectionChangeListener(((oldSelection, newSelection) ->
System.out.println("New indent left -> " + newSelection)))
.build();

// "Share" anchored menu
Command shareEntrySendMail = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.email.text"))
.setAction(commandActionEvent -> System.out.println("Shared to email"))
.build();

Command shareEntrySendHtml = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.html.text"))
.setAction(commandActionEvent -> System.out.println("Shared to browser"))
.build();

Command shareEntrySendDoc = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.word.text"))
.setAction(commandActionEvent -> System.out.println("Shared to Word"))
.build();

this.anchoredShareCommand = Command.builder()
.setText(resourceBundle.getString("Share.title"))
.setIconFactory(Internet_mail.factory())
.setSecondaryContentModel(new CommandMenuContentModel(new CommandGroup(
shareEntrySendMail, shareEntrySendHtml, shareEntrySendDoc)))
.build();
}

private void createStyleGalleryModel() {
Expand Down Expand Up @@ -1726,28 +1751,7 @@ public void configureRibbon() {
this.getRibbon().addTask(animationsTask);
this.getRibbon().addTask(wrappedTask);

// "Share" anchored menu
Command shareEntrySendMail = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.email.text"))
.setAction(commandActionEvent -> System.out.println("Shared to email"))
.build();

Command shareEntrySendHtml = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.html.text"))
.setAction(commandActionEvent -> System.out.println("Shared to browser"))
.build();

Command shareEntrySendDoc = Command.builder()
.setText(resourceBundle.getString("AppMenuSend.word.text"))
.setAction(commandActionEvent -> System.out.println("Shared to Word"))
.build();

this.getRibbon().addAnchoredCommand(Command.builder()
.setText(resourceBundle.getString("Share.title"))
.setIconFactory(Internet_mail.factory())
.setSecondaryContentModel(new CommandMenuContentModel(new CommandGroup(
shareEntrySendMail, shareEntrySendHtml, shareEntrySendDoc)))
.build()
this.getRibbon().addAnchoredCommand(this.anchoredShareCommand
.project(CommandButtonPresentationModel.builder()
.setPopupPlacementStrategy(RadianceThemingSlices.PopupPlacementStrategy.Downward.HALIGN_END)
.setPopupKeyTip("GS")
Expand Down Expand Up @@ -2298,7 +2302,7 @@ private RibbonTask getContextualRibbonTask(String title, String keyTip) {
private JPanel getControlPanel() {
FormBuilder builder = FormBuilder.create().
columns("right:pref, 8dlu, fill:pref:grow").
rows("p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p").
rows("p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p, $lg, p").
padding(new EmptyBorder(20, 4, 0, 4));

final JCheckBox group1Visible = new JCheckBox("visible");
Expand Down Expand Up @@ -2383,15 +2387,37 @@ private String getRandomString(int minLength, int maxLength) {
}));
builder.add("Change 'Paste'").xy(1, 13).add(changePaste).xy(3, 13);

builder.add("Locale").xy(1, 15).add(new RadianceLocaleSelector(false, selected -> {
JButton changeShare = new JButton("change");
changeShare
.addActionListener(actionEvent -> SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
anchoredShareCommand.setText(getRandomString(5, 10));
anchoredShareCommand.setIconFactory(Help_browser.factory());
}

private String getRandomString(int minLength, int maxLength) {
Random random = new Random();
int len = minLength + random.nextInt(maxLength - minLength);
String newTitle = new String();
String letters = "abcdefghijklmnopqrstubvwxyz ";
for (int i = 0; i < len; i++) {
newTitle += letters.charAt(random.nextInt(letters.length()));
}
return newTitle;
}
}));
builder.add("Change 'Share'").xy(1, 15).add(changeShare).xy(3, 15);

builder.add("Locale").xy(1, 17).add(new RadianceLocaleSelector(false, selected -> {
currLocale = selected;
resourceBundle = ResourceBundle.getBundle(
"org.pushingpixels.radiance.demo.component.resource.Resources", currLocale);
for (Window window : Window.getWindows()) {
window.applyComponentOrientation(ComponentOrientation.getOrientation(currLocale));
SwingUtilities.updateComponentTreeUI(window);
}
})).xy(3, 15);
})).xy(3, 17);

JButton galleryUpdate = new JButton("update");
galleryUpdate.addActionListener(actionEvent -> {
Expand Down Expand Up @@ -2431,8 +2457,8 @@ private String getRandomString(int minLength, int maxLength) {
// And mark the second new command as the new selection in the gallery
this.styleGalleryContentModel.setSelectedCommand(this.styleGalleryCommandGroup1.getCommands().get(1));
});
builder.add("Update gallery").xy(1, 17).add(galleryUpdate).xy(3, 17);
builder.add(new RadianceFontScaleSelector()).xyw(1, 19, 3);
builder.add("Update gallery").xy(1, 19).add(galleryUpdate).xy(3, 19);
builder.add(new RadianceFontScaleSelector()).xyw(1, 21, 3);

return builder.build();
}
Expand Down

0 comments on commit 4c3aace

Please sign in to comment.