RenderUI.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * RenderUI.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. */
  15. import java.awt.BorderLayout;
  16. import java.awt.Dimension;
  17. import java.awt.GridBagConstraints;
  18. import java.awt.GridBagLayout;
  19. import java.awt.GridLayout;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.io.File;
  25. import java.net.URL;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.ImageIcon;
  28. import javax.swing.JButton;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JEditorPane;
  31. import javax.swing.JFrame;
  32. import javax.swing.JLabel;
  33. import javax.swing.JMenu;
  34. import javax.swing.JMenuBar;
  35. import javax.swing.JMenuItem;
  36. import javax.swing.JOptionPane;
  37. import javax.swing.JPanel;
  38. import javax.swing.JScrollPane;
  39. import javax.swing.JTextArea;
  40. import javax.swing.JTextField;
  41. import com.cognos.developer.schemas.bibus._3.BaseClass;
  42. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  43. import com.cognos.developer.schemas.bibus._3.PropEnum;
  44. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  45. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  46. import com.cognos.developer.schemas.bibus._3.Sort;
  47. // This Java class extends the JFrame class so that you can
  48. // display a window.
  49. public class RenderUI extends JFrame
  50. {
  51. private CRNConnect connect;
  52. // The following variables represent the dialog components.
  53. private JTextArea textAreaPane;
  54. private JTextField cmURL;
  55. private JTextField selectedSearchPath;
  56. private JButton renderButton;
  57. private JComboBox repSelectOption;
  58. private static Logon sessionLogon;
  59. private static BaseClassWrapper selectedReport = null;
  60. // This is the constructor.
  61. public RenderUI(String title, CRNConnect connection)
  62. {
  63. // Set the title of the frame, even before the variables are declared.
  64. super(title);
  65. connect = connection;
  66. addComponents();
  67. }
  68. // Add all components to the frame's panel.
  69. private void addComponents()
  70. {
  71. JMenuBar mBar = new JMenuBar();
  72. this.setJMenuBar(mBar);
  73. //declare menuItems
  74. JMenuItem exit;
  75. JMenuItem about;
  76. JMenuItem overview;
  77. //Add and populate the File menu.
  78. JMenu fileMenu = new JMenu("File");
  79. mBar.add(fileMenu);
  80. exit = new JMenuItem("Exit");
  81. fileMenu.add(exit);
  82. exit.addActionListener(new MenuHandler());
  83. //Add and populate the Help menu.
  84. JMenu helpMenu = new JMenu("Help");
  85. mBar.add(helpMenu);
  86. about = new JMenuItem("About");
  87. helpMenu.add(about);
  88. about.addActionListener(new MenuHandler());
  89. overview = new JMenuItem("Overview");
  90. helpMenu.add(overview);
  91. overview.addActionListener(new MenuHandler());
  92. JPanel mainPanel = createMainPanel();
  93. JPanel outputNavPanel = createOutputPanel();
  94. JPanel panel = new JPanel(new BorderLayout());
  95. panel.add(mainPanel, BorderLayout.NORTH);
  96. panel.add(outputNavPanel);
  97. setContentPane(panel);
  98. }
  99. private JPanel createOutputPanel()
  100. {
  101. //Create the html scrollPane
  102. textAreaPane = new JTextArea();
  103. JScrollPane textScrollPane = new JScrollPane(textAreaPane);
  104. textScrollPane.setVerticalScrollBarPolicy(
  105. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  106. textScrollPane.setPreferredSize(new Dimension(500, 275));
  107. //Create the output panel and it's layout objects
  108. GridBagLayout layout = new GridBagLayout();
  109. JPanel outputNavPanel = new JPanel(layout);
  110. GridBagConstraints layoutConstraints = new GridBagConstraints();
  111. //Set the layout for the scroll pane and add it
  112. layoutConstraints.weightx = 1.0;
  113. layoutConstraints.weighty = 1.0;
  114. layoutConstraints.fill = GridBagConstraints.BOTH;
  115. layout.setConstraints(textScrollPane, layoutConstraints);
  116. outputNavPanel.add(textScrollPane);
  117. //put a border around the output and nav buttons
  118. outputNavPanel.setBorder(
  119. BorderFactory.createCompoundBorder(
  120. BorderFactory.createCompoundBorder(
  121. BorderFactory.createTitledBorder("Output"),
  122. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  123. outputNavPanel.getBorder()));
  124. return outputNavPanel;
  125. }
  126. private JPanel createMainPanel()
  127. {
  128. // Add the URL text field and label
  129. cmURL = new JTextField(CRNConnect.CM_URL.length() - 10);
  130. cmURL.setText(CRNConnect.CM_URL);
  131. cmURL.setEditable(false);
  132. //Put together a panel for the URL
  133. JPanel cmURLPanel = new JPanel();
  134. cmURLPanel.add(new JLabel("Server URL:"));
  135. cmURLPanel.add(cmURL);
  136. // Create the searchPath text field and label
  137. selectedSearchPath = new JTextField(CRNConnect.CM_URL.length() - 10); //same as above
  138. selectedSearchPath.setText("");
  139. selectedSearchPath.setEditable(false);
  140. selectedSearchPath.setAutoscrolls(true);
  141. //Put together a panel for the search path
  142. JPanel searchPathPanel = new JPanel();
  143. //searchPathPanel.add(new JLabel("SearchPath:"));
  144. searchPathPanel.add(selectedSearchPath);
  145. //get the button panel
  146. JPanel buttonPanel = createMainButtonPanel();
  147. //
  148. // create the main panel and add the components
  149. //
  150. JPanel mainPanel = new JPanel(new GridLayout(3,0));
  151. // Add everything to the main panel
  152. mainPanel.add(cmURLPanel);
  153. mainPanel.add(buttonPanel);
  154. mainPanel.add(searchPathPanel);
  155. return mainPanel;
  156. }
  157. private JPanel createMainButtonPanel()
  158. {
  159. // Create the button Panel
  160. JPanel buttonPanel = new JPanel();
  161. // Create and add the select report combo box
  162. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  163. repSelectOption = new JComboBox(listOfReports);
  164. repSelectOption.setSelectedItem(null);
  165. repSelectOption.addActionListener(new ReportSelectionHandler());
  166. buttonPanel.add(repSelectOption, BorderLayout.CENTER);
  167. // Create and add the Button
  168. renderButton = new JButton("Render");
  169. renderButton.addActionListener(new allButtonsHandler());
  170. buttonPanel.add(renderButton, BorderLayout.EAST);
  171. return buttonPanel;
  172. }
  173. private class MenuHandler implements ActionListener
  174. {
  175. public void actionPerformed(ActionEvent e)
  176. {
  177. if (e.getActionCommand().startsWith("http://"))
  178. {
  179. connect.connectionChange(e.getActionCommand());
  180. }
  181. try
  182. {
  183. JMenuItem menuClicked = (JMenuItem)e.getSource();
  184. if (menuClicked.getText() == "Exit")
  185. {
  186. System.exit(0);
  187. }
  188. if (menuClicked.getText() == "About")
  189. {
  190. JOptionPane.showMessageDialog(
  191. ((JMenuItem)e.getSource()).getParent(),
  192. "IBM Cognos Sample Application\n\n"
  193. + "Version 1.0.0\n"
  194. + "This application demonstrates the IBM Cognos SDK",
  195. "About the Samples",
  196. JOptionPane.INFORMATION_MESSAGE,
  197. new ImageIcon("../Common/about.gif"));
  198. }
  199. if (menuClicked.getText().compareTo("Overview") == 0)
  200. {
  201. JFrame explainWindow =
  202. new JFrame("Overview for RenderUI");
  203. File explainFile = new File("Java_RenderUI_Explain.html");
  204. if (! explainFile.exists())
  205. {
  206. JOptionPane.showMessageDialog(null, "Explain file not found");
  207. return;
  208. }
  209. URL explainURL =
  210. new URL("file:///" + explainFile.getAbsolutePath());
  211. JEditorPane explainPane = new JEditorPane();
  212. explainPane.setPage(explainURL);
  213. explainPane.setEditable(false);
  214. JScrollPane explainScroll =
  215. new JScrollPane(
  216. explainPane,
  217. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  218. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  219. explainWindow.getContentPane().add(explainScroll);
  220. explainWindow.setSize(640, 480);
  221. explainWindow.setVisible(true);
  222. }
  223. }
  224. catch (Exception ex)
  225. {}
  226. }
  227. }
  228. // The following is the button event handler.
  229. // Note: A SWITCH statement cannot be used here because we are comparing
  230. // objects.
  231. private class allButtonsHandler implements ActionListener
  232. {
  233. public void actionPerformed(ActionEvent e)
  234. {
  235. if (!Logon.loggedIn(connect))
  236. {
  237. try
  238. {
  239. sessionLogon.logon(connect);
  240. }
  241. catch (Exception logonException)
  242. {}
  243. }
  244. JButton buttonPressed = ((JButton)e.getSource());
  245. String output = new String();
  246. if (buttonPressed == renderButton)
  247. {
  248. Render reportRenderer = new Render();
  249. try
  250. {
  251. output = reportRenderer.renderReport(
  252. connect,
  253. selectedReport,
  254. null);
  255. }
  256. catch (Exception ex)
  257. {
  258. System.out.println(ex.getMessage());
  259. output =
  260. "An error occurred.\nMake sure a "
  261. + "Report Name is selected and IBM Cognos is running";
  262. }
  263. }
  264. if (output.compareTo("") != 0)
  265. {
  266. textAreaPane.setText("");
  267. textAreaPane.append(output);
  268. }
  269. }
  270. }
  271. private class ReportSelectionHandler implements ActionListener
  272. {
  273. public void actionPerformed(ActionEvent repSelectedEvent)
  274. {
  275. selectedReport = (BaseClassWrapper) repSelectOption.getSelectedItem();
  276. if (selectedReport != null)
  277. {
  278. selectedSearchPath.setText(selectedReport.getBaseClassObject().getSearchPath().getValue());
  279. return;
  280. }
  281. selectedSearchPath.setText("");
  282. }
  283. }
  284. //This is a method for retrieving a list of the available reports to run
  285. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  286. {
  287. BaseClassWrapper reportAndQueryList[] = null;
  288. BaseClass reports[] = new BaseClass[0];
  289. BaseClass queries[] = new BaseClass[0];
  290. int reportAndQueryIndex = 0;
  291. int reportIndex = 0;
  292. int queryIndex = 0;
  293. if (connection == null)
  294. {
  295. System.out.println(
  296. "Invalid parameter passed to getListOfReports()\n");
  297. return null;
  298. }
  299. PropEnum props[] =
  300. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  301. Sort sortOptions[] = { new Sort()};
  302. sortOptions[0].setOrder(OrderEnum.ascending);
  303. sortOptions[0].setPropName(PropEnum.defaultName);
  304. if (!Logon.loggedIn(connect))
  305. {
  306. try
  307. {
  308. sessionLogon.logon(connect);
  309. }
  310. catch (Exception logonException)
  311. {}
  312. }
  313. try
  314. {
  315. SearchPathMultipleObject reportsPath = new SearchPathMultipleObject("/content//report");
  316. SearchPathMultipleObject queriesPath = new SearchPathMultipleObject("/content//query");
  317. reports =
  318. connection.getCMService().query(
  319. reportsPath,
  320. props,
  321. sortOptions,
  322. new QueryOptions());
  323. queries =
  324. connection.getCMService().query(
  325. queriesPath,
  326. props,
  327. sortOptions,
  328. new QueryOptions());
  329. }
  330. catch (java.rmi.RemoteException remoteEx)
  331. {
  332. System.out.println("Caught Remote Exception:\n");
  333. remoteEx.printStackTrace();
  334. }
  335. reportAndQueryList = new BaseClassWrapper[reports.length + queries.length];
  336. if ((reports != null) && (reports.length > 0))
  337. {
  338. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  339. {
  340. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(reports[reportIndex]);
  341. }
  342. }
  343. if ((queries != null) && (queries.length > 0))
  344. {
  345. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  346. {
  347. reportAndQueryList[reportAndQueryIndex++] =
  348. new BaseClassWrapper(queries[queryIndex]);
  349. }
  350. }
  351. return reportAndQueryList;
  352. }
  353. // Create the main method to execute the application.
  354. public static void main(String args[])
  355. {
  356. CRNConnect connection = new CRNConnect();
  357. connection.connectToCognosServer();
  358. sessionLogon = new Logon();
  359. String output = "";
  360. while (!Logon.loggedIn(connection))
  361. {
  362. output = sessionLogon.logon(connection);
  363. if (!Logon.loggedIn(connection))
  364. {
  365. int retry =
  366. JOptionPane.showConfirmDialog(
  367. null,
  368. "Login Failed. Please try again.",
  369. "Login Failed",
  370. JOptionPane.OK_CANCEL_OPTION);
  371. if (retry != JOptionPane.OK_OPTION)
  372. {
  373. System.exit(0);
  374. }
  375. }
  376. }
  377. RenderUI frame = new RenderUI("IBM Cognos Sample", connection);
  378. // Create a WindowAdapter so the application
  379. // is exited when the window is closed.
  380. frame.addWindowListener(new WindowAdapter()
  381. {
  382. public void windowClosing(WindowEvent e)
  383. {
  384. System.exit(0);
  385. }
  386. });
  387. frame.textAreaPane.setText(output);
  388. // Set the size of the frame and display it.
  389. frame.setSize(640, 480);
  390. frame.setVisible(true);
  391. frame.setResizable(true);
  392. }
  393. }