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.SButton;
017: import org.wings.SCheckBox;
018: import org.wings.SComboBox;
019: import org.wings.SComponent;
020: import org.wings.SConstants;
021: import org.wings.SDefaultListCellRenderer;
022: import org.wings.SDimension;
023: import org.wings.SFont;
024: import org.wings.SGridBagLayout;
025: import org.wings.SLabel;
026: import org.wings.SPanel;
027: import org.wings.STextField;
028: import org.wings.SToolBar;
029: import org.wings.border.*;
030: import org.wings.session.SessionManager;
031: import org.wings.style.CSSProperty;
032:
033: import java.awt.*;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.util.Iterator;
037: import java.util.LinkedList;
038: import java.util.List;
039: import java.util.Arrays;
040: import java.beans.PropertyChangeListener;
041: import java.beans.PropertyChangeEvent;
042:
043: /**
044: * A visual control used in many WingSet demos.
045: *
046: * @author hengels
047: */
048: public class ComponentControls extends SPanel {
049: protected static final Object[] BORDERS = new Object[] {
050: new Object[] { "default", SDefaultBorder.INSTANCE },
051: new Object[] { "none", null },
052: new Object[] { "raised",
053: new SBevelBorder(SBevelBorder.RAISED) },
054: new Object[] { "lowered",
055: new SBevelBorder(SBevelBorder.LOWERED) },
056: new Object[] { "line", new SLineBorder(2) },
057: new Object[] { "grooved",
058: new SEtchedBorder(SEtchedBorder.LOWERED) },
059: new Object[] { "ridged",
060: new SEtchedBorder(SEtchedBorder.RAISED) },
061: new Object[] { "empty", new SEmptyBorder(5, 5, 5, 5) } };
062:
063: protected static final Object[] COLORS = new Object[] {
064: new Object[] { "none", null },
065: new Object[] { "gray", Color.GRAY },
066: new Object[] { "yellow", new Color(255, 255, 100) },
067: new Object[] { "red", new Color(255, 100, 100) },
068: new Object[] { "green", new Color(100, 255, 100) },
069: new Object[] { "blue", new Color(100, 100, 255) }, };
070:
071: protected static final Object[] FONTS = new Object[] {
072: new Object[] { "default font", null },
073: new Object[] {
074: "serif italic bold",
075: new SFont("Times,Times New Roman,serif",
076: SFont.ITALIC | SFont.BOLD, 10) },
077: new Object[] {
078: "serif",
079: new SFont("Times,Times New Roman,serif",
080: SFont.PLAIN, SFont.DEFAULT_SIZE) },
081: new Object[] { "16 sans bold",
082: new SFont("Arial,sans-serif", SFont.BOLD, 16) },
083: new Object[] {
084: "24 fantasy italic",
085: new SFont("Comic,Comic Sans MS,fantasy",
086: SFont.ITALIC, 24) } };
087:
088: protected final List components = new LinkedList();
089:
090: protected final SToolBar globalControls = new SToolBar();
091: protected final SToolBar localControls = new SToolBar();
092:
093: protected final SCheckBox ajaxCheckBox = new SCheckBox("AJAX");
094:
095: protected final STextField widthTextField = new STextField();
096: protected final STextField heightTextField = new STextField();
097: protected final STextField insetsTextField = new STextField();
098:
099: protected final SComboBox borderStyleComboBox = new SComboBox(
100: BORDERS);
101: protected final SComboBox borderColorComboBox = new SComboBox(
102: COLORS);
103: protected final STextField borderThicknessTextField = new STextField();
104:
105: protected final SComboBox backgroundComboBox = new SComboBox(COLORS);
106: protected final SComboBox foregroundComboBox = new SComboBox(COLORS);
107: protected final SComboBox fontComboBox = new SComboBox(FONTS);
108: protected final SCheckBox formComponentCheckBox = new SCheckBox(
109: "as form");
110:
111: private SLabel placeHolder = new SLabel("<html> ");
112:
113: public ComponentControls() {
114: super (new SGridBagLayout());
115: setBackground(new Color(240, 240, 240));
116: setPreferredSize(SDimension.FULLWIDTH);
117:
118: SBorder border = new SLineBorder(1, new Insets(0, 3, 0, 6));
119: border.setColor(new Color(255, 255, 255), SConstants.TOP);
120: border.setColor(new Color(255, 255, 255), SConstants.LEFT);
121: border.setColor(new Color(190, 190, 190), SConstants.RIGHT);
122: border.setColor(new Color(190, 190, 190), SConstants.BOTTOM);
123: globalControls.setBorder(border);
124: globalControls.setHorizontalAlignment(SConstants.LEFT_ALIGN);
125: ((SBoxLayout) globalControls.getLayout()).setHgap(3);
126: ((SBoxLayout) globalControls.getLayout()).setVgap(4);
127:
128: border = new SLineBorder(1, new Insets(0, 3, 0, 6));
129: border.setColor(new Color(255, 255, 255), SConstants.TOP);
130: border.setColor(new Color(255, 255, 255), SConstants.LEFT);
131: border.setColor(new Color(190, 190, 190), SConstants.RIGHT);
132: border.setColor(new Color(190, 190, 190), SConstants.BOTTOM);
133: localControls.setBorder(border);
134: localControls.setHorizontalAlignment(SConstants.LEFT_ALIGN);
135: ((SBoxLayout) localControls.getLayout()).setHgap(6);
136: ((SBoxLayout) localControls.getLayout()).setVgap(4);
137:
138: GridBagConstraints c = new GridBagConstraints();
139: c.gridwidth = GridBagConstraints.RELATIVE;
140: c.gridwidth = GridBagConstraints.REMAINDER;
141: c.gridheight = 1;
142: add(globalControls, c);
143: add(localControls, c);
144:
145: ajaxCheckBox.setSelected(SessionManager.getSession()
146: .getRootFrame().isUpdateEnabled());
147: widthTextField.setColumns(3);
148: widthTextField
149: .setToolTipText("length with unit (example: '200px')");
150: heightTextField.setColumns(3);
151: heightTextField
152: .setToolTipText("length with unit (example: '200px')");
153: insetsTextField.setColumns(1);
154: insetsTextField
155: .setToolTipText("length only (example: '8')\n(applies to the border!)");
156: borderThicknessTextField.setColumns(1);
157: borderThicknessTextField
158: .setToolTipText("length only (example: '2')");
159: borderStyleComboBox.setRenderer(new ObjectPairCellRenderer());
160: borderColorComboBox.setRenderer(new ObjectPairCellRenderer());
161: backgroundComboBox.setRenderer(new ObjectPairCellRenderer());
162: foregroundComboBox.setRenderer(new ObjectPairCellRenderer());
163: fontComboBox.setRenderer(new ObjectPairCellRenderer());
164: formComponentCheckBox
165: .setToolTipText("show as form component .. i.e. trigger form submission");
166:
167: globalControls.add(ajaxCheckBox);
168: globalControls.addSeparator();
169: globalControls.add(new SLabel("width"));
170: globalControls.add(widthTextField);
171: globalControls.add(new SLabel("height"));
172: globalControls.add(heightTextField);
173: globalControls.add(new SLabel("border"));
174: globalControls.add(borderThicknessTextField);
175: globalControls.add(borderStyleComboBox);
176: globalControls.add(borderColorComboBox);
177: globalControls.add(new SLabel("insets"));
178: globalControls.add(insetsTextField);
179: globalControls.add(new SLabel("color"));
180: globalControls.add(foregroundComboBox);
181: globalControls.add(backgroundComboBox);
182: globalControls.add(new SLabel("font"));
183: globalControls.add(fontComboBox);
184: globalControls.add(new SLabel(""));
185: globalControls.add(formComponentCheckBox);
186:
187: localControls.add(placeHolder);
188:
189: ActionListener ajaxListener = new ActionListener() {
190: public void actionPerformed(ActionEvent event) {
191: ajax();
192: }
193: };
194: ajaxCheckBox.addActionListener(ajaxListener);
195:
196: ActionListener preferredSizeListener = new ActionListener() {
197: public void actionPerformed(ActionEvent event) {
198: preferredSize();
199: }
200: };
201: widthTextField.addActionListener(preferredSizeListener);
202: heightTextField.addActionListener(preferredSizeListener);
203:
204: ActionListener borderListener = new ActionListener() {
205: public void actionPerformed(ActionEvent event) {
206: border();
207: }
208: };
209: borderThicknessTextField.addActionListener(borderListener);
210: borderStyleComboBox.addActionListener(borderListener);
211: borderColorComboBox.addActionListener(borderListener);
212: insetsTextField.addActionListener(borderListener);
213:
214: ActionListener foregroundListener = new ActionListener() {
215: public void actionPerformed(ActionEvent event) {
216: foreground();
217: }
218: };
219: foregroundComboBox.addActionListener(foregroundListener);
220:
221: ActionListener backgroundListener = new ActionListener() {
222: public void actionPerformed(ActionEvent event) {
223: background();
224: }
225: };
226: backgroundComboBox.addActionListener(backgroundListener);
227:
228: ActionListener fontListener = new ActionListener() {
229: public void actionPerformed(ActionEvent event) {
230: font();
231: }
232: };
233: fontComboBox.addActionListener(fontListener);
234:
235: ActionListener showAsFormComponentListener = new ActionListener() {
236: public void actionPerformed(ActionEvent event) {
237: showAsFormComponent();
238: }
239: };
240: formComponentCheckBox
241: .addActionListener(showAsFormComponentListener);
242:
243: SessionManager.getSession().addPropertyChangeListener("ajax",
244: new PropertyChangeListener() {
245: public void propertyChange(PropertyChangeEvent evt) {
246: ajaxCheckBox.setSelected((Boolean) evt
247: .getNewValue());
248: }
249: });
250: }
251:
252: void ajax() {
253: SessionManager.getSession().getRootFrame().setUpdateEnabled(
254: ajaxCheckBox.isSelected());
255: SessionManager.getSession().setProperty("ajax",
256: ajaxCheckBox.isSelected());
257: }
258:
259: void preferredSize() {
260: for (Iterator iterator = components.iterator(); iterator
261: .hasNext();) {
262: SComponent component = (SComponent) iterator.next();
263:
264: SDimension preferredSize = new SDimension();
265: preferredSize.setWidth(widthTextField.getText());
266: preferredSize.setHeight(heightTextField.getText());
267:
268: component.setPreferredSize(preferredSize);
269: }
270: }
271:
272: void border() {
273: int insets = 0;
274: try {
275: insets = Integer.parseInt(insetsTextField.getText());
276: } catch (NumberFormatException e) {
277: }
278:
279: int borderThickness = 1;
280: try {
281: borderThickness = Integer.parseInt(borderThicknessTextField
282: .getText());
283: } catch (NumberFormatException e) {
284: }
285:
286: SAbstractBorder border = (SAbstractBorder) getSelectedObject(borderStyleComboBox);
287: if (border != null && border != SDefaultBorder.INSTANCE) {
288: border
289: .setColor((Color) getSelectedObject(borderColorComboBox));
290: border
291: .setInsets(new Insets(insets, insets, insets,
292: insets));
293: border.setThickness(borderThickness);
294: }
295:
296: for (Iterator iterator = components.iterator(); iterator
297: .hasNext();) {
298: SComponent component = (SComponent) iterator.next();
299: component.setBorder(border != null ? (SBorder) border
300: .clone() : null);
301: }
302: }
303:
304: void background() {
305: for (Iterator iterator = components.iterator(); iterator
306: .hasNext();) {
307: SComponent component = (SComponent) iterator.next();
308: component
309: .setBackground((Color) getSelectedObject(backgroundComboBox));
310: }
311: }
312:
313: void foreground() {
314: for (Iterator iterator = components.iterator(); iterator
315: .hasNext();) {
316: SComponent component = (SComponent) iterator.next();
317: component
318: .setForeground((Color) getSelectedObject(foregroundComboBox));
319: }
320: }
321:
322: void font() {
323: for (Iterator iterator = components.iterator(); iterator
324: .hasNext();) {
325: SComponent component = (SComponent) iterator.next();
326: component.setFont((SFont) getSelectedObject(fontComboBox));
327: }
328: }
329:
330: void showAsFormComponent() {
331: for (Iterator iterator = components.iterator(); iterator
332: .hasNext();) {
333: SComponent component = (SComponent) iterator.next();
334: component.setShowAsFormComponent(formComponentCheckBox
335: .isSelected());
336: }
337: }
338:
339: protected Object getSelectedObject(SComboBox combo) {
340: return combo.getSelectedIndex() != -1 ? ((Object[]) combo
341: .getSelectedItem())[1] : null;
342: }
343:
344: public void removeGlobalControl(SComponent control) {
345: int index = Arrays.asList(globalControls.getComponents())
346: .indexOf(control);
347: if (index >= 0) {
348: globalControls.remove(index); // comp
349: if (globalControls.getComponent(index - 1) instanceof SLabel)
350: globalControls.remove(index - 1); // label
351: }
352: }
353:
354: public void addControl(SComponent component) {
355: if (localControls.getComponent(0) == placeHolder)
356: localControls.removeAll();
357: localControls.add(component);
358: }
359:
360: public void addControllable(SComponent component) {
361: components.add(component);
362: }
363:
364: public List getControllables() {
365: return components;
366: }
367:
368: /**
369: * Renderer which expects <code>Object[]</code> values and returns the first value
370: */
371: protected static class ObjectPairCellRenderer extends
372: SDefaultListCellRenderer {
373: public SComponent getListCellRendererComponent(SComponent list,
374: Object value, boolean selected, int row) {
375: Object[] objects = (Object[]) value;
376: value = objects[0];
377: return super .getListCellRendererComponent(list, value,
378: selected, row);
379: }
380: }
381: }
382:
383: /*
384: SButton switchDebugViewButton = new SButton("Toggle AJAX debug view");
385: switchStyleButton.setShowAsFormComponent(false);
386: switchDebugViewButton.addScriptListener(new JavaScriptListener(
387: JavaScriptEvent.ON_CLICK,
388: "wingS.ajax.toggleDebugView(); return false;"
389: ));
390: */
|