KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.servlet.http.Cookie JavaDoc;
37
38 import nextapp.echo2.app.ApplicationInstance;
39 import nextapp.echo2.app.Button;
40 import nextapp.echo2.app.Column;
41 import nextapp.echo2.app.Component;
42 import nextapp.echo2.app.Extent;
43 import nextapp.echo2.app.Insets;
44 import nextapp.echo2.app.Label;
45 import nextapp.echo2.app.Table;
46 import nextapp.echo2.app.event.ActionEvent;
47 import nextapp.echo2.app.event.ActionListener;
48 import nextapp.echo2.app.layout.SplitPaneLayoutData;
49 import nextapp.echo2.app.table.DefaultTableModel;
50 import nextapp.echo2.app.table.TableCellRenderer;
51 import nextapp.echo2.webcontainer.ContainerContext;
52 import nextapp.echo2.webcontainer.command.BrowserSetCookieCommand;
53 import nextapp.echo2.webrender.ClientProperties;
54
55 /**
56  * A test which displays the contents of the <code>ClientProperties</code>
57  * object.
58  * <p>
59  * Note that this object has a dependency on the Web Application Container
60  * and Web Renderer.
61  */

62 public class ContainerContextTest extends Column {
63     
64     private class PropertyTableCellRenderer
65     implements TableCellRenderer {
66
67         /**
68          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
69          * java.lang.Object, int, int)
70          */

71         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
72             String JavaDoc labelValue;
73             if (value instanceof Object JavaDoc[]) {
74                 Object JavaDoc[] array = (Object JavaDoc[]) value;
75                 StringBuffer JavaDoc out = new StringBuffer JavaDoc();
76                 for (int i = 0; i < array.length; ++i) {
77                     out.append(array[i]);
78                     if (i < array.length - 1) {
79                         out.append(",");
80                     }
81                 }
82                 labelValue = out.toString();
83             } else {
84                 labelValue = value.toString();
85             }
86             
87             Label label = new Label(labelValue);
88             label.setStyleName(row % 2 == 0 ? "EvenCellLabel" : "OddCellLabel");
89             return label;
90         }
91     }
92
93     public ContainerContextTest() {
94         super();
95         
96         setCellSpacing(new Extent(10));
97         
98         SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
99         splitPaneLayoutData.setInsets(new Insets(10));
100         setLayoutData(splitPaneLayoutData);
101         
102         ApplicationInstance app = ApplicationInstance.getActive();
103         ContainerContext containerContext
104                 = (ContainerContext) app.getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
105         
106         Column clientPropertiesColumn = new Column();
107         add(clientPropertiesColumn);
108         clientPropertiesColumn.add(new Label("Client Properties"));
109         clientPropertiesColumn.add(createClientPropertiesTable(containerContext));
110         
111         Column initialParametersColumn = new Column();
112         add(initialParametersColumn);
113         initialParametersColumn.add(new Label("Initial Parameters"));
114         initialParametersColumn.add(createInitialParametersTable(containerContext));
115         
116         Column applicationPropertiesColumn = new Column();
117         add(applicationPropertiesColumn);
118         applicationPropertiesColumn.add(new Label("ApplicationInstance Properties"));
119         applicationPropertiesColumn.add(createApplicationPropertiesTable(app));
120         
121         Column cookiesColumn = new Column();
122         add(cookiesColumn);
123         cookiesColumn.add(new Label("Cookies"));
124         cookiesColumn.add(createCookieTable(containerContext));
125         Button setCookieButton = new Button("Set Cookie");
126         setCookieButton.setStyleName("Default");
127         setCookieButton.addActionListener(new ActionListener() {
128         
129             public void actionPerformed(ActionEvent e) {
130                 int value = (int) (Math.random() * 3);
131                 Cookie JavaDoc cookie = new Cookie JavaDoc("Test Cookie " + value, "Mmmmm Cookies " + value);
132                 BrowserSetCookieCommand command = new BrowserSetCookieCommand(cookie);
133                 ApplicationInstance.getActive().enqueueCommand(command);
134             }
135         });
136         cookiesColumn.add(setCookieButton);
137     }
138     
139     private Table createApplicationPropertiesTable(ApplicationInstance app) {
140         Table table = new Table();
141         table.setStyleName("Default");
142         table.setDefaultRenderer(Object JavaDoc.class, new PropertyTableCellRenderer());
143         
144         DefaultTableModel model = (DefaultTableModel) table.getModel();
145         model.setColumnCount(2);
146
147         table.getColumnModel().getColumn(0).setHeaderValue("Property");
148         table.getColumnModel().getColumn(1).setHeaderValue("Value");
149         
150         model.addRow(new Object JavaDoc[]{"Locale", app.getLocale()});
151         model.addRow(new Object JavaDoc[]{"Layout Direction", app.getLayoutDirection()});
152         
153         return table;
154     }
155
156     private Table createClientPropertiesTable(ContainerContext containerContext) {
157         ClientProperties clientProperties = containerContext.getClientProperties();
158         String JavaDoc[] propertyNames = clientProperties.getPropertyNames();
159         Arrays.sort(propertyNames);
160         
161         Table table = new Table();
162         table.setStyleName("Default");
163         table.setDefaultRenderer(Object JavaDoc.class, new PropertyTableCellRenderer());
164         
165         DefaultTableModel model = (DefaultTableModel) table.getModel();
166         model.setColumnCount(2);
167
168         table.getColumnModel().getColumn(0).setHeaderValue("Property");
169         table.getColumnModel().getColumn(1).setHeaderValue("Value");
170
171         for (int i = 0; i < propertyNames.length; ++i) {
172             Object JavaDoc propertyValue = clientProperties.get(propertyNames[i]);
173             model.addRow(new Object JavaDoc[]{propertyNames[i], propertyValue});
174         }
175         
176         return table;
177     }
178     
179     private Table createCookieTable(ContainerContext containerContext) {
180         Cookie JavaDoc[] cookies = containerContext.getCookies();
181         
182         Table table = new Table();
183         table.setStyleName("Default");
184         table.setDefaultRenderer(Object JavaDoc.class, new PropertyTableCellRenderer());
185         
186         DefaultTableModel model = (DefaultTableModel) table.getModel();
187         model.setColumnCount(3);
188
189         table.getColumnModel().getColumn(0).setHeaderValue("Name");
190         table.getColumnModel().getColumn(1).setHeaderValue("Max Age");
191         table.getColumnModel().getColumn(2).setHeaderValue("Value");
192
193         for (int i = 0; i < cookies.length; ++i) {
194             model.addRow(new Object JavaDoc[]{cookies[i].getName(), Integer.toString(cookies[i].getMaxAge()), cookies[i].getValue()});
195         }
196         
197         return table;
198     }
199     
200     private Table createInitialParametersTable(ContainerContext containerContext) {
201         Map JavaDoc initialParameterMap = containerContext.getInitialRequestParameterMap();
202         
203         Table table = new Table();
204         table.setStyleName("Default");
205         table.setDefaultRenderer(Object JavaDoc.class, new PropertyTableCellRenderer());
206         
207         DefaultTableModel model = (DefaultTableModel) table.getModel();
208         model.setColumnCount(2);
209
210         table.getColumnModel().getColumn(0).setHeaderValue("Property");
211         table.getColumnModel().getColumn(1).setHeaderValue("Value");
212         
213         Iterator JavaDoc it = initialParameterMap.keySet().iterator();
214         while (it.hasNext()) {
215             String JavaDoc key = (String JavaDoc) it.next();
216             model.addRow(new Object JavaDoc[]{key, initialParameterMap.get(key)});
217         }
218         
219         return table;
220     }
221 }
222
Popular Tags