001: package org.osbl.client.wings;
002:
003: import java.awt.Dimension;
004: import java.awt.image.BufferedImage;
005: import java.util.*;
006:
007: import javax.swing.Action;
008:
009: import org.wings.*;
010: import org.osbl.client.wings.shell.SVGButtonRenderer;
011: import org.osbl.client.wings.shell.Client;
012:
013: /**
014: * XButton
015: * <p/>
016: */
017: public class XButton extends SButton {
018:
019: protected final static Map<BufferedImage, SIcon> iconCache = Collections
020: .synchronizedMap(new HashMap<BufferedImage, SIcon>());
021:
022: SIcon icon;
023: SIcon disabledIcon;
024:
025: /**
026: * Creates a button with no set text or icon.
027: */
028: public XButton() {
029: setStyle("XButton");
030: }
031:
032: /**
033: * Creates a button with a icon
034: *
035: * @param icon the Icon image to display on the button
036: */
037: public XButton(SIcon icon) {
038: super (icon);
039: setStyle("XButton");
040: }
041:
042: /**
043: * Creates a button with text.
044: *
045: * @param text the text of the button
046: */
047: public XButton(String text) {
048: super (text);
049: setStyle("XButton");
050: }
051:
052: public XButton(Action action) {
053: super (action);
054: setStyle("XButton");
055: }
056:
057: /**
058: * LEAVE it!
059: * Otherwise a label with the link will be displayed next to the button!
060: */
061: public String getText() {
062: return null;
063: }
064:
065: public SIcon getIcon() {
066: if (icon == null && super .getText() != null)
067: icon = renderIcon();
068: return icon;
069: }
070:
071: public SIcon getDisabledIcon() {
072: if (disabledIcon == null && super .getText() != null)
073: disabledIcon = renderDisabledIcon();
074: return disabledIcon;
075: }
076:
077: private SIcon renderIcon() {
078: SVGButtonRenderer renderer = SVGButtonRenderer.getInstance();
079: Dimension dim = new Dimension(-1, -1);
080: Dimension newDim = renderer.calculateButtonImageSize(super
081: .getText());
082: if (newDim.width > dim.width) {
083: dim.width = newDim.width;
084: }
085: if (newDim.height > dim.height) {
086: dim.height = newDim.height;
087: }
088:
089: return renderer.retrieveRenderedButtonIcon(super .getText(),
090: "svg/toolbar_button.svg", dim);
091: }
092:
093: private SIcon renderDisabledIcon() {
094: SVGButtonRenderer renderer = SVGButtonRenderer.getInstance();
095: Dimension dim = new Dimension(-1, -1);
096: Dimension newDim = renderer.calculateButtonImageSize(super
097: .getText());
098: if (newDim.width > dim.width) {
099: dim.width = newDim.width;
100: }
101: if (newDim.height > dim.height) {
102: dim.height = newDim.height;
103: }
104:
105: return renderer.retrieveRenderedButtonIcon(super .getText(),
106: "svg/toolbar_button_disabled.svg", dim);
107: }
108:
109: public void setAction(Action a) {
110: Client.getInstance().getActionProvider().configure(a);
111: String oldText = getText(getAction());
112: super .setAction(a);
113: if (!oldText.equals(getText(a))) {
114: icon = renderIcon();
115: }
116: SIcon icon = (SIcon) a.getValue(Action.SMALL_ICON);
117: if (icon != null) {
118: this .icon = icon;
119: this .disabledIcon = icon;
120: }
121: }
122:
123: private String getText(Action aAction) {
124: if (aAction != null && aAction.getValue(Action.NAME) != null) {
125: return (String) aAction.getValue(Action.NAME);
126: }
127: return super .getText() != null ? super .getText() : "";
128: }
129: }
|