001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui;
029:
030: import thinwire.ui.event.ActionListener;
031: import thinwire.util.ImageInfo;
032:
033: /**
034: * A Button is a component that typically causes an action when activated.
035: * <p>
036: * <b>Example:</b> <br>
037: * <img src="doc-files/Button-1.png"> <br>
038: *
039: * <pre>
040: * Button b = new Button("Click Me!");
041: * b.setBounds(20, 20, 150, 30);
042: * b.addActionListener(Button.ACTION_CLICK, new ActionListener() {
043: * public void actionPerformed(ActionEvent e) {
044: * ((Button) e.getSource()).setText("You Clicked Me!");
045: * }
046: * });
047: *
048: * Dialog d = new Dialog("Button Test");
049: * d.setBounds(20, 20, 200, 100);
050: * d.getChildren().add(b);
051: * d.setVisible(true);
052: * </pre>
053: *
054: * </p>
055: * <p>
056: * <b>Keyboard Navigation:</b><br>
057: * <table border="1">
058: * <tr>
059: * <td>KEY</td>
060: * <td>RESPONSE</td>
061: * <td>NOTE</td>
062: * </tr>
063: * <tr>
064: * <td>Space</td>
065: * <td>Fires ActionEvent( action = Button.ACTION_CLICK )</td>
066: * <td>Only if the component has focus.</td>
067: * </tr>
068: * <tr>
069: * <td>Enter</td>
070: * <td>Fires ActionEvent( action = Button.ACTION_CLICK )</td>
071: * <td> Only if the 'standard' property is set to 'true' and ANY component in the Window has focus. The only exception is if you are
072: * focused on a component that has it's own behavior for for the Enter key, in which case Ctrl-Enter may be used. </td>
073: * </tr>
074: * <tr>
075: * <td>Ctrl-Enter</td>
076: * <td>Fires ActionEvent( action=Button.ACTION_CLICK )</td>
077: * <td>Only if the 'standard' property is set to 'true' and ANY component in the Window has focus.</td>
078: * </tr>
079: * </table>
080: * </p>
081: * @author Joshua J. Gertzen
082: */
083: public class Button extends AbstractTextComponent implements
084: ImageComponent {
085: public static final String PROPERTY_STANDARD = "standard";
086:
087: private boolean standard;
088: private ImageInfo imageInfo = new ImageInfo(null);
089:
090: /**
091: * Constructs a new Button with no text or image.
092: */
093: public Button() {
094: this (null, null);
095: }
096:
097: /**
098: * Constructs a new Button with the specified text and no image.
099: * @param text the text to display on the Button.
100: */
101: public Button(String text) {
102: this (text, null);
103: }
104:
105: /**
106: * Constructs a new Button with the specified text and image.
107: * @param text the text to display on the Button.
108: * @param image the name of a image file resource to display on the Button.
109: */
110: public Button(String text, String image) {
111: setText(text);
112: setImage(image);
113: }
114:
115: public String getImage() {
116: return imageInfo.getName();
117: }
118:
119: public void setImage(String image) {
120: String oldImage = this .imageInfo.getName();
121: imageInfo = new ImageInfo(image);
122: firePropertyChange(this , PROPERTY_IMAGE, oldImage,
123: this .imageInfo.getName());
124: }
125:
126: public ImageInfo getImageInfo() {
127: return imageInfo;
128: }
129:
130: /**
131: * Returns whether the button is the standard button (i.e. default).
132: * @return true if this button is the standard button
133: */
134: public final boolean isStandard() {
135: return standard;
136: }
137:
138: /**
139: * Sets whether this button is the standard button.
140: * There can only be a single standard button per <code>Window</code>,
141: * therefore setting this property to true will result in
142: * the 'standard' property of the current standard button to be set to false.
143: * @param standard true to make this the standard button, false otherwise.
144: * @throws UnsupportedOperationException if this Button's parent is not a Container.
145: */
146: public final void setStandard(boolean standard) {
147: boolean oldStandard = this .standard;
148: this .standard = standard;
149: Object o = getParent();
150:
151: if (o instanceof AbstractContainer) {
152: if (standard && !oldStandard) {
153: ((AbstractContainer) o)
154: .updateStandardButton(this , true);
155: } else if (!standard && oldStandard) {
156: ((AbstractContainer) o).updateStandardButton(this ,
157: false);
158: }
159: }
160:
161: firePropertyChange(this, PROPERTY_STANDARD, oldStandard,
162: standard);
163: }
164: }
|