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:
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.util.Iterator;
020:
021: /**
022: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
023: */
024: public class ToggleButtonExample extends WingSetPane {
025: final static int[] textHPos = new int[] { SConstants.LEFT,
026: SConstants.CENTER, SConstants.RIGHT };
027: final static int[] textVPos = new int[] { SConstants.TOP,
028: SConstants.CENTER, SConstants.BOTTOM };
029:
030: static final SIcon icon = new SURLIcon("../icons/ButtonIcon.gif");
031: static final SIcon disabledIcon = new SURLIcon(
032: "../icons/ButtonDisabledIcon.gif");
033: static final SIcon pressedIcon = new SURLIcon(
034: "../icons/ButtonPressedIcon.gif");
035: static final SIcon rolloverIcon = new SURLIcon(
036: "../icons/ButtonRolloverIcon.gif");
037: private ButtonControls controls;
038:
039: private final SLabel reportLabel = new SLabel("No button pressed");
040: protected ActionListener action = new java.awt.event.ActionListener() {
041: public void actionPerformed(ActionEvent e) {
042: reportLabel.setText("<html>Button <b>'"
043: + e.getActionCommand() + "'</b> pressed");
044: }
045: };
046: private SGridLayout grid;
047: private SPanel gridPanel;
048:
049: protected SComponent createControls() {
050: controls = new ButtonControls();
051: return controls;
052: }
053:
054: public SComponent createExample() {
055: return createButtonExample();
056: }
057:
058: SContainer createButtonExample() {
059: SButtonGroup group = new SButtonGroup();
060: SToggleButton[] buttons = new SToggleButton[9];
061:
062: for (int i = 0; i < buttons.length; i++) {
063: SToggleButton button = buttons[i] = new SToggleButton(
064: "Text " + (i + 1));
065: button.setShowAsFormComponent(true);
066: button.setActionCommand(button.getText());
067: if (i != 4) {
068: button.setIcon(icon);
069: button.setDisabledIcon(disabledIcon);
070: button.setRolloverIcon(rolloverIcon);
071: button.setPressedIcon(pressedIcon);
072: button.setSelectedIcon(pressedIcon);
073: }
074: button.setToolTipText("ToggleButton " + (i + 1));
075: button.setName("tb" + (i + 1));
076: button.setVerticalTextPosition(textVPos[(i / 3) % 3]);
077: button.setHorizontalTextPosition(textHPos[i % 3]);
078: group.add(button);
079: controls.addControllable(button);
080: }
081:
082: grid = new SGridLayout(3);
083: gridPanel = new SPanel(grid);
084: grid.setHgap(10);
085: grid.setVgap(10);
086:
087: for (int i = 0; i < buttons.length; i++) {
088: buttons[i].addActionListener(action);
089: gridPanel.add(buttons[i]);
090: }
091:
092: final SPanel panel = new SPanel(new SGridLayout(2, 1, 0, 20));
093: panel.add(gridPanel);
094: panel.add(reportLabel);
095:
096: return panel;
097: }
098:
099: class ButtonControls extends ComponentControls {
100: public ButtonControls() {
101: formComponentCheckBox.setSelected(true);
102: formComponentCheckBox
103: .addActionListener(new ActionListener() {
104: public void actionPerformed(ActionEvent e) {
105: grid.setBorder(formComponentCheckBox
106: .isSelected() ? 0 : 1);
107: gridPanel.reload();
108: }
109: });
110:
111: final SCheckBox useImages = new SCheckBox("Use Icons");
112: useImages.setSelected(true);
113: useImages
114: .addActionListener(new java.awt.event.ActionListener() {
115: public void actionPerformed(ActionEvent e) {
116: boolean use = useImages.isSelected();
117:
118: for (Iterator iterator = components
119: .iterator(); iterator.hasNext();) {
120: SAbstractButton component = (SAbstractButton) iterator
121: .next();
122: if (!"tb5".equals(component.getName())) {
123: component
124: .setIcon(use ? icon : null);
125: component
126: .setDisabledIcon(use ? disabledIcon
127: : null);
128: component
129: .setRolloverIcon(use ? rolloverIcon
130: : null);
131: component
132: .setPressedIcon(use ? pressedIcon
133: : null);
134: component
135: .setSelectedIcon(use ? pressedIcon
136: : null);
137: }
138: }
139: }
140: });
141: addControl(useImages);
142: }
143: }
144: }
|