ReportWizardDialog.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * ReportWizardDialog.java
  10. *
  11. * Copyright (C) 2005 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. * Description: This code sample creates a dialog box for adding
  15. * columns to a report.
  16. */
  17. import java.awt.Container;
  18. import java.awt.Frame;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.WindowAdapter;
  22. import java.awt.event.WindowEvent;
  23. import java.util.Vector;
  24. import javax.swing.JButton;
  25. import javax.swing.JComboBox;
  26. import javax.swing.JDialog;
  27. import javax.swing.JLabel;
  28. import javax.swing.JList;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JScrollPane;
  31. public class ReportWizardDialog extends JDialog
  32. {
  33. // Define the components of the dialog box.
  34. private JLabel tablesLabel;
  35. private JComboBox tablesComboBox;
  36. private JLabel columnsLabel;
  37. private JList availableColumnsList;
  38. private JLabel selectedLabel;
  39. private JList selectedColumnsList;
  40. private JButton okButton;
  41. private JButton cancelButton;
  42. private JButton addButton;
  43. private JButton deleteButton;
  44. private JButton modifyColumnTitleButton;
  45. // Define the vector that will contain the tables and columns for
  46. // the metadata in the package.
  47. public Vector m_MetaData;
  48. // Define the vector that will contain the column names to be displayed
  49. // in the list of selected columns.
  50. public Vector m_selectedColumns;
  51. private Vector m_originalSelectedColumns;
  52. // Define the vector that will contain the fully qualified names of the
  53. // columns used to create the report.
  54. // Each column name in this vector is associated with a column name in
  55. // the m_selectedColumns vector.
  56. public Vector m_qualifiedColumns;
  57. public Vector m_columnTitles;
  58. public Vector m_columnTitleToChange;
  59. public Vector m_columnTitleNewName;
  60. boolean m_bShowModifyTitleButton;
  61. public ReportWizardDialog(
  62. Frame owner,
  63. String title,
  64. boolean modal,
  65. Vector md,
  66. Vector displayColumns,
  67. Vector fullNameColumns,
  68. Vector columnTitles,
  69. Vector columnTitleExisting,
  70. Vector columnTitleNew,
  71. boolean bShowModifyTitleButton)
  72. {
  73. super(owner, title, modal);
  74. // Initialize the arrays that contain the table and column data.
  75. m_selectedColumns = displayColumns;
  76. m_originalSelectedColumns = (Vector)displayColumns.clone();
  77. m_MetaData = md;
  78. m_qualifiedColumns = fullNameColumns;
  79. m_columnTitles = columnTitles;
  80. m_columnTitleToChange = columnTitleExisting;
  81. m_columnTitleNewName = columnTitleNew;
  82. m_bShowModifyTitleButton = bShowModifyTitleButton;
  83. // Place all components in the window and given each component
  84. // an initial value.
  85. buildDialogWindow(getContentPane());
  86. addWindowListener(new WindowAdapter()
  87. {
  88. public void windowClosing(WindowEvent event)
  89. {
  90. cancelButtonClicked();
  91. }
  92. });
  93. setSize(580, 430);
  94. }
  95. /**
  96. * This Java method adds the components to the user interface.
  97. *
  98. * @param aContainer
  99. * Specifies a container for the components, such as the
  100. * content pane of the window.
  101. */
  102. private void buildDialogWindow(Container aContainer)
  103. {
  104. // Specify that a layout manager is not needed to determine how
  105. // components are sized and positioned within the container.
  106. aContainer.setLayout(null);
  107. // Create a label that introduces the tables
  108. // and set its location, size, and contents.
  109. tablesLabel = new JLabel("Tables:");
  110. tablesLabel.setLocation(10, 1);
  111. tablesLabel.setSize(80, 30);
  112. aContainer.add(tablesLabel);
  113. // Create a vector that contains the tables from
  114. // the metadata in the package.
  115. Vector tables = new Vector();
  116. for (int i = 0; i < m_MetaData.size(); i++)
  117. {
  118. tables.add(
  119. i,
  120. ((MetaData)m_MetaData.elementAt(i)).getQuerySubject());
  121. }
  122. // Create a combo box and set its location, size, and the event handler
  123. // to respond to the user's actions, such as a mouse click.
  124. tablesComboBox = new JComboBox(tables);
  125. tablesComboBox.setLocation(10, 30);
  126. tablesComboBox.setSize(250, 25);
  127. tablesComboBox.addActionListener(new tablesComboBoxListener());
  128. aContainer.add(tablesComboBox);
  129. // Create a list of available columns from the metadata in the package.
  130. // Set the size, location, and scrolling of the list.
  131. // Initialize the list with the columns from the first table.
  132. MetaData md = (MetaData)m_MetaData.elementAt(0);
  133. availableColumnsList = new JList(md.getQueryItem());
  134. JScrollPane scrollPane = new JScrollPane(availableColumnsList);
  135. scrollPane.setLocation(10, 100);
  136. scrollPane.setSize(200, 230);
  137. aContainer.add(scrollPane);
  138. // Create a label that introduces the available columns and set
  139. // its location, size, and contents.
  140. columnsLabel = new JLabel("Available columns:");
  141. columnsLabel.setLocation(10, 70);
  142. columnsLabel.setSize(120, 30);
  143. aContainer.add(columnsLabel);
  144. // Create a list of selected columns and set the size and location
  145. // of the list.
  146. selectedColumnsList = new JList(m_selectedColumns);
  147. JScrollPane scrollPane2 = new JScrollPane(selectedColumnsList);
  148. scrollPane2.setLocation(325, 100);
  149. scrollPane2.setSize(200, 230);
  150. aContainer.add(scrollPane2);
  151. // Create a label that introduces the selected columns and set
  152. // its location, size, and contents.
  153. selectedLabel = new JLabel("Selected columns:");
  154. selectedLabel.setLocation(325, 70);
  155. selectedLabel.setSize(120, 30);
  156. aContainer.add(selectedLabel);
  157. // Create an Add button and set
  158. // its location, size, and the event handler to respond to the user's
  159. // actions, such as a mouse click.
  160. addButton = new JButton(">>>");
  161. addButton.setLocation(230, 150);
  162. addButton.setSize(75, 30);
  163. addButton.addActionListener(new addButtonListener());
  164. aContainer.add(addButton);
  165. // Create a Delete button and set
  166. // its location, size, and the event handler to respond to the user's
  167. // actions, such as a mouse click.
  168. deleteButton = new JButton("<<<");
  169. deleteButton.setLocation(230, 220);
  170. deleteButton.setSize(75, 30);
  171. deleteButton.addActionListener(new deleteButtonListener());
  172. aContainer.add(deleteButton);
  173. // Create a Modify Column Title button and set
  174. // its location, size, and the event handler to respond to the user's
  175. // actions, such as a mouse click.
  176. if (m_bShowModifyTitleButton)
  177. {
  178. modifyColumnTitleButton = new JButton("Modify Column Title");
  179. modifyColumnTitleButton.setLocation(325, 30);
  180. modifyColumnTitleButton.setSize(150, 30);
  181. modifyColumnTitleButton.addActionListener(
  182. new modifyColumnTitleButtonListener());
  183. aContainer.add(modifyColumnTitleButton);
  184. }
  185. // Create an OK button and set
  186. // its location, size, and the event handler to respond to the user's
  187. // actions, such as a mouse click.
  188. okButton = new JButton("Ok");
  189. okButton.setLocation(325, 350);
  190. okButton.setSize(85, 30);
  191. okButton.addActionListener(new okButtonListener());
  192. aContainer.add(okButton);
  193. // Create a Cancel button and set
  194. // its location, size, and the event handler to respond to the user's
  195. // actions, such as a mouse click.
  196. cancelButton = new JButton("Cancel");
  197. cancelButton.setLocation(440, 350);
  198. cancelButton.setSize(85, 30);
  199. cancelButton.addActionListener(new cancelButtonListener());
  200. aContainer.add(cancelButton);
  201. }
  202. /**
  203. * This Java class is an event handler that runs when a user selects a table.
  204. * It adds a column to the array of available columns for each column in
  205. * the table.
  206. **/
  207. private class tablesComboBoxListener implements ActionListener
  208. {
  209. public void actionPerformed(ActionEvent e)
  210. {
  211. String selectedTable = (String)tablesComboBox.getSelectedItem();
  212. System.out.println("Selected item=" + selectedTable);
  213. for (int i = 0; i < m_MetaData.size(); i++)
  214. {
  215. String findTable =
  216. ((MetaData)m_MetaData.elementAt(i)).getQuerySubject();
  217. if (selectedTable.equals(findTable))
  218. {
  219. availableColumnsList.setListData(
  220. ((MetaData)m_MetaData.elementAt(i)).getQueryItem());
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * This Java class is an event handler that runs when a user clicks the Add
  227. * button. It adds the selected columns to the array of available columns.
  228. **/
  229. private class addButtonListener implements ActionListener
  230. {
  231. public void actionPerformed(ActionEvent e)
  232. {
  233. String selectedColumn =
  234. (String)availableColumnsList.getSelectedValue();
  235. String selectedTable = (String)tablesComboBox.getSelectedItem();
  236. if (selectedColumn == null)
  237. {
  238. return;
  239. }
  240. m_selectedColumns.add(selectedColumn);
  241. m_columnTitles.add(selectedColumn);
  242. selectedColumnsList.setListData(m_selectedColumns);
  243. // When we add to the display list, also add to the fullName list.
  244. for (int i = 0; i < m_MetaData.size(); i++)
  245. {
  246. String findTable =
  247. ((MetaData)m_MetaData.elementAt(i)).getQuerySubject();
  248. // This is a matching table.
  249. if (selectedTable.equals(findTable))
  250. {
  251. // Find a matching column.
  252. Vector columnsList =
  253. ((MetaData)m_MetaData.elementAt(i)).getQueryItem();
  254. for (int j = 0; j < columnsList.size(); j++)
  255. {
  256. String findColumn = (String)columnsList.elementAt(j);
  257. if (selectedColumn.equals(findColumn))
  258. {
  259. MetaData m_MetaDataElement =
  260. (MetaData)m_MetaData.elementAt(i);
  261. String columnFullName =
  262. (String)m_MetaDataElement
  263. .getFullNameQueryItems()
  264. .elementAt(
  265. j);
  266. m_qualifiedColumns.add(columnFullName);
  267. return;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. /**
  275. * This Java class is an event handler that runs when a user clicks the
  276. * Delete button. It removes the full names of the selected columns
  277. * from the array of columns used to create the report.
  278. **/
  279. private class deleteButtonListener implements ActionListener
  280. {
  281. public void actionPerformed(ActionEvent e)
  282. {
  283. String selectedColumn =
  284. (String)selectedColumnsList.getSelectedValue();
  285. if (selectedColumn != null)
  286. {
  287. int index = m_selectedColumns.indexOf(selectedColumn);
  288. m_selectedColumns.remove(selectedColumn);
  289. m_qualifiedColumns.removeElementAt(index);
  290. m_columnTitles.removeElementAt(index);
  291. selectedColumnsList.setListData(m_selectedColumns);
  292. }
  293. }
  294. }
  295. /**
  296. * This Java class is an event handler that runs when a user clicks the
  297. * OK button. The event handler calls a Java method that releases the
  298. * resources for the dialog box.
  299. **/
  300. private class okButtonListener implements ActionListener
  301. {
  302. public void actionPerformed(ActionEvent e)
  303. {
  304. okButtonClicked();
  305. }
  306. }
  307. /**
  308. * This Java class is an event handler that runs when a user selects the
  309. * title of a column. It opens an input dialog box that prompts user to
  310. * enter a new title for the column, and then removed the old title from
  311. * the array of column titles and adds the new title.
  312. **/
  313. private class modifyColumnTitleButtonListener implements ActionListener
  314. {
  315. public void actionPerformed(ActionEvent e)
  316. {
  317. String selectedColumn =
  318. (String)selectedColumnsList.getSelectedValue();
  319. if (selectedColumn != null)
  320. {
  321. int index = m_selectedColumns.indexOf(selectedColumn);
  322. String columnTitleToChange = (String)m_columnTitles.get(index);
  323. String columnTitleNewName = null;
  324. do
  325. {
  326. columnTitleNewName =
  327. JOptionPane.showInputDialog(
  328. "Please enter a new column "
  329. + "name for column < "
  330. + columnTitleToChange
  331. + " >");
  332. }
  333. while (columnTitleNewName == null);
  334. m_columnTitleToChange.add(columnTitleToChange);
  335. m_columnTitleNewName.add(columnTitleNewName);
  336. int index2 = m_columnTitles.indexOf(columnTitleToChange);
  337. m_columnTitles.remove(index2);
  338. m_columnTitles.add(index2, columnTitleNewName);
  339. }
  340. }
  341. }
  342. /**
  343. * This Java class is an event handler that runs when a user clicks the Cancel button.
  344. * The event handler calls a Java method that cancels the current action.
  345. **/
  346. private class cancelButtonListener implements ActionListener
  347. {
  348. public void actionPerformed(ActionEvent e)
  349. {
  350. cancelButtonClicked();
  351. }
  352. }
  353. /**
  354. * This Java method releases all screen resources used by this
  355. * dialog box, its subcomponents, and all of its children.
  356. **/
  357. private void okButtonClicked()
  358. {
  359. dispose();
  360. }
  361. /**
  362. * This Java method removes the elements in the array that contains the
  363. * fully qualified names of the columns used to create the report and
  364. * releases all screen resources used by this dialog box, its subcomponents,
  365. * and all of its children.
  366. **/
  367. private void cancelButtonClicked()
  368. {
  369. //restore the orginal columns to clear any changes that might have been made
  370. m_qualifiedColumns.removeAllElements();
  371. m_qualifiedColumns.addAll((Vector)m_originalSelectedColumns.clone());
  372. dispose();
  373. }
  374. }