SecurityUI.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. * SecurityUI.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.JComboBox;
  28. import javax.swing.JEditorPane;
  29. import javax.swing.JFrame;
  30. import javax.swing.JLabel;
  31. import javax.swing.JMenu;
  32. import javax.swing.JMenuBar;
  33. import javax.swing.JMenuItem;
  34. import javax.swing.JOptionPane;
  35. import javax.swing.JPanel;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JTextArea;
  38. import javax.swing.JTextField;
  39. // This Java class extends the JFrame class so that you can display a window.
  40. public class SecurityUI 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 cmdOption;
  47. private JComboBox cboOption;
  48. private static Logon sessionLogon;
  49. // This is the constructor.
  50. public SecurityUI(String title, CRNConnect connection)
  51. {
  52. // Set the title of the frame, even before the variables are declared.
  53. super(title);
  54. connect = connection;
  55. addComponents();
  56. }
  57. // Add all components to the frame's panel.
  58. private void addComponents()
  59. {
  60. JMenuBar mBar = new JMenuBar();
  61. this.setJMenuBar(mBar);
  62. //declare menuItems
  63. JMenuItem exit;
  64. JMenuItem about;
  65. JMenuItem overview;
  66. //Add and populate the File menu.
  67. JMenu fileMenu = new JMenu("File");
  68. mBar.add(fileMenu);
  69. exit = new JMenuItem("Exit");
  70. fileMenu.add(exit);
  71. exit.addActionListener(new MenuHandler());
  72. //Add and populate the Help menu.
  73. JMenu helpMenu = new JMenu("Help");
  74. mBar.add(helpMenu);
  75. about = new JMenuItem("About");
  76. helpMenu.add(about);
  77. about.addActionListener(new MenuHandler());
  78. overview = new JMenuItem("Overview");
  79. helpMenu.add(overview);
  80. overview.addActionListener(new MenuHandler());
  81. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  82. // create a cmURL panel
  83. JPanel cmURLPanel = new JPanel();
  84. // Add the URL text field and label
  85. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  86. cmURL.setText(CRNConnect.CM_URL);
  87. cmURL.setEditable(false);
  88. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  89. cmURLPanel.add(cmURL, BorderLayout.EAST);
  90. // Create the buttonPanel
  91. JPanel buttonPanel = new JPanel();
  92. // Create and add the option combo box
  93. String cmdList[] = { "Get Logon Info", "Logoff" };
  94. cboOption = new JComboBox(cmdList);
  95. buttonPanel.add(cboOption, BorderLayout.WEST);
  96. // Create and add the Button
  97. cmdOption = new JButton("Run Option");
  98. cmdOption.addActionListener(new allButtonsHandler());
  99. buttonPanel.add(cmdOption, BorderLayout.EAST);
  100. // Add the status text pane.
  101. textAreaPane = new JTextArea();
  102. // Add the panels to the mainPanel
  103. mainPanel.add(cmURLPanel);
  104. mainPanel.add(buttonPanel);
  105. mainPanel.add(textAreaPane);
  106. //Add the ScrollPane to outputPanel
  107. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  108. areaScrollPane.setVerticalScrollBarPolicy(
  109. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  110. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  111. areaScrollPane.setBorder(
  112. BorderFactory.createCompoundBorder(
  113. BorderFactory.createCompoundBorder(
  114. BorderFactory.createTitledBorder("Output"),
  115. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  116. areaScrollPane.getBorder()));
  117. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  118. outputPanel.add(areaScrollPane);
  119. JPanel panel = new JPanel(new BorderLayout());
  120. panel.add(mainPanel, BorderLayout.NORTH);
  121. panel.add(outputPanel);
  122. setContentPane(panel);
  123. }
  124. private class MenuHandler implements ActionListener
  125. {
  126. public void actionPerformed(ActionEvent e)
  127. {
  128. if (e.getActionCommand().startsWith("http://"))
  129. {
  130. connect.connectionChange(e.getActionCommand());
  131. }
  132. try
  133. {
  134. JMenuItem menuClicked = (JMenuItem)e.getSource();
  135. if (menuClicked.getText() == "Exit")
  136. {
  137. System.exit(0);
  138. }
  139. if (menuClicked.getText() == "About")
  140. {
  141. JOptionPane.showMessageDialog(
  142. ((JMenuItem)e.getSource()).getParent(),
  143. "IBM Cognos Sample Application\n\n"
  144. + "Version 1.0.0\n"
  145. + "This application uses the IBM Cognos Software Development Kit",
  146. "About IBM Cognos Samples",
  147. JOptionPane.INFORMATION_MESSAGE,
  148. new ImageIcon("../Common/about.gif"));
  149. System.out.println(System.getProperties());
  150. }
  151. if (menuClicked.getText().compareTo("Overview") == 0)
  152. {
  153. JFrame explainWindow =
  154. new JFrame("Overview for Security Sample");
  155. File explainFile = new File("Java_SecurityUI_Explain.html");
  156. if (! explainFile.exists())
  157. {
  158. JOptionPane.showMessageDialog(null, "Explain file not found");
  159. return;
  160. }
  161. URL explainURL =
  162. new URL("file:///" + explainFile.getAbsolutePath());
  163. JEditorPane explainPane = new JEditorPane();
  164. explainPane.setPage(explainURL);
  165. explainPane.setEditable(false);
  166. JScrollPane explainScroll =
  167. new JScrollPane(
  168. explainPane,
  169. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  170. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  171. explainWindow.getContentPane().add(explainScroll);
  172. explainWindow.setSize(640, 480);
  173. explainWindow.setVisible(true);
  174. }
  175. }
  176. catch (Exception ex)
  177. {}
  178. }
  179. }
  180. // The following is the button event handler.
  181. // Note: A SWITCH statement cannot be used here because we are comparing
  182. // objects.
  183. private class allButtonsHandler implements ActionListener
  184. {
  185. public void actionPerformed(ActionEvent e)
  186. {
  187. if (!Logon.loggedIn(connect))
  188. {
  189. try
  190. {
  191. sessionLogon.logon(connect);
  192. }
  193. catch (Exception logonException)
  194. {}
  195. }
  196. JButton buttonPressed = ((JButton)e.getSource());
  197. String output = new String();
  198. if (buttonPressed == cmdOption)
  199. {
  200. String cboOptionText = (String)cboOption.getSelectedItem();
  201. if (cboOptionText == "Get Logon Info")
  202. {
  203. output = sessionLogon.logonInfo(connect);
  204. }
  205. else if (cboOptionText == "Logoff")
  206. {
  207. output = sessionLogon.logoff(connect);
  208. }
  209. else
  210. {
  211. //error
  212. }
  213. }
  214. if (output.compareTo("") != 0)
  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. SecurityUI frame = new SecurityUI("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(850, 440);
  258. frame.setVisible(true);
  259. frame.setResizable(true);
  260. }
  261. }