View Javadoc
1 /*** 2 * ReportDialog.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 08.11.2002 pko creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency.gui; 31 32 import info.clearthought.layout.TableLayout; 33 import java.awt.*; 34 import java.awt.event.ActionEvent; 35 import java.awt.event.ActionListener; 36 import javax.swing.*; 37 import javax.swing.border.Border; 38 39 import ch.elca.dependency.core.DependencyModel; 40 import ch.elca.dependency.report.Report; 41 import ch.elca.dependency.report.ReportFormatter; 42 import ch.elca.dependency.report.ReportManager; 43 44 /*** 45 * The class <code>ReportDialog</code> here is a dialog, which can be 46 * used to generate Reports. 47 * 48 * @author Pawel Kowalski 49 * @version 1.0-beta 50 * 51 * @tbd provide a superclass because the structure of the Jframes is mostly the same. 52 */ 53 public class ReportDialog extends JDialog { 54 55 //*********************************************************************************/ 56 // constants, every ReportDialog will use. 57 //*********************************************************************************/ 58 59 private static final boolean IS_MODAL = false; 60 private static final boolean IS_RESIZABLE = false; 61 private static final int ROW_HEIGHT = 30; 62 private static final int INSET = 10; 63 private static final int VERTICAL_INSET = 10; 64 private static final int DIALOG_WIDTH = 300; 65 private static final int TEXT_FRAME_WIDTH = 520; 66 private static final int TEXT_FRAME_HEIGHT = 600; 67 68 private static final String DIALOG_NAME = "Dependency Reports"; 69 private static final String FORMATTERS = "Choose a report formatter"; 70 private static final String REPORT_INFOS = "Choose report infos"; 71 private static final String USING_DEFAULT_FORMATTER = "could not set formatter, using default formatter"; 72 private static final String OK = "Report"; 73 private static final String REPORT = "Report"; 74 private static final String CLOSE = "Close"; 75 76 //*********************************************************************************/ 77 // some gui elements. 78 //*********************************************************************************/ 79 80 private JDialog m_textFrame = null; 81 private JButton m_okButton = null; 82 private JButton m_closeButton = null; 83 private ButtonGroup m_formatButtonGroup = null; 84 private JRadioButton[] m_formatButtons = null; 85 private JCheckBox[] m_reportInfoButtons = null; 86 87 //*********************************************************************************/ 88 // some vars associated with an instance of DPToolErrorDialog. 89 //*********************************************************************************/ 90 91 private int m_dialogHeight = 0; 92 private int m_reportInfoRowSize = 0; 93 private int m_formatRowSize = 0; 94 private Class[] m_reportInfoClasses = null; 95 private Class[] m_reportFormatterClasses = null; 96 97 //*********************************************************************************/ 98 // constructor 99 //*********************************************************************************/ 100 101 /*** 102 * Creates a new <code>ReportDialog</code> instance. 103 * 104 * @param owner a <code>Frame</code> value 105 */ 106 public ReportDialog(Frame owner) { 107 super(owner, IS_MODAL); 108 109 initStructure(); 110 initDialog(); 111 setContentPane(makeGUI()); 112 initListeners(); 113 } 114 115 //*********************************************************************************/ 116 // init methods 117 //*********************************************************************************/ 118 119 /*** 120 * Initialize structure of this Dialog. 121 */ 122 private void initStructure() { 123 m_reportFormatterClasses = ReportManager.getAvailableFormatters(); 124 m_reportInfoClasses = ReportManager.getAvailableReportInfos(); 125 } 126 127 /*** 128 * Initialize this Dialog. 129 */ 130 private void initDialog() { 131 132 m_formatRowSize = (1 + m_reportFormatterClasses.length) 133 * ROW_HEIGHT + INSET; 134 m_reportInfoRowSize = (1 + m_reportInfoClasses.length) 135 * ROW_HEIGHT + INSET; 136 m_dialogHeight = m_formatRowSize + m_reportInfoRowSize + 3 137 * ROW_HEIGHT + 1 * INSET; 138 139 setResizable(IS_RESIZABLE); 140 setSize(DIALOG_WIDTH, m_dialogHeight); 141 setTitle(DIALOG_NAME); 142 143 Toolkit tollkit = Toolkit.getDefaultToolkit(); 144 int screenWidth = tollkit.getScreenSize().width; 145 int screenHeight = tollkit.getScreenSize().height; 146 147 setLocation((screenWidth) / 2, 148 (screenHeight - m_dialogHeight) / 2); 149 } 150 151 /*** 152 * Describe <code>makeGUI</code> method here. 153 * 154 * @return a <code>Container</code> value 155 */ 156 private Container makeGUI() { 157 158 double[] columns = {-2.0, -1.0}; 159 double[] rows = {-2.0, m_formatRowSize, VERTICAL_INSET, m_reportInfoRowSize, VERTICAL_INSET, ROW_HEIGHT}; 160 double[][] table = {columns, rows}; 161 162 // content pane 163 // 164 JPanel contentPane = new JPanel(); 165 contentPane.setLayout(new TableLayout(table)); 166 contentPane.setBorder(BorderFactory.createEmptyBorder(INSET, VERTICAL_INSET, 167 INSET, VERTICAL_INSET)); 168 169 // prepare some borders 170 // 171 Border etchedBorder = BorderFactory.createEtchedBorder(); 172 Border emptyBorder = BorderFactory.createEmptyBorder(INSET, INSET, 173 INSET, INSET); 174 Border compoundBorder = null; 175 Border titledBorder = null; 176 177 // report formatters, 1'st row 178 // 179 JPanel formatButtonsPanel 180 = new JPanel(new GridLayout(m_reportFormatterClasses.length, 1)); 181 titledBorder = BorderFactory.createTitledBorder(etchedBorder, FORMATTERS); 182 compoundBorder = BorderFactory.createCompoundBorder(titledBorder, emptyBorder); 183 formatButtonsPanel.setBorder(compoundBorder); 184 m_formatButtonGroup = new ButtonGroup(); 185 m_formatButtons = new JRadioButton[m_reportFormatterClasses.length]; 186 String formatterName = ""; 187 for (int i = 0; i < m_reportFormatterClasses.length; i++) { 188 formatterName = m_reportFormatterClasses[i].getName(); 189 formatterName = formatterName.substring(formatterName.lastIndexOf('.') + 1); 190 m_formatButtons[i] 191 = new JRadioButton(formatterName); 192 m_formatButtonGroup.add(m_formatButtons[i]); 193 formatButtonsPanel.add(m_formatButtons[i]); 194 } 195 m_formatButtons[0].setSelected(true); 196 contentPane.add(formatButtonsPanel, "1, 1, F, F"); 197 198 // unused, 2'nd row 199 // 200 contentPane.add(new JLabel(), "1, 2, F, F"); 201 202 // report infos, 3'rd row 203 // 204 JPanel reportInfosPanel 205 = new JPanel(new GridLayout(m_reportInfoClasses.length, 1)); 206 titledBorder = BorderFactory.createTitledBorder(etchedBorder, REPORT_INFOS); 207 compoundBorder = BorderFactory.createCompoundBorder(titledBorder, emptyBorder); 208 reportInfosPanel.setBorder(compoundBorder); 209 m_reportInfoButtons = new JCheckBox[m_reportInfoClasses.length]; 210 String reportInfoName = ""; 211 for (int i = 0; i < m_reportInfoClasses.length; i++) { 212 reportInfoName = m_reportInfoClasses[i].getName(); 213 reportInfoName = reportInfoName.substring(reportInfoName.lastIndexOf('.') + 1); 214 m_reportInfoButtons[i] 215 = new JCheckBox(reportInfoName); 216 reportInfosPanel.add(m_reportInfoButtons[i]); 217 } 218 219 // TBD: this is very dangereous, in case the m_reportInfoButtons array 220 // has not the required length, this will throw NullPointerExceptions 221 // HACK! 222 // 223 m_reportInfoButtons[0].setSelected(true); 224 m_reportInfoButtons[3].setSelected(true); 225 226 contentPane.add(reportInfosPanel, "1, 3, F, F"); 227 228 // unused, 4'th row 229 // 230 contentPane.add(new JLabel(), "1, 4, F, F"); 231 232 // controls, 5'th row 233 // 234 JPanel controlpanel = new JPanel(); 235 controlpanel.add(m_okButton = new JButton(OK)); 236 controlpanel.add(m_closeButton = new JButton(CLOSE)); 237 contentPane.add(controlpanel, "1, 5, C, C"); 238 239 return contentPane; 240 } 241 242 /*** 243 * Initialize all Listeners. 244 */ 245 private void initListeners() { 246 m_okButton.addActionListener(new ActionListener() { 247 public void actionPerformed(ActionEvent e) { 248 handleOkButton(); 249 } 250 }); 251 m_closeButton.addActionListener(new ActionListener() { 252 public void actionPerformed(ActionEvent e) { 253 handleCloseButton(); 254 } 255 }); 256 } 257 258 //*********************************************************************************/ 259 // handle actions methods 260 //*********************************************************************************/ 261 262 /*** 263 * Handles OK-Button Events. 264 */ 265 private void handleOkButton() { 266 try { 267 Report report = new Report(); 268 ReportFormatter reportFormatter = null; 269 for (int i = 0; i < m_formatButtons.length; i++) { 270 if (m_formatButtonGroup.isSelected(m_formatButtons[i].getModel())) { 271 reportFormatter 272 = (ReportFormatter)m_reportFormatterClasses[i].newInstance(); 273 break; 274 } 275 } 276 277 for (int i = 0; i < m_reportInfoButtons.length; i++) { 278 if (m_reportInfoButtons[i].isSelected()) { 279 report.addReportInfo(m_reportInfoClasses[i]); 280 } 281 } 282 283 report.initReport(DependencyModel.getDependencyModel()); 284 if (reportFormatter != null) { 285 showText(report.formatReport(reportFormatter)); 286 } else { 287 showOkDialog(USING_DEFAULT_FORMATTER); 288 showOkDialog(report.formatReport()); 289 } 290 291 } catch (Exception e) { 292 showOkDialog(e.getMessage()); 293 e.printStackTrace(); 294 } 295 } 296 297 /*** 298 * Handles Close-Button Events. 299 */ 300 private void handleCloseButton() { 301 if (m_textFrame != null) { 302 m_textFrame.dispose(); 303 } 304 this.setVisible(false); 305 } 306 307 //*********************************************************************************/ 308 // private utility methods 309 //*********************************************************************************/ 310 311 /*** 312 * Shows the generated report Text in a separate Frame. 313 * 314 * @param text a <code>String</code> value 315 */ 316 private void showText(String text) { 317 318 // dispose last shown frame 319 // 320 if (m_textFrame != null) { 321 m_textFrame.dispose(); 322 m_textFrame = null; 323 } 324 325 // construct new text frame 326 // 327 JTextArea textArea = new JTextArea(); 328 textArea.setText(text); 329 textArea.setFont(new Font("courier", Font.PLAIN, 12)); 330 textArea.setEditable(false); 331 JScrollPane scrollPane = new JScrollPane(textArea); 332 m_textFrame = new JDialog(this, REPORT); 333 m_textFrame.setLocation(100, 100); 334 m_textFrame.setSize(TEXT_FRAME_WIDTH, TEXT_FRAME_HEIGHT); 335 m_textFrame.getContentPane().setLayout(new BorderLayout()); 336 m_textFrame.getContentPane().add(scrollPane, BorderLayout.CENTER); 337 m_textFrame.setVisible(true); 338 } 339 340 /*** 341 * Shows a confirm dialog with the specified message. 342 * 343 * @param message a <code>String</code> value 344 */ 345 private void showOkDialog(String message) { 346 String type = "Error"; 347 Object[] options = {"OK"}; 348 JOptionPane.showOptionDialog(null, message, type, JOptionPane.DEFAULT_OPTION, 349 JOptionPane.WARNING_MESSAGE, null, options, options[0]); 350 } 351 }

This page was automatically generated by Maven