ViewPackagesUI.java 7.3 KB

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