KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > testapp > interactive > testscreen > GridTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.testapp.interactive.testscreen;
31
32 import nextapp.echo2.app.Alignment;
33 import nextapp.echo2.app.Border;
34 import nextapp.echo2.app.Button;
35 import nextapp.echo2.app.Color;
36 import nextapp.echo2.app.Extent;
37 import nextapp.echo2.app.Grid;
38 import nextapp.echo2.app.Insets;
39 import nextapp.echo2.app.Label;
40 import nextapp.echo2.app.Column;
41 import nextapp.echo2.app.SplitPane;
42 import nextapp.echo2.app.event.ActionEvent;
43 import nextapp.echo2.app.event.ActionListener;
44 import nextapp.echo2.app.layout.GridLayoutData;
45 import nextapp.echo2.testapp.interactive.ButtonColumn;
46 import nextapp.echo2.testapp.interactive.StyleUtil;
47 import nextapp.echo2.testapp.interactive.Styles;
48
49 /**
50  * Interactive test for <code>Grid</code> components.
51  */

52 public class GridTest extends SplitPane {
53
54     private int nextCellNumber = 0;
55     private Button selectedButton;
56     
57     private ActionListener cellButtonActionListener = new ActionListener() {
58         public void actionPerformed(ActionEvent e) {
59             Button button = (Button) e.getSource();
60             selectCellButton(button);
61         }
62     };
63     
64     public GridTest() {
65         super(SplitPane.ORIENTATION_HORIZONTAL, new Extent(250, Extent.PX));
66         setStyleName("DefaultResizable");
67         
68         Column groupContainerColumn = new Column();
69         groupContainerColumn.setCellSpacing(new Extent(5));
70         groupContainerColumn.setStyleName("TestControlsColumn");
71         add(groupContainerColumn);
72         
73         Column testColumn = new Column();
74         add(testColumn);
75
76         ButtonColumn controlsColumn;
77         
78         controlsColumn = new ButtonColumn();
79         controlsColumn.add(new Label("Insert/Delete Cells"));
80         groupContainerColumn.add(controlsColumn);
81         
82         final Grid grid = new Grid(4);
83         grid.setBorder(new Border(new Extent(1), Color.BLUE, Border.STYLE_SOLID));
84         while (nextCellNumber < 17) {
85             grid.add(createGridCellButton());
86         }
87         testColumn.add(grid);
88
89         controlsColumn.addButton("Clear Selection", new ActionListener() {
90             public void actionPerformed(ActionEvent e) {
91                 selectCellButton(null);
92             }
93         });
94
95         controlsColumn.addButton("Insert Cell Before Selected", new ActionListener() {
96             public void actionPerformed(ActionEvent e) {
97                 if (selectedButton != null) {
98                     grid.add(createGridCellButton(), grid.indexOf(selectedButton));
99                 }
100             }
101         });
102
103         controlsColumn.addButton("Append New Cell", new ActionListener() {
104             public void actionPerformed(ActionEvent e) {
105                 Button button = createGridCellButton();
106                 grid.add(button);
107                 selectCellButton(button);
108             }
109         });
110
111         controlsColumn.addButton("Append 10 New Cells", new ActionListener() {
112             public void actionPerformed(ActionEvent e) {
113                 Button button = null;
114                 for (int i = 0; i < 10; ++i) {
115                     button = createGridCellButton();
116                     grid.add(button);
117                 }
118                 selectCellButton(button);
119             }
120         });
121
122         controlsColumn.addButton("Delete Selected Cell", new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 if (selectedButton != null) {
125                     int index = grid.indexOf(selectedButton);
126                     grid.remove(selectedButton);
127                     if (grid.getComponentCount() != 0) {
128                         if (index < grid.getComponentCount()) {
129                             selectCellButton((Button) grid.getComponent(index));
130                         } else {
131                             selectCellButton((Button) grid.getComponent(grid.getComponentCount() - 1));
132                         }
133                     } else {
134                         selectCellButton(null);
135                     }
136                 }
137             }
138         });
139         
140         controlsColumn = new ButtonColumn();
141         controlsColumn.add(new Label("Configure Grid"));
142         groupContainerColumn.add(controlsColumn);
143         
144         controlsColumn.addButton("Swap Orientation", new ActionListener() {
145             public void actionPerformed(ActionEvent e) {
146                 grid.setOrientation(grid.getOrientation() == Grid.ORIENTATION_VERTICAL
147                         ? Grid.ORIENTATION_HORIZONTAL : Grid.ORIENTATION_VERTICAL);
148             }
149         });
150         
151         controlsColumn.addButton("[+] Size", new ActionListener() {
152             public void actionPerformed(ActionEvent e) {
153                 grid.setSize(grid.getSize() + 1);
154             }
155         });
156
157         controlsColumn.addButton("[-] Size", new ActionListener() {
158             public void actionPerformed(ActionEvent e) {
159                 if (grid.getSize() > 1) {
160                     grid.setSize(grid.getSize() - 1);
161                 }
162             }
163         });
164         controlsColumn.addButton("Change Foreground", new ActionListener() {
165             public void actionPerformed(ActionEvent e) {
166                 grid.setForeground(StyleUtil.randomColor());
167             }
168         });
169         controlsColumn.addButton("Change Background", new ActionListener() {
170             public void actionPerformed(ActionEvent e) {
171                 grid.setBackground(StyleUtil.randomColor());
172             }
173         });
174         controlsColumn.addButton("Change Border (All Attributes)", new ActionListener() {
175             public void actionPerformed(ActionEvent e) {
176                 grid.setBorder(StyleUtil.randomBorder());
177             }
178         });
179         controlsColumn.addButton("Change Border Color", new ActionListener() {
180             public void actionPerformed(ActionEvent e) {
181                 Border border = grid.getBorder();
182                 grid.setBorder(new Border(border.getSize(), StyleUtil.randomColor(), border.getStyle()));
183             }
184         });
185         controlsColumn.addButton("Change Border Size", new ActionListener() {
186             public void actionPerformed(ActionEvent e) {
187                 grid.setBorder(StyleUtil.nextBorderSize(grid.getBorder()));
188             }
189         });
190         controlsColumn.addButton("Change Border Style", new ActionListener() {
191             public void actionPerformed(ActionEvent e) {
192                 grid.setBorder(StyleUtil.nextBorderStyle(grid.getBorder()));
193             }
194         });
195         
196         controlsColumn.addButton("Set Insets 0px", new ActionListener() {
197             public void actionPerformed(ActionEvent e) {
198                 grid.setInsets(new Insets(0));
199             }
200         });
201         controlsColumn.addButton("Set Insets 2px", new ActionListener() {
202             public void actionPerformed(ActionEvent e) {
203                 grid.setInsets(new Insets(2));
204             }
205         });
206         controlsColumn.addButton("Set Insets 10/5px", new ActionListener() {
207             public void actionPerformed(ActionEvent e) {
208                 grid.setInsets(new Insets(10, 5));
209             }
210         });
211         controlsColumn.addButton("Set Insets 10/20/30/40px", new ActionListener() {
212             public void actionPerformed(ActionEvent e) {
213                 grid.setInsets(new Insets(10, 20, 30, 40));
214             }
215         });
216         controlsColumn.addButton("Set Width = null", new ActionListener() {
217             public void actionPerformed(ActionEvent e) {
218                 grid.setWidth(null);
219             }
220         });
221         controlsColumn.addButton("Set Width = 500px", new ActionListener() {
222             public void actionPerformed(ActionEvent e) {
223                 grid.setWidth(new Extent(500));
224             }
225         });
226         controlsColumn.addButton("Set Width = 100%", new ActionListener() {
227             public void actionPerformed(ActionEvent e) {
228                 grid.setWidth(new Extent(100, Extent.PERCENT));
229             }
230         });
231         
232         controlsColumn = new ButtonColumn();
233         controlsColumn.add(new Label("Configure Cell"));
234         groupContainerColumn.add(controlsColumn);
235         
236         controlsColumn.addButton("[+] Column Span", new ActionListener() {
237             public void actionPerformed(ActionEvent e) {
238                 if (selectedButton != null) {
239                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
240                     if (layoutData.getColumnSpan() < 1) {
241                         layoutData.setColumnSpan(1);
242                     } else {
243                         layoutData.setColumnSpan(layoutData.getColumnSpan() + 1);
244                     }
245                     selectedButton.setLayoutData(layoutData);
246                     retitle(selectedButton);
247                 }
248             }
249         });
250
251         controlsColumn.addButton("[-] Column Span", new ActionListener() {
252             public void actionPerformed(ActionEvent e) {
253                 if (selectedButton != null) {
254                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
255                     if (layoutData.getColumnSpan() > 1) {
256                         layoutData.setColumnSpan(layoutData.getColumnSpan() - 1);
257                     } else {
258                         layoutData.setColumnSpan(1);
259                     }
260                     selectedButton.setLayoutData(layoutData);
261                     retitle(selectedButton);
262                 }
263             }
264         });
265         
266         controlsColumn.addButton("[+] Row Span", new ActionListener() {
267             public void actionPerformed(ActionEvent e) {
268                 if (selectedButton != null) {
269                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
270                     if (layoutData.getRowSpan() < 1) {
271                         layoutData.setRowSpan(1);
272                     } else {
273                         layoutData.setRowSpan(layoutData.getRowSpan() + 1);
274                     }
275                     selectedButton.setLayoutData(layoutData);
276                     retitle(selectedButton);
277                 }
278             }
279         });
280
281         controlsColumn.addButton("[-] Row Span", new ActionListener() {
282             public void actionPerformed(ActionEvent e) {
283                 if (selectedButton != null) {
284                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
285                     if (layoutData.getRowSpan() > 1) {
286                         layoutData.setRowSpan(layoutData.getRowSpan() - 1);
287                     } else {
288                         layoutData.setRowSpan(1);
289                     }
290                     selectedButton.setLayoutData(layoutData);
291                     retitle(selectedButton);
292                 }
293             }
294         });
295
296         controlsColumn.addButton("Column Span: FILL", new ActionListener() {
297             public void actionPerformed(ActionEvent e) {
298                 if (selectedButton != null) {
299                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
300                     layoutData.setColumnSpan(GridLayoutData.SPAN_FILL);
301                     selectedButton.setLayoutData(layoutData);
302                     retitle(selectedButton);
303                 }
304             }
305         });
306
307         controlsColumn.addButton("Row Span: FILL", new ActionListener() {
308             public void actionPerformed(ActionEvent e) {
309                 if (selectedButton != null) {
310                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
311                     layoutData.setRowSpan(GridLayoutData.SPAN_FILL);
312                     selectedButton.setLayoutData(layoutData);
313                     retitle(selectedButton);
314                 }
315             }
316         });
317
318         controlsColumn.addButton("Set Insets 0px", new ActionListener() {
319             public void actionPerformed(ActionEvent e) {
320                 if (selectedButton != null) {
321                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
322                     layoutData.setInsets(new Insets(0));
323                     selectedButton.setLayoutData(layoutData);
324                 }
325             }
326         });
327         controlsColumn.addButton("Set Insets 2px", new ActionListener() {
328             public void actionPerformed(ActionEvent e) {
329                 if (selectedButton != null) {
330                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
331                     layoutData.setInsets(new Insets(2));
332                     selectedButton.setLayoutData(layoutData);
333                 }
334             }
335         });
336         controlsColumn.addButton("Set Insets 10/5px", new ActionListener() {
337             public void actionPerformed(ActionEvent e) {
338                 if (selectedButton != null) {
339                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
340                     layoutData.setInsets(new Insets(10, 5));
341                     selectedButton.setLayoutData(layoutData);
342                 }
343             }
344         });
345         controlsColumn.addButton("Set Insets 10/20/30/40px", new ActionListener() {
346             public void actionPerformed(ActionEvent e) {
347                 if (selectedButton != null) {
348                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
349                     layoutData.setInsets(new Insets(10, 20, 30, 40));
350                     selectedButton.setLayoutData(layoutData);
351                 }
352             }
353         });
354         controlsColumn.addButton("Set Alignment = Default", new ActionListener() {
355             public void actionPerformed(ActionEvent e) {
356                 if (selectedButton != null) {
357                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
358                     layoutData.setAlignment(null);
359                     selectedButton.setLayoutData(layoutData);
360                 }
361             }
362         });
363         controlsColumn.addButton("Set Alignment = Leading/Top", new ActionListener() {
364             public void actionPerformed(ActionEvent e) {
365                 if (selectedButton != null) {
366                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
367                     layoutData.setAlignment(new Alignment(Alignment.LEADING, Alignment.TOP));
368                     selectedButton.setLayoutData(layoutData);
369                 }
370             }
371         });
372         controlsColumn.addButton("Set Alignment = Trailing/Bottom", new ActionListener() {
373             public void actionPerformed(ActionEvent e) {
374                 if (selectedButton != null) {
375                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
376                     layoutData.setAlignment(new Alignment(Alignment.TRAILING, Alignment.BOTTOM));
377                     selectedButton.setLayoutData(layoutData);
378                 }
379             }
380         });
381         controlsColumn.addButton("Set Alignment = Left/Top", new ActionListener() {
382             public void actionPerformed(ActionEvent e) {
383                 if (selectedButton != null) {
384                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
385                     layoutData.setAlignment(new Alignment(Alignment.LEFT, Alignment.TOP));
386                     selectedButton.setLayoutData(layoutData);
387                 }
388             }
389         });
390         controlsColumn.addButton("Set Alignment = Right/Bottom", new ActionListener() {
391             public void actionPerformed(ActionEvent e) {
392                 if (selectedButton != null) {
393                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
394                     layoutData.setAlignment(new Alignment(Alignment.RIGHT, Alignment.BOTTOM));
395                     selectedButton.setLayoutData(layoutData);
396                 }
397             }
398         });
399         controlsColumn.addButton("Set Alignment = Center/Center", new ActionListener() {
400             public void actionPerformed(ActionEvent e) {
401                 if (selectedButton != null) {
402                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
403                     layoutData.setAlignment(new Alignment(Alignment.CENTER, Alignment.CENTER));
404                     selectedButton.setLayoutData(layoutData);
405                 }
406             }
407         });
408         controlsColumn.addButton("Set BackgroundImage", new ActionListener() {
409             public void actionPerformed(ActionEvent e) {
410                 if (selectedButton != null) {
411                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
412                     layoutData.setBackgroundImage(Styles.BG_SHADOW_DARK_BLUE);
413                     selectedButton.setLayoutData(layoutData);
414                 }
415             }
416         });
417         controlsColumn.addButton("Clear BackgroundImage", new ActionListener() {
418             public void actionPerformed(ActionEvent e) {
419                 if (selectedButton != null) {
420                     GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
421                     layoutData.setBackgroundImage(null);
422                     selectedButton.setLayoutData(layoutData);
423                 }
424             }
425         });
426
427         controlsColumn = new ButtonColumn();
428         controlsColumn.add(new Label("Configure Rows/Columns"));
429         groupContainerColumn.add(controlsColumn);
430         
431         controlsColumn.addButton("Clear Widths of First 16 Columns", new ActionListener() {
432             public void actionPerformed(ActionEvent e) {
433                 for (int i = 0; i < 16; ++i) {
434                     grid.setColumnWidth(i, null);
435                 }
436             }
437         });
438         
439         controlsColumn.addButton("Set First 16 Columns to 100px Width", new ActionListener() {
440             public void actionPerformed(ActionEvent e) {
441                 Extent width = new Extent(100);
442                 for (int i = 0; i < 16; ++i) {
443                     grid.setColumnWidth(i, width);
444                 }
445             }
446         });
447         
448         controlsColumn.addButton("Set First 16 Columns to Random Width", new ActionListener() {
449             public void actionPerformed(ActionEvent e) {
450                 for (int i = 0; i < 16; ++i) {
451                     grid.setColumnWidth(i, new Extent( ((int) (Math.random() * 100)) + 50));
452                 }
453             }
454         });
455
456         controlsColumn.addButton("Clear Heights of First 16 Rows", new ActionListener() {
457             public void actionPerformed(ActionEvent e) {
458                 for (int i = 0; i < 16; ++i) {
459                     grid.setRowHeight(i, null);
460                 }
461             }
462         });
463         
464         controlsColumn.addButton("Set First 16 Rows to 100px Height", new ActionListener() {
465             public void actionPerformed(ActionEvent e) {
466                 Extent height = new Extent(100);
467                 for (int i = 0; i < 16; ++i) {
468                     grid.setRowHeight(i, height);
469                 }
470             }
471         });
472
473         controlsColumn.addButton("Set First 16 Rows to Random Height", new ActionListener() {
474             public void actionPerformed(ActionEvent e) {
475                 for (int i = 0; i < 16; ++i) {
476                     grid.setRowHeight(i, new Extent( ((int) (Math.random() * 100)) + 50));
477                 }
478             }
479         });
480     }
481
482     public Button createGridCellButton() {
483         Button button = new Button("Grid Cell #" + nextCellNumber++);
484         GridLayoutData layoutData = new GridLayoutData();
485         button.setLayoutData(layoutData);
486         button.addActionListener(cellButtonActionListener);
487         return button;
488     }
489     
490     private void retitle(Button button) {
491         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
492         GridLayoutData layoutData = (GridLayoutData) selectedButton.getLayoutData();
493         if (layoutData.getColumnSpan() != 1 || layoutData.getRowSpan() != 1) {
494             out.append("[" + (
495                     layoutData.getColumnSpan() == GridLayoutData.SPAN_FILL
496                     ? "F" : Integer.toString(layoutData.getColumnSpan())) + "x" +
497                     (layoutData.getRowSpan() == GridLayoutData.SPAN_FILL
498                     ? "F" : Integer.toString(layoutData.getRowSpan())) + "]");
499         }
500         String JavaDoc text = button.getText();
501         if (text.indexOf(":") == -1) {
502             if (out.length() == 0) {
503                 return;
504             }
505             text = text + " : " + out;
506         } else {
507             if (out.length() == 0) {
508                 text = text.substring(0, text.indexOf(":"));
509             } else {
510                 text = text.substring(0, text.indexOf(":") + 2) + out;
511             }
512         }
513         button.setText(text);
514     }
515     
516     private void selectCellButton(Button button) {
517         GridLayoutData layoutData;
518         if (selectedButton != null) {
519             layoutData = (GridLayoutData) selectedButton.getLayoutData();
520             layoutData.setBackground(null);
521             selectedButton.setLayoutData(layoutData);
522         }
523         if (button != null) {
524             layoutData = (GridLayoutData) button.getLayoutData();
525             layoutData.setBackground(new Color(0xefefaf));
526             button.setLayoutData(layoutData);
527         }
528         selectedButton = button;
529     }
530 }
531
Popular Tags