Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
Ability to define a custom stylename for a dialog action #616
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaurite committed Sep 20, 2021
1 parent 9003cd6 commit f53a976
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
98 changes: 98 additions & 0 deletions ui/src/main/java/io/jmix/ui/action/DialogAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package io.jmix.ui.action;

import io.jmix.ui.Dialogs;
import io.jmix.ui.component.KeyCombination;
import io.jmix.ui.icon.JmixIcon;
import io.jmix.ui.icon.Icons;

import javax.annotation.Nullable;
import java.util.function.Consumer;

/**
* Standard action for option dialogs.
* <br>
Expand All @@ -35,6 +39,7 @@
* new DialogAction(Type.NO)
* .withCaption("Print selected")
* .withIcon(JmixIcon.PRINT.source())
* .withStyleName("print-selected")
* .withHandler(event -> {
* // add action logic here
* }),
Expand Down Expand Up @@ -77,6 +82,11 @@ public Icons.Icon getIconKey() {
}

protected Type type;
protected String styleName;

public DialogAction(String id) {
super(id);
}

public DialogAction(Type type) {
super(type.id);
Expand All @@ -94,7 +104,95 @@ public DialogAction(Type type, Status status) {
this.primary = status == Status.PRIMARY;
}

@Nullable
public Type getType() {
return type;
}

/**
* @return style name or {@code null} if not set
*/
@Nullable
public String getStyleName() {
return styleName;
}

/**
* Sets style name that will be used in the corresponding button of the dialog.
*
* @param styleName style name
* @return current instance of action
*/
public DialogAction withStyleName(@Nullable String styleName) {
this.styleName = styleName;
return this;
}

/**
* Set caption using fluent API method.
*
* @param caption caption
* @return current instance of action
*/
public DialogAction withCaption(@Nullable String caption) {
this.caption = caption;
return this;
}

/**
* Set description using fluent API method.
*
* @param description description
* @return current instance of action
*/
public DialogAction withDescription(@Nullable String description) {
this.description = description;
return this;
}

/**
* Set icon using fluent API method.
*
* @param icon icon
* @return current instance of action
*/
public DialogAction withIcon(@Nullable String icon) {
this.icon = icon;
return this;
}

/**
* Set shortcut using fluent API method.
*
* @param shortcut shortcut
* @return current instance of action
*/
public DialogAction withShortcut(@Nullable String shortcut) {
if (shortcut != null) {
this.shortcut = KeyCombination.create(shortcut);
}
return this;
}

/**
* Set action performed event handler using fluent API method. Can be used instead of subclassing BaseAction class.
*
* @param handler action performed handler
* @return current instance of action
*/
public DialogAction withHandler(Consumer<ActionPerformedEvent> handler) {
getEventHub().subscribe(ActionPerformedEvent.class, handler);
return this;
}

/**
* Set whether this action is primary using fluent API method. Can be used instead of subclassing BaseAction class.
*
* @param primary primary
* @return current instance of action
*/
public DialogAction withPrimary(boolean primary) {
this.primary = primary;
return this;
}
}
10 changes: 6 additions & 4 deletions ui/src/main/java/io/jmix/ui/sys/DialogsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ public JmixButton createButton(Action action) {

if (action instanceof DialogAction) {
DialogAction.Type type = ((DialogAction) action).getType();

button.setCaption(messages.getMessage(type.getMsgKey()));
String iconPath = icons.get(type.getIconKey());
button.setIcon(iconResolver.getIconResource(iconPath));
if (type != null) {
button.setCaption(messages.getMessage(type.getMsgKey()));
String iconPath = icons.get(type.getIconKey());
button.setIcon(iconResolver.getIconResource(iconPath));
}
button.setStyleName(((DialogAction) action).getStyleName());
}

button.setEnabled(action.isEnabled());
Expand Down

0 comments on commit f53a976

Please sign in to comment.