TreeBrowserNodeDetailTableModel.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2006
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * TreeBrowserNodeDetailTableModel.java
  10. *
  11. * Copyright (C) 2006 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. */
  15. import javax.swing.JTable;
  16. import javax.swing.table.AbstractTableModel;
  17. import javax.swing.tree.TreePath;
  18. import java.util.ArrayList;
  19. import java.lang.reflect.Method;
  20. import java.lang.reflect.Array;
  21. class TreeBrowserNodeDetailTableModel
  22. extends AbstractTableModel
  23. implements java.awt.event.ActionListener
  24. {
  25. // private Object myObject;
  26. private String[] columnNames = { "Property Name", "Property Value" };
  27. private ArrayList data = new ArrayList();
  28. private ArrayList navigationPoints = new ArrayList();
  29. private int navigationIndex = -1;
  30. public TreeBrowserNodeDetailTableModel(Object root)
  31. {
  32. //super(root);
  33. navigationPoints.add(root);
  34. navigationIndex++;
  35. renderNavigationChange();
  36. }
  37. public Object getChild(Object parentObject, int index)
  38. {
  39. return getRowObject(index);
  40. }
  41. public int getColumnCount()
  42. {
  43. return columnNames.length;
  44. }
  45. public boolean isLeaf(Object parentObject)
  46. {
  47. return true;
  48. }
  49. public int getIndexOfChild(Object parent, Object child)
  50. {
  51. for(int i = 0; i < getRowCount(); i++)
  52. {
  53. if (child == getRowObject(i))
  54. {
  55. return i;
  56. }
  57. }
  58. return -1;
  59. }
  60. public void valueForPathChanged(TreePath path, Object newValue)
  61. {
  62. }
  63. public int getChildCount(Object parentObject)
  64. {
  65. return getRowCount();
  66. }
  67. public int getRowCount()
  68. {
  69. return data.size();
  70. }
  71. public Object getValueAt(Object parent, int index)
  72. {
  73. return getChild(parent, index);
  74. }
  75. public String getColumnName(int col)
  76. {
  77. return columnNames[col];
  78. }
  79. public Object getRowObject(int row)
  80. {
  81. return data.get(row);
  82. }
  83. public Object getValueAt(int row, int col)
  84. {
  85. if (col == 0)
  86. {
  87. try
  88. {
  89. return propertyFromGetMethod(((Method)data.get(row)).getName());
  90. }
  91. catch (ClassCastException ccEx)
  92. {}
  93. return data.get(row);
  94. }
  95. if (col == 1)
  96. {
  97. try
  98. {
  99. Object propertyValue =
  100. ((Method)data.get(row)).invoke(
  101. navigationPoints.get(navigationIndex),
  102. new Object[] {});
  103. if (propertyValue != null)
  104. {
  105. Class propertyValueClass = propertyValue.getClass();
  106. Method[] propertyValueMethods =
  107. propertyValueClass.getMethods();
  108. for (int i = 0; i < propertyValueMethods.length; i++)
  109. {
  110. if (propertyValueMethods[i]
  111. .getName()
  112. .compareTo("getValue")
  113. == 0)
  114. {
  115. return propertyValueMethods[i].invoke(
  116. propertyValue,
  117. new Object[] {});
  118. }
  119. }
  120. }
  121. return propertyValue;
  122. }
  123. catch (java.lang.reflect.InvocationTargetException itEx)
  124. {}
  125. catch (java.lang.IllegalAccessException iaEx)
  126. {}
  127. catch (ClassCastException ccEx)
  128. {
  129. return data.get(row);
  130. }
  131. }
  132. return null;
  133. }
  134. public void setValueAt(Object o, int row, int col)
  135. {
  136. data.add(row, o);
  137. }
  138. public Class getColumnClass(int c)
  139. {
  140. return getValueAt(0, c).getClass();
  141. }
  142. public void add(Object o)
  143. {
  144. data.add(o);
  145. }
  146. public void clear()
  147. {
  148. data.clear();
  149. }
  150. public static String propertyFromGetMethod(String methodName)
  151. {
  152. String propertyName = null;
  153. if (methodName.indexOf("get") == 0)
  154. {
  155. propertyName =
  156. methodName.substring(3, 4).toLowerCase()
  157. + methodName.substring(4);
  158. }
  159. return propertyName;
  160. }
  161. public static String propertyFromSetMethod(String methodName)
  162. {
  163. String propertyName = null;
  164. if (methodName.indexOf("set") == 0)
  165. {
  166. propertyName =
  167. methodName.substring(3, 4).toLowerCase()
  168. + methodName.substring(4);
  169. }
  170. return propertyName;
  171. }
  172. public void incrementNavigation(JTable navTable)
  173. {
  174. if (navTable.getSelectedRow() < 0)
  175. {
  176. return;
  177. }
  178. navigationPoints.add( getValueAt(navTable.getSelectedRow(), 1));
  179. navigationIndex++;
  180. renderNavigationChange();
  181. navTable.revalidate();
  182. navTable.repaint();
  183. }
  184. public void renderNavigationChange()
  185. {
  186. if(navigationPoints.get(navigationIndex).getClass().isArray())
  187. {
  188. if (Array.getLength(navigationPoints.get(navigationIndex)) <= 0)
  189. {
  190. return;
  191. }
  192. clear();
  193. for ( int i = 0; i < (Array.getLength(navigationPoints.get(navigationIndex))); i++ )
  194. {
  195. data.add( Array.get(navigationPoints.get(navigationIndex), i) );
  196. }
  197. return;
  198. }
  199. Method[] objectMethods = navigationPoints.get(navigationIndex).getClass().getMethods();
  200. if(objectMethods.length <= 0)
  201. {
  202. return;
  203. }
  204. clear();
  205. for(int i = 0; i < objectMethods.length; i++)
  206. {
  207. if (objectMethods[i].getName().indexOf("get") == 0)
  208. {
  209. if (objectMethods[i].getParameterTypes().length == 0)
  210. {
  211. data.add(objectMethods[i]);
  212. }
  213. }
  214. }
  215. }
  216. public void decrementNavigation(JTable navTable)
  217. {
  218. if (navigationIndex <= 0)
  219. {
  220. return;
  221. }
  222. navigationPoints.remove(navigationIndex);
  223. navigationIndex--;
  224. renderNavigationChange();
  225. navTable.revalidate();
  226. navTable.repaint();
  227. }
  228. /** Required by ActionListener */
  229. public void actionPerformed(java.awt.event.ActionEvent action)
  230. {
  231. }
  232. public Object getCurrentNode()
  233. {
  234. return navigationPoints.get(navigationIndex);
  235. }
  236. }