DrillThroughUI.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * DrillThroughUI.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 java.util.Hashtable;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JButton;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JEditorPane;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JMenu;
  35. import javax.swing.JMenuBar;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.JPanel;
  39. import javax.swing.JScrollPane;
  40. import javax.swing.JTextArea;
  41. import javax.swing.JTextField;
  42. public class DrillThroughUI extends JFrame {
  43. private CRNConnect connect;
  44. private static DrillThroughUI frame;
  45. // The following variables represent the dialog components.
  46. private JTextArea textAreaPane;
  47. private JTextField cmURL;
  48. private JButton createDefinitionButton;
  49. private JComboBox packageOption;
  50. private static Logon sessionLogon;
  51. private BaseClassWrapper chosenPackage = null;
  52. DrillThrough newDrillThrough = new DrillThrough();
  53. ReportParameters newReportParameters = new ReportParameters();
  54. BaseReportAndParameters ReportAndPara = new BaseReportAndParameters();
  55. // This is the constructor.
  56. public DrillThroughUI(String title, CRNConnect connection) {
  57. // Set the title of the frame, even before the variables are declared.
  58. super(title);
  59. connect = connection;
  60. addComponents();
  61. }
  62. // Add all components to the frame's panel.
  63. private void addComponents() {
  64. JMenuBar mBar = new JMenuBar();
  65. this.setJMenuBar(mBar);
  66. // declare menuItems
  67. JMenuItem exit;
  68. JMenuItem about;
  69. JMenuItem overview;
  70. // Add and populate the File menu.
  71. JMenu fileMenu = new JMenu("File");
  72. mBar.add(fileMenu);
  73. exit = new JMenuItem("Exit");
  74. fileMenu.add(exit);
  75. exit.addActionListener(new MenuHandler());
  76. // Add and populate the Help menu.
  77. JMenu helpMenu = new JMenu("Help");
  78. mBar.add(helpMenu);
  79. about = new JMenuItem("About");
  80. helpMenu.add(about);
  81. about.addActionListener(new MenuHandler());
  82. overview = new JMenuItem("Overview");
  83. helpMenu.add(overview);
  84. overview.addActionListener(new MenuHandler());
  85. JPanel mainPanel = createMainPanel();
  86. JPanel outputNavPanel = createOutputPanel();
  87. JPanel panel = new JPanel(new BorderLayout());
  88. panel.add(mainPanel, BorderLayout.NORTH);
  89. panel.add(outputNavPanel);
  90. setContentPane(panel);
  91. }
  92. private JPanel createOutputPanel() {
  93. // Add the status text pane.
  94. textAreaPane = new JTextArea();
  95. // Add the ScrollPane to outputPanel
  96. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  97. areaScrollPane
  98. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  99. areaScrollPane.setPreferredSize(new Dimension(500, 275));
  100. //Create the output panel and it's layout objects
  101. GridBagLayout layout = new GridBagLayout(); //GridLayout(0, 1)
  102. JPanel outputNavPanel = new JPanel(layout);
  103. GridBagConstraints layoutConstraints = new GridBagConstraints();
  104. //Set the layout for the scroll pane and add it
  105. layoutConstraints.weightx = 1.0;
  106. layoutConstraints.weighty = 1.0;
  107. layoutConstraints.fill = GridBagConstraints.BOTH;
  108. layout.setConstraints(areaScrollPane, layoutConstraints);
  109. outputNavPanel.add(areaScrollPane);
  110. outputNavPanel.setBorder(BorderFactory.createCompoundBorder(
  111. BorderFactory.createCompoundBorder(BorderFactory
  112. .createTitledBorder("Output"), BorderFactory
  113. .createEmptyBorder(5, 5, 5, 5)), outputNavPanel
  114. .getBorder()));
  115. return outputNavPanel;
  116. }
  117. private JPanel createMainPanel() {
  118. // Add the URL text field and label
  119. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  120. cmURL.setText(CRNConnect.CM_URL);
  121. cmURL.setEditable(false);
  122. // Put together a panel for the URl
  123. JPanel cmURLPanel = new JPanel();
  124. cmURLPanel.add(new JLabel("Server URL:"));
  125. cmURLPanel.add(cmURL);
  126. //get dropdown box and button panel
  127. JPanel optionButtonPanel = createMainButtonPanel();
  128. //create the main panel and add the components
  129. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  130. // Add the panels to the mainPanel
  131. mainPanel.add(cmURLPanel);
  132. mainPanel.add(optionButtonPanel);
  133. return mainPanel;
  134. }
  135. private JPanel createMainButtonPanel() {
  136. // Create the button Panel
  137. JPanel buttonPanel = new JPanel();
  138. // Create and add the package output type combo box
  139. BaseClassWrapper[] packageList = ReportAndPara
  140. .getListOfPackages(connect);
  141. packageOption = new JComboBox(packageList);
  142. packageOption.setSelectedItem(null);
  143. packageOption.addActionListener(new packageSelectionHandler());
  144. buttonPanel.add(new JLabel("Package Options:"), BorderLayout.WEST);
  145. buttonPanel.add(packageOption, BorderLayout.WEST);
  146. // Create and add the Button
  147. createDefinitionButton = new JButton("Create Drill-through definition");
  148. createDefinitionButton.addActionListener(new allButtonsHandler());
  149. buttonPanel.add(createDefinitionButton, BorderLayout.EAST);
  150. return buttonPanel;
  151. }
  152. // handle menu bar buttons
  153. private class MenuHandler implements ActionListener {
  154. public void actionPerformed(ActionEvent e) {
  155. if (e.getActionCommand().startsWith("http://")) {
  156. connect.connectionChange(e.getActionCommand());
  157. }
  158. try {
  159. JMenuItem menuClicked = (JMenuItem) e.getSource();
  160. if (menuClicked.getText() == "Exit") {
  161. System.exit(0);
  162. }
  163. if (menuClicked.getText() == "About") {
  164. JOptionPane.showMessageDialog(((JMenuItem) e.getSource())
  165. .getParent(), "IBM Cognos Sample Application\n\n"
  166. + "Version 1.0.0\n"
  167. + "This application uses the IBM Cognos Software Development Kit",
  168. "About IBM Cognos Samples",
  169. JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
  170. "../Common/about.gif"));
  171. }
  172. if (menuClicked.getText().compareTo("Overview") == 0) {
  173. JFrame explainWindow = new JFrame(
  174. "Overview for Drill-through Definition Sample");
  175. File explainFile = new File(
  176. "Java_DrillThroughUI_Explain.html");
  177. if (!explainFile.exists()) {
  178. JOptionPane.showMessageDialog(null,
  179. "Explain file not found");
  180. return;
  181. }
  182. URL explainURL = new URL("file:///"
  183. + explainFile.getAbsolutePath());
  184. JEditorPane explainPane = new JEditorPane();
  185. explainPane.setPage(explainURL);
  186. explainPane.setEditable(false);
  187. JScrollPane explainScroll = new JScrollPane(explainPane,
  188. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  189. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  190. explainWindow.getContentPane().add(explainScroll);
  191. explainWindow.setSize(640, 480);
  192. explainWindow.setVisible(true);
  193. }
  194. } catch (Exception ex) {
  195. }
  196. }
  197. }
  198. // The following is the button event handler.
  199. private class allButtonsHandler implements ActionListener {
  200. public void actionPerformed(ActionEvent e) {
  201. if (!Logon.loggedIn(connect)) {
  202. try {
  203. sessionLogon.logon(connect);
  204. } catch (Exception logonException) {
  205. System.out.println("The error: "
  206. + logonException.getMessage());
  207. }
  208. }
  209. JButton buttonPressed = ((JButton) e.getSource());
  210. if (buttonPressed == createDefinitionButton) {
  211. textAreaPane.setText("");
  212. if (chosenPackage != null) {
  213. String searchPath = null;
  214. searchPath = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='"
  215. + chosenPackage.toString() + "']//report";
  216. Hashtable reportWithPara = ReportAndPara
  217. .hashReportNameAndPara(connect, searchPath);
  218. if (reportWithPara != null) {
  219. BaseClassWrapper[] reportList = ReportAndPara
  220. .getReports(reportWithPara);
  221. if (reportList != null && reportList.length > 0) {
  222. Object selectedReport = JOptionPane
  223. .showInputDialog(null, null,
  224. "Select the target report",
  225. JOptionPane.INFORMATION_MESSAGE,
  226. null, reportList, null);
  227. BaseClassWrapper chosenReport = (BaseClassWrapper) selectedReport;
  228. if (selectedReport != null) {
  229. String inputValue = null;
  230. do {
  231. inputValue = JOptionPane
  232. .showInputDialog(
  233. null,
  234. "Name:",
  235. "Specify a name - New Drill-through Definition",
  236. 3);
  237. if (inputValue == null) {
  238. return;
  239. }
  240. } while (inputValue.length() == 0);
  241. if (inputValue != null
  242. && inputValue.length() > 0) {
  243. String rs = newDrillThrough
  244. .createDrillThroughDefinition(
  245. connect, inputValue,
  246. chosenPackage,
  247. chosenReport,
  248. reportWithPara);
  249. packageOption.setSelectedItem(null);
  250. if (rs != null) {
  251. textAreaPane
  252. .setText("The Drill-through definition '"
  253. + inputValue
  254. + "' has been created successfully in the content store"
  255. + "\n"
  256. + "The Event ID:"
  257. + "\n" + " " + rs);
  258. } else {
  259. textAreaPane
  260. .setText("Failed to create the Drill-through definition in the content store");
  261. }
  262. }
  263. } else {
  264. packageOption.setSelectedItem(null);
  265. textAreaPane.setText("");
  266. }
  267. } else {
  268. JOptionPane.showMessageDialog(null, "No entries",
  269. "Select the target report",
  270. JOptionPane.INFORMATION_MESSAGE);
  271. }
  272. } else {
  273. packageOption.setSelectedItem(null);
  274. textAreaPane
  275. .setText("Error getting report parameters."
  276. + "\nPlease see the console windows for details.");
  277. }
  278. } else {
  279. JOptionPane.showMessageDialog(null,
  280. "Please select a package.",
  281. "Select package", JOptionPane.INFORMATION_MESSAGE);
  282. }
  283. }
  284. }
  285. }
  286. // This is the package select combo box event handler.
  287. private class packageSelectionHandler implements ActionListener {
  288. public void actionPerformed(ActionEvent packageTypeSelectedEvent) {
  289. textAreaPane.setText("");
  290. chosenPackage = (BaseClassWrapper) packageOption.getSelectedItem();
  291. }
  292. }
  293. // Create the main method to execute the application.
  294. public static void main(String args[]) {
  295. CRNConnect connection = new CRNConnect();
  296. connection.connectToCognosServer();
  297. sessionLogon = new Logon();
  298. String logonOutput = "";
  299. while (!Logon.loggedIn(connection)) {
  300. logonOutput = sessionLogon.logon(connection);
  301. if (!Logon.loggedIn(connection)) {
  302. int retry = JOptionPane.showConfirmDialog(null,
  303. "Login Failed. Please try again.", "Login Failed",
  304. JOptionPane.OK_CANCEL_OPTION);
  305. if (retry != JOptionPane.OK_OPTION) {
  306. System.exit(0);
  307. }
  308. }
  309. }
  310. frame = new DrillThroughUI("IBM Cognos Sample", connection);
  311. // Create a WindowAdapter so the application
  312. // is exited when the window is closed.
  313. frame.addWindowListener(new WindowAdapter() {
  314. public void windowClosing(WindowEvent e) {
  315. System.exit(0);
  316. }
  317. });
  318. frame.textAreaPane.setText(logonOutput);
  319. // Set the size of the frame and display it.
  320. frame.setSize(800, 440);
  321. frame.setVisible(true);
  322. frame.setResizable(true);
  323. }
  324. }