CSExplorerTree.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * CSExplorerTree.java
  10. *
  11. * Copyright (C) 2005 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. */
  14. import java.awt.BorderLayout;
  15. import java.awt.Cursor;
  16. import java.awt.Dimension;
  17. import java.awt.GridLayout;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.MouseEvent;
  21. import java.lang.reflect.Method;
  22. import java.util.HashMap;
  23. import javax.swing.BoxLayout;
  24. import javax.swing.JButton;
  25. import javax.swing.JFrame;
  26. import javax.swing.JLabel;
  27. import javax.swing.JMenuItem;
  28. import javax.swing.JPanel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JSplitPane;
  31. import javax.swing.JTable;
  32. import javax.swing.JTextField;
  33. import javax.swing.JTree;
  34. import javax.swing.event.TreeExpansionEvent;
  35. import javax.swing.event.TreeExpansionListener;
  36. import javax.swing.event.TreeSelectionEvent;
  37. import javax.swing.event.TreeSelectionListener;
  38. import javax.swing.tree.DefaultMutableTreeNode;
  39. import javax.swing.tree.TreePath;
  40. import javax.swing.tree.TreeSelectionModel;
  41. import com.cognos.developer.schemas.bibus._3.BaseClass;
  42. import com.cognos.developer.schemas.bibus._3.PropEnum;
  43. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  44. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  45. import com.cognos.developer.schemas.bibus._3.Sort;
  46. public class CSExplorerTree
  47. extends JPanel
  48. implements
  49. TreeSelectionListener,
  50. TreeExpansionListener,
  51. java.awt.event.MouseListener,
  52. java.awt.event.ActionListener
  53. {
  54. private JScrollPane detailsPane;
  55. private JSplitPane splitPane;
  56. private JScrollPane treeView;
  57. private JTable detailsTable;
  58. private JTable tableOfDetails;
  59. private JTree tree;
  60. private JTextField currentNode;
  61. private JButton forwardButton;
  62. private JButton backButton;
  63. private static String defaultRootSearchPath = "/";
  64. private static String DETAILS_MENU_STRING = "Details...";
  65. private CRNConnect connection;
  66. private TreeBrowserNodeDetailTableModel tableModelForDetails;
  67. public CSExplorerTree(CRNConnect connect)
  68. {
  69. super(new GridLayout(1, 0));
  70. connection = connect;
  71. //Create the nodes.
  72. DefaultMutableTreeNode top = new DefaultMutableTreeNode();
  73. TreeBrowserNode cmRootNode =
  74. new TreeBrowserNode(defaultRootSearchPath, connection);
  75. cmRootNode.setContainer(top);
  76. createNodes(top);
  77. for (int i = 0; i < top.getChildCount(); i++)
  78. {
  79. createNodes((DefaultMutableTreeNode)top.getChildAt(i));
  80. }
  81. ((TreeBrowserNode)top.getUserObject()).setChildrenPopulated(true);
  82. //Create a tree that allows one selection at a time.
  83. tree = new JTree(top);
  84. tree.getSelectionModel().setSelectionMode(
  85. TreeSelectionModel.SINGLE_TREE_SELECTION);
  86. //Listen for when the selection changes.
  87. tree.addTreeExpansionListener(this);
  88. tree.addTreeSelectionListener(this);
  89. tree.addMouseListener(this);
  90. tree.setCellRenderer(new TreeBrowserCellRenderer());
  91. //Create the scroll pane and add the tree to it.
  92. treeView = new JScrollPane(tree);
  93. //Create the HTML viewing pane.
  94. detailsTable = new JTable(new TreeBrowserTableModel());
  95. detailsTable.addMouseListener(this);
  96. detailsTable.setShowGrid(false);
  97. //Create the output pane.
  98. detailsPane = new JScrollPane(detailsTable);
  99. //Add the scroll panes to a split pane.
  100. splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  101. splitPane.setLeftComponent(treeView);
  102. splitPane.setRightComponent(detailsPane);
  103. Dimension minimumSize = new Dimension(200, 50);
  104. detailsPane.setMinimumSize(minimumSize);
  105. treeView.setMinimumSize(minimumSize);
  106. splitPane.setDividerLocation(.5);
  107. splitPane.setPreferredSize(new Dimension(700, 500));
  108. //Add the split pane to this panel.
  109. add(splitPane);
  110. //Set the root node as the currently selected node in the tree
  111. tree.setSelectionRow(0);
  112. }
  113. /** Required by MouseListener interface. */
  114. public void mousePressed(MouseEvent mEvent)
  115. {}
  116. public void mouseEntered(MouseEvent mEvent)
  117. {}
  118. public void mouseExited(MouseEvent mEvent)
  119. {}
  120. public void mouseReleased(MouseEvent mEvent)
  121. {}
  122. public void mouseClicked(MouseEvent mEvent)
  123. {
  124. if (javax.swing.SwingUtilities.isRightMouseButton(mEvent))
  125. {
  126. javax.swing.JMenuItem detailsItem =
  127. new javax.swing.JMenuItem(DETAILS_MENU_STRING);
  128. detailsItem.addActionListener(this);
  129. javax.swing.JPopupMenu dropDownMenu = new javax.swing.JPopupMenu();
  130. dropDownMenu.add(detailsItem);
  131. //dropDownMenu.add(exploreItem);
  132. dropDownMenu.show(
  133. mEvent.getComponent(),
  134. mEvent.getX(),
  135. mEvent.getY());
  136. }
  137. if (javax.swing.SwingUtilities.isLeftMouseButton(mEvent))
  138. {
  139. if (mEvent.getClickCount() > 1)
  140. {
  141. if (mEvent.getSource().getClass() == JTable.class)
  142. {
  143. JTable tableForDBLClick = (JTable)mEvent.getSource();
  144. TreeBrowserNode nodeForDBLClick =
  145. (
  146. (TreeBrowserTableModel)tableForDBLClick
  147. .getModel())
  148. .getTbnForRow(
  149. tableForDBLClick.getSelectedRow());
  150. exploreNode(nodeForDBLClick);
  151. }
  152. }
  153. }
  154. }
  155. public void exploreNode(TreeBrowserNode node)
  156. {
  157. tree.setSelectionPath(new TreePath((node.getContainer().getPath())));
  158. }
  159. public void treeCollapsed(TreeExpansionEvent teEvent)
  160. {}
  161. public void treeExpanded(TreeExpansionEvent teEvent)
  162. {
  163. this.getParent().setCursor(
  164. Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
  165. DefaultMutableTreeNode currentExpandedNode =
  166. (DefaultMutableTreeNode)teEvent.getPath().getLastPathComponent();
  167. for (int i = 0; i < currentExpandedNode.getChildCount(); i++)
  168. {
  169. createNodes(
  170. (DefaultMutableTreeNode)currentExpandedNode.getChildAt(i));
  171. }
  172. this.getParent().setCursor(Cursor.getDefaultCursor());
  173. }
  174. /** Required by TreeSelectionListener interface. */
  175. public void valueChanged(TreeSelectionEvent e)
  176. {
  177. DefaultMutableTreeNode node =
  178. (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
  179. if (node != null)
  180. {
  181. createNodes(node);
  182. displayInfo((TreeBrowserNode)node.getUserObject());
  183. if ((this.getParent() != null)
  184. && (this
  185. .getParent()
  186. .getParent()
  187. .getParent()
  188. .getParent()
  189. .getParent()
  190. != null))
  191. {
  192. (
  193. (CSExplorer)this
  194. .getParent()
  195. .getParent()
  196. .getParent()
  197. .getParent()
  198. .getParent())
  199. .updateSelectedSearchPath(
  200. ((TreeBrowserNode)node.getUserObject())
  201. .getCMObject()
  202. .getSearchPath()
  203. .getValue());
  204. }
  205. }
  206. }
  207. public void actionPerformed(java.awt.event.ActionEvent action)
  208. {
  209. JMenuItem actionSource = (JMenuItem)action.getSource();
  210. if (DETAILS_MENU_STRING.compareTo(actionSource.getText()) == 0)
  211. {
  212. TreeBrowserNode nodeForPopup;
  213. if (((javax.swing.JPopupMenu)actionSource.getParent())
  214. .getInvoker()
  215. .getClass()
  216. == JTree.class)
  217. {
  218. nodeForPopup =
  219. (TreeBrowserNode) ((DefaultMutableTreeNode)tree
  220. .getLastSelectedPathComponent())
  221. .getUserObject();
  222. showDetailPane(nodeForPopup, connection);
  223. }
  224. if (((javax.swing.JPopupMenu)actionSource.getParent())
  225. .getInvoker()
  226. .getClass()
  227. == JTable.class)
  228. {
  229. JTable tableForPopup =
  230. (JTable) ((javax.swing.JPopupMenu)actionSource.getParent())
  231. .getInvoker();
  232. if (tableForPopup.getSelectedRow() != -1)
  233. {
  234. nodeForPopup =
  235. (
  236. (TreeBrowserTableModel)tableForPopup
  237. .getModel())
  238. .getTbnForRow(
  239. tableForPopup.getSelectedRow());
  240. showDetailPane(nodeForPopup, connection);
  241. }
  242. }
  243. }
  244. }
  245. public void showDetailPane(TreeBrowserNode node, CRNConnect connection)
  246. {
  247. BaseClass cmObject = node.getCMObject();
  248. Class cmObjectClass = cmObject.getClass();
  249. Method[] cmObjectMethods = cmObjectClass.getMethods();
  250. HashMap objectProperties = new HashMap();
  251. for (int i = 0; i < cmObjectMethods.length; i++)
  252. {
  253. String methodName = cmObjectMethods[i].getName();
  254. try
  255. {
  256. PropEnum theProperty =
  257. PropEnum.fromString(
  258. TreeBrowserNodeDetailTableModel.propertyFromGetMethod(
  259. methodName));
  260. objectProperties.put(cmObjectMethods[i], theProperty);
  261. }
  262. catch (IllegalArgumentException isEx)
  263. {
  264. cmObjectMethods[i] = null;
  265. }
  266. }
  267. PropEnum[] allTheProps = new PropEnum[objectProperties.size()];
  268. int j = 0;
  269. for (int i = 0; i < cmObjectMethods.length; i++)
  270. {
  271. if (cmObjectMethods[i] != null)
  272. {
  273. try
  274. {
  275. allTheProps[j++] =
  276. PropEnum.fromString(
  277. ((PropEnum)objectProperties
  278. .get(cmObjectMethods[i]))
  279. .getValue());
  280. }
  281. catch (IllegalArgumentException isEx)
  282. {
  283. j--;
  284. }
  285. }
  286. }
  287. try
  288. {
  289. if (!node.getDetailsPopulated())
  290. {
  291. SearchPathMultipleObject cmObjPath = new SearchPathMultipleObject();
  292. cmObjPath.set_value(cmObject.getSearchPath().getValue());
  293. node.setCMObject(
  294. connection.getCMService().query(
  295. cmObjPath,
  296. allTheProps,
  297. new Sort[] {},
  298. new QueryOptions())[0]);
  299. node.setDetailsPopulated(true);
  300. }
  301. }
  302. catch (java.rmi.RemoteException remoteEx)
  303. {
  304. System.out.println("remoteException");
  305. }
  306. tableModelForDetails =
  307. new TreeBrowserNodeDetailTableModel(node.getCMObject());
  308. tableOfDetails = new JTable(tableModelForDetails);
  309. JScrollPane detailsPane = new JScrollPane(tableOfDetails);
  310. JPanel navPanel = createNavPanel(node);
  311. JPanel detailsAndNav = new JPanel();
  312. detailsAndNav.setLayout(new BoxLayout(detailsAndNav, BoxLayout.Y_AXIS));
  313. detailsAndNav.add(navPanel, BorderLayout.NORTH);
  314. detailsAndNav.add(detailsPane);
  315. JFrame detailsFrame =
  316. new JFrame(
  317. "Details for "
  318. + node.getCMObject().getDefaultName().getValue());
  319. detailsFrame.getContentPane().add(detailsAndNav);
  320. detailsFrame.pack();
  321. detailsFrame.setVisible(true);
  322. }
  323. private JPanel createNavPanel(TreeBrowserNode node)
  324. {
  325. // Add the current node text field and label
  326. currentNode = new JTextField(40);
  327. currentNode.setText(node.getCMObject().getDefaultName().getValue());
  328. currentNode.setEditable(false);
  329. //Put together a panel for the current node
  330. JPanel nodePanel = new JPanel();
  331. nodePanel.add(new JLabel("Current Node:"));
  332. nodePanel.add(currentNode);
  333. //get the button panel
  334. JPanel buttonPanel = createMainButtonPanel();
  335. // create the main panel and add the components
  336. JPanel mainPanel = new JPanel(new GridLayout(3,0));
  337. // Add everything to the main panel
  338. mainPanel.add(nodePanel);
  339. mainPanel.add(buttonPanel);
  340. return mainPanel;
  341. }
  342. private JPanel createMainButtonPanel()
  343. {
  344. // Create the button Panel
  345. JPanel buttonPanel = new JPanel();
  346. // Create and add the Buttons
  347. backButton = new JButton("<< Back");
  348. backButton.addActionListener(new navBackButtonHandler());
  349. buttonPanel.add(backButton);
  350. forwardButton = new JButton("Forward >>");
  351. forwardButton.addActionListener(new navFwdButtonHandler());
  352. buttonPanel.add(forwardButton);
  353. return buttonPanel;
  354. }
  355. private class navFwdButtonHandler implements ActionListener
  356. {
  357. public void actionPerformed(ActionEvent e)
  358. {
  359. if (tableOfDetails.getSelectedRow() >= 0)
  360. {
  361. currentNode.setText((tableOfDetails.getValueAt(tableOfDetails.getSelectedRow(),1)).toString());
  362. tableModelForDetails.incrementNavigation(tableOfDetails);
  363. tableOfDetails.changeSelection(0, 0, false, false);
  364. }
  365. }
  366. }
  367. private class navBackButtonHandler implements ActionListener
  368. {
  369. public void actionPerformed(ActionEvent e)
  370. {
  371. tableModelForDetails.decrementNavigation(tableOfDetails);
  372. currentNode.setText(tableModelForDetails.getCurrentNode().toString());
  373. tableOfDetails.changeSelection(0, 0, false, false);
  374. }
  375. }
  376. private void displayInfo(TreeBrowserNode node)
  377. {
  378. if (node != null)
  379. {
  380. ((TreeBrowserTableModel)detailsTable.getModel()).clear();
  381. for (int i = 0; i < node.getNumChildren(); i++)
  382. {
  383. detailsTable.getModel().setValueAt(
  384. node.getChild(i, connection),
  385. i,
  386. 0);
  387. }
  388. detailsTable.revalidate();
  389. detailsTable.repaint();
  390. }
  391. return;
  392. }
  393. private void createNodes(DefaultMutableTreeNode top)
  394. {
  395. if (((TreeBrowserNode)top.getUserObject()).getChildrenPopulated())
  396. {
  397. return;
  398. }
  399. DefaultMutableTreeNode subNode = null;
  400. TreeBrowserNode tmpNode = (TreeBrowserNode)top.getUserObject();
  401. for (int i = 0; i < tmpNode.getNumChildren(); i++)
  402. {
  403. TreeBrowserNode child = tmpNode.getChild(i, connection);
  404. subNode = new DefaultMutableTreeNode();
  405. child.setContainer(subNode);
  406. top.add(subNode);
  407. }
  408. ((TreeBrowserNode)top.getUserObject()).setChildrenPopulated(true);
  409. }
  410. }