cmQueryUI.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * cmQueryUI.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.GridLayout;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import java.io.File;
  23. import java.net.URL;
  24. import javax.swing.BorderFactory;
  25. import javax.swing.ImageIcon;
  26. import javax.swing.JButton;
  27. import javax.swing.JEditorPane;
  28. import javax.swing.JFrame;
  29. import javax.swing.JLabel;
  30. import javax.swing.JMenu;
  31. import javax.swing.JMenuBar;
  32. import javax.swing.JMenuItem;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.JTextArea;
  37. import javax.swing.JTextField;
  38. // This Java class extends the JFrame class so that you can
  39. // display a window.
  40. public class cmQueryUI extends JFrame
  41. {
  42. private CRNConnect connect;
  43. // The following variables represent the dialog components.
  44. private JTextArea textAreaPane;
  45. private JButton cmSampleQueryButton;
  46. private JButton clearButton;
  47. private JTextField searchPathString;
  48. private String defaultSearchPath = "/*";
  49. private JTextField cmURL;
  50. private static Logon sessionLogon;
  51. // This is the constructor.
  52. public cmQueryUI(String title, CRNConnect connection)
  53. {
  54. // Set the title of the frame, even before the variables are declared.
  55. super(title);
  56. connect = connection;
  57. addComponents();
  58. }
  59. // Add all components to the frame's panel.
  60. private void addComponents()
  61. {
  62. JMenuBar mBar = new JMenuBar();
  63. this.setJMenuBar(mBar);
  64. JPanel mainPanel = new JPanel(new GridLayout(3,0));
  65. mainPanel.setPreferredSize(new Dimension(150, 100));
  66. JPanel queryDataPanel = new JPanel();
  67. JPanel buttonPanel = new JPanel();
  68. //declare menuItems
  69. JMenuItem exit;
  70. JMenuItem about;
  71. JMenuItem overview;
  72. //Add and populate the File menu.
  73. JMenu fileMenu = new JMenu("File");
  74. mBar.add(fileMenu);
  75. exit = new JMenuItem("Exit");
  76. fileMenu.add(exit);
  77. exit.addActionListener(new MenuHandler());
  78. //Add and populate the Help menu.
  79. JMenu helpMenu = new JMenu("Help");
  80. mBar.add(helpMenu);
  81. about = new JMenuItem("About");
  82. helpMenu.add(about);
  83. about.addActionListener(new MenuHandler());
  84. overview = new JMenuItem("Overview");
  85. helpMenu.add(overview);
  86. overview.addActionListener(new MenuHandler());
  87. // Add the status text pane.
  88. textAreaPane = new JTextArea();
  89. //mainPanel.add(textAreaPane);
  90. // create a cmURL panel
  91. JPanel cmURLPanel = new JPanel();
  92. // Add the URL text field and label
  93. cmURL = new JTextField(CRNConnect.CM_URL.length());
  94. cmURL.setText(CRNConnect.CM_URL);
  95. cmURL.setEditable(false);
  96. JLabel cmURLLabel = new JLabel("Server URL:");
  97. cmURLLabel.setLabelFor(cmURL);
  98. cmURLPanel.add(cmURLLabel, BorderLayout.WEST);
  99. cmURLPanel.add(cmURL, BorderLayout.EAST);
  100. // Add the searchPath field and label
  101. JLabel searchPathLabel = new JLabel("Search Path:");
  102. queryDataPanel.add(searchPathLabel, BorderLayout.CENTER);
  103. searchPathString = new JTextField(CRNConnect.CM_URL.length());
  104. searchPathString.setText(defaultSearchPath);
  105. queryDataPanel.add(searchPathString, BorderLayout.EAST);
  106. // Add the Buttons
  107. cmSampleQueryButton = new JButton("Execute Query");
  108. cmSampleQueryButton.addActionListener(new allButtonsHandler());
  109. buttonPanel.add(cmSampleQueryButton, BorderLayout.EAST);
  110. mainPanel.add(cmURLPanel, BorderLayout.NORTH);
  111. mainPanel.add(queryDataPanel, BorderLayout.CENTER);
  112. mainPanel.add(buttonPanel, BorderLayout.SOUTH);
  113. //Add the ScrollPane to panel2
  114. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  115. areaScrollPane.setVerticalScrollBarPolicy(
  116. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  117. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  118. areaScrollPane.setBorder(
  119. BorderFactory.createCompoundBorder(
  120. BorderFactory.createCompoundBorder(
  121. BorderFactory.createTitledBorder("Results Display Window"),
  122. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  123. areaScrollPane.getBorder()));
  124. JPanel panel2 = new JPanel(new GridLayout(0, 1));
  125. panel2.add(areaScrollPane);
  126. JPanel panel = new JPanel(new BorderLayout(5, 5));
  127. panel.add(mainPanel, BorderLayout.NORTH);
  128. panel.add(panel2);
  129. setContentPane(panel);
  130. }
  131. private class MenuHandler implements ActionListener
  132. {
  133. public void actionPerformed(ActionEvent e)
  134. {
  135. if (e.getActionCommand().startsWith("http://"))
  136. {
  137. connect.connectionChange(e.getActionCommand());
  138. }
  139. try
  140. {
  141. JMenuItem menuClicked = (JMenuItem)e.getSource();
  142. if (menuClicked.getText() == "Exit")
  143. {
  144. System.exit(0);
  145. }
  146. if (menuClicked.getText() == "About")
  147. {
  148. JOptionPane.showMessageDialog(
  149. ((JMenuItem)e.getSource()).getParent(),
  150. "IBM Cognos Sample Application\n\n"
  151. + "Version 1.0.0\n"
  152. + "This application uses the IBM Cognos Software Development Kit",
  153. "About IBM Cognos Samples",
  154. JOptionPane.INFORMATION_MESSAGE,
  155. new ImageIcon("../Common/about.gif"));
  156. }
  157. if (menuClicked.getText().compareTo("Overview") == 0)
  158. {
  159. JFrame explainWindow =
  160. new JFrame("Overview for CM Query sample");
  161. File explainFile = new File("Java_cmQueryUI_Explain.html");
  162. if (!explainFile.exists())
  163. {
  164. JOptionPane.showMessageDialog(
  165. null,
  166. "Explain file not found");
  167. return;
  168. }
  169. URL explainURL =
  170. new URL("file:///" + explainFile.getAbsolutePath());
  171. JEditorPane explainPane = new JEditorPane();
  172. explainPane.setPage(explainURL);
  173. explainPane.setEditable(false);
  174. JScrollPane explainScroll =
  175. new JScrollPane(
  176. explainPane,
  177. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  178. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  179. explainWindow.getContentPane().add(explainScroll);
  180. explainWindow.setSize(640, 480);
  181. explainWindow.setVisible(true);
  182. }
  183. }
  184. catch (Exception ex)
  185. {}
  186. }
  187. }
  188. // The following is the button event handler.
  189. // Note: A SWITCH statement cannot be used here because we are comparing
  190. // objects.
  191. private class allButtonsHandler implements ActionListener
  192. {
  193. public void actionPerformed(ActionEvent e)
  194. {
  195. if (!Logon.loggedIn(connect))
  196. {
  197. try
  198. {
  199. sessionLogon.logon(connect);
  200. }
  201. catch (Exception logonException)
  202. {}
  203. }
  204. JButton buttonPressed = ((JButton)e.getSource());
  205. String output = new String();
  206. if (buttonPressed == cmSampleQueryButton)
  207. {
  208. cmQuerySample sampleQuery = new cmQuerySample();
  209. output
  210. += sampleQuery.prepareQuery(
  211. connect,
  212. searchPathString.getText());
  213. }
  214. if (output.compareTo("") != 0 || buttonPressed == clearButton)
  215. {
  216. textAreaPane.setText("");
  217. textAreaPane.append(output);
  218. }
  219. }
  220. }
  221. // Create the main method to execute the application.
  222. public static void main(String args[])
  223. {
  224. CRNConnect connection = new CRNConnect();
  225. connection.connectToCognosServer();
  226. sessionLogon = new Logon();
  227. String output = "";
  228. while (!Logon.loggedIn(connection))
  229. {
  230. output = sessionLogon.logon(connection);
  231. if (!Logon.loggedIn(connection))
  232. {
  233. int retry =
  234. JOptionPane.showConfirmDialog(
  235. null,
  236. "Login Failed. Please try again.",
  237. "Login Failed",
  238. JOptionPane.OK_CANCEL_OPTION);
  239. if (retry != JOptionPane.OK_OPTION)
  240. {
  241. System.exit(0);
  242. }
  243. }
  244. }
  245. cmQueryUI frame = new cmQueryUI("IBM Cognos Sample", connection);
  246. // Create a WindowAdapter so the application
  247. // is exited when the window is closed.
  248. frame.addWindowListener(new WindowAdapter()
  249. {
  250. public void windowClosing(WindowEvent e)
  251. {
  252. System.exit(0);
  253. }
  254. });
  255. frame.textAreaPane.setText(output);
  256. // Set the size of the frame and display it.
  257. frame.setSize(680, 440);
  258. frame.setVisible(true);
  259. frame.setResizable(true);
  260. }
  261. }