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.SBoxLayout;
016: import org.wings.SButtonGroup;
017: import org.wings.SComponent;
018: import org.wings.SConstants;
019: import org.wings.SContainer;
020: import org.wings.SDimension;
021: import org.wings.SFont;
022: import org.wings.SGridLayout;
023: import org.wings.SIcon;
024: import org.wings.SLabel;
025: import org.wings.SPanel;
026: import org.wings.SRadioButton;
027: import org.wings.SResourceIcon;
028: import org.wings.SSpacer;
029: import org.wings.SURLIcon;
030:
031: import java.awt.*;
032: import java.awt.event.ActionEvent;
033:
034: /**
035: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
036: */
037: public class LabelExample extends WingSetPane {
038: private static final SIcon WAIT_ICON = new SResourceIcon(
039: "org/wings/icons/Wait.gif");
040: private static final String directions[] = { "nw", "n", "ne", "w",
041: "e", "sw", "s", "se" };
042: private static final SIcon onIcons[];
043: private static final SIcon offIcons[];
044: private ComponentControls controls;
045:
046: static {
047: onIcons = new SIcon[directions.length];
048: offIcons = new SIcon[directions.length];
049: for (int i = 0; i < directions.length; ++i) {
050: String d = directions[i];
051: onIcons[i] = new SURLIcon("../icons/Label" + d + "On.gif");
052: offIcons[i] = new SURLIcon("../icons/Label" + d + "Off.gif");
053: }
054: }
055:
056: protected SComponent createControls() {
057: controls = new ComponentControls();
058: controls.fontComboBox.setSelectedIndex(1);
059: controls.foregroundComboBox.setSelectedIndex(3);
060: return controls;
061: }
062:
063: public SComponent createExample() {
064: final SLabel testLabel = new SLabel(
065: "A styled SLabel with an icon");
066: testLabel.setHorizontalAlignment(LEFT);
067: testLabel.setIcon(WAIT_ICON);
068: testLabel.setForeground(Color.RED);
069: testLabel.setFont(new SFont("serif", SFont.ITALIC | SFont.BOLD,
070: 10));
071: controls.addControllable(testLabel);
072:
073: SPanel testPanel = new SPanel();
074: testPanel.setPreferredSize(new SDimension(250,
075: SDimension.AUTO_INT));
076: testPanel.add(testLabel);
077:
078: SLabel titleResult = new SLabel("Result");
079: titleResult.setHorizontalAlignment(SConstants.CENTER_ALIGN);
080:
081: SGridLayout layout = new SGridLayout(3, 3, 10, 10);
082: layout.setRenderFirstLineAsHeader(true);
083: SPanel panel = new SPanel(layout);
084: panel.add(new SLabel("Control the label's text position"));
085: panel.add(new SSpacer(50, 1));
086: panel.add(titleResult);
087: panel.add(createRoundRadio(testLabel));
088: panel.add(new SLabel());
089: panel.add(testPanel);
090:
091: return panel;
092: }
093:
094: private SRadioButton createRadio(SPanel p, String constraint,
095: SButtonGroup buttonGroup, String toolTip, int icon) {
096: SRadioButton button = new SRadioButton();
097: button.setIcon(offIcons[icon]);
098: button.setSelectedIcon(onIcons[icon]);
099: button.setToolTipText(toolTip);
100: p.add(button, constraint);
101: buttonGroup.add(button);
102: return button;
103: }
104:
105: SContainer createRoundRadio(final SLabel label) {
106: SPanel b = new SPanel(new SBoxLayout(SBoxLayout.Y_AXIS));
107:
108: SPanel sp = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS));
109: SButtonGroup g = new SButtonGroup();
110: final SRadioButton nw = createRadio(sp, "p=nw", g,
111: "North West", 0);
112: final SRadioButton n = createRadio(sp, "p=n", g, "North", 1);
113: final SRadioButton ne = createRadio(sp, "p=ne", g,
114: "North East", 2);
115: b.add(sp);
116:
117: SPanel sp1 = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS));
118:
119: final SRadioButton w = createRadio(sp1, "p=w", g, "West", 3);
120: SLabel sl = new SLabel();
121: sl.setIcon(new SURLIcon("../icons/cowSmall.gif"));
122: sp1.add(sl);
123: final SRadioButton e = createRadio(sp1, "p=e", g, "East", 4);
124: b.add(sp1);
125:
126: SPanel sp2 = new SPanel(new SBoxLayout(SBoxLayout.X_AXIS));
127: final SRadioButton sw = createRadio(sp2, "p=sw", g,
128: "South West", 5);
129: final SRadioButton s = createRadio(sp2, "p=s", g, "South", 6);
130: final SRadioButton se = createRadio(sp2, "p=se", g,
131: "South East", 7);
132:
133: nw.setShowAsFormComponent(false);
134: n.setShowAsFormComponent(false);
135: ne.setShowAsFormComponent(false);
136: w.setShowAsFormComponent(false);
137: e.setShowAsFormComponent(false);
138: sw.setShowAsFormComponent(false);
139: s.setShowAsFormComponent(false);
140: se.setShowAsFormComponent(false);
141:
142: b.add(sp2);
143:
144: g.addActionListener(new java.awt.event.ActionListener() {
145: public void actionPerformed(ActionEvent ev) {
146: Object button = ((SButtonGroup) ev.getSource())
147: .getSelection();
148: if (button == n) {
149: label.setVerticalTextPosition(TOP);
150: label.setHorizontalTextPosition(CENTER);
151: } else if (button == nw) {
152: label.setVerticalTextPosition(TOP);
153: label.setHorizontalTextPosition(LEFT);
154: } else if (button == ne) {
155: label.setVerticalTextPosition(TOP);
156: label.setHorizontalTextPosition(RIGHT);
157: } else if (button == w) {
158: label.setVerticalTextPosition(CENTER);
159: label.setHorizontalTextPosition(LEFT);
160: } else if (button == e) {
161: label.setVerticalTextPosition(CENTER);
162: label.setHorizontalTextPosition(RIGHT);
163: } else if (button == sw) {
164: label.setVerticalTextPosition(BOTTOM);
165: label.setHorizontalTextPosition(LEFT);
166: } else if (button == se) {
167: label.setVerticalTextPosition(BOTTOM);
168: label.setHorizontalTextPosition(RIGHT);
169: } else if (button == s) {
170: label.setVerticalTextPosition(BOTTOM);
171: label.setHorizontalTextPosition(CENTER);
172: }
173: /*
174: else if (button == cc) {
175: label.setVerticalTextPosition(CENTER);
176: label.setHorizontalTextPosition(CENTER);
177: }
178: */
179:
180: }
181: });
182:
183: n.setSelected(true);
184:
185: return b;
186: }
187: }
|