KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > ButtonExample


1 /*
2  * $Id: ButtonExample.java,v 1.14 2005/06/03 13:17:17 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.SAbstractButton;
17 import org.wings.SButton;
18 import org.wings.SCheckBox;
19 import org.wings.SComponent;
20 import org.wings.SConstants;
21 import org.wings.SContainer;
22 import org.wings.SForm;
23 import org.wings.SGridLayout;
24 import org.wings.SIcon;
25 import org.wings.SLabel;
26 import org.wings.SPanel;
27 import org.wings.SURLIcon;
28 import org.wings.border.SEmptyBorder;
29
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
36  * @version $Revision: 1.14 $
37  */

38 public class ButtonExample extends WingSetPane {
39     // icons
40
private static final SIcon icon = new SURLIcon("../icons/ButtonIcon.gif");
41     private static final SIcon disabledIcon = new SURLIcon("../icons/ButtonDisabledIcon.gif");
42     private static final SIcon pressedIcon = new SURLIcon("../icons/ButtonPressedIcon.gif");
43     private static final SIcon rolloverIcon = new SURLIcon("../icons/ButtonRolloverIcon.gif");
44
45     // pressed label & handler
46
private final SLabel buttonPressedLabel = new SLabel("No button pressed");
47     private final ActionListener JavaDoc reportButtonPressedAction = new ActionListener JavaDoc() {
48         public void actionPerformed(ActionEvent JavaDoc e) {
49             buttonPressedLabel.setText("<html>Button <b>'" + e.getActionCommand() + "'</b> pressed");
50         }
51     };
52
53     // button control itself
54
private ButtonControls controls;
55
56     public SComponent createExample() {
57         controls = new ButtonControls();
58         controls.getApplyButton().addActionListener(reportButtonPressedAction);
59
60         final SForm form = new SForm(new SGridLayout(1));
61         final SContainer buttonExamplePanel = createButtonExample(form);
62
63         form.add(controls);
64         form.add(buttonExamplePanel);
65         return form;
66     }
67
68     private SContainer createButtonExample(SForm containingForm) {
69         final SButton[] buttons = new SButton[9];
70         final int[] textHPos = new int[] {SConstants.LEFT, SConstants.CENTER, SConstants.RIGHT};
71         final int[] textVPos = new int[] {SConstants.TOP, SConstants.CENTER, SConstants.BOTTOM};
72
73         for (int i = 0; i < buttons.length; i++) {
74             final String JavaDoc buttonName = "Text " + (i + 1);
75             buttons[i] = new SButton(buttonName);
76             buttons[i].setActionCommand(buttons[i].getText());
77             if (i != 4) {
78                 buttons[i].setIcon(icon);
79                 buttons[i].setDisabledIcon(disabledIcon);
80                 buttons[i].setRolloverIcon(rolloverIcon);
81                 buttons[i].setPressedIcon(pressedIcon);
82             }
83
84             buttons[i].setToolTipText("Button " + (i + 1));
85             buttons[i].setName("button" + (i + 1));
86             buttons[i].setShowAsFormComponent(false);
87             buttons[i].setVerticalTextPosition(textVPos[(i / 3)% 3]);
88             buttons[i].setHorizontalTextPosition(textHPos[i % 3]);
89             buttons[i].setActionCommand(buttonName);
90             controls.addSizable(buttons[i]);
91         }
92
93         final SGridLayout grid = new SGridLayout(3);
94         final SPanel buttonGrid = new SPanel(grid);
95         grid.setBorder(1);
96         grid.setHgap(10);
97         grid.setVgap(10);
98         buttonGrid.setHorizontalAlignment(CENTER);
99         buttonGrid.setBorder(new SEmptyBorder(10, 0, 10, 0));
100
101         for (int i = 0; i < buttons.length; i++) {
102             buttons[i].addActionListener(reportButtonPressedAction);
103             buttonGrid.add(buttons[i]);
104         }
105
106         // default button handling ==> if enter is pressed
107
final SButton defaultButton = new SButton();
108         defaultButton.addActionListener(reportButtonPressedAction);
109         defaultButton.setActionCommand("Default Button (Enter key)");
110         containingForm.setDefaultButton(defaultButton);
111
112         final SPanel panel = new SPanel(new SGridLayout(1));
113         final SLabel description = new SLabel("Click on the buttons and see how The label changes\n" +
114                 "Click into a input box and press apply or enter to see the effect of the enter button capturing.");
115         panel.setBorder(new SEmptyBorder(10, 10, 10, 10));
116         panel.add(description);
117         panel.add(buttonGrid);
118         panel.add(buttonPressedLabel);
119
120         return panel;
121     }
122
123     private class ButtonControls extends ComponentControls {
124         public ButtonControls() {
125             final SCheckBox showAsFormComponent = new SCheckBox("Show as Form Component");
126             showAsFormComponent.addActionListener(new ActionListener JavaDoc() {
127                 public void actionPerformed(ActionEvent JavaDoc e) {
128                     for (Iterator JavaDoc iterator = components.iterator(); iterator.hasNext();) {
129                         SComponent component = (SComponent) iterator.next();
130                         component.setShowAsFormComponent(showAsFormComponent.isSelected());
131                     }
132                 }
133             });
134             add(showAsFormComponent);
135
136             final SCheckBox useImages = new SCheckBox("Use Icons");
137             useImages.setSelected(true);
138             useImages.addActionListener(new ActionListener JavaDoc() {
139                 public void actionPerformed(ActionEvent JavaDoc e) {
140                     boolean use = useImages.isSelected();
141
142                     for (Iterator JavaDoc iterator = components.iterator(); iterator.hasNext();) {
143                         SAbstractButton component = (SAbstractButton) iterator.next();
144                         component.setIcon(use ? icon : null);
145                         component.setDisabledIcon(use ? disabledIcon : null);
146                         component.setRolloverIcon(use ? rolloverIcon : null);
147                         component.setPressedIcon(use ? pressedIcon : null);
148                     }
149                 }
150             });
151             add(useImages);
152         }
153
154         public SButton getApplyButton() {
155             return super.applyButton;
156         }
157     }
158 }
159
Popular Tags