echopointng.ButtonEx Maven / Gradle / Ivy
Show all versions of ibis-echo2 Show documentation
package echopointng;
/*
* This file is part of the Echo Point Project. This project is a collection
* of Components that have extended the Echo Web Application Framework.
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Style;
import nextapp.echo2.app.button.ButtonModel;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import echopointng.able.AccessKeyable;
import echopointng.able.Attributeable;
import echopointng.able.Borderable;
import echopointng.able.Insetable;
import echopointng.able.MouseCursorable;
import echopointng.able.Sizeable;
import echopointng.able.ToolTipable;
import echopointng.model.ActionEventEx;
import echopointng.model.ButtonModelEx;
import echopointng.model.DefaultButtonModelEx;
import echopointng.util.ColorKit;
import echopointng.xhtml.XhtmlFragment;
/**
*
* The ButtonEx
class provides extra EchoPoint features beyond
* the nextapp.echo2.app.ButtonEx
*
* Its rollover functions are enabled by default and its supports the extra
* EchoPoint interfaces such as Borderable, MouseCursorable, AccessKeyable.
*
* By default ButtonEx
uses a ButtonModelEx
button
* model and this raises ActionEventEx
objects which contain the
* meta key information in play when the button was pressed. Hence you can
* distinguish a control-click from a shift-click or a single click if you wish.
*
* @author Brad Baker
*/
public class ButtonEx extends Button implements Borderable, MouseCursorable, AccessKeyable, Insetable, Sizeable, ToolTipable, Attributeable {
public static final String PROPERTY_INTERPRET_NEWLINES = "interpretNewlines";
public static final Style DEFAULT_STYLE;
static {
MutableStyleEx style = new MutableStyleEx();
style.setProperty(PROPERTY_LINE_WRAP, false);
style.setProperty(PROPERTY_BACKGROUND, ColorKit.makeColor("#F2F2ED"));
style.setProperty(Borderable.PROPERTY_BORDER, new Border(1, ColorKit.makeColor("#D6D3CE"), Border.STYLE_SOLID));
style.setProperty(PROPERTY_ROLLOVER_ENABLED, true);
style.setProperty(PROPERTY_ROLLOVER_BACKGROUND, ColorKit.makeColor("#DEF3FF"));
style.setProperty(PROPERTY_ROLLOVER_BORDER, new Border(1, ColorKit.makeColor("#3169C6"), Border.STYLE_SOLID));
style.setProperty(Insetable.PROPERTY_INSETS, new Insets(2));
style.setProperty(Insetable.PROPERTY_OUTSETS, new Insets(1));
style.setProperty(PROPERTY_TEXT_ALIGNMENT, new Alignment(Alignment.TRAILING, Alignment.DEFAULT));
style.setProperty(PROPERTY_ICON_TEXT_MARGIN, new Extent(3));
style.setProperty(PROPERTY_MOUSE_CURSOR, CURSOR_POINTER);
style.setProperty(PROPERTY_DISABLED_FOREGROUND, Color.LIGHTGRAY);
DEFAULT_STYLE = style;
}
/**
* Forwards events generated by the model to listeners registered with the
* component instance.
*/
private ActionListener actionForwarder = new ActionListener() {
/**
* @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
*/
public void actionPerformed(ActionEvent modelEvent) {
ActionEventEx buttonEvent;
if (modelEvent instanceof ActionEventEx) {
buttonEvent = new ActionEventEx(ButtonEx.this, modelEvent.getActionCommand(), ((ActionEventEx) modelEvent).getMetaKeyInfo());
} else {
buttonEvent = new ActionEventEx(ButtonEx.this, modelEvent.getActionCommand());
}
fireActionPerformed(buttonEvent);
}
};
private Map attributeMap;
/**
* Creates a button with no text or icon.
*/
public ButtonEx() {
this((String) null, null);
}
/**
* Creates a button with text.
*
* @param text
* A text label to display in the button.
*/
public ButtonEx(String text) {
this(text, null);
}
/**
* Creates a button with an icon.
*
* @param icon
* An icon to display in the button.
*/
public ButtonEx(ImageReference icon) {
this((String) null, icon);
}
/**
* Creates a button with text and an icon.
*
* @param text
* A text label to display in the button.
* @param icon
* An icon to display in the button.
*/
public ButtonEx(String text, ImageReference icon) {
super();
setModel(new DefaultButtonModelEx());
setIcon(icon);
setText(text);
}
/**
* Creates a button with an XhtmlFragment
.
*
* @param text
* A XhtmlFragment
to display in the button.
*/
public ButtonEx(XhtmlFragment text) {
this(text, null);
}
/**
* Creates a button with an XhtmlFragment
and an icon.
*
* @param text
* A XhtmlFragment
to display in the button.
* @param icon
* An icon to display in the button.
*/
public ButtonEx(XhtmlFragment text, ImageReference icon) {
super();
setModel(new DefaultButtonModelEx());
setIcon(icon);
setText(text);
}
/**
* @see nextapp.echo2.app.button.AbstractButton#processInput(java.lang.String,
* java.lang.Object)
*/
public void processInput(String name, Object value) {
if (INPUT_CLICK.equals(name)) {
int metaKeyInfo = (value == null ? 0 : ((Integer) value).intValue());
ButtonModel model = getModel();
if (model instanceof ButtonModelEx) {
((ButtonModelEx) model).doAction(metaKeyInfo);
} else {
getModel().doAction();
}
}
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
return "echopointng.ButtonEx : " + getText();
}
/**
* @see echopointng.able.AccessKeyable#getAccessKey()
*/
public String getAccessKey() {
return (String) getProperty(PROPERTY_ACCESS_KEY);
}
/**
* @see echopointng.able.Insetable#getInsets()
*/
public Insets getInsets() {
return (Insets) getProperty(Insetable.PROPERTY_INSETS);
}
/**
* @see echopointng.able.MouseCursorable#getMouseCursor()
*/
public int getMouseCursor() {
return ComponentEx.getProperty(this, MouseCursorable.PROPERTY_MOUSE_CURSOR, MouseCursorable.CURSOR_AUTO);
}
/**
* @see echopointng.able.MouseCursorable#getMouseCursorUri()
*/
public String getMouseCursorUri() {
return (String) getProperty(PROPERTY_MOUSE_CURSOR_URI);
}
/**
* @see echopointng.able.Insetable#getOutsets()
*/
public Insets getOutsets() {
return (Insets) getProperty(PROPERTY_OUTSETS);
}
/**
* @see echopointng.able.AccessKeyable#setAccessKey(java.lang.String)
*/
public void setAccessKey(String newValue) {
setProperty(PROPERTY_ACCESS_KEY, newValue);
}
/**
* @see echopointng.able.Insetable#setInsets(nextapp.echo2.app.Insets)
*/
public void setInsets(Insets newValue) {
setProperty(Insetable.PROPERTY_INSETS, newValue);
}
/**
* @see echopointng.able.MouseCursorable#setMouseCursor(int)
*/
public void setMouseCursor(int mouseCursor) {
ComponentEx.setProperty(this, MouseCursorable.PROPERTY_MOUSE_CURSOR, mouseCursor);
}
/**
* @see echopointng.able.MouseCursorable#setMouseCursorUri(java.lang.String)
*/
public void setMouseCursorUri(String mouseCursorURI) {
setProperty(PROPERTY_MOUSE_CURSOR_URI, mouseCursorURI);
}
/**
* @see echopointng.able.Insetable#setOutsets(nextapp.echo2.app.Insets)
*/
public void setOutsets(Insets newValue) {
setProperty(PROPERTY_OUTSETS, newValue);
}
/**
* @see echopointng.able.Attributeable#getAttribute(java.lang.String)
*/
public Object getAttribute(String attributeName) {
if (attributeMap != null) {
return attributeMap.get(attributeName);
}
return null;
}
/**
* @see echopointng.able.Attributeable#setAttribute(java.lang.String,
* java.lang.Object)
*/
public void setAttribute(String attributeName, Object attributeValue) {
if (attributeMap == null) {
attributeMap = new HashMap();
}
attributeMap.put(attributeName, attributeValue);
}
/**
* @see echopointng.able.Attributeable#getAttributeNames()
*/
public String[] getAttributeNames() {
if (attributeMap == null) {
return new String[0];
}
int count = 0;
String[] attributeNames = new String[attributeMap.keySet().size()];
for (Iterator iter = attributeMap.keySet().iterator(); iter.hasNext();) {
attributeNames[count++] = (String) iter.next();
}
return attributeNames;
}
/**
* @return true if new lines in the LabelEx
text are
* interpreted. This is off by default.
*/
public boolean isInterpretNewlines() {
return ComponentEx.getProperty(this, PROPERTY_INTERPRET_NEWLINES, false);
}
/**
* Sets whether newlines in the text of the LabelEx
are
* intepreted or not. For example if this is true
on a
* browser client, the new lines will be displayed using <br/> tags.
*
* @param newValue -
* the new value of the flag
*/
public void setInterpretNewlines(boolean newValue) {
ComponentEx.setProperty(this, PROPERTY_INTERPRET_NEWLINES, newValue);
}
/**
* If the value in PROPERTY_TEXT
is a
* XhtmlFragment
, then the
* XhtmlFragment.toString()
is returned, otherwise a
* String
value is returned.
*
* If you you want access to the underlying XhtmlFragment
* object itself, then use:
*
*
* XhtmlFragment fragment = (XhtmlFragment) getProperty(PROPERTY_TEXT);
*
*
* @see nextapp.echo2.app.button.AbstractButton#getText()
*/
public String getText() {
Object text = getProperty(PROPERTY_TEXT);
if (text instanceof XhtmlFragment) {
return text.toString();
}
return (String) text;
}
/**
* This version of setText()
will set an XhtmlFragment inside
* the ButtonEx
.
*
* @param fragment -
* the XhtmlFragment to use
* @see XhtmlFragment
*/
public void setText(XhtmlFragment fragment) {
setProperty(PROPERTY_TEXT, fragment);
}
/**
* @see nextapp.echo2.app.button.AbstractButton#setModel(nextapp.echo2.app.button.ButtonModel)
*/
public void setModel(ButtonModel newValue) {
if (newValue == null) {
throw new IllegalArgumentException("Model may not be null.");
}
ButtonModel oldValue = getModel();
if (oldValue != null) {
oldValue.removeActionListener(actionForwarder);
}
newValue.addActionListener(actionForwarder);
setProperty(PROPERTY_MODEL, newValue);
}
}