PermissionsUI.java 7.1 KB

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