001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016: import org.wings.script.JavaScriptListener;
017: import org.wings.script.JavaScriptEvent;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.util.Iterator;
022:
023: /**
024: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
025: */
026: public class ButtonExample extends WingSetPane {
027: final static int[] textHPos = new int[] { SConstants.LEFT,
028: SConstants.CENTER, SConstants.RIGHT };
029: final static int[] textVPos = new int[] { SConstants.TOP,
030: SConstants.CENTER, SConstants.BOTTOM };
031:
032: // icons
033: private static final SIcon icon = new SResourceIcon(
034: "org/wings/icons/star.png");
035: private static final SIcon disabledIcon = new SResourceIcon(
036: "org/wings/icons/star_disabled.png");
037: private static final SIcon pressedIcon = new SResourceIcon(
038: "org/wings/icons/star.png");
039: private static final SIcon rolloverIcon = new SResourceIcon(
040: "org/wings/icons/star.png");
041:
042: // pressed label & handler
043: private final SLabel reportLabel = new SLabel("No button pressed");
044: private final ActionListener action = new ActionListener() {
045: public void actionPerformed(ActionEvent e) {
046: reportLabel.setText("<html>Button <b>'"
047: + e.getActionCommand() + "'</b> pressed");
048: }
049: };
050:
051: // button control itself
052: private ButtonControls controls;
053: private SButton[] buttons;
054: private SGridLayout grid;
055: private SPanel gridPanel;
056:
057: protected SComponent createControls() {
058: controls = new ButtonControls();
059: return controls;
060: }
061:
062: public SComponent createExample() {
063: SContainer example = createButtonExample();
064:
065: final SButton defaultButton = new SButton();
066: defaultButton.addActionListener(action);
067: defaultButton.setActionCommand("Default Button (Enter key)");
068: return example;
069: }
070:
071: private SContainer createButtonExample() {
072: buttons = new SButton[9];
073:
074: for (int i = 0; i < buttons.length; i++) {
075: final String buttonName = "Text " + (i + 1);
076: SButton button = buttons[i] = new SButton(buttonName);
077: button.setShowAsFormComponent(true);
078: button.setActionCommand(button.getText());
079:
080: button.setToolTipText("Button " + (i + 1));
081: button.setName("bu" + (i + 1));
082: button.setVerticalTextPosition(textVPos[(i / 3) % 3]);
083: button.setHorizontalTextPosition(textHPos[i % 3]);
084: button.setActionCommand(buttonName);
085: controls.addControllable(button);
086: }
087:
088: updateIconUsage(true);
089:
090: grid = new SGridLayout(3);
091: gridPanel = new SPanel(grid);
092: grid.setHgap(10);
093: grid.setVgap(10);
094:
095: for (int i = 0; i < buttons.length; i++) {
096: buttons[i].addActionListener(action);
097: gridPanel.add(buttons[i]);
098: }
099:
100: final SPanel panel = new SPanel(new SGridLayout(2, 1, 0, 20));
101: panel.add(gridPanel);
102: panel.add(reportLabel);
103:
104: addSomeConfirmDialogues();
105:
106: return panel;
107: }
108:
109: /**
110: * Register and use some <code>JavaScriptEvent.ON_CLICK</code> listeners to react on button click.
111: * This tests the feature that return false aborts the button submit.
112: */
113: private void addSomeConfirmDialogues() {
114: for (int i = 0; i < 3; i++) {
115: buttons[i].addScriptListener(new JavaScriptListener(
116: JavaScriptEvent.ON_CLICK,
117: "return window.confirm('Please confirm click of button "
118: + (i + 1) + "');"));
119: }
120: }
121:
122: /**
123: * Use some icons in buttons or not.
124: */
125: private void updateIconUsage(boolean useIcons) {
126: for (int i = 0; i < buttons.length; i++) {
127: final SButton button = buttons[i];
128: if (i != 4) {
129: button.setIcon(useIcons ? icon : null);
130: button.setDisabledIcon(useIcons ? disabledIcon : null);
131: button.setRolloverIcon(useIcons ? rolloverIcon : null);
132: button.setPressedIcon(useIcons ? pressedIcon : null);
133: }
134: }
135: }
136:
137: /**
138: * Update {@link SComponent#setEnabled(boolean)}
139: */
140: private void updateEnabled(boolean allEnabled) {
141: for (int i = 0; i < buttons.length; i++) {
142: buttons[i].setEnabled(allEnabled || Math.random() > 0.3);
143: }
144: }
145:
146: /**
147: * The additional control toolbar for the button example
148: */
149: private class ButtonControls extends ComponentControls {
150: private STextField iconTextGap = new STextField("4");
151:
152: public ButtonControls() {
153: formComponentCheckBox.setSelected(true);
154: formComponentCheckBox
155: .addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: grid.setBorder(formComponentCheckBox
158: .isSelected() ? 0 : 1);
159: gridPanel.reload();
160: }
161: });
162:
163: final SCheckBox useImages = new SCheckBox("Use Icons");
164: useImages.setSelected(true);
165: useImages
166: .addActionListener(new java.awt.event.ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: updateIconUsage(useImages.isSelected());
169: }
170: });
171: addControl(useImages);
172:
173: final SCheckBox disableSomeButtons = new SCheckBox(
174: "Disable a few buttons");
175: disableSomeButtons
176: .addActionListener(new java.awt.event.ActionListener() {
177: public void actionPerformed(ActionEvent e) {
178: updateEnabled(!disableSomeButtons
179: .isSelected());
180: }
181: });
182:
183: iconTextGap.addActionListener(new ActionListener() {
184: public void actionPerformed(ActionEvent e) {
185: for (Iterator iterator = getControllables()
186: .iterator(); iterator.hasNext();) {
187: SAbstractIconTextCompound component = (SAbstractIconTextCompound) iterator
188: .next();
189: try {
190: component.setIconTextGap(Integer
191: .parseInt(iconTextGap.getText()));
192: } catch (NumberFormatException invalidNumber) {
193: component.setIconTextGap(0);
194: }
195: }
196: }
197: });
198: addControl(new SLabel(""));
199: addControl(disableSomeButtons);
200: addControl(new SLabel("iconTextGap"));
201: addControl(iconTextGap);
202: }
203: }
204: }
|