DialogButtons DialogButton
public enum DialogButtons { OK(AbstractDialog.LBL_OK), OK_CANCEL(AbstractDialog.LBL_OK, AbstractDialog.LBL_CANCEL), YES_NO(AbstractDialog.LBL_YES, AbstractDialog.LBL_NO), YES_NO_CANCEL(AbstractDialog.LBL_YES, AbstractDialog.LBL_NO, AbstractDialog.LBL_CANCEL); private final List<DialogButton> buttons; private DialogButtons(String... labels) { this.buttons = new ArrayList<DialogButton>(); for (String label : labels) { this.buttons.add(new DialogButton(label)); } } public List<DialogButton> toList() { return this.buttons; } } |
public class DialogButton implements IClusterable {private static final long serialVersionUID = 1L; private static short sequence = 0; /** * Gets the next id-sequence. This is used to generate the markupId * @return 0x0000 to 0x7FFF */ private static synchronized int nextSequence() { return (DialogButton.sequence++ % Short.MAX_VALUE); } private final int id; private final String text; private String icon; private boolean enabled; private boolean visible = true; /** * Constructor * @param text the button's text */ public DialogButton(String text) { this(text, null, true); } /** * Constructor * @param text the button's text * @param icon the button's icon */ public DialogButton(String text, String icon) { this(text, icon, true); } /** * Constructor * @param text the button's text * @param enabled indicates whether the button is enabled */ public DialogButton(String text, boolean enabled) { this(text, null, enabled); } /** * Constructor * @param text the button's text * @param icon the button's icon * @param enabled indicates whether the button is enabled */ public DialogButton(String text, String icon, boolean enabled) { this.id = DialogButton.nextSequence(); this.text = text; this.icon = icon; this.enabled = enabled; } /** * Constructor * @param model the button's text model */ public DialogButton(final IModel<String> model) { this(model.getObject(), null, true); } /** * Constructor * @param model the button's text model * @param icon the button's icon */ public DialogButton(final IModel<String> model, String icon) { this(model.getObject(), icon, true); } /** * Constructor * @param model the button's text model * @param enabled indicates whether the button is enabled */ public DialogButton(final IModel<String> model, boolean enabled) { this(model.getObject(), null, enabled); } /** * Constructor * @param model the button's text model * @param icon the button's icon * @param enabled indicates whether the button is enabled */ public DialogButton(final IModel<String> model, String icon, boolean enabled) { this(model.getObject(), icon, enabled); } // Properties // /** * Gets the button's icon * @return the button's icon */ public String getIcon() { return this.icon; } /** * Sets the button's icon * @param icon the css class (ie: ui-my-icon) */ public void setIcon(String icon) { this.icon = icon; } /** * Indicates whether the button is enabled * @return true or false */ public boolean isEnabled() { return this.enabled; } /** * Sets the enable state of the button * @param enabled true or false */ public void setEnabled(boolean enabled) { this.enabled = enabled; } /** * Sets the enable state of the button * @param enabled true or false * @param target the {@link AjaxRequestTarget} */ public void setEnabled(boolean enabled, AjaxRequestTarget target) { if (enabled) { this.enable(target); } else { this.disable(target); } } /** * Sets the visible state of the button * @param visible true or false * @param target the {@link AjaxRequestTarget} */ public void setVisible(boolean visible, AjaxRequestTarget target) { if (this.visible != visible) { this.visible = visible; if (this.visible) { this.show(target); } else { this.hide(target); } } } /** * Gets the markupId of the specified button.<br/> * This can be used to enable/disable the button * * @return the markupId */ protected String getMarkupId() { return String.format("btn%02x", this.id).toLowerCase(); } // Methods // /** * Enables the button * @param target the {@link AjaxRequestTarget} */ private void enable(AjaxRequestTarget target) { target.appendJavaScript(String.format("jQuery('#%s').button('enable');", this.getMarkupId())); } /** * Disables the button * @param target the {@link AjaxRequestTarget} */ private void disable(AjaxRequestTarget target) { target.appendJavaScript(String.format("jQuery('#%s').button('disable');", this.getMarkupId())); } /** * Shows the button * @param target the {@link AjaxRequestTarget} */ private void show(AjaxRequestTarget target) { target.appendJavaScript(String.format("jQuery('#%s').show();", this.getMarkupId())); } /** * Hides the button * @param target the {@link AjaxRequestTarget} */ private void hide(AjaxRequestTarget target) { target.appendJavaScript(String.format("jQuery('#%s').hide();", this.getMarkupId())); } @Override public int hashCode() { return this.id; } /** * Indicates whether this {@link DialogButton} is equal to another {@link DialogButton}. Are considered equals buttons having the same text representation ({@link #toString()}), which is the text supplied to the constructor (if not overridden). * * @param object either a {@link DialogButton} or a {@link String} * @return true if considered as equal */ @Override public boolean equals(Object object) { return (object != null) && (object.toString().equals(this.toString())); } @Override public String toString() { return this.text; } } |
public enum DialogIcon { NONE(""), INFO(JQueryFeedbackPanel.INFO_ICO, JQueryFeedbackPanel.INFO_CSS), WARN(JQueryFeedbackPanel.WARN_ICO, JQueryFeedbackPanel.WARN_CSS), ERROR(JQueryFeedbackPanel.ERROR_ICO, JQueryFeedbackPanel.ERROR_CSS), LIGHT(JQueryFeedbackPanel.LIGHT_ICO, JQueryFeedbackPanel.LIGHT_ICO); private final String icon; private final String style; /** * Private constructor * @param icon the icon jQuery UI class */ private DialogIcon(String icon) { this(icon, ""); } /** * Private constructor * @param icon the icon jQuery UI class * @param style the surrounding style */ private DialogIcon(String icon, String style) { this.icon = icon; this.style = style; } /** * Gets the style variation (the jQuery style) * @return the style */ public String getStyle() { return this.style; } @Override public String toString() { return this.icon; } } |
public interface IClusterable extends Serializable { // nothing here, as it is a .... brrrr .... tagging interface! } |
public abstract class DialogBehavior extends JQueryBehavior implements IJQueryAjaxAware, IDialogListener { private static final long serialVersionUID = 1L; private static final String METHOD = "dialog"; private JQueryAjaxBehavior onDefaultClose = null; /** * Constructor * * @param selector the html selector (ie: "#myId") */ public DialogBehavior(String selector) { super(selector, METHOD); } /** * Constructor * * @param selector the html selector (ie: "#myId") * @param options the {@link Options} */ public DialogBehavior(String selector, Options options) { super(selector, METHOD, options); } // Properties // /** * Gets the dialog's buttons.<br/> * * @return the {@link List} of {@link Button} */ protected abstract List<DialogButton> getButtons(); // Methods // @Override public void bind(Component component) { super.bind(component); for (DialogButton button : this.getButtons()) { component.add(this.newButtonAjaxBehavior(this, button)); } if (this.isDefaultCloseEventEnabled()) { component.add(this.onDefaultClose = this.newDefaultCloseBehavior()); } } /** * Opens the dialogs in ajax.<br/> * @param target the {@link AjaxRequestTarget} */ public void open(AjaxRequestTarget target) { target.appendJavaScript(this.$("'open'")); } /** * Closes the dialogs in ajax.<br/> * @param target the {@link AjaxRequestTarget} */ public void close(AjaxRequestTarget target) { target.appendJavaScript(this.$("'close'")); } // Events // @Override public void onConfigure(Component component) { super.onConfigure(component); if (this.onDefaultClose != null) { this.setOption("close", this.onDefaultClose.getCallbackFunction()); } // buttons events // StringBuffer buttons = new StringBuffer("[ "); int index = 0; for(ButtonAjaxBehavior behavior : component.getBehaviors(ButtonAjaxBehavior.class)) { DialogButton button = behavior.getButton(); if (index++ > 0) { buttons.append(", "); } buttons.append("{"); buttons.append("'id': '").append(button.getMarkupId()).append("', "); buttons.append("'text': '").append(button.toString()).append("', "); if (!button.isEnabled()) { buttons.append("'disabled': true, "); } if (button.getIcon() != null) { buttons.append("icons: { primary: '").append(button.getIcon()).append("' }, "); } buttons.append("'click': function() { ").append(behavior.getCallbackScript()).append(" }"); buttons.append("}"); } buttons.append(" ]"); this.setOption("buttons", buttons); } @Override public void onAjax(AjaxRequestTarget target, JQueryEvent event) { if (event instanceof ClickEvent) { this.onClick(target, ((ClickEvent) event).getButton()); } else if (event instanceof CloseEvent) { this.onClose(target, null); } } // Factories // /** * Gets a new ButtonAjaxBehavior that will be called by the corresponding dialog's button.<br/> * This method mays be overridden internally to provide another behavior; * * @param source the {@link IJQueryAjaxAware} source * @param button the button that is passed to the behavior so it can be retrieved via the {@link ClickEvent} * @return the {@link ButtonAjaxBehavior} */ protected abstract ButtonAjaxBehavior newButtonAjaxBehavior(IJQueryAjaxAware source, DialogButton button); /** * Gets the ajax behavior that will be triggered when the user clicks on the X-icon * * @return the {@link JQueryAjaxBehavior} */ protected JQueryAjaxBehavior newDefaultCloseBehavior() { return new JQueryAjaxBehavior(this) { private static final long serialVersionUID = 1L; @Override public String getCallbackFunction() { return "function(event, ui) { if (event.button == 0) { " + this.getCallbackScript() + " } }"; } @Override protected JQueryEvent newEvent() { return new CloseEvent(); } }; } // Ajax behaviors // /** * Provides the {@link JQueryAjaxBehavior} being called by the button(s). */ protected static class ButtonAjaxBehavior extends JQueryAjaxBehavior { private static final long serialVersionUID = 1L; private final DialogButton button; /** * Constructor * @param source the {@link IJQueryAjaxAware} * @param button the {@link DialogButton} to attach to the {@link ClickEvent} */ public ButtonAjaxBehavior(IJQueryAjaxAware source, DialogButton button) { super(source); this.button = button; } /** * Gets the {@link DialogButton} * @return the {@link DialogButton} */ public DialogButton getButton() { return this.button; } @Override protected JQueryEvent newEvent() { return new ClickEvent(this.button); } } // Events classes // /** * Provides a dialog event that will be transmitted to the {@link AbstractDialog} */ protected static class ClickEvent extends JQueryEvent { private final DialogButton button; public ClickEvent(DialogButton button) { super(); this.button = button; } /** * Get the button that has been attached to this event object * @return the button */ public DialogButton getButton() { return this.button; } } /** * An event object that will be broadcasted when the user clicks on the X-icon */ protected static class CloseEvent extends JQueryEvent { } } |