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.SBorderLayout;
016: import org.wings.SBoxLayout;
017: import org.wings.SComboBox;
018: import org.wings.SComponent;
019: import org.wings.SConstants;
020: import org.wings.SDimension;
021: import org.wings.SFlowDownLayout;
022: import org.wings.SFlowLayout;
023: import org.wings.SGridBagLayout;
024: import org.wings.SGridLayout;
025: import org.wings.SLabel;
026: import org.wings.SLayoutManager;
027: import org.wings.SPanel;
028: import org.wings.border.SBorder;
029: import org.wings.border.SLineBorder;
030:
031: import java.awt.*;
032: import java.awt.event.ItemEvent;
033: import java.awt.event.ItemListener;
034:
035: /**
036: * A quickhack example to show the capabilities of the dynamic wings layout managers.
037: *
038: * @author bschmid
039: */
040: public class DynamicLayoutExample extends WingSetPane {
041: private final SPanel[] demoPanels = { new BorderLayoutDemoPanel(),
042: new FlowLayoutDemoPanel(), new GridBagDemoPanel(),
043: new GridLayoutDemoPanel(), new BoxLayoutDemoPanel() };
044: private final static String[] demoManagerNames = { "SBorderLayout",
045: "SFlowLayout/SFlowDownLayout", "SGridBagLayout",
046: "SGridLayout", "SBoxLayout" };
047: private final SComboBox selectLayoutManager = new SComboBox(
048: demoManagerNames);
049: protected SPanel panel;
050: private ComponentControls controls;
051:
052: protected SComponent createControls() {
053: controls = new LayoutControls();
054: return controls;
055: }
056:
057: protected SComponent createExample() {
058: panel = new SPanel(new SBorderLayout(10, 10));
059: panel.setPreferredSize(SDimension.FULLWIDTH);
060: panel.add(demoPanels[0], SBorderLayout.CENTER);
061: return panel;
062: }
063:
064: private static class BorderLayoutDemoPanel extends SPanel {
065: private int i = 0;
066: private static final Color[] COLORS = new Color[] { Color.GRAY,
067: new Color(255, 150, 150), new Color(255, 255, 150),
068: new Color(150, 255, 150), new Color(150, 255, 255),
069: new Color(150, 150, 255), };
070:
071: public BorderLayoutDemoPanel() {
072: super (new SFlowDownLayout());
073:
074: add(createDescriptionLabel("The box layout allows to position a component in various regions of a panel: "
075: + "NORTH, SOUTH, EAST, WEST and CENTER.\n"
076: + "Normally these sections are invisible but we encouloured them here for demonstration the component"
077: + "alignment inside the cells."));
078:
079: add(new SLabel("\nA default SBorderLayout"));
080: SPanel borderDemoPanel1 = new SPanel(new SBorderLayout());
081: borderDemoPanel1.add(
082: wrapIntoColouredPane(createDummyLabel(0)),
083: SBorderLayout.NORTH);
084: borderDemoPanel1.add(
085: wrapIntoColouredPane(createDummyLabel(1)),
086: SBorderLayout.SOUTH);
087: borderDemoPanel1.add(
088: wrapIntoColouredPane(createDummyLabel(2)),
089: SBorderLayout.EAST);
090: borderDemoPanel1.add(
091: wrapIntoColouredPane(createDummyLabel(3)),
092: SBorderLayout.WEST);
093: borderDemoPanel1.add(
094: wrapIntoColouredPane(createDummyLabel(4)),
095: SBorderLayout.CENTER);
096: add(borderDemoPanel1);
097: borderDemoPanel1.setPreferredSize(new SDimension(800, 150));
098: borderDemoPanel1.setBackground(new Color(210, 210, 210));
099: }
100:
101: private SComponent wrapIntoColouredPane(SComponent c) {
102: SPanel colouredPanel = new SPanel(new SBoxLayout(
103: SBoxLayout.VERTICAL));
104: colouredPanel.add(c);
105: colouredPanel.setPreferredSize(SDimension.FULLAREA);
106: colouredPanel
107: .setVerticalAlignment(c.getVerticalAlignment());
108: colouredPanel.setHorizontalAlignment(c
109: .getHorizontalAlignment());
110: colouredPanel.setBackground(COLORS[i++ % COLORS.length]);
111: return colouredPanel;
112: }
113: }
114:
115: private static class BoxLayoutDemoPanel extends SPanel {
116: public BoxLayoutDemoPanel() {
117: super (new SFlowDownLayout());
118:
119: add(createDescriptionLabel("The box layout is a simple version of gridlayout. It allows you to arrange "
120: + "components in a simple line - either horizontally or vertically aligned. Look at SFlowLayout "
121: + "if you require a wrapping horizontal alignment."));
122:
123: add(new SLabel(
124: "\nHorizontal box layout with padding & border"));
125: SBoxLayout horizontalLayout = new SBoxLayout(
126: SBoxLayout.HORIZONTAL);
127: horizontalLayout.setHgap(10);
128: horizontalLayout.setVgap(10);
129: horizontalLayout.setBorder(1);
130: add(createPanel(horizontalLayout, 4));
131:
132: add(new SLabel("\nVertical vanilla box layout"));
133: SBoxLayout verticalLayout = new SBoxLayout(
134: SBoxLayout.VERTICAL);
135: add(createPanel(verticalLayout, 4));
136: }
137: }
138:
139: private static class FlowLayoutDemoPanel extends SPanel {
140: public FlowLayoutDemoPanel() {
141: super (new SFlowDownLayout());
142: add(createDescriptionLabel("Demonstration of flowing layout managers SFlowLayout and SFlowDownLayout\n"
143: + "Try out to resize the browser window to smaller/larger size. These labels below should 'flow'. "
144: + "This means they should break into the next line if the space is not sufficient to display them"
145: + "in the current line.\nPlease note that some component alignments won't work correctly "
146: + "due to restrictions of the rendering engines."));
147:
148: // SFlowLayout
149: add(new SLabel(" "));
150: add(createDescriptionLabel("SFlowLayout"));
151: add(new SLabel(
152: "SFlowLayout - Layout Alignment: default (SFlowLayout.LEFT) - Container alignment: default"));
153: add(createPanel(new SFlowLayout(SFlowLayout.LEFT), 4));
154: add(new SLabel(
155: "\nSFlowLayout - Alignment: SFlowLayout.CENTER - Container alignment: center"));
156: final SPanel panel2 = createPanel(new SFlowLayout(
157: SFlowLayout.CENTER), 4);
158: panel2.setHorizontalAlignment(CENTER);
159: add(panel2);
160: add(new SLabel(
161: "\nSFlowLayout - Alignment: SFlowLayout.RIGHT - Container alignment: right"));
162: final SPanel panel3 = createPanel(new SFlowLayout(
163: SFlowLayout.RIGHT), 4);
164: panel3.setHorizontalAlignment(RIGHT);
165: add(panel3);
166:
167: // SFlowDownLayout
168: add(new SLabel(" "));
169: add(createDescriptionLabel("SFlowDownLayout"));
170: add(new SLabel(
171: "SFlowDownLayout - Container alignment: default"));
172: add(createPanel(new SFlowDownLayout(), 4));
173: add(new SLabel(
174: "\nSFlowDownLayout - Container alignment: center"));
175: final SPanel panel4 = createPanel(new SFlowDownLayout(), 3);
176: panel4.setHorizontalAlignment(CENTER);
177: add(panel4);
178: add(new SLabel(
179: "\nSFlowDownLayout - Container alignment: right"));
180: final SPanel panel5 = createPanel(new SFlowDownLayout(), 4);
181: panel5.setHorizontalAlignment(RIGHT);
182: add(panel5);
183: }
184: }
185:
186: private static class GridLayoutDemoPanel extends SPanel {
187: public GridLayoutDemoPanel() {
188: super (new SFlowDownLayout());
189: add(createDescriptionLabel("The grid layout is a simple way to arrange your components in a tabular manner.\n"
190: + "The example below adds 9 components in a 3-columned grid layout."));
191:
192: add(new SLabel(
193: "\nGrid Layout panel with 3 colums, border, 10px horizontal gap, 40 vertical gap"));
194: SGridLayout layout1 = new SGridLayout(3);
195: layout1.setBorder(1);
196: layout1.setHgap(10);
197: layout1.setVgap(40);
198:
199: add(createPanel(layout1, 12));
200: }
201: }
202:
203: private static class GridBagDemoPanel extends SPanel {
204: public GridBagDemoPanel() {
205: setLayout(new SFlowDownLayout());
206:
207: add(createDescriptionLabel("The SGridBagLayout is a powerful layout manager to align components "
208: + "in a very complex manner. "));
209:
210: addRemainderDemo();
211: addPredefinedGridXAndXDemo();
212: addRandomAddngWithPreDefinedGridXandY();
213: addWeightBasedDemo();
214: addGridWidthRelativeDemo();
215: addGridHeightRelativeDemo();
216: }
217:
218: private void addGridHeightRelativeDemo() {
219: SGridBagLayout layout;
220: SPanel p;
221: GridBagConstraints c;
222: add(new SLabel("\nVertical adding with gridheight=RELATIVE"));
223: layout = new SGridBagLayout();
224: layout.setBorder(1);
225: p = new SPanel(layout);
226: add(p);
227: c = new GridBagConstraints();
228:
229: c.gridx = 0;
230: p.add(new SLabel("1"), c);
231: p.add(new SLabel("2"), c);
232: p.add(new SLabel("3"), c);
233: p.add(new SLabel("4"), c);
234: c.gridheight = GridBagConstraints.RELATIVE;
235: p.add(new SLabel("5"), c);
236: c.gridheight = 1;
237: p.add(new SLabel("end #1"), c);
238:
239: c.gridx = 1;
240: p.add(new SLabel("6"), c);
241: p.add(new SLabel("7"), c);
242: p.add(new SLabel("8"), c);
243: c.gridheight = GridBagConstraints.RELATIVE;
244: p.add(new SLabel("9"), c);
245: c.gridheight = 1;
246: p.add(new SLabel("end #2"), c);
247:
248: c.gridx = 2;
249: p.add(new SLabel("10"), c);
250: p.add(new SLabel("11"), c);
251: c.gridheight = GridBagConstraints.RELATIVE;
252: p.add(new SLabel("12"), c);
253: c.gridheight = 1;
254: p.add(new SLabel("end #3"), c);
255:
256: c.gridx = 3;
257: p.add(new SLabel("13"), c);
258: c.gridheight = GridBagConstraints.RELATIVE;
259: p.add(new SLabel("14"), c);
260: c.gridheight = 1;
261: p.add(new SLabel("end #4"), c);
262:
263: c.gridx = 4;
264: c.gridheight = GridBagConstraints.RELATIVE;
265: p.add(new SLabel("15"), c);
266: c.gridheight = 1;
267: p.add(new SLabel("end #5"), c);
268: }
269:
270: private void addGridWidthRelativeDemo() {
271: SGridBagLayout layout;
272: SPanel p;
273: GridBagConstraints c;
274: add(new SLabel("\nAdding with gridwidth=RELATIVE"));
275: layout = new SGridBagLayout();
276: layout.setBorder(1);
277: p = new SPanel(layout);
278: add(p);
279: c = new GridBagConstraints();
280: p.add(new SLabel("1"), c);
281: p.add(new SLabel("2"), c);
282: p.add(new SLabel("3"), c);
283: p.add(new SLabel("4"), c);
284: c.gridwidth = GridBagConstraints.RELATIVE;
285: p.add(new SLabel("5"), c);
286: c.gridwidth = 1;
287: p.add(new SLabel("end #1"), c);
288:
289: p.add(new SLabel("6"), c);
290: p.add(new SLabel("7"), c);
291: p.add(new SLabel("8"), c);
292: c.gridwidth = GridBagConstraints.RELATIVE;
293: p.add(new SLabel("9"), c);
294: c.gridwidth = 1;
295: p.add(new SLabel("end #2"), c);
296:
297: p.add(new SLabel("10"), c);
298: p.add(new SLabel("11"), c);
299: c.gridwidth = GridBagConstraints.RELATIVE;
300: p.add(new SLabel("12"), c);
301: c.gridwidth = 1;
302: p.add(new SLabel("end #3"), c);
303:
304: p.add(new SLabel("13"), c);
305: c.gridwidth = GridBagConstraints.RELATIVE;
306: p.add(new SLabel("14"), c);
307: c.gridwidth = 1;
308: p.add(new SLabel("end #4"), c);
309:
310: c.gridwidth = GridBagConstraints.RELATIVE;
311: p.add(new SLabel("15"), c);
312: c.gridwidth = 1;
313: p.add(new SLabel("end #5"), c);
314: }
315:
316: private void addWeightBasedDemo() {
317: SGridBagLayout layout;
318: SPanel p;
319: GridBagConstraints c;
320: add(new SLabel("\nUsing weight"));
321: layout = new SGridBagLayout();
322: layout.setBorder(1);
323: p = new SPanel(layout);
324: add(p);
325: p.setPreferredSize(new SDimension(500, 500));
326:
327: c = new GridBagConstraints();
328: c.gridx = 0;
329: c.gridy = 0;
330: c.weightx = 0;
331: c.weighty = 0;
332: p.add(new SLabel("1"), c);
333: c.gridx = 1;
334: c.gridy = 0;
335: c.weightx = 1;
336: c.weighty = 0;
337: p.add(new SLabel("2"), c);
338: c.gridx = 2;
339: c.gridy = 0;
340: c.weightx = 2;
341: c.weighty = 0;
342: p.add(new SLabel("3"), c);
343: c.gridx = 3;
344: c.gridy = 0;
345: c.weightx = 1;
346: c.weighty = 0;
347: p.add(new SLabel("4"), c);
348: c.gridx = 4;
349: c.gridy = 0;
350: c.weightx = 0;
351: c.weighty = 0;
352: p.add(new SLabel("5"), c);
353:
354: c.gridx = 0;
355: c.gridy = 1;
356: c.weightx = 0;
357: c.weighty = 1;
358: p.add(new SLabel("6"), c);
359: c.gridx = 1;
360: c.gridy = 1;
361: c.weightx = 1;
362: c.weighty = 1;
363: p.add(new SLabel("7"), c);
364: c.gridx = 2;
365: c.gridy = 1;
366: c.weightx = 2;
367: c.weighty = 1;
368: p.add(new SLabel("8"), c);
369: c.gridx = 3;
370: c.gridy = 1;
371: c.weightx = 1;
372: c.weighty = 1;
373: p.add(new SLabel("9"), c);
374: c.gridx = 4;
375: c.gridy = 1;
376: c.weightx = 0;
377: c.weighty = 1;
378: p.add(new SLabel("10"), c);
379:
380: c.gridx = 0;
381: c.gridy = 2;
382: c.weightx = 0;
383: c.weighty = 2;
384: p.add(new SLabel("11"), c);
385: c.gridx = 1;
386: c.gridy = 2;
387: c.weightx = 1;
388: c.weighty = 2;
389: p.add(new SLabel("12"), c);
390: c.gridx = 2;
391: c.gridy = 2;
392: c.weightx = 2;
393: c.weighty = 2;
394: p.add(new SLabel("13"), c);
395: c.gridx = 3;
396: c.gridy = 2;
397: c.weightx = 1;
398: c.weighty = 2;
399: p.add(new SLabel("14"), c);
400: c.gridx = 4;
401: c.gridy = 2;
402: c.weightx = 0;
403: c.weighty = 2;
404: p.add(new SLabel("15"), c);
405: c.gridx = 0;
406: c.gridy = 3;
407: c.weightx = 0;
408: c.weighty = 1;
409: p.add(new SLabel("16"), c);
410: c.gridx = 1;
411: c.gridy = 3;
412: c.weightx = 1;
413: c.weighty = 1;
414: p.add(new SLabel("17"), c);
415: c.gridx = 2;
416: c.gridy = 3;
417: c.weightx = 2;
418: c.weighty = 1;
419: p.add(new SLabel("18"), c);
420: c.gridx = 3;
421: c.gridy = 3;
422: c.weightx = 1;
423: c.weighty = 1;
424: p.add(new SLabel("19"), c);
425: c.gridx = 4;
426: c.gridy = 3;
427: c.weightx = 0;
428: c.weighty = 1;
429: p.add(new SLabel("20"), c);
430: c.gridx = 0;
431: c.gridy = 4;
432: c.weightx = 0;
433: c.weighty = 0;
434: p.add(new SLabel("21"), c);
435: c.gridx = 1;
436: c.gridy = 4;
437: c.weightx = 1;
438: c.weighty = 0;
439: p.add(new SLabel("22"), c);
440: c.gridx = 2;
441: c.gridy = 4;
442: c.weightx = 2;
443: c.weighty = 0;
444: p.add(new SLabel("23"), c);
445: c.gridx = 3;
446: c.gridy = 4;
447: c.weightx = 1;
448: c.weighty = 0;
449: p.add(new SLabel("24"), c);
450: c.gridx = 4;
451: c.gridy = 4;
452: c.weightx = 0;
453: c.weighty = 0;
454: p.add(new SLabel("25"), c);
455: }
456:
457: private void addRandomAddngWithPreDefinedGridXandY() {
458: SGridBagLayout layout;
459: SPanel p;
460: GridBagConstraints c;
461: add(new SLabel(
462: "\nRandom adding with pre-defined gridx+gridy"));
463: layout = new SGridBagLayout();
464: layout.setBorder(1);
465: p = new SPanel(layout);
466: add(p);
467:
468: c = new GridBagConstraints();
469: c.gridx = 4;
470: c.gridy = 0;
471: p.add(new SLabel("1"), c);
472: c.gridx = 3;
473: c.gridy = 1;
474: p.add(new SLabel("2"), c);
475: c.gridx = 2;
476: c.gridy = 2;
477: p.add(new SLabel("3"), c);
478: c.gridx = 1;
479: c.gridy = 3;
480: p.add(new SLabel("4"), c);
481: c.gridx = 0;
482: c.gridy = 4;
483: p.add(new SLabel("5"), c);
484: }
485:
486: private void addPredefinedGridXAndXDemo() {
487: SGridBagLayout layout;
488: SPanel p;
489: GridBagConstraints c;
490: add(new SLabel("\nVertical adding using pre-defined gridx"));
491: layout = new SGridBagLayout();
492: layout.setBorder(1);
493: p = new SPanel(layout);
494: add(p);
495:
496: c = new GridBagConstraints();
497:
498: // 1st column
499: c.gridx = 0;
500: c.gridheight = GridBagConstraints.REMAINDER;
501: p.add(new SLabel("1"), c);
502:
503: // 2nd column
504: c.gridx = 1;
505: c.gridheight = 1;
506: p.add(new SLabel("2"), c);
507:
508: c.gridheight = GridBagConstraints.REMAINDER;
509: p.add(new SLabel("3"), c);
510: c.gridheight = 1;
511:
512: // 3rd column
513: c.gridx = 2;
514: c.gridheight = 1;
515: p.add(new SLabel("4"), c);
516: p.add(new SLabel("5"), c);
517:
518: c.gridheight = GridBagConstraints.REMAINDER;
519: p.add(new SLabel("6"), c);
520:
521: // 4th column
522: c.gridx = 3;
523: c.gridheight = 1;
524: p.add(new SLabel("7"), c);
525: p.add(new SLabel("8"), c);
526: p.add(new SLabel("9"), c);
527:
528: c.gridheight = GridBagConstraints.REMAINDER;
529: p.add(new SLabel("10"), c);
530: }
531:
532: private void addRemainderDemo() {
533: add(new SLabel("\nHorizontal adding using REMAINDER"));
534: SGridBagLayout layout = new SGridBagLayout();
535: layout.setBorder(1);
536: SPanel p = new SPanel(layout);
537: p.setPreferredSize(new SDimension(300, 100));
538: p.setBackground(Color.red);
539: add(p);
540:
541: GridBagConstraints c = new GridBagConstraints();
542: c.gridwidth = GridBagConstraints.REMAINDER;
543: p.add(new SLabel("1"), c);
544: c.gridwidth = 1;
545:
546: p.add(new SLabel("2"), c);
547: c.gridwidth = GridBagConstraints.REMAINDER;
548: p.add(new SLabel("3"), c);
549: c.gridwidth = 1;
550:
551: p.add(new SLabel("4"), c);
552: p.add(new SLabel("5"), c);
553: c.gridwidth = GridBagConstraints.REMAINDER;
554: p.add(new SLabel("6"), c);
555: c.gridwidth = 1;
556:
557: p.add(new SLabel("7"), c);
558: p.add(new SLabel("8"), c);
559: p.add(new SLabel("9"), c);
560: c.gridwidth = GridBagConstraints.REMAINDER;
561: p.add(new SLabel("10"), c);
562: }
563: }
564:
565: /** Returns a formatted component describing the current layout exampel. */
566: private static SComponent createDescriptionLabel(String description) {
567: SLabel multiLineLabel = new SLabel();
568: //multiLineLabel.setEditable(false);
569: multiLineLabel.setWordWrap(true);
570: multiLineLabel.setPreferredSize(SDimension.FULLWIDTH);
571: multiLineLabel.setText(description);
572: multiLineLabel.setBorder(new SLineBorder(Color.gray, 1));
573: multiLineLabel.setBackground(new Color(255, 255, 150));
574: return multiLineLabel;
575: }
576:
577: /**
578: * Creates a new panel with desired amount of dummy labels on it.
579: */
580: private static SPanel createPanel(SLayoutManager layout,
581: int amountOfDummyLabels) {
582: final SPanel panel = new SPanel(layout);
583: panel.setBackground(new Color(210, 210, 210));
584: for (int i = 0; i < amountOfDummyLabels; i++) {
585: panel.add(createDummyLabel(i));
586: }
587: return panel;
588: }
589:
590: /* Create a dummy label with a specific label, color, border and alignment depending on index. */
591: private static SLabel createDummyLabel(int i) {
592: final String[] texts = {
593: "[%] A short component (Top/Left)",
594: "[%] A longer, unbreakable label for wrapping demo (Default)",
595: "[%] Another short one (Right/Bottom)",
596: "[%] A 2-line\nlabel (Center/Center)" };
597: final SBorder greenLineBorder = new SLineBorder(2);
598: greenLineBorder.setColor(Color.red);
599:
600: final SLabel label = new SLabel(texts[i % 4].replace('%',
601: Integer.toString((i + 1)).charAt(0)));
602: label.setBorder(greenLineBorder);
603: label.setBackground(new Color(0xEE, 0xEE, 0xEE));
604:
605: if (i % texts.length == 0) {
606: label.setVerticalAlignment(TOP);
607: label.setHorizontalAlignment(LEFT);
608: } else if (i % texts.length == 1)
609: ;
610: else if (i % texts.length == 2) {
611: label.setHorizontalAlignment(RIGHT);
612: label.setVerticalAlignment(BOTTOM);
613: } else if (i % texts.length == 3) {
614: label.setHorizontalAlignment(CENTER);
615: label.setVerticalAlignment(CENTER);
616: }
617:
618: return label;
619: }
620:
621: class LayoutControls extends ComponentControls {
622: public LayoutControls() {
623: globalControls.setVisible(false);
624:
625: selectLayoutManager.addItemListener(new ItemListener() {
626: public void itemStateChanged(ItemEvent e) {
627: panel.remove(0);
628: panel.add(demoPanels[selectLayoutManager
629: .getSelectedIndex()]);
630: }
631: });
632: selectLayoutManager
633: .setHorizontalAlignment(SConstants.LEFT_ALIGN);
634:
635: addControl(new SLabel(" choose layout manager"));
636: addControl(selectLayoutManager);
637: }
638: }
639: }
|