Last active
February 23, 2017 13:43
-
-
Save dashorst/d668f57d0961445faf9a0df5bab355e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface BootstrapAction | |
{ | |
BootstrapAction confirm(String text, SerializableRunnable action); | |
BootstrapAction label(String label); | |
BootstrapAction icon(FontAwesomeIcon icon); | |
BootstrapAction iconPre(String icon); | |
BootstrapAction iconPost(String icon); | |
BootstrapAction onClick(SerializableRunnable action); | |
BootstrapAction onClick(SerializableConsumer<AjaxRequestTarget> action); | |
BootstrapAction onSubmit(Form< ? > form, SerializableRunnable action); | |
BootstrapAction onSubmit(Form< ? > form, SerializableConsumer<AjaxRequestTarget> action); | |
BootstrapAction visibleWhen(SerializableSupplier<Boolean> visiblity); | |
BootstrapAction title(String string); | |
BootstrapAction withClass(String cssClass); | |
default BootstrapAction onSubmit(Form< ? > form) | |
{ | |
return onSubmit(form, () -> {}); | |
} | |
default BootstrapAction visibleWhen(Boolean visible) | |
{ | |
return visibleWhen(() -> visible); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BootstrapActionPanel extends Panel implements BootstrapAction | |
{ | |
private static final long serialVersionUID = 1L; | |
private WebMarkupContainer button; | |
private List<String> cssClasses = new ArrayList<>(); | |
private BootstrapActionIcon iconPre; | |
private String label; | |
private String title; | |
private BootstrapActionIcon iconPost; | |
private SerializableSupplier<Boolean> visibility; | |
public BootstrapActionPanel(String id) | |
{ | |
super(id); | |
button = new WebMarkupContainer("button"); | |
add(button); | |
iconPre = new BootstrapActionIcon("icon-pre"); | |
button.add(iconPre); | |
iconPost = new BootstrapActionIcon("icon-post"); | |
button.add(iconPost); | |
} | |
@Override | |
public BootstrapAction confirm(String message, SerializableRunnable action) | |
{ | |
button = new ConfirmationLink<Void>("button", message) | |
{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
protected void onComponentTag(ComponentTag tag) | |
{ | |
super.onComponentTag(tag); | |
cssClasses.forEach(cssClass -> tag.append("class", cssClass, " ")); | |
} | |
@Override | |
public void onClick() | |
{ | |
action.run(); | |
} | |
}; | |
return this; | |
} | |
@Override | |
public BootstrapAction onClick(SerializableConsumer<AjaxRequestTarget> action) | |
{ | |
button = new AjaxLink<Void>("button") | |
{ | |
private static final long serialVersionUID = 1L; | |
private boolean previousVisibility = true; | |
@Override | |
protected void onInitialize() | |
{ | |
super.onInitialize(); | |
setOutputMarkupPlaceholderTag(true); | |
} | |
@Override | |
protected void onConfigure() | |
{ | |
super.onConfigure(); | |
previousVisibility = false; | |
} | |
@Override | |
public void onEvent(IEvent< ? > event) | |
{ | |
super.onEvent(event); | |
if (event.getPayload() instanceof AjaxRequestTarget) | |
{ | |
determineButtonVisibility(); | |
if (previousVisibility != isVisible()) | |
{ | |
((AjaxRequestTarget) event.getPayload()).add(this); | |
} | |
} | |
} | |
@Override | |
protected void onComponentTag(ComponentTag tag) | |
{ | |
super.onComponentTag(tag); | |
previousVisibility = true; | |
cssClasses.forEach(cssClass -> tag.append("class", cssClass, " ")); | |
} | |
@Override | |
public void onClick(AjaxRequestTarget target) | |
{ | |
action.accept(target); | |
} | |
}; | |
return this; | |
} | |
@Override | |
public BootstrapAction onClick(SerializableRunnable action) | |
{ | |
button = new Link<Void>("button") | |
{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
protected void onComponentTag(ComponentTag tag) | |
{ | |
super.onComponentTag(tag); | |
cssClasses.forEach(cssClass -> tag.append("class", cssClass, " ")); | |
} | |
@Override | |
public void onClick() | |
{ | |
action.run(); | |
} | |
}; | |
return this; | |
} | |
@Override | |
public BootstrapAction onSubmit(Form< ? > form, SerializableConsumer<AjaxRequestTarget> action) | |
{ | |
button = new AjaxSubmitLink("button", form) | |
{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
protected void onInitialize() | |
{ | |
super.onInitialize(); | |
setOutputMarkupPlaceholderTag(true); | |
} | |
@Override | |
protected void onComponentTag(ComponentTag tag) | |
{ | |
super.onComponentTag(tag); | |
cssClasses.forEach(cssClass -> tag.append("class", cssClass, " ")); | |
} | |
@Override | |
public void onSubmit(AjaxRequestTarget target, Form< ? > f) | |
{ | |
action.accept(target); | |
} | |
}; | |
return this; | |
} | |
@Override | |
public BootstrapAction onSubmit(Form< ? > form, SerializableRunnable action) | |
{ | |
button = new SubmitLink("button", form) | |
{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
protected void onComponentTag(ComponentTag tag) | |
{ | |
super.onComponentTag(tag); | |
cssClasses.forEach(cssClass -> tag.append("class", cssClass, " ")); | |
} | |
@Override | |
public void onSubmit() | |
{ | |
action.run(); | |
} | |
}; | |
return this; | |
} | |
@Override | |
public BootstrapAction withClass(String cssClass) | |
{ | |
this.cssClasses.add(cssClass); | |
return this; | |
} | |
@Override | |
public BootstrapAction icon(FontAwesomeIcon icon) | |
{ | |
switch (icon.getDefaultPosition()) | |
{ | |
case left: | |
iconPre(icon.toString()); | |
break; | |
case right: | |
iconPost(icon.toString()); | |
break; | |
} | |
return this; | |
} | |
@Override | |
public BootstrapAction iconPre(String icon) | |
{ | |
iconPre.setIcon(icon); | |
return this; | |
} | |
@Override | |
public BootstrapAction label(String label) | |
{ | |
this.label = label; | |
return this; | |
} | |
@Override | |
public BootstrapAction title(String title) | |
{ | |
this.title = title; | |
return this; | |
} | |
@Override | |
public BootstrapAction iconPost(String icon) | |
{ | |
iconPost.setIcon(icon); | |
return this; | |
} | |
@Override | |
public BootstrapAction visibleWhen(SerializableSupplier<Boolean> visiblity) | |
{ | |
this.visibility = visiblity; | |
return this; | |
} | |
@Override | |
protected void onConfigure() | |
{ | |
super.onConfigure(); | |
addOrReplace(button); | |
button.addOrReplace(iconPre); | |
button.addOrReplace(new Label("label", label)); | |
button.addOrReplace(iconPost); | |
if (StringUtil.isNotEmpty(title)) | |
button.add(new TitleModifier()); | |
determineButtonVisibility(); | |
} | |
private void determineButtonVisibility() | |
{ | |
if (visibility != null) | |
{ | |
Boolean visible = visibility.get(); | |
button.setVisible(visible == null || visible); | |
} | |
} | |
private final class TitleModifier extends Behavior | |
{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void onComponentTag(Component component, ComponentTag tag) | |
{ | |
super.onComponentTag(component, tag); | |
tag.append("data-toggle", "tooltip", ""); | |
tag.append("title", title, ""); | |
} | |
@Override | |
public boolean isTemporary(Component component) | |
{ | |
return true; | |
} | |
} | |
@Override | |
public Component component() | |
{ | |
return button; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface BootstrapActions | |
{ | |
BootstrapAction addLeftAction(); | |
BootstrapDropdownAction addLeftDropdown(); | |
BootstrapAction addRightAction(); | |
BootstrapDropdownAction addRightDropdown(); | |
public static <R, S> void addSelectionButtons(BootstrapActions actions, Form< ? > form, | |
ISelectionComponent<R, S> selection, String btnClasses) | |
{ | |
BootstrapDropdownAction dropdown = actions.addLeftDropdown() | |
.label("Select...") | |
.icon(FontAwesomeIcon.select) | |
.withClass(btnClasses); | |
dropdown.addAction().label("Select none") | |
.icon(FontAwesomeIcon.selectNone) | |
.onSubmit( | |
form, target -> { | |
selection.getSelection().clear(); | |
target.add(form); | |
}); | |
dropdown.addAction() | |
.label("Select page") | |
.icon(FontAwesomeIcon.selectPage) | |
.onSubmit(form, target -> { | |
CustomDataPanel<S> datapanel = selection.getDataPanel(); | |
long rows = datapanel.getItemsPerPage(); | |
long page = datapanel.getCurrentPage(); | |
Iterator< ? extends S> it = datapanel.getDataProvider().iterator(page * rows, rows); | |
while (it.hasNext()) | |
{ | |
selection.getSelection().add(it.next()); | |
} | |
target.add(form); | |
}); | |
dropdown.addAction().label("Select all") | |
.icon(FontAwesomeIcon.selectAll) | |
.onSubmit( | |
form, target -> { | |
CustomDataPanel<S> datapanel = selection.getDataPanel(); | |
selection.getSelection().addAll(datapanel.getDataProvider()); | |
target.add(form); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment