001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive.testscreen;
031:
032: import java.util.Arrays;
033: import java.util.Iterator;
034: import java.util.Map;
035:
036: import javax.servlet.http.Cookie;
037:
038: import nextapp.echo2.app.ApplicationInstance;
039: import nextapp.echo2.app.Button;
040: import nextapp.echo2.app.Column;
041: import nextapp.echo2.app.Component;
042: import nextapp.echo2.app.Extent;
043: import nextapp.echo2.app.Insets;
044: import nextapp.echo2.app.Label;
045: import nextapp.echo2.app.Table;
046: import nextapp.echo2.app.event.ActionEvent;
047: import nextapp.echo2.app.event.ActionListener;
048: import nextapp.echo2.app.layout.SplitPaneLayoutData;
049: import nextapp.echo2.app.table.DefaultTableModel;
050: import nextapp.echo2.app.table.TableCellRenderer;
051: import nextapp.echo2.webcontainer.ContainerContext;
052: import nextapp.echo2.webcontainer.command.BrowserSetCookieCommand;
053: import nextapp.echo2.webrender.ClientProperties;
054:
055: /**
056: * A test which displays the contents of the <code>ClientProperties</code>
057: * object.
058: * <p>
059: * Note that this object has a dependency on the Web Application Container
060: * and Web Renderer.
061: */
062: public class ContainerContextTest extends Column {
063:
064: private class PropertyTableCellRenderer implements
065: TableCellRenderer {
066:
067: /**
068: * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
069: * java.lang.Object, int, int)
070: */
071: public Component getTableCellRendererComponent(Table table,
072: Object value, int column, int row) {
073: String labelValue;
074: if (value instanceof Object[]) {
075: Object[] array = (Object[]) value;
076: StringBuffer out = new StringBuffer();
077: for (int i = 0; i < array.length; ++i) {
078: out.append(array[i]);
079: if (i < array.length - 1) {
080: out.append(",");
081: }
082: }
083: labelValue = out.toString();
084: } else {
085: labelValue = value.toString();
086: }
087:
088: Label label = new Label(labelValue);
089: label.setStyleName(row % 2 == 0 ? "EvenCellLabel"
090: : "OddCellLabel");
091: return label;
092: }
093: }
094:
095: public ContainerContextTest() {
096: super ();
097:
098: setCellSpacing(new Extent(10));
099:
100: SplitPaneLayoutData splitPaneLayoutData = new SplitPaneLayoutData();
101: splitPaneLayoutData.setInsets(new Insets(10));
102: setLayoutData(splitPaneLayoutData);
103:
104: ApplicationInstance app = ApplicationInstance.getActive();
105: ContainerContext containerContext = (ContainerContext) app
106: .getContextProperty(ContainerContext.CONTEXT_PROPERTY_NAME);
107:
108: Column clientPropertiesColumn = new Column();
109: add(clientPropertiesColumn);
110: clientPropertiesColumn.add(new Label("Client Properties"));
111: clientPropertiesColumn
112: .add(createClientPropertiesTable(containerContext));
113:
114: Column initialParametersColumn = new Column();
115: add(initialParametersColumn);
116: initialParametersColumn.add(new Label("Initial Parameters"));
117: initialParametersColumn
118: .add(createInitialParametersTable(containerContext));
119:
120: Column applicationPropertiesColumn = new Column();
121: add(applicationPropertiesColumn);
122: applicationPropertiesColumn.add(new Label(
123: "ApplicationInstance Properties"));
124: applicationPropertiesColumn
125: .add(createApplicationPropertiesTable(app));
126:
127: Column cookiesColumn = new Column();
128: add(cookiesColumn);
129: cookiesColumn.add(new Label("Cookies"));
130: cookiesColumn.add(createCookieTable(containerContext));
131: Button setCookieButton = new Button("Set Cookie");
132: setCookieButton.setStyleName("Default");
133: setCookieButton.addActionListener(new ActionListener() {
134:
135: public void actionPerformed(ActionEvent e) {
136: int value = (int) (Math.random() * 3);
137: Cookie cookie = new Cookie("Test Cookie " + value,
138: "Mmmmm Cookies " + value);
139: BrowserSetCookieCommand command = new BrowserSetCookieCommand(
140: cookie);
141: ApplicationInstance.getActive().enqueueCommand(command);
142: }
143: });
144: cookiesColumn.add(setCookieButton);
145: }
146:
147: private Table createApplicationPropertiesTable(
148: ApplicationInstance app) {
149: Table table = new Table();
150: table.setStyleName("Default");
151: table.setDefaultRenderer(Object.class,
152: new PropertyTableCellRenderer());
153:
154: DefaultTableModel model = (DefaultTableModel) table.getModel();
155: model.setColumnCount(2);
156:
157: table.getColumnModel().getColumn(0).setHeaderValue("Property");
158: table.getColumnModel().getColumn(1).setHeaderValue("Value");
159:
160: model.addRow(new Object[] { "Locale", app.getLocale() });
161: model.addRow(new Object[] { "Layout Direction",
162: app.getLayoutDirection() });
163:
164: return table;
165: }
166:
167: private Table createClientPropertiesTable(
168: ContainerContext containerContext) {
169: ClientProperties clientProperties = containerContext
170: .getClientProperties();
171: String[] propertyNames = clientProperties.getPropertyNames();
172: Arrays.sort(propertyNames);
173:
174: Table table = new Table();
175: table.setStyleName("Default");
176: table.setDefaultRenderer(Object.class,
177: new PropertyTableCellRenderer());
178:
179: DefaultTableModel model = (DefaultTableModel) table.getModel();
180: model.setColumnCount(2);
181:
182: table.getColumnModel().getColumn(0).setHeaderValue("Property");
183: table.getColumnModel().getColumn(1).setHeaderValue("Value");
184:
185: for (int i = 0; i < propertyNames.length; ++i) {
186: Object propertyValue = clientProperties
187: .get(propertyNames[i]);
188: model
189: .addRow(new Object[] { propertyNames[i],
190: propertyValue });
191: }
192:
193: return table;
194: }
195:
196: private Table createCookieTable(ContainerContext containerContext) {
197: Cookie[] cookies = containerContext.getCookies();
198:
199: Table table = new Table();
200: table.setStyleName("Default");
201: table.setDefaultRenderer(Object.class,
202: new PropertyTableCellRenderer());
203:
204: DefaultTableModel model = (DefaultTableModel) table.getModel();
205: model.setColumnCount(3);
206:
207: table.getColumnModel().getColumn(0).setHeaderValue("Name");
208: table.getColumnModel().getColumn(1).setHeaderValue("Max Age");
209: table.getColumnModel().getColumn(2).setHeaderValue("Value");
210:
211: for (int i = 0; i < cookies.length; ++i) {
212: model.addRow(new Object[] { cookies[i].getName(),
213: Integer.toString(cookies[i].getMaxAge()),
214: cookies[i].getValue() });
215: }
216:
217: return table;
218: }
219:
220: private Table createInitialParametersTable(
221: ContainerContext containerContext) {
222: Map initialParameterMap = containerContext
223: .getInitialRequestParameterMap();
224:
225: Table table = new Table();
226: table.setStyleName("Default");
227: table.setDefaultRenderer(Object.class,
228: new PropertyTableCellRenderer());
229:
230: DefaultTableModel model = (DefaultTableModel) table.getModel();
231: model.setColumnCount(2);
232:
233: table.getColumnModel().getColumn(0).setHeaderValue("Property");
234: table.getColumnModel().getColumn(1).setHeaderValue("Value");
235:
236: Iterator it = initialParameterMap.keySet().iterator();
237: while (it.hasNext()) {
238: String key = (String) it.next();
239: model.addRow(new Object[] { key,
240: initialParameterMap.get(key) });
241: }
242:
243: return table;
244: }
245: }
|