CreateAgentUI.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * CreateAgentUI.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 CreateAgentUI 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 createAgentButton;
  47. private static Logon sessionLogon;
  48. private String packageName="GO Data Warehouse (query)";
  49. private String reportName="Retailer Contact";
  50. CreateAgent anAgent=new CreateAgent();
  51. // This is the constructor.
  52. public CreateAgentUI(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. //declare menuItems
  65. JMenuItem exit;
  66. JMenuItem about;
  67. JMenuItem overview;
  68. //Add and populate the File menu.
  69. JMenu fileMenu = new JMenu("File");
  70. mBar.add(fileMenu);
  71. exit = new JMenuItem("Exit");
  72. fileMenu.add(exit);
  73. exit.addActionListener(new MenuHandler());
  74. //Add and populate the Help menu.
  75. JMenu helpMenu = new JMenu("Help");
  76. mBar.add(helpMenu);
  77. about = new JMenuItem("About");
  78. helpMenu.add(about);
  79. about.addActionListener(new MenuHandler());
  80. overview = new JMenuItem("Overview");
  81. helpMenu.add(overview);
  82. overview.addActionListener(new MenuHandler());
  83. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  84. // create a cmURL panel
  85. JPanel cmURLPanel = new JPanel();
  86. // Add the URL text field and label
  87. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  88. cmURL.setText(CRNConnect.CM_URL);
  89. cmURL.setEditable(false);
  90. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  91. cmURLPanel.add(cmURL, BorderLayout.EAST);
  92. // Create the button Panel
  93. JPanel buttonPanel = new JPanel();
  94. // Create and add the Button
  95. createAgentButton = new JButton("Create an Agent");
  96. createAgentButton.addActionListener(new allButtonsHandler());
  97. buttonPanel.add(createAgentButton, BorderLayout.EAST);
  98. // Add the status text pane.
  99. textAreaPane = new JTextArea();
  100. // Add the panels to the mainPanel
  101. mainPanel.add(cmURLPanel);
  102. mainPanel.add(buttonPanel);
  103. mainPanel.add(textAreaPane);
  104. //Add the ScrollPane to outputPanel
  105. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  106. areaScrollPane.setVerticalScrollBarPolicy(
  107. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  108. areaScrollPane.setPreferredSize(new Dimension(200, 275));
  109. areaScrollPane.setBorder(
  110. BorderFactory.createCompoundBorder(
  111. BorderFactory.createCompoundBorder(
  112. BorderFactory.createTitledBorder("Output"),
  113. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  114. areaScrollPane.getBorder()));
  115. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  116. outputPanel.add(areaScrollPane);
  117. JPanel panel = new JPanel(new BorderLayout());
  118. panel.add(mainPanel, BorderLayout.NORTH);
  119. panel.add(outputPanel);
  120. setContentPane(panel);
  121. }
  122. private class MenuHandler implements ActionListener
  123. {
  124. public void actionPerformed(ActionEvent e)
  125. {
  126. if (e.getActionCommand().startsWith("http://"))
  127. {
  128. connect.connectionChange(e.getActionCommand());
  129. }
  130. try
  131. {
  132. JMenuItem menuClicked = (JMenuItem)e.getSource();
  133. if (menuClicked.getText() == "Exit")
  134. {
  135. System.exit(0);
  136. }
  137. if (menuClicked.getText() == "About")
  138. {
  139. JOptionPane.showMessageDialog(
  140. ((JMenuItem)e.getSource()).getParent(),
  141. "IBM Cognos Sample Application\n\n"
  142. + "Version 1.0.0\n"
  143. + "This application uses the IBM Cognos Software Development Kit",
  144. "About IBM Cognos Samples",
  145. JOptionPane.INFORMATION_MESSAGE,
  146. new ImageIcon("../Common/about.gif"));
  147. }
  148. if (menuClicked.getText().compareTo("Overview") == 0)
  149. {
  150. JFrame explainWindow =
  151. new JFrame("Overview for Create Agent Sample");
  152. File explainFile = new File("Java_CreateAgentUI_Explain.html");
  153. if (! explainFile.exists())
  154. {
  155. JOptionPane.showMessageDialog(null, "Explain file not found");
  156. return;
  157. }
  158. URL explainURL =
  159. new URL("file:///" + explainFile.getAbsolutePath());
  160. JEditorPane explainPane = new JEditorPane();
  161. explainPane.setPage(explainURL);
  162. explainPane.setEditable(false);
  163. JScrollPane explainScroll =
  164. new JScrollPane(
  165. explainPane,
  166. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  167. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  168. explainWindow.getContentPane().add(explainScroll);
  169. explainWindow.setSize(640, 480);
  170. explainWindow.setVisible(true);
  171. }
  172. }
  173. catch (Exception ex)
  174. {}
  175. }
  176. }
  177. // The following is the button event handler.
  178. private class allButtonsHandler implements ActionListener
  179. {
  180. public void actionPerformed(ActionEvent e)
  181. {
  182. if (!Logon.loggedIn(connect))
  183. {
  184. try
  185. {
  186. sessionLogon.logon(connect);
  187. }
  188. catch (Exception logonException)
  189. {}
  190. }
  191. JButton buttonPressed = ((JButton)e.getSource());
  192. String output = new String("");
  193. if (buttonPressed == createAgentButton)
  194. {
  195. textAreaPane.setText("");
  196. try
  197. {
  198. //Display a prompt asking user to select a package
  199. String defaultPackageName = null;
  200. String[] packageOptions = {packageName};
  201. defaultPackageName =
  202. (String)JOptionPane.showInputDialog(null,
  203. "Choose package name",
  204. "Package name",
  205. JOptionPane.INFORMATION_MESSAGE,
  206. null,
  207. packageOptions,
  208. packageOptions[0]);
  209. if ((defaultPackageName == null) || (defaultPackageName.length() == 0))
  210. {
  211. textAreaPane.setText("");
  212. textAreaPane.append("Invalid select / Action cancelled");
  213. return;
  214. }
  215. // Prompt the user for an agent name.
  216. String agentName = null;
  217. agentName =
  218. JOptionPane.showInputDialog(
  219. "Please input an agent name");
  220. if ((agentName == null) || (agentName.length() == 0))
  221. {
  222. textAreaPane.setText("");
  223. textAreaPane.append("Invalid input / Action cancelled");
  224. return;
  225. }
  226. //Display a prompt asking user to select a report
  227. String selectedReport=null;
  228. String[] reportOptions = {reportName};
  229. selectedReport = (String)JOptionPane.showInputDialog(null,
  230. "Please select a report",
  231. "Add a task - Run a report...",
  232. JOptionPane.INFORMATION_MESSAGE,
  233. null,
  234. reportOptions,
  235. reportOptions[0]);
  236. if((selectedReport==null)||(selectedReport.length()==0))
  237. {
  238. textAreaPane.setText("");
  239. textAreaPane.append("Invalid select / Action cancelled");
  240. return;
  241. }
  242. String parentSearchPath = "/content/folder[@name='Samples']/folder[@name='Models']/package[@name='"+defaultPackageName+"']";
  243. boolean createAnAgent=anAgent.addNewAgent(connect, parentSearchPath,agentName,selectedReport);
  244. if(createAnAgent)
  245. {
  246. textAreaPane.setText("");
  247. textAreaPane.append("Agent " + agentName + " successfully created and ran.");
  248. }
  249. else
  250. {
  251. textAreaPane.setText("");
  252. textAreaPane.append("Agent " + agentName + " failed to create.");
  253. }
  254. }
  255. catch (Exception ex)
  256. {
  257. System.out.println(ex.getMessage());
  258. output = "\nCreate agent:\nAn error occurred\n";
  259. }
  260. }
  261. if (output.compareTo("") != 0)
  262. {
  263. textAreaPane.append(output);
  264. }
  265. }
  266. }
  267. // Create the main method to execute the application.
  268. public static void main(String args[])
  269. {
  270. CRNConnect connection = new CRNConnect();
  271. connection.connectToCognosServer();
  272. sessionLogon = new Logon();
  273. String output = "";
  274. while (!Logon.loggedIn(connection))
  275. {
  276. output = sessionLogon.logon(connection);
  277. if (!Logon.loggedIn(connection))
  278. {
  279. int retry =
  280. JOptionPane.showConfirmDialog(
  281. null,
  282. "Login Failed. Please try again.",
  283. "Login Failed",
  284. JOptionPane.OK_CANCEL_OPTION);
  285. if (retry != JOptionPane.OK_OPTION)
  286. {
  287. System.exit(0);
  288. }
  289. }
  290. }
  291. CreateAgentUI frame = new CreateAgentUI("IBM Cognos Sample", connection);
  292. // Create a WindowAdapter so the application
  293. // is exited when the window is closed.
  294. frame.addWindowListener(new WindowAdapter()
  295. {
  296. public void windowClosing(WindowEvent e)
  297. {
  298. System.exit(0);
  299. }
  300. });
  301. frame.textAreaPane.setText(output);
  302. // Set the size of the frame and display it.
  303. frame.setSize(780, 440);
  304. frame.setVisible(true);
  305. frame.setResizable(true);
  306. }
  307. }