CopyMoveReportUI.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. * CopyMoveReportUI.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.GridBagConstraints;
  18. import java.awt.GridBagLayout;
  19. import java.awt.GridLayout;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.awt.event.WindowAdapter;
  23. import java.awt.event.WindowEvent;
  24. import java.io.File;
  25. import java.net.URL;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.DefaultComboBoxModel;
  28. import javax.swing.ImageIcon;
  29. import javax.swing.JButton;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JEditorPane;
  32. import javax.swing.JFrame;
  33. import javax.swing.JLabel;
  34. import javax.swing.JMenu;
  35. import javax.swing.JMenuBar;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.JPanel;
  39. import javax.swing.JScrollPane;
  40. import javax.swing.JTextArea;
  41. import javax.swing.JTextField;
  42. public class CopyMoveReportUI extends JFrame {
  43. private CRNConnect connect;
  44. private static CopyMoveReportUI frame;
  45. // The following variables represent the dialog components.
  46. private JTextArea textAreaPane;
  47. private JTextField cmURL;
  48. private JButton copyMoveButton;
  49. private JComboBox runOption;
  50. private JComboBox reportOption;
  51. private static Logon sessionLogon;
  52. private static final String COPY = "Copy";
  53. private static final String COPYRENAME = "Copy Rename";
  54. private static final String MOVE = "Move";
  55. private static final String MOVERENAME = "Move Rename";
  56. private String chosenRunOption = null;
  57. private BaseClassWrapper chosenReport = null;
  58. CopyMoveReport newCopyMoveReport = new CopyMoveReport();
  59. // This is the constructor.
  60. public CopyMoveReportUI(String title, CRNConnect connection) {
  61. // Set the title of the frame, even before the variables are declared.
  62. super(title);
  63. connect = connection;
  64. addComponents();
  65. }
  66. // Add all components to the frame's panel.
  67. private void addComponents() {
  68. JMenuBar mBar = new JMenuBar();
  69. this.setJMenuBar(mBar);
  70. // declare menuItems
  71. JMenuItem exit;
  72. JMenuItem about;
  73. JMenuItem overview;
  74. // Add and populate the File menu.
  75. JMenu fileMenu = new JMenu("File");
  76. mBar.add(fileMenu);
  77. exit = new JMenuItem("Exit");
  78. fileMenu.add(exit);
  79. exit.addActionListener(new MenuHandler());
  80. // Add and populate the Help menu.
  81. JMenu helpMenu = new JMenu("Help");
  82. mBar.add(helpMenu);
  83. about = new JMenuItem("About");
  84. helpMenu.add(about);
  85. about.addActionListener(new MenuHandler());
  86. overview = new JMenuItem("Overview");
  87. helpMenu.add(overview);
  88. overview.addActionListener(new MenuHandler());
  89. JPanel mainPanel = createMainPanel();
  90. JPanel outputNavPanel = createOutputPanel();
  91. JPanel panel = new JPanel(new BorderLayout());
  92. panel.add(mainPanel, BorderLayout.NORTH);
  93. panel.add(outputNavPanel);
  94. setContentPane(panel);
  95. }
  96. private JPanel createOutputPanel() {
  97. // Add the status text pane.
  98. textAreaPane = new JTextArea();
  99. // Add the ScrollPane to outputPanel
  100. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  101. areaScrollPane
  102. .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  103. areaScrollPane.setPreferredSize(new Dimension(500, 275));
  104. //Create the output panel and it's layout objects
  105. GridBagLayout layout = new GridBagLayout(); //GridLayout(0, 1)
  106. JPanel outputNavPanel = new JPanel(layout);
  107. GridBagConstraints layoutConstraints = new GridBagConstraints();
  108. //Set the layout for the scroll pane and add it
  109. layoutConstraints.weightx = 1.0;
  110. layoutConstraints.weighty = 1.0;
  111. layoutConstraints.fill = GridBagConstraints.BOTH;
  112. layout.setConstraints(areaScrollPane, layoutConstraints);
  113. outputNavPanel.add(areaScrollPane);
  114. outputNavPanel.setBorder(BorderFactory.createCompoundBorder(
  115. BorderFactory.createCompoundBorder(BorderFactory
  116. .createTitledBorder("Output"), BorderFactory
  117. .createEmptyBorder(5, 5, 5, 5)), outputNavPanel
  118. .getBorder()));
  119. return outputNavPanel;
  120. }
  121. private JPanel createMainPanel() {
  122. // Add the URL text field and label
  123. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  124. cmURL.setText(CRNConnect.CM_URL);
  125. cmURL.setEditable(false);
  126. // Put together a panel for the URl
  127. JPanel cmURLPanel = new JPanel();
  128. cmURLPanel.add(new JLabel("Server URL:"));
  129. cmURLPanel.add(cmURL);
  130. //get dropdown box and button panel
  131. JPanel optionButtonPanel = createMainButtonPanel();
  132. //create the main panel and add the components
  133. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  134. // Add the panels to the mainPanel
  135. mainPanel.add(cmURLPanel);
  136. mainPanel.add(optionButtonPanel);
  137. return mainPanel;
  138. }
  139. private JPanel createMainButtonPanel() {
  140. // Create the button Panel
  141. JPanel buttonPanel = new JPanel();
  142. // Create and add the run option combo box
  143. String commandList[] = { COPY, COPYRENAME, MOVE, MOVERENAME };
  144. runOption = new JComboBox(commandList);
  145. runOption.setSelectedItem(null);
  146. runOption.addActionListener(new RunOptionHandler());
  147. buttonPanel.add(runOption, BorderLayout.WEST);
  148. // Create and add the package output type combo box
  149. BaseClassWrapper[] reportList = newCopyMoveReport
  150. .getListOfReports(connect);
  151. reportOption = new JComboBox(reportList);
  152. reportOption.setSelectedItem(null);
  153. reportOption.addActionListener(new reportSelectionHandler());
  154. buttonPanel.add(reportOption, BorderLayout.CENTER);
  155. // Create and add the Run Option Button
  156. copyMoveButton = new JButton("Run Option");
  157. copyMoveButton.addActionListener(new allButtonsHandler());
  158. buttonPanel.add(copyMoveButton, BorderLayout.EAST);
  159. return buttonPanel;
  160. }
  161. // handle menu bar buttons
  162. private class MenuHandler implements ActionListener {
  163. public void actionPerformed(ActionEvent e) {
  164. if (e.getActionCommand().startsWith("http://")) {
  165. connect.connectionChange(e.getActionCommand());
  166. }
  167. try {
  168. JMenuItem menuClicked = (JMenuItem) e.getSource();
  169. if (menuClicked.getText() == "Exit") {
  170. System.exit(0);
  171. }
  172. if (menuClicked.getText() == "About") {
  173. JOptionPane.showMessageDialog(((JMenuItem) e.getSource())
  174. .getParent(), "IBM Cognos Sample Application\n\n"
  175. + "Version 1.0.0\n"
  176. + "This application uses the IBM Cognos Software Development Kit",
  177. "About IBM Cognos Samples",
  178. JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
  179. "../Common/about.gif"));
  180. }
  181. if (menuClicked.getText().compareTo("Overview") == 0) {
  182. JFrame explainWindow = new JFrame(
  183. "Overview for Copy and Move Sample");
  184. File explainFile = new File(
  185. "Java_CopyMoveReportUI_Explain.html");
  186. if (!explainFile.exists()) {
  187. JOptionPane.showMessageDialog(null,
  188. "Explain file not found");
  189. return;
  190. }
  191. URL explainURL = new URL("file:///"
  192. + explainFile.getAbsolutePath());
  193. JEditorPane explainPane = new JEditorPane();
  194. explainPane.setPage(explainURL);
  195. explainPane.setEditable(false);
  196. JScrollPane explainScroll = new JScrollPane(explainPane,
  197. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  198. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  199. explainWindow.getContentPane().add(explainScroll);
  200. explainWindow.setSize(640, 480);
  201. explainWindow.setVisible(true);
  202. }
  203. } catch (Exception ex) {
  204. }
  205. }
  206. }
  207. // The following is the button event handler.
  208. private class allButtonsHandler implements ActionListener {
  209. public void actionPerformed(ActionEvent e) {
  210. if (!Logon.loggedIn(connect)) {
  211. try {
  212. sessionLogon.logon(connect);
  213. } catch (Exception logonException) {
  214. System.out.println("The error: "
  215. + logonException.getMessage());
  216. }
  217. }
  218. JButton buttonPressed = ((JButton) e.getSource());
  219. if (buttonPressed == copyMoveButton) {
  220. textAreaPane.setText("");
  221. String selectedReportName = null;
  222. if (chosenRunOption == null || chosenReport == null) {
  223. return;
  224. } else {
  225. selectedReportName = chosenReport.toString();
  226. }
  227. if (chosenRunOption.compareToIgnoreCase(COPY) == 0) {
  228. String copyResults = newCopyMoveReport.copyMoveReport(
  229. connect, chosenReport, chosenRunOption, null);
  230. runOption.setSelectedItem(null);
  231. reportOption.setSelectedItem(null);
  232. if (copyResults != null) {
  233. textAreaPane
  234. .setText("The report - "
  235. + selectedReportName
  236. + " has been successfully copied to the 'My Folders' in the content store."
  237. + "\n" + "The Event ID: " + copyResults);
  238. } else {
  239. textAreaPane
  240. .setText("The report - "
  241. + selectedReportName
  242. + " was not successfully copied to the target location.");
  243. }
  244. } else if (chosenRunOption.compareToIgnoreCase(COPYRENAME) == 0
  245. && chosenReport != null) {
  246. String inputValue = null;
  247. do {
  248. inputValue = JOptionPane
  249. .showInputDialog("Please type in the target report name \nSource report is '"
  250. + selectedReportName + "'");
  251. if (inputValue == null) {
  252. return;
  253. }
  254. } while (inputValue.length() == 0);
  255. String copyRenameResults = newCopyMoveReport
  256. .copyMoveReport(connect, chosenReport,
  257. chosenRunOption, inputValue);
  258. runOption.setSelectedItem(null);
  259. reportOption.setSelectedItem(null);
  260. if (copyRenameResults != null) {
  261. textAreaPane
  262. .setText("The report - "
  263. + selectedReportName
  264. + " has been successfully copied to the 'My Folders' in the content store"
  265. + "\nwith the different name - "
  266. + inputValue + "\n" + "The Event ID: "
  267. + copyRenameResults);
  268. } else {
  269. textAreaPane
  270. .setText("The report - "
  271. + selectedReportName
  272. + " was not successfully copied with a new name");
  273. }
  274. } else if (chosenRunOption.compareToIgnoreCase(MOVE) == 0
  275. && chosenReport != null) {
  276. String moveResults = newCopyMoveReport.copyMoveReport(
  277. connect, chosenReport, chosenRunOption, null);
  278. if (moveResults != null) {
  279. reportOption.removeAllItems();
  280. BaseClassWrapper[] newReportList = newCopyMoveReport
  281. .getListOfReports(connect);
  282. reportOption.setModel(new DefaultComboBoxModel(
  283. newReportList));
  284. reportOption.setSelectedItem(null);
  285. runOption.setSelectedItem(null);
  286. textAreaPane
  287. .setText("The report - "
  288. + selectedReportName
  289. + " has been successfully moved to the 'My Folders' in the content store."
  290. + "\n" + "The Event ID: " + moveResults);
  291. } else {
  292. runOption.setSelectedItem(null);
  293. reportOption.setSelectedItem(null);
  294. textAreaPane
  295. .setText("The report - "
  296. + selectedReportName
  297. + " was not successfully moved to the target location.");
  298. }
  299. } else if (chosenRunOption.compareToIgnoreCase(MOVERENAME) == 0) {
  300. String inputValue = null;
  301. do {
  302. inputValue = JOptionPane
  303. .showInputDialog("Please give the target report name: \nSource report is called '"
  304. + selectedReportName + "'");
  305. if (inputValue == null) {
  306. return;
  307. }
  308. } while (inputValue.length() == 0);
  309. String moveRenameResults = newCopyMoveReport
  310. .copyMoveReport(connect, chosenReport,
  311. chosenRunOption, inputValue);
  312. if (moveRenameResults != null) {
  313. reportOption.removeAllItems();
  314. BaseClassWrapper[] newReportList = newCopyMoveReport
  315. .getListOfReports(connect);
  316. reportOption.setModel(new DefaultComboBoxModel(
  317. newReportList));
  318. reportOption.setSelectedItem(null);
  319. runOption.setSelectedItem(null);
  320. textAreaPane
  321. .setText("The report - "
  322. + selectedReportName
  323. + " has been successfully moved to the 'My Folders' in the content store"
  324. + "\nwith the different name - "
  325. + inputValue + "\n" + "The Event ID: "
  326. + moveRenameResults);
  327. reportOption.removeItem(chosenReport);
  328. } else {
  329. runOption.setSelectedItem(null);
  330. reportOption.setSelectedItem(null);
  331. textAreaPane.setText("The report - "
  332. + selectedReportName
  333. + " was not successfully move with a new name");
  334. }
  335. }
  336. }
  337. }
  338. }
  339. // This is the report select combo box event handler.
  340. private class reportSelectionHandler implements ActionListener {
  341. public void actionPerformed(ActionEvent reportSelectedEvent) {
  342. textAreaPane.setText("");
  343. chosenReport = (BaseClassWrapper) reportOption.getSelectedItem();
  344. }
  345. }
  346. // This is the run option combo box event handler.
  347. private class RunOptionHandler implements ActionListener {
  348. public void actionPerformed(ActionEvent runOptionSelectedEvent) {
  349. textAreaPane.setText("");
  350. chosenRunOption = (String) runOption.getSelectedItem();
  351. }
  352. }
  353. // Create the main method to execute the application.
  354. public static void main(String args[]) {
  355. CRNConnect connection = new CRNConnect();
  356. connection.connectToCognosServer();
  357. sessionLogon = new Logon();
  358. String Output = "";
  359. while (!Logon.loggedIn(connection)) {
  360. Output = sessionLogon.logon(connection);
  361. if (!Logon.loggedIn(connection)) {
  362. int retry = JOptionPane.showConfirmDialog(null,
  363. "Login Failed. Please try again.", "Login Failed",
  364. JOptionPane.OK_CANCEL_OPTION);
  365. if (retry != JOptionPane.OK_OPTION) {
  366. System.exit(0);
  367. }
  368. }
  369. }
  370. frame = new CopyMoveReportUI("IBM Cognos Sample", connection);
  371. // Create a WindowAdapter so the application
  372. // is exited when the window is closed.
  373. frame.addWindowListener(new WindowAdapter() {
  374. public void windowClosing(WindowEvent e) {
  375. System.exit(0);
  376. }
  377. });
  378. frame.textAreaPane.setText(Output);
  379. // Set the size of the frame and display it.
  380. frame.setSize(880, 440);
  381. frame.setVisible(true);
  382. frame.setResizable(true);
  383. }
  384. }