KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > DynamicLayoutExample


1 /*
2  * $Id: DynamicLayoutExample.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.*;
17 import org.wings.border.SBorder;
18 import org.wings.border.SLineBorder;
19 import org.wings.script.JavaScriptListener;
20
21 import java.awt.*;
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24
25 /**
26  * A quickhack example to show the capabilities of the dynamic wings layout managers.
27  *
28  * @author bschmid
29  */

30 public class DynamicLayoutExample extends WingSetPane {
31     private final SForm panel = new SForm();
32     private final SPanel[] demoPanels = {new BorderLayoutDemoPanel(), new FlowLayoutDemoPanel(), new GridBagDemoPanel(), new GridLayoutDemoPanel(), new BoxLayoutDemoPanel()};
33     private final static String JavaDoc[] demoManagerNames = {"SBorderLayout", "SFlowLayout", "SGridBagLayout", "SGridLayout", "SBoxLayout"};
34     private final SComboBox selectLayoutManager = new SComboBox(demoManagerNames);
35
36     protected SComponent createExample() {
37         selectLayoutManager.addItemListener(new ItemListener JavaDoc() {
38             public void itemStateChanged(ItemEvent JavaDoc e) {
39                 panel.remove(1);
40                 panel.add(demoPanels[selectLayoutManager.getSelectedIndex()]);
41             }
42         });
43         selectLayoutManager.addScriptListener(new JavaScriptListener("onChange", "this.form.submit()"));
44
45         panel.add(selectLayoutManager);
46         panel.add(demoPanels[0]);
47
48         return panel;
49     }
50
51     private static void addDummyLabels(final SPanel panel, final int amount) {
52         for (int i = 0; i < amount; i++) {
53             panel.add(createDummyLabel(i));
54         }
55     }
56
57     private static SLabel createDummyLabel(int i) {
58         final String JavaDoc[] texts = {"<html><nobr>[%] A very short component (TopLeft)</nobr>",
59                                 "<html><nobr>[%] A much longer, unbreakable label for wrapping demo (Default)</nobr>",
60                                 "<html><nobr>[%] And again a short one (RightBottom, red)</nobr>",
61                                 "<html><nobr>[%] A 2-line</nobr><br/><nobr>label (CenterCenter, bold-italic)</nobr>"};
62         final SFont boldItalic = new SFont(null, SFont.BOLD + SFont.ITALIC, SFont.DEFAULT_SIZE);
63         final SBorder greenLineBorder = new SLineBorder();
64         final SLabel label = new SLabel(texts[i % 4].replace('%', Integer.toString((i + 1)).charAt(0)));
65         greenLineBorder.setColor(Color.green);
66         label.setBorder(greenLineBorder);
67         if (i % texts.length == 0) {
68             label.setVerticalAlignment(TOP);
69             label.setHorizontalAlignment(LEFT);
70         }
71         if (i % texts.length == 1) ;
72         if (i % texts.length == 2) {
73             label.setForeground(Color.RED);
74             label.setHorizontalAlignment(RIGHT);
75             label.setVerticalAlignment(BOTTOM);
76         }
77         if (i % texts.length == 3) {
78             label.setHorizontalAlignment(CENTER);
79             label.setVerticalAlignment(CENTER);
80             label.setFont(boldItalic);
81         }
82         return label;
83     }
84
85     private static SPanel createPanel(SLayoutManager layout, int amount) {
86         final SPanel panel = new SPanel(layout);
87         panel.setBackground(new Color(210, 210, 210));
88         addDummyLabels(panel, amount);
89         return panel;
90     }
91
92     private static class BorderLayoutDemoPanel extends SPanel {
93         int i = 0;
94         public BorderLayoutDemoPanel() {
95             super(new SFlowDownLayout());
96             add(new SLabel("Default"));
97             SPanel borderDemoPanel1 = new SPanel(new SBorderLayout());
98             borderDemoPanel1.add(wrap(createDummyLabel(0)), SBorderLayout.NORTH);
99             borderDemoPanel1.add(wrap(createDummyLabel(1)), SBorderLayout.SOUTH);
100             borderDemoPanel1.add(wrap(createDummyLabel(2)), SBorderLayout.EAST);
101             borderDemoPanel1.add(wrap(createDummyLabel(3)), SBorderLayout.WEST);
102             borderDemoPanel1.add(wrap(createDummyLabel(4)), SBorderLayout.CENTER);
103             add(borderDemoPanel1);
104             borderDemoPanel1.setPreferredSize(new SDimension(800,200));
105             borderDemoPanel1.setBackground(new Color(210, 210, 210));
106
107             add(new SLabel("Spacing and Border"));
108             SPanel borderDemoPanel2 = new SPanel(new SBorderLayout(10, 1));
109             borderDemoPanel2.add(wrap(createDummyLabel(0+3)), SBorderLayout.NORTH);
110             borderDemoPanel2.add(wrap(createDummyLabel(1+3)), SBorderLayout.SOUTH);
111             borderDemoPanel2.add(wrap(createDummyLabel(2+3)), SBorderLayout.EAST);
112             borderDemoPanel2.add(wrap(createDummyLabel(3+3)), SBorderLayout.WEST);
113             borderDemoPanel2.add(wrap(createDummyLabel(4+3)), SBorderLayout.CENTER);
114             add(borderDemoPanel2);
115             borderDemoPanel2.setPreferredSize(new SDimension(800,200));
116             borderDemoPanel2.setBackground(new Color(210, 210, 210));
117         }
118
119         private SPanel wrap(SComponent c) {
120             final Color[] colors = { Color.red, Color.green, Color.pink, Color.magenta, Color.gray, Color.cyan };
121             SPanel p = new SPanel();
122             p.add(c);
123             p.setPreferredSize(new SDimension("100%","100%"));
124             p.setVerticalAlignment(c.getVerticalAlignment());
125             p.setHorizontalAlignment(c.getHorizontalAlignment());
126             p.setBackground(colors[i++ % colors.length]);
127             return p;
128         }
129     }
130
131     private static class BoxLayoutDemoPanel extends SPanel {
132         public BoxLayoutDemoPanel() {
133             add(new SLabel("Horizontal box layout with padding & border"));
134             SBoxLayout horizontalLayout = new SBoxLayout(SBoxLayout.HORIZONTAL);
135             horizontalLayout.setHgap(10);
136             horizontalLayout.setVgap(10);
137             horizontalLayout.setBorder(1);
138             add(createPanel(horizontalLayout, 5));
139
140             add(new SLabel("Vertical vanilla box layout"));
141             SBoxLayout verticalLayout = new SBoxLayout(SBoxLayout.VERTICAL);
142             add(createPanel(verticalLayout, 5));
143         }
144     }
145
146     private static class FlowLayoutDemoPanel extends SPanel {
147         public FlowLayoutDemoPanel() {
148             super(new SFlowDownLayout());
149             add(new SLabel("Horizontal, centered FlowLayout"));
150             add(createPanel(new SFlowLayout(SFlowLayout.CENTER), 4));
151             add(new SLabel("Horizontal, left-aligned FlowLayout"));
152             add(createPanel(new SFlowLayout(SFlowLayout.LEFT), 4));
153             add(new SLabel("Horizontal, right-aligned FlowLayout"));
154             add(createPanel(new SFlowLayout(SFlowLayout.RIGHT), 4));
155             add(new SLabel("<html>&nbsp;"));
156             add(new SLabel("Vertical, centered FlowLayout"));
157             add(createPanel(new SFlowDownLayout(SConstants.CENTER), 4));
158             add(new SLabel("Vertical, left-aligned FlowLayout"));
159             add(createPanel(new SFlowDownLayout(), 3));
160             add(new SLabel("Vertical, right-aligned FlowLayout"));
161             add(createPanel(new SFlowDownLayout(SConstants.RIGHT), 4));
162         }
163     }
164
165     private static class GridLayoutDemoPanel extends SPanel {
166         public GridLayoutDemoPanel() {
167             add(new SLabel("Grid Layout panel with 3 colums, border, 10px horizontal gap, 80 vertical gap"));
168             SGridLayout layout1 = new SGridLayout(3);
169             layout1.setBorder(1);
170             layout1.setHgap(10);
171             layout1.setVgap(80);
172
173             add(createPanel(layout1, 12));
174         }
175     }
176
177     private static class GridBagDemoPanel extends SPanel {
178         public GridBagDemoPanel() {
179             SFlowDownLayout flowLayout = new SFlowDownLayout();
180             setLayout(flowLayout);
181             setHorizontalAlignment(CENTER);
182
183             add(new SLabel("Horizontal adding using REMAINDER"));
184             SGridBagLayout layout = new SGridBagLayout();
185             layout.setBorder(1);
186             SPanel p = new SPanel(layout);
187             p.setPreferredSize(new SDimension(300, 100));
188             p.setBackground(Color.red);
189             add(p);
190
191             GridBagConstraints c = new GridBagConstraints();
192             c.gridwidth = GridBagConstraints.REMAINDER;
193             p.add(new SLabel("1"), c);
194             c.gridwidth = 1;
195
196             p.add(new SLabel("2"), c);
197             c.gridwidth = GridBagConstraints.REMAINDER;
198             p.add(new SLabel("3"), c);
199             c.gridwidth = 1;
200
201             p.add(new SLabel("4"), c);
202             p.add(new SLabel("5"), c);
203             c.gridwidth = GridBagConstraints.REMAINDER;
204             p.add(new SLabel("6"), c);
205             c.gridwidth = 1;
206
207             p.add(new SLabel("7"), c);
208             p.add(new SLabel("8"), c);
209             p.add(new SLabel("9"), c);
210             c.gridwidth = GridBagConstraints.REMAINDER;
211             p.add(new SLabel("10"), c);
212
213
214             add(new SLabel("Vertical adding using pre-defined gridx"));
215             layout = new SGridBagLayout();
216             layout.setBorder(1);
217             p = new SPanel(layout);
218             add(p);
219
220             c = new GridBagConstraints();
221             c.gridx = 0;
222             c.gridheight = GridBagConstraints.REMAINDER;
223             p.add(new SLabel("1"), c);
224             c.gridheight = 1;
225
226             c.gridx = 1;
227             p.add(new SLabel("2"), c);
228             c.gridheight = GridBagConstraints.REMAINDER;
229             p.add(new SLabel("3"), c);
230             c.gridheight = 1;
231
232             c.gridx = 2;
233             p.add(new SLabel("4"), c);
234             p.add(new SLabel("5"), c);
235             c.gridheight = GridBagConstraints.REMAINDER;
236             p.add(new SLabel("6"), c);
237             c.gridheight = 1;
238
239             c.gridx = 3;
240             p.add(new SLabel("7"), c);
241             p.add(new SLabel("8"), c);
242             p.add(new SLabel("9"), c);
243             c.gridheight = GridBagConstraints.REMAINDER;
244             p.add(new SLabel("10"), c);
245
246             add(new SLabel("Random adding with pre-defined gridx+gridy"));
247             layout = new SGridBagLayout();
248             layout.setBorder(1);
249             p = new SPanel(layout);
250             add(p);
251
252             c = new GridBagConstraints();
253             c.gridx = 4;
254             c.gridy = 0;
255             p.add(new SLabel("1"), c);
256             c.gridx = 3;
257             c.gridy = 1;
258             p.add(new SLabel("2"), c);
259             c.gridx = 2;
260             c.gridy = 2;
261             p.add(new SLabel("3"), c);
262             c.gridx = 1;
263             c.gridy = 3;
264             p.add(new SLabel("4"), c);
265             c.gridx = 0;
266             c.gridy = 4;
267             p.add(new SLabel("5"), c);
268
269
270             add(new SLabel("Using weight"));
271             layout = new SGridBagLayout();
272             layout.setBorder(1);
273             p = new SPanel(layout);
274             add(p);
275             p.setPreferredSize(new SDimension(500, 500));
276
277             c = new GridBagConstraints();
278             c.gridx = 0;
279             c.gridy = 0;
280             c.weightx = 0;
281             c.weighty = 0;
282             p.add(new SLabel("1"), c);
283             c.gridx = 1;
284             c.gridy = 0;
285             c.weightx = 1;
286             c.weighty = 0;
287             p.add(new SLabel("2"), c);
288             c.gridx = 2;
289             c.gridy = 0;
290             c.weightx = 2;
291             c.weighty = 0;
292             p.add(new SLabel("3"), c);
293             c.gridx = 3;
294             c.gridy = 0;
295             c.weightx = 1;
296             c.weighty = 0;
297             p.add(new SLabel("4"), c);
298             c.gridx = 4;
299             c.gridy = 0;
300             c.weightx = 0;
301             c.weighty = 0;
302             p.add(new SLabel("5"), c);
303
304             c.gridx = 0;
305             c.gridy = 1;
306             c.weightx = 0;
307             c.weighty = 1;
308             p.add(new SLabel("6"), c);
309             c.gridx = 1;
310             c.gridy = 1;
311             c.weightx = 1;
312             c.weighty = 1;
313             p.add(new SLabel("7"), c);
314             c.gridx = 2;
315             c.gridy = 1;
316             c.weightx = 2;
317             c.weighty = 1;
318             p.add(new SLabel("8"), c);
319             c.gridx = 3;
320             c.gridy = 1;
321             c.weightx = 1;
322             c.weighty = 1;
323             p.add(new SLabel("9"), c);
324             c.gridx = 4;
325             c.gridy = 1;
326             c.weightx = 0;
327             c.weighty = 1;
328             p.add(new SLabel("10"), c);
329
330             c.gridx = 0;
331             c.gridy = 2;
332             c.weightx = 0;
333             c.weighty = 2;
334             p.add(new SLabel("11"), c);
335             c.gridx = 1;
336             c.gridy = 2;
337             c.weightx = 1;
338             c.weighty = 2;
339             p.add(new SLabel("12"), c);
340             c.gridx = 2;
341             c.gridy = 2;
342             c.weightx = 2;
343             c.weighty = 2;
344             p.add(new SLabel("13"), c);
345             c.gridx = 3;
346             c.gridy = 2;
347             c.weightx = 1;
348             c.weighty = 2;
349             p.add(new SLabel("14"), c);
350             c.gridx = 4;
351             c.gridy = 2;
352             c.weightx = 0;
353             c.weighty = 2;
354             p.add(new SLabel("15"), c);
355             c.gridx = 0;
356             c.gridy = 3;
357             c.weightx = 0;
358             c.weighty = 1;
359             p.add(new SLabel("16"), c);
360             c.gridx = 1;
361             c.gridy = 3;
362             c.weightx = 1;
363             c.weighty = 1;
364             p.add(new SLabel("17"), c);
365             c.gridx = 2;
366             c.gridy = 3;
367             c.weightx = 2;
368             c.weighty = 1;
369             p.add(new SLabel("18"), c);
370             c.gridx = 3;
371             c.gridy = 3;
372             c.weightx = 1;
373             c.weighty = 1;
374             p.add(new SLabel("19"), c);
375             c.gridx = 4;
376             c.gridy = 3;
377             c.weightx = 0;
378             c.weighty = 1;
379             p.add(new SLabel("20"), c);
380             c.gridx = 0;
381             c.gridy = 4;
382             c.weightx = 0;
383             c.weighty = 0;
384             p.add(new SLabel("21"), c);
385             c.gridx = 1;
386             c.gridy = 4;
387             c.weightx = 1;
388             c.weighty = 0;
389             p.add(new SLabel("22"), c);
390             c.gridx = 2;
391             c.gridy = 4;
392             c.weightx = 2;
393             c.weighty = 0;
394             p.add(new SLabel("23"), c);
395             c.gridx = 3;
396             c.gridy = 4;
397             c.weightx = 1;
398             c.weighty = 0;
399             p.add(new SLabel("24"), c);
400             c.gridx = 4;
401             c.gridy = 4;
402             c.weightx = 0;
403             c.weighty = 0;
404             p.add(new SLabel("25"), c);
405
406
407             add(new SLabel("Adding with gridwidth=RELATIVE"));
408             layout = new SGridBagLayout();
409             layout.setBorder(1);
410             p = new SPanel(layout);
411             add(p);
412             c = new GridBagConstraints();
413             p.add(new SLabel("1"), c);
414             p.add(new SLabel("2"), c);
415             p.add(new SLabel("3"), c);
416             p.add(new SLabel("4"), c);
417             c.gridwidth = GridBagConstraints.RELATIVE;
418             p.add(new SLabel("5"), c);
419             c.gridwidth = 1;
420             p.add(new SLabel("end #1"), c);
421
422             p.add(new SLabel("6"), c);
423             p.add(new SLabel("7"), c);
424             p.add(new SLabel("8"), c);
425             c.gridwidth = GridBagConstraints.RELATIVE;
426             p.add(new SLabel("9"), c);
427             c.gridwidth = 1;
428             p.add(new SLabel("end #2"), c);
429
430             p.add(new SLabel("10"), c);
431             p.add(new SLabel("11"), c);
432             c.gridwidth = GridBagConstraints.RELATIVE;
433             p.add(new SLabel("12"), c);
434             c.gridwidth = 1;
435             p.add(new SLabel("end #3"), c);
436
437             p.add(new SLabel("13"), c);
438             c.gridwidth = GridBagConstraints.RELATIVE;
439             p.add(new SLabel("14"), c);
440             c.gridwidth = 1;
441             p.add(new SLabel("end #4"), c);
442
443             c.gridwidth = GridBagConstraints.RELATIVE;
444             p.add(new SLabel("15"), c);
445             c.gridwidth = 1;
446             p.add(new SLabel("end #5"), c);
447
448
449             add(new SLabel("Vertical adding with gridheight=RELATIVE"));
450             layout = new SGridBagLayout();
451             layout.setBorder(1);
452             p = new SPanel(layout);
453             add(p);
454             c = new GridBagConstraints();
455
456             c.gridx = 0;
457             p.add(new SLabel("1"), c);
458             p.add(new SLabel("2"), c);
459             p.add(new SLabel("3"), c);
460             p.add(new SLabel("4"), c);
461             c.gridheight = GridBagConstraints.RELATIVE;
462             p.add(new SLabel("5"), c);
463             c.gridheight = 1;
464             p.add(new SLabel("end #1"), c);
465
466             c.gridx = 1;
467             p.add(new SLabel("6"), c);
468             p.add(new SLabel("7"), c);
469             p.add(new SLabel("8"), c);
470             c.gridheight = GridBagConstraints.RELATIVE;
471             p.add(new SLabel("9"), c);
472             c.gridheight = 1;
473             p.add(new SLabel("end #2"), c);
474
475             c.gridx = 2;
476             p.add(new SLabel("10"), c);
477             p.add(new SLabel("11"), c);
478             c.gridheight = GridBagConstraints.RELATIVE;
479             p.add(new SLabel("12"), c);
480             c.gridheight = 1;
481             p.add(new SLabel("end #3"), c);
482
483             c.gridx = 3;
484             p.add(new SLabel("13"), c);
485             c.gridheight = GridBagConstraints.RELATIVE;
486             p.add(new SLabel("14"), c);
487             c.gridheight = 1;
488             p.add(new SLabel("end #4"), c);
489
490             c.gridx = 4;
491             c.gridheight = GridBagConstraints.RELATIVE;
492             p.add(new SLabel("15"), c);
493             c.gridheight = 1;
494             p.add(new SLabel("end #5"), c);
495         }
496
497     }
498
499
500 }
501
Popular Tags