PrintUI.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. * PrintUI.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. import com.cognos.developer.schemas.bibus._3.BaseClass;
  40. import com.cognos.developer.schemas.bibus._3.OrderEnum;
  41. import com.cognos.developer.schemas.bibus._3.PropEnum;
  42. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  43. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  44. import com.cognos.developer.schemas.bibus._3.Sort;
  45. // This Java class extends the JFrame class so that you can
  46. // display a window.
  47. public class PrintUI extends JFrame
  48. {
  49. private CRNConnect connect;
  50. private CSHandlers csHandler = new CSHandlers();
  51. // The following variables represent the dialog components.
  52. private JTextArea textAreaPane;
  53. private JTextField cmURL;
  54. private JButton sampleOptionButton;
  55. private JComboBox sampleOption;
  56. private JComboBox repSelectOption;
  57. private static Logon sessionLogon;
  58. private static final String GETAVAILABLEPRINTERS = "getAvailablePrinters";
  59. private static final String ADDPRINTER = "addPrinter";
  60. private static final String DELETEPRINTER = "deletePrinter";
  61. private static final String CHANGEPRINTERNAME = "changePrinterName";
  62. private static final String CHANGEPRINTERADDRESS = "changePrinterAddress";
  63. private static final String STARTPRINT = "startPrint";
  64. private static String selectedSampleOption = GETAVAILABLEPRINTERS;
  65. private static BaseClassWrapper bcReport = null;
  66. // This is the constructor.
  67. public PrintUI(String title, CRNConnect connection)
  68. {
  69. // Set the title of the frame, even before the variables are declared.
  70. super(title);
  71. connect = connection;
  72. addComponents();
  73. }
  74. // Add all components to the frame's panel.
  75. private void addComponents()
  76. {
  77. JMenuBar mBar = new JMenuBar();
  78. this.setJMenuBar(mBar);
  79. //declare menuItems
  80. JMenuItem exit;
  81. JMenuItem about;
  82. JMenuItem overview;
  83. //Add and populate the File menu.
  84. JMenu fileMenu = new JMenu("File");
  85. mBar.add(fileMenu);
  86. exit = new JMenuItem("Exit");
  87. fileMenu.add(exit);
  88. exit.addActionListener(new MenuHandler());
  89. //Add and populate the Help menu.
  90. JMenu helpMenu = new JMenu("Help");
  91. mBar.add(helpMenu);
  92. about = new JMenuItem("About");
  93. helpMenu.add(about);
  94. about.addActionListener(new MenuHandler());
  95. overview = new JMenuItem("Overview");
  96. helpMenu.add(overview);
  97. overview.addActionListener(new MenuHandler());
  98. JPanel mainPanel = new JPanel(new GridLayout(2, 0));
  99. // create a cmURL panel
  100. JPanel cmURLPanel = new JPanel();
  101. // Add the URL text field and label
  102. cmURL = new JTextField(CRNConnect.CM_URL.length() + 10);
  103. cmURL.setText(CRNConnect.CM_URL);
  104. cmURL.setEditable(false);
  105. cmURLPanel.add(new JLabel("Server URL:"), BorderLayout.WEST);
  106. cmURLPanel.add(cmURL, BorderLayout.EAST);
  107. // Create the button Panel
  108. JPanel buttonPanel = new JPanel();
  109. //Create and add the report output type combo box
  110. String optionType[] =
  111. {
  112. GETAVAILABLEPRINTERS,
  113. ADDPRINTER,
  114. DELETEPRINTER,
  115. CHANGEPRINTERNAME,
  116. CHANGEPRINTERADDRESS,
  117. STARTPRINT };
  118. sampleOption = new JComboBox(optionType);
  119. sampleOption.setSelectedItem(null);
  120. sampleOption.addActionListener(new sampleOptionSelectionHandler());
  121. buttonPanel.add(sampleOption, BorderLayout.WEST);
  122. // Create and add the select report combo box
  123. BaseClassWrapper listOfReports[] = getListOfReports(connect);
  124. repSelectOption = new JComboBox(listOfReports);
  125. repSelectOption.setSelectedItem(null);
  126. repSelectOption.addActionListener(new ReportSelectionHandler());
  127. buttonPanel.add(repSelectOption, BorderLayout.CENTER);
  128. // Create and add the Button
  129. sampleOptionButton = new JButton("Run Option");
  130. sampleOptionButton.addActionListener(new allButtonsHandler());
  131. buttonPanel.add(sampleOptionButton, BorderLayout.EAST);
  132. // Add the status text pane.
  133. textAreaPane = new JTextArea();
  134. // Add the panels to the mainPanel
  135. mainPanel.add(cmURLPanel);
  136. mainPanel.add(buttonPanel);
  137. mainPanel.add(textAreaPane);
  138. //Add the ScrollPane to outputPanel
  139. JScrollPane areaScrollPane = new JScrollPane(textAreaPane);
  140. areaScrollPane.setVerticalScrollBarPolicy(
  141. JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  142. areaScrollPane.setPreferredSize(new Dimension(300, 275));
  143. areaScrollPane.setBorder(
  144. BorderFactory.createCompoundBorder(
  145. BorderFactory.createCompoundBorder(
  146. BorderFactory.createTitledBorder("Output"),
  147. BorderFactory.createEmptyBorder(5, 5, 5, 5)),
  148. areaScrollPane.getBorder()));
  149. JPanel outputPanel = new JPanel(new GridLayout(0, 1));
  150. outputPanel.add(areaScrollPane);
  151. JPanel panel = new JPanel(new BorderLayout());
  152. panel.add(mainPanel, BorderLayout.NORTH);
  153. panel.add(outputPanel);
  154. setContentPane(panel);
  155. }
  156. private class MenuHandler implements ActionListener
  157. {
  158. public void actionPerformed(ActionEvent e)
  159. {
  160. if (e.getActionCommand().startsWith("http://"))
  161. {
  162. connect.connectionChange(e.getActionCommand());
  163. }
  164. try
  165. {
  166. JMenuItem menuClicked = (JMenuItem)e.getSource();
  167. if (menuClicked.getText() == "Exit")
  168. {
  169. System.exit(0);
  170. }
  171. if (menuClicked.getText() == "About")
  172. {
  173. JOptionPane.showMessageDialog(
  174. ((JMenuItem)e.getSource()).getParent(),
  175. "IBM Cognos Sample Application\n\n"
  176. + "Version 1.0.0\n"
  177. + "This application uses the IBM Cognos Software Development Kit",
  178. "About IBM Cognos Samples",
  179. JOptionPane.INFORMATION_MESSAGE,
  180. new ImageIcon("../Common/about.gif"));
  181. }
  182. if (menuClicked.getText().compareTo("Overview") == 0)
  183. {
  184. JFrame explainWindow =
  185. new JFrame("Overview for Print Report Sample");
  186. File explainFile = new File("Java_PrintUI_Explain.html");
  187. if (! explainFile.exists())
  188. {
  189. JOptionPane.showMessageDialog(null, "Explain file not found");
  190. return;
  191. }
  192. URL explainURL =
  193. new URL("file:///" + explainFile.getAbsolutePath());
  194. JEditorPane explainPane = new JEditorPane();
  195. explainPane.setPage(explainURL);
  196. explainPane.setEditable(false);
  197. JScrollPane explainScroll =
  198. new JScrollPane(
  199. explainPane,
  200. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  201. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  202. explainWindow.getContentPane().add(explainScroll);
  203. explainWindow.setSize(640, 480);
  204. explainWindow.setVisible(true);
  205. }
  206. }
  207. catch (Exception ex)
  208. {}
  209. }
  210. }
  211. // The following is the button event handler.
  212. // Note: A SWITCH statement cannot be used here because we are comparing
  213. // objects.
  214. private class allButtonsHandler implements ActionListener
  215. {
  216. public void actionPerformed(ActionEvent e)
  217. {
  218. if (!Logon.loggedIn(connect))
  219. {
  220. try
  221. {
  222. sessionLogon.logon(connect);
  223. }
  224. catch (Exception logonException)
  225. {
  226. textAreaPane.setText("");
  227. textAreaPane.append("Login Failed. Please try again.\n");
  228. return;
  229. }
  230. }
  231. JButton buttonPressed = ((JButton)e.getSource());
  232. String output = new String();
  233. if (buttonPressed == sampleOptionButton)
  234. {
  235. Print printObj = new Print();
  236. if (selectedSampleOption.compareTo(GETAVAILABLEPRINTERS) == 0)
  237. {
  238. BaseClass[] printers =
  239. printObj.getAvailablePrinters(connect);
  240. if (printers.length <= 0)
  241. {
  242. textAreaPane.setText("");
  243. textAreaPane.append(
  244. "There are no printers available.\n");
  245. return;
  246. }
  247. else
  248. {
  249. output = "List of Available Printers:\n\n";
  250. for (int i = 0; i < printers.length; i++)
  251. {
  252. output += "\tName: "
  253. + printers[i].getDefaultName().getValue()
  254. + "\n";
  255. output += "\tPath: "
  256. + printers[i].getSearchPath().getValue()
  257. + "\n\n";
  258. }
  259. }
  260. }
  261. else if (selectedSampleOption.compareTo(ADDPRINTER) == 0)
  262. {
  263. String printerName =
  264. JOptionPane.showInputDialog(
  265. "Please enter a name for the new printer.");
  266. String printerAddress =
  267. JOptionPane.showInputDialog(
  268. "Please enter a valid Network Address for the new printer.");
  269. if ((printerName != null) && (printerAddress != null))
  270. {
  271. output =
  272. printObj.addPrinter(
  273. connect,
  274. printerName,
  275. printerAddress);
  276. }
  277. else
  278. {
  279. output = "Action cancelled.";
  280. }
  281. }
  282. else if (selectedSampleOption.compareTo(DELETEPRINTER) == 0)
  283. {
  284. BaseClass[] printers =
  285. printObj.getAvailablePrinters(connect);
  286. if (printers.length <= 0)
  287. {
  288. textAreaPane.setText("");
  289. textAreaPane.append(
  290. "There are no printers available.\n");
  291. return;
  292. }
  293. else
  294. {
  295. String possibleValues[] = new String[printers.length];
  296. for (int i = 0; i < printers.length; i++)
  297. {
  298. possibleValues[i] =
  299. printers[i].getDefaultName().getValue();
  300. }
  301. //Display a prompt asking the user to select a printer.
  302. Object selectedValue =
  303. JOptionPane.showInputDialog(
  304. null,
  305. "Please choose a printer",
  306. "Choose Printer",
  307. JOptionPane.INFORMATION_MESSAGE,
  308. null,
  309. possibleValues,
  310. possibleValues[0]);
  311. String printerName=(String)selectedValue;
  312. int proceed =
  313. JOptionPane.showConfirmDialog(
  314. null,
  315. "The printer '"
  316. + printerName
  317. + "' will be deleted.");
  318. if (proceed == JOptionPane.YES_OPTION)
  319. {
  320. output =
  321. printObj.deletePrinter(
  322. connect,
  323. printerName);
  324. }
  325. else
  326. {
  327. output = "Action cancelled.";
  328. }
  329. }
  330. }
  331. else if (
  332. selectedSampleOption.compareTo(CHANGEPRINTERNAME) == 0)
  333. {
  334. BaseClass[] printers =
  335. printObj.getAvailablePrinters(connect);
  336. if (printers.length <= 0)
  337. {
  338. textAreaPane.setText("");
  339. textAreaPane.append(
  340. "There are no printers available.\n");
  341. return;
  342. }
  343. else
  344. {
  345. String possibleValues[] = new String[printers.length];
  346. for (int i = 0; i < printers.length; i++)
  347. {
  348. possibleValues[i] =
  349. printers[i].getDefaultName().getValue();
  350. }
  351. //Display a prompt asking the user to select a printer.
  352. Object selectedValue =
  353. JOptionPane.showInputDialog(
  354. null,
  355. "Please choose a printer",
  356. "Choose Printer",
  357. JOptionPane.INFORMATION_MESSAGE,
  358. null,
  359. possibleValues,
  360. possibleValues[0]);
  361. String printerName=(String)selectedValue;
  362. String newPrinterName =
  363. JOptionPane.showInputDialog(
  364. "Please enter a new printer name for "
  365. + printerName);
  366. if (newPrinterName != null)
  367. {
  368. output =
  369. printObj.changePrinterName(
  370. connect,
  371. printerName,
  372. newPrinterName);
  373. }
  374. else
  375. {
  376. output = "Action cancelled.";
  377. }
  378. }
  379. }
  380. else if (
  381. selectedSampleOption.compareTo(CHANGEPRINTERADDRESS) == 0)
  382. {
  383. BaseClass[] printers =
  384. printObj.getAvailablePrinters(connect);
  385. if (printers.length <= 0)
  386. {
  387. textAreaPane.setText("");
  388. textAreaPane.append(
  389. "There are no printers available.\n");
  390. return;
  391. }
  392. else
  393. {
  394. String possibleValues[] = new String[printers.length];
  395. for (int i = 0; i < printers.length; i++)
  396. {
  397. possibleValues[i] =
  398. printers[i].getDefaultName().getValue();
  399. }
  400. //Display a prompt asking the user to select a printer.
  401. Object selectedValue =
  402. JOptionPane.showInputDialog(
  403. null,
  404. "Please choose a printer",
  405. "Choose Printer",
  406. JOptionPane.INFORMATION_MESSAGE,
  407. null,
  408. possibleValues,
  409. possibleValues[0]);
  410. String printerName=(String)selectedValue;
  411. String newPrinterAddress =
  412. JOptionPane.showInputDialog(
  413. "Please enter a new printer address for "
  414. + printerName);
  415. if (newPrinterAddress != null)
  416. {
  417. output =
  418. printObj.changePrinterAddress(
  419. connect,
  420. printerName,
  421. newPrinterAddress);
  422. }
  423. else
  424. {
  425. output = "Action cancelled";
  426. }
  427. }
  428. }
  429. else if (selectedSampleOption.compareTo(STARTPRINT) == 0)
  430. {
  431. try
  432. {
  433. BaseClass[] printers =
  434. printObj.getAvailablePrinters(connect);
  435. String possibleValues[] = new String[printers.length];
  436. String printerPath = new String();
  437. String reportPath = new String();
  438. for (int i = 0; i < printers.length; i++)
  439. {
  440. possibleValues[i] =
  441. printers[i].getDefaultName().getValue();
  442. }
  443. //Display a prompt asking the user to select a printer.
  444. Object selectedValue =
  445. JOptionPane.showInputDialog(
  446. null,
  447. "Please choose a printer",
  448. "Choose Printer",
  449. JOptionPane.INFORMATION_MESSAGE,
  450. null,
  451. possibleValues,
  452. possibleValues[0]);
  453. //Get the searchPath for the selected printer.
  454. for (int i = 0; i < printers.length; i++)
  455. {
  456. if (((String)selectedValue)
  457. .equals(
  458. printers[i].getDefaultName().getValue()))
  459. {
  460. printerPath =
  461. printers[i].getSearchPath().getValue();
  462. break;
  463. }
  464. }
  465. printObj.print(
  466. connect,
  467. bcReport,
  468. printerPath,
  469. null);
  470. }
  471. catch (Exception ex)
  472. {
  473. System.out.println(ex.getMessage());
  474. output =
  475. "Print Report:\nAn error occurred\nMake sure a "
  476. + "Report Name is selected and IBM Cognos is running";
  477. }
  478. }
  479. }
  480. if (output.compareTo("") != 0)
  481. {
  482. textAreaPane.setText("");
  483. textAreaPane.append(output);
  484. }
  485. }
  486. }
  487. private class ReportSelectionHandler implements ActionListener
  488. {
  489. public void actionPerformed(ActionEvent repSelectedEvent)
  490. {
  491. bcReport = (BaseClassWrapper)repSelectOption.getSelectedItem();
  492. }
  493. }
  494. private class sampleOptionSelectionHandler implements ActionListener
  495. {
  496. public void actionPerformed(ActionEvent optionSelectedEvent)
  497. {
  498. selectedSampleOption = (String)sampleOption.getSelectedItem();
  499. }
  500. }
  501. // This is a method for retrieving a list of the available reports to run
  502. protected BaseClassWrapper[] getListOfReports(CRNConnect connection)
  503. {
  504. BaseClassWrapper reportAndQueryList[] = null;
  505. BaseClass reports[] = new BaseClass[0];
  506. BaseClass queries[] = new BaseClass[0];
  507. int reportAndQueryIndex = 0;
  508. int reportIndex = 0;
  509. int queryIndex = 0;
  510. if (connection == null)
  511. {
  512. System.out.println(
  513. "Invalid parameter passed to getListOfReports()\n");
  514. return null;
  515. }
  516. PropEnum props[] =
  517. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  518. Sort sortOptions[] = { new Sort()};
  519. sortOptions[0].setOrder(OrderEnum.ascending);
  520. sortOptions[0].setPropName(PropEnum.defaultName);
  521. if (!Logon.loggedIn(connect))
  522. {
  523. try
  524. {
  525. sessionLogon.logon(connect);
  526. }
  527. catch (Exception logonException)
  528. {}
  529. }
  530. try
  531. {
  532. reports =
  533. connection.getCMService().query(
  534. new SearchPathMultipleObject("/content//report"),
  535. props,
  536. sortOptions,
  537. new QueryOptions());
  538. queries =
  539. connection.getCMService().query(
  540. new SearchPathMultipleObject("/content//query"),
  541. props,
  542. sortOptions,
  543. new QueryOptions());
  544. }
  545. catch (java.rmi.RemoteException remoteEx)
  546. {
  547. System.out.println("Caught Remote Exception:\n");
  548. remoteEx.printStackTrace();
  549. }
  550. reportAndQueryList = new BaseClassWrapper[reports.length + queries.length];
  551. if ((reports != null) && (reports.length > 0))
  552. {
  553. for (reportIndex = 0; reportIndex < reports.length; reportIndex++)
  554. {
  555. reportAndQueryList[reportAndQueryIndex++] = new BaseClassWrapper(reports[reportIndex]);
  556. }
  557. }
  558. if ((queries != null) && (queries.length > 0))
  559. {
  560. for (queryIndex = 0; queryIndex < queries.length; queryIndex++)
  561. {
  562. reportAndQueryList[reportAndQueryIndex++] =
  563. new BaseClassWrapper(queries[queryIndex]);
  564. }
  565. }
  566. return reportAndQueryList;
  567. }
  568. // Create the main method to execute the application.
  569. public static void main(String args[])
  570. {
  571. CRNConnect connection = new CRNConnect();
  572. connection.connectToCognosServer();
  573. sessionLogon = new Logon();
  574. String output = "";
  575. while (!Logon.loggedIn(connection))
  576. {
  577. output = sessionLogon.logon(connection);
  578. if (!Logon.loggedIn(connection))
  579. {
  580. int retry =
  581. JOptionPane.showConfirmDialog(
  582. null,
  583. "Login Failed. Please try again.",
  584. "Login Failed",
  585. JOptionPane.OK_CANCEL_OPTION);
  586. if (retry != JOptionPane.OK_OPTION)
  587. {
  588. System.exit(0);
  589. }
  590. }
  591. }
  592. PrintUI frame = new PrintUI("IBM Cognos Sample", connection);
  593. // Create a WindowAdapter so the application
  594. // is exited when the window is closed.
  595. frame.addWindowListener(new WindowAdapter()
  596. {
  597. public void windowClosing(WindowEvent e)
  598. {
  599. System.exit(0);
  600. }
  601. });
  602. frame.textAreaPane.setText(output);
  603. // Set the size of the frame and display it.
  604. frame.setSize(850, 440);
  605. frame.setVisible(true);
  606. frame.setResizable(true);
  607. }
  608. }