1 29 30 package nextapp.echo2.testapp.interactive.testscreen; 31 32 import java.util.Arrays ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 36 import javax.servlet.http.Cookie ; 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 62 public class ContainerContextTest extends Column { 63 64 private class PropertyTableCellRenderer 65 implements TableCellRenderer { 66 67 71 public Component getTableCellRendererComponent(Table table, Object value, int column, int row) { 72 String labelValue; 73 if (value instanceof Object []) { 74 Object [] array = (Object []) value; 75 StringBuffer out = new StringBuffer (); 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 cookie = new Cookie ("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 .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 []{"Locale", app.getLocale()}); 151 model.addRow(new Object []{"Layout Direction", app.getLayoutDirection()}); 152 153 return table; 154 } 155 156 private Table createClientPropertiesTable(ContainerContext containerContext) { 157 ClientProperties clientProperties = containerContext.getClientProperties(); 158 String [] propertyNames = clientProperties.getPropertyNames(); 159 Arrays.sort(propertyNames); 160 161 Table table = new Table(); 162 table.setStyleName("Default"); 163 table.setDefaultRenderer(Object .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 propertyValue = clientProperties.get(propertyNames[i]); 173 model.addRow(new Object []{propertyNames[i], propertyValue}); 174 } 175 176 return table; 177 } 178 179 private Table createCookieTable(ContainerContext containerContext) { 180 Cookie [] cookies = containerContext.getCookies(); 181 182 Table table = new Table(); 183 table.setStyleName("Default"); 184 table.setDefaultRenderer(Object .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 []{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 initialParameterMap = containerContext.getInitialRequestParameterMap(); 202 203 Table table = new Table(); 204 table.setStyleName("Default"); 205 table.setDefaultRenderer(Object .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 it = initialParameterMap.keySet().iterator(); 214 while (it.hasNext()) { 215 String key = (String ) it.next(); 216 model.addRow(new Object []{key, initialParameterMap.get(key)}); 217 } 218 219 return table; 220 } 221 } 222 | Popular Tags |