KickJava   Java API By Example, From Geeks To Geeks.

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


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 import nextapp.echo2.app.Button;
32 import nextapp.echo2.app.CheckBox;
33 import nextapp.echo2.app.Column;
34 import nextapp.echo2.app.Component;
35 import nextapp.echo2.app.ContentPane;
36 import nextapp.echo2.app.Extent;
37 import nextapp.echo2.app.Grid;
38 import nextapp.echo2.app.IllegalChildException;
39 import nextapp.echo2.app.Label;
40 import nextapp.echo2.app.ListBox;
41 import nextapp.echo2.app.PasswordField;
42 import nextapp.echo2.app.RadioButton;
43 import nextapp.echo2.app.Row;
44 import nextapp.echo2.app.SelectField;
45 import nextapp.echo2.app.SplitPane;
46 import nextapp.echo2.app.TextArea;
47 import nextapp.echo2.app.TextField;
48 import nextapp.echo2.app.WindowPane;
49 import nextapp.echo2.app.event.ActionEvent;
50 import nextapp.echo2.app.event.ActionListener;
51 import nextapp.echo2.app.list.DefaultListModel;
52 import nextapp.echo2.app.list.ListModel;
53 import nextapp.echo2.testapp.interactive.InteractiveApp;
54
55 /**
56  * A test to ensure proper rendering when arbitrary components are
57  * user-selected together in parent child relationships.
58  */

59 public class HierarchyTest extends SplitPane {
60     
61     private abstract class ComponentEntry {
62         
63         public abstract String JavaDoc getName();
64         
65         public abstract Component newInstance();
66         
67         public String JavaDoc toString() {
68             return getName();
69         }
70     }
71     
72     private ComponentEntry buttonEntry = new ComponentEntry(){
73         public String JavaDoc getName() {
74             return "Button";
75         }
76
77         public Component newInstance() {
78             Button button = new Button("Button");
79             button.setStyleName("Default");
80             return button;
81         }
82     };
83
84     private ComponentEntry checkBoxEntry = new ComponentEntry(){
85         public String JavaDoc getName() {
86             return "CheckBox";
87         }
88
89         public Component newInstance() {
90             CheckBox checkBox = new CheckBox("CheckBox");
91             checkBox.setStyleName("Default");
92             return checkBox;
93         }
94     };
95
96     private ComponentEntry columnEntry = new ComponentEntry(){
97         public String JavaDoc getName() {
98             return "Column";
99         }
100
101         public Component newInstance() {
102             Column column = new Column();
103             return column;
104         }
105     };
106     
107     private ComponentEntry contentPaneEntry = new ComponentEntry(){
108         public String JavaDoc getName() {
109             return "ContentPane";
110         }
111
112         public Component newInstance() {
113             ContentPane contentPane = new ContentPane();
114             return contentPane;
115         }
116     };
117     
118     private ComponentEntry gridEntry = new ComponentEntry(){
119         public String JavaDoc getName() {
120             return "Grid";
121         }
122
123         public Component newInstance() {
124             Grid grid = new Grid();
125             return grid;
126         }
127     };
128     
129     private ComponentEntry labelEntry = new ComponentEntry(){
130         public String JavaDoc getName() {
131             return "Label";
132         }
133
134         public Component newInstance() {
135             return new Label("Label");
136         }
137     };
138
139     private ComponentEntry listBoxEntry = new ComponentEntry(){
140         public String JavaDoc getName() {
141             return "ListBox";
142         }
143
144         public Component newInstance() {
145             ListBox listBox = new ListBox(new String JavaDoc[]{"alpha", "bravo", "charlie", "delta"});
146             listBox.setStyleName("Default");
147             return listBox;
148         }
149     };
150
151     private ComponentEntry passwordFieldEntry = new ComponentEntry(){
152         public String JavaDoc getName() {
153             return "PasswordField";
154         }
155
156         public Component newInstance() {
157             PasswordField passwordField = new PasswordField();
158             passwordField.setStyleName("Default");
159             passwordField.setText("Password");
160             return passwordField;
161         }
162     };
163     
164     private ComponentEntry radioButtonEntry = new ComponentEntry(){
165         public String JavaDoc getName() {
166             return "RadioButton";
167         }
168
169         public Component newInstance() {
170             RadioButton radioButton = new RadioButton("RadioButton");
171             radioButton.setStyleName("Default");
172             return radioButton;
173         }
174     };
175     
176     private ComponentEntry rowEntry = new ComponentEntry(){
177         public String JavaDoc getName() {
178             return "Row";
179         }
180
181         public Component newInstance() {
182             Row row = new Row();
183             return row;
184         }
185     };
186
187     private ComponentEntry selectFieldEntry = new ComponentEntry(){
188         public String JavaDoc getName() {
189             return "SelectField";
190         }
191
192         public Component newInstance() {
193             SelectField selectField = new SelectField(new String JavaDoc[]{"alpha", "bravo", "charlie", "delta"});
194             selectField.setStyleName("Default");
195             return selectField;
196         }
197     };
198
199     private ComponentEntry splitPaneEntry = new ComponentEntry(){
200         public String JavaDoc getName() {
201             return "SplitPane";
202         }
203
204         public Component newInstance() {
205             SplitPane splitPane = new SplitPane();
206             splitPane.setStyleName("DefaultResizable");
207             return splitPane;
208         }
209     };
210     
211     private ComponentEntry textAreaEntry = new ComponentEntry(){
212         public String JavaDoc getName() {
213             return "TextArea";
214         }
215
216         public Component newInstance() {
217             TextArea textArea = new TextArea();
218             textArea.setStyleName("Default");
219             textArea.setText("TextArea");
220             return textArea;
221         }
222     };
223     
224     private ComponentEntry textFieldEntry = new ComponentEntry(){
225         public String JavaDoc getName() {
226             return "TextField";
227         }
228
229         public Component newInstance() {
230             TextField textField = new TextField();
231             textField.setStyleName("Default");
232             textField.setText("TextField");
233             return textField;
234         }
235     };
236     
237     private ComponentEntry windowPaneEntry = new ComponentEntry(){
238         public String JavaDoc getName() {
239             return "WindowPane";
240         }
241
242         public Component newInstance() {
243             WindowPane windowPane = new WindowPane();
244             windowPane.setStyleName("Default");
245             return windowPane;
246         }
247     };
248     
249     private ListModel componentListModel = new DefaultListModel(new Object JavaDoc[]{
250             "(None)",
251             buttonEntry,
252             checkBoxEntry,
253             columnEntry,
254             contentPaneEntry,
255             gridEntry,
256             labelEntry,
257             listBoxEntry,
258             passwordFieldEntry,
259             radioButtonEntry,
260             rowEntry,
261             selectFieldEntry,
262             splitPaneEntry,
263             textAreaEntry,
264             textFieldEntry,
265             windowPaneEntry
266     });
267                 
268     private Component parentComponentInstance;
269     private Component[] childComponentInstances;
270     private SelectField parentSelectField;
271     private SelectField[] childSelectFields;
272     private ContentPane testPane;
273
274     public HierarchyTest() {
275         super(SplitPane.ORIENTATION_HORIZONTAL, new Extent(250, Extent.PX));
276         setStyleName("DefaultResizable");
277         
278         Column controlGroupsColumn = new Column();
279         controlGroupsColumn.setCellSpacing(new Extent(5));
280         controlGroupsColumn.setStyleName("TestControlsColumn");
281         add(controlGroupsColumn);
282         
283         Column parentSelectColumn = new Column();
284         controlGroupsColumn.add(parentSelectColumn);
285         
286         parentSelectColumn.add(new Label("Parent"));
287                         
288         parentSelectField = new SelectField(componentListModel);
289         parentSelectField.addActionListener(new ActionListener(){
290             public void actionPerformed(ActionEvent e) {
291                 if (parentSelectField.getSelectedItem() instanceof ComponentEntry) {
292                     parentComponentInstance = ((ComponentEntry) parentSelectField.getSelectedItem()).newInstance();
293                 } else {
294                     parentComponentInstance = null;
295                 }
296                 update();
297             }
298         });
299         parentSelectColumn.add(parentSelectField);
300         
301         childSelectFields = new SelectField[4];
302         childComponentInstances = new Component[4];
303         for (int i = 0; i < childSelectFields.length; ++i) {
304             Column childSelectColumn = new Column();
305             controlGroupsColumn.add(childSelectColumn);
306             
307             childSelectColumn.add(new Label("Child #" + i));
308                             
309             final int childNumber = i;
310             childSelectFields[i] = new SelectField(componentListModel);
311             childSelectFields[i].addActionListener(new ActionListener(){
312                 public void actionPerformed(ActionEvent e) {
313                     if (childSelectFields[childNumber].getSelectedItem() instanceof ComponentEntry) {
314                         childComponentInstances[childNumber]
315                                 = ((ComponentEntry) childSelectFields[childNumber].getSelectedItem()).newInstance();
316                     } else {
317                         childComponentInstances[childNumber] = null;
318                     }
319                     update();
320                 }
321             });
322             childSelectColumn.add(childSelectFields[i]);
323         }
324         
325         testPane = new ContentPane();
326         add(testPane);
327     }
328     
329     public void update() {
330         testPane.removeAll();
331         if (parentComponentInstance == null) {
332             return;
333         }
334         parentComponentInstance.removeAll();
335         for (int i = 0; i < childComponentInstances.length; ++i) {
336             if (childComponentInstances[i] != null) {
337                 try {
338                     parentComponentInstance.add(childComponentInstances[i]);
339                 } catch (IllegalChildException ex) {
340                     InteractiveApp.getApp().consoleWrite(ex.toString());
341                 }
342             }
343         }
344         testPane.add(parentComponentInstance);
345     }
346 }
347
Popular Tags