| 1 package org.sape.carbon.services.console; 2 3 import java.beans.BeanInfo ; 4 import java.beans.Introspector ; 5 import java.beans.PropertyDescriptor ; 6 import java.lang.reflect.Array ; 7 import java.lang.reflect.Method ; 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.List ; 11 12 import org.sape.carbon.core.config.Configuration; 13 import org.sape.carbon.core.config.type.ConfigurationTypeService; 14 import org.sape.carbon.core.config.type.ConfigurationTypeServiceFactory; 15 import org.sape.carbon.core.exception.ExceptionUtility; 16 import org.sape.carbon.services.swing.treetable.AbstractTreeTableModel; 17 import org.sape.carbon.services.swing.treetable.TreeTableModel; 18 19 28 public class ObjectTreeModel extends AbstractTreeTableModel { 29 private static String [] COLUMN_NAMES = 30 new String [] { "Attribute", "Value", "Editer" }; 31 32 private Configuration config; 33 private TreeNode rootNode; 34 35 public ObjectTreeModel(Configuration config) { 36 super(new ObjectTreeModel.ConfigNode(config)); 37 this.rootNode = (TreeNode)super.root; 38 this.config = config; 40 41 fireTreeStructureChanged(this,null, null, null); 42 } 43 44 public Object getChild(Object obj, int param) { 45 return getChildren(obj)[param]; 47 } 48 49 public int getChildCount(Object obj) { 50 Object [] arr = getChildren(obj); 51 if (arr != null) 52 return arr.length; 53 else 54 return 0; 55 } 56 57 public Object [] getChildren(Object obj) { 58 return ((TreeNode)obj).getChildren(); 59 } 60 61 64 public int getColumnCount() { 65 return COLUMN_NAMES.length; 66 } 67 68 71 public String getColumnName(int column) { 72 return COLUMN_NAMES[column]; 73 } 74 75 79 public Object getValueAt(Object node, int column) { 80 switch (column) { 82 case 0: 83 if (node instanceof TreeNode) { 84 return node; 85 } else { 86 return ""; 87 } 88 case 1: 89 if (node instanceof ArrayNode) { 90 return node; } else if (node instanceof TreeNode) { 92 return ((TreeNode)node).getValue(); 93 } else { 94 return ""; 95 } 96 case 2: 97 Class type = ((TreeNode)node).getType(); 98 if (type != null) { 99 String name = type.getName(); 100 return name.substring(name.lastIndexOf('.')+1); 101 } else { 102 return null; 103 } 104 default: 105 return null; 106 } 107 } 108 109 110 public boolean isCellEditable(Object node, int column) { 111 112 switch (column) { 113 case 0: 114 return true; 115 case 1: 116 return ((TreeNode)node).isEditable(); 117 default: 118 return false; 119 } 120 121 } 122 123 124 125 public Class getColumnClass(int column) { 126 switch (column) { 127 case 0: 128 return TreeTableModel.class; 129 case 1: 130 case 2: 131 default: 132 return String .class; 133 } 134 } 135 136 137 public void setValueAt(Object aValue, Object node, int column) { 138 139 if (column == 1) { 140 TreeNode tNode = (TreeNode)node; 141 142 tNode.setValue(aValue); 143 } 144 145 } 146 147 148 149 150 151 152 153 154 155 public static interface TreeNode { 156 Object getValue(); 157 void setValue(Object obj); 158 Class getType(); 159 TreeNode[] getChildren(); 160 boolean isEditable(); 161 } 162 163 public static class ConfigNode implements TreeNode { 164 private Configuration config; 165 private BeanInfo beanInfo; 166 167 private static final List IGNORED_ATTRIBUTES = 168 Arrays.asList(new String [] 169 { "DataStructure", "RootElement", "Class", "Name" }); 170 171 public ConfigNode(Configuration config) { 172 this.config = config; 173 try { 174 this.beanInfo = 175 Introspector.getBeanInfo(config.getClass()); 176 } catch (Exception e) { 177 e.printStackTrace(); 178 } 179 } 180 181 public TreeNode[] getChildren() { 182 183 PropertyDescriptor [] props = this.beanInfo.getPropertyDescriptors(); 184 ArrayList arraylist = new ArrayList (); 185 186 try { 187 for (int i=0; i< props.length; i++) { 188 TreeNode newNode; 189 String attributeName = props[i].getName(); 190 attributeName = capitalize(attributeName); 191 Class returnType = props[i].getPropertyType(); 192 193 if (!IGNORED_ATTRIBUTES.contains(attributeName)) { 194 195 if (returnType.isArray()) { 196 newNode = new ArrayNode(this.config, props[i]); 197 } else if (Configuration.class.isAssignableFrom(returnType)) { 198 newNode = new ConfigNode( 199 ((Configuration)props[i].getReadMethod().invoke(this.config,new Object []{}))); 200 } else { 201 newNode = new DataNode(this.config, props[i]); 202 } 203 204 arraylist.add(newNode); 205 } 206 } 207 } catch (Exception e) {e.printStackTrace(); } 208 209 TreeNode[] foo = new TreeNode[arraylist.size()]; 210 return (TreeNode[]) arraylist.toArray(foo); 211 } 212 213 public Object getValue() { 214 try { 215 return this.config.getConfigurationName(); } catch (Exception e) { 217 return "couldn't determine config type"; 219 } 220 } 221 222 public String toString() { 223 return this.config.getConfigurationName(); 224 } 225 226 public void setValue(Object value) { } 227 228 public Class getType() { 229 return null; 230 } 231 232 public boolean isEditable() { 233 return false; 234 } 235 236 } 237 238 public static class ArrayNode implements TreeNode { 239 private Configuration parent; 240 private PropertyDescriptor descriptor; 241 242 public ArrayNode(Configuration parent, PropertyDescriptor descriptor) { 243 this.parent = parent; 244 this.descriptor = descriptor; 245 } 246 247 public TreeNode[] getChildren() { 248 return (TreeNode[]) getArray(); 249 } 250 251 public Object [] getArray() { 252 try { 253 Method m = descriptor.getReadMethod(); 254 Object arr = m.invoke(this.parent, new Object []{}); 256 257 ArrayList list = new ArrayList (); 258 for (int i = 0; i < Array.getLength(arr); i++) { 259 if (!Configuration.class.isAssignableFrom(m.getReturnType().getComponentType())) { 260 list.add(new ArrayNodeDataValue(this, Array.get(arr, i), i)); 261 } else { 262 list.add(new ArrayNodeConfigurationValue(this,(Configuration)Array.get(arr, i), i)); 263 } 264 } 265 TreeNode[] treeNodes = new TreeNode[list.size()]; 266 return list.toArray(treeNodes); 267 268 } catch (Exception e) { 269 e.printStackTrace(); 270 return new Object []{}; 271 } 272 } 273 274 public Object getValue() { 275 try { 276 Method m = descriptor.getReadMethod(); 277 return m.invoke(this.parent, new Object []{}); 279 } catch (Exception e) { 280 e.printStackTrace(); 281 return null; 282 } 283 } 284 285 public String toString() { 286 return this.descriptor.getName(); 287 } 288 289 public void setValue(Object obj) { 290 291 297 try { 298 Method m = this.descriptor.getWriteMethod(); 299 300 m.invoke(this.parent, new Object [] { obj }); 301 } catch (Exception e) { 302 e.printStackTrace(); 303 } 304 } 305 306 public Class getType() { 307 return ArrayNode.class; 308 } 309 310 public boolean isEditable() { 311 return true; 312 } 313 314 } 315 316 public static class ArrayNodeDataValue implements TreeNode { 317 private ArrayNode parent; 318 private int index; 319 private Object data; 320 public ArrayNodeDataValue(ArrayNode parent,Object data, int index) { 321 this.parent = parent; 322 this.data = data; 323 this.index = index; 324 } 325 326 public TreeNode[] getChildren() { 327 return null; 328 } 329 public Object getValue() { 330 Object obj = this.data; 331 if (obj != null) { 332 return obj.toString(); 333 } else { 334 return ""; 335 } 336 } 337 public String toString() { 338 return parent.toString() + "[" + index + "]"; 339 } 340 341 public void setValue(Object obj) { 342 } 343 344 public Class getType() { 345 return null; 346 } 347 348 public boolean isEditable() { 349 return false; 350 } 351 352 } 353 public static class ArrayNodeConfigurationValue extends ConfigNode { 354 private ArrayNode parent; 355 private int index; 356 public ArrayNodeConfigurationValue(ArrayNode parent,Configuration config, int index) { 357 super(config); 358 this.parent = parent; 359 this.index = index; 360 } 361 362 public String toString() { 363 return parent.toString() + "[" + index + "]"; 364 } 365 366 } 367 368 public static class DataNode implements TreeNode { 369 private Configuration parent; 370 private PropertyDescriptor descriptor; 373 374 public DataNode(Configuration parent, PropertyDescriptor pd) { 375 this.parent = parent; 376 this.descriptor = pd; 377 } 378 379 380 381 public TreeNode[] getChildren() { 382 return null; 383 } 384 385 public Object getValue() { 386 try { 387 Method m = this.descriptor.getReadMethod(); 388 Object obj = m.invoke(this.parent, new Object []{}); 389 ConfigurationTypeService typeService = 390 ConfigurationTypeServiceFactory.getInstance(); 391 392 return obj; } catch (Exception e) { 394 return null; 395 } 396 } 397 398 public String toString() { 399 return capitalize(this.descriptor.getName()); 400 } 401 402 public void setValue(Object obj) { 403 System.out.println("Altering [" + toString() + "] = " + 404 obj); 405 try { 406 Method m = this.descriptor.getWriteMethod(); 407 408 ConfigurationTypeService typeService = 409 ConfigurationTypeServiceFactory.getInstance(); 410 411 Object value = 412 typeService.toObject( 413 this.descriptor.getPropertyType(), 414 obj.toString()); 415 416 m.invoke(this.parent, new Object []{ value }); 417 418 } catch (UnsupportedOperationException uoe) { 419 System.out.println(ExceptionUtility.printStackTracesToString(uoe)); 420 } catch (Exception e) { 421 System.out.println(ExceptionUtility.printStackTracesToString(e)); 423 } 424 425 } 426 427 public Class getType() { 428 if (this.descriptor != null) { 429 Class type = this.descriptor.getPropertyType(); 430 return type; 431 } else { 432 return null; 433 } 434 } 435 436 public boolean isEditable() { 437 return (null != this.descriptor.getWriteMethod()); 438 } 439 440 } 441 442 public static String capitalize(String string) { 443 return Character.toUpperCase(string.toCharArray()[0]) + 444 string.substring(1); 445 } 446 447 } 448 | Popular Tags |