1 32 33 package com.jeantessier.dependencyfinder.gui; 34 35 import java.awt.*; 36 import java.awt.event.*; 37 import java.io.*; 38 import java.util.*; 39 40 import javax.swing.*; 41 import javax.swing.border.*; 42 import javax.swing.table.*; 43 import javax.swing.text.*; 44 45 import org.apache.log4j.*; 46 47 import org.xml.sax.*; 48 49 import com.jeantessier.classreader.*; 50 import com.jeantessier.commandline.*; 51 import com.jeantessier.metrics.*; 52 53 public class OOMetrics extends JFrame { 54 private static final TableCellRenderer RENDERER = new MeasurementTableCellRenderer(); 55 56 private MetricsFactory factory; 57 58 private JMenuBar menuBar = new JMenuBar(); 59 private JMenu fileMenu = new JMenu(); 60 private JMenu helpMenu = new JMenu(); 61 private JToolBar toolbar = new JToolBar(); 62 private JTextArea projectArea = new JTextArea(); 63 private JButton filterButton = new JButton("Filter:"); 64 private JTextField filterField = new JTextField("//"); 65 private StatusLine statusLine = new StatusLine(420); 66 private JProgressBar progressBar = new JProgressBar(); 67 68 private OOMetricsTableModel groupsModel; 69 private OOMetricsTableModel classesModel; 70 private OOMetricsTableModel methodsModel; 71 72 private File inputFile = new File("."); 73 74 public OOMetrics(CommandLine commandLine, MetricsFactory factory) { 75 this.factory = factory; 76 77 this.setSize(new Dimension(800, 600)); 78 this.setTitle("OO Metrics"); 79 this.setIconImage(new ImageIcon(getClass().getResource("icons/logoicon.gif")).getImage()); 80 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 81 this.addWindowListener(new WindowKiller()); 82 83 groupsModel = new OOMetricsTableModel(factory.getConfiguration().getGroupMeasurements()); 84 classesModel = new OOMetricsTableModel(factory.getConfiguration().getClassMeasurements()); 85 methodsModel = new OOMetricsTableModel(factory.getConfiguration().getMethodMeasurements()); 86 87 buildMenus(commandLine); 88 buildUI(); 89 90 try { 91 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 92 SwingUtilities.updateComponentTreeUI(this); 93 } catch (Exception ex) { 94 Logger.getLogger(OOMetrics.class).error("Unable to set look and feel", ex); 95 } 96 97 statusLine.showInfo("Ready."); 98 } 99 100 MetricsFactory getMetricsFactory() { 101 return factory; 102 } 103 104 void setMetricsFactory(MetricsFactory factory) { 105 this.factory = factory; 106 } 107 108 JTextArea getProjectArea() { 109 return projectArea; 110 } 111 112 OOMetricsTableModel getGroupsModel() { 113 return groupsModel; 114 } 115 116 OOMetricsTableModel getClassesModel() { 117 return classesModel; 118 } 119 120 OOMetricsTableModel getMethodsModel() { 121 return methodsModel; 122 } 123 124 File getInputFile() { 125 return inputFile; 126 } 127 128 void setInputFile(File inputFile) { 129 this.inputFile = inputFile; 130 } 131 132 JTextComponent getFilterField() { 133 return filterField; 134 } 135 136 StatusLine getStatusLine() { 137 return statusLine; 138 } 139 140 JProgressBar getProgressBar() { 141 return progressBar; 142 } 143 144 private void buildMenus(CommandLine commandLine) { 145 buildFileMenu(commandLine); 146 buildHelpMenu(commandLine); 147 148 this.setJMenuBar(menuBar); 149 } 150 151 private void buildFileMenu(CommandLine commandLine) { 152 menuBar.add(fileMenu); 153 154 fileMenu.setText("File"); 155 156 Action action; 157 JMenuItem menuItem; 158 JButton button; 159 160 action = new MetricsExtractAction(this); 161 menuItem = fileMenu.add(action); 162 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK)); 163 menuItem.setMnemonic('e'); 164 button = toolbar.add(action); 165 button.setToolTipText((String ) action.getValue(Action.LONG_DESCRIPTION)); 166 167 toolbar.addSeparator(); 168 fileMenu.addSeparator(); 169 170 action = new NewMetricsAction(this); 171 menuItem = fileMenu.add(action); 172 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK)); 173 menuItem.setMnemonic('n'); 174 button = toolbar.add(action); 175 button.setToolTipText((String ) action.getValue(Action.LONG_DESCRIPTION)); 176 177 toolbar.addSeparator(); 178 fileMenu.addSeparator(); 179 180 action = new ExitAction(this); 181 menuItem = fileMenu.add(action); 182 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK)); 183 menuItem.setMnemonic('x'); 184 185 this.setJMenuBar(menuBar); 186 } 187 188 private void buildHelpMenu(CommandLine commandLine) { 189 menuBar.add(helpMenu); 190 191 helpMenu.setText("Help"); 192 193 Action action; 194 JMenuItem menuItem; 195 196 action = new AboutAction(this); 197 menuItem = helpMenu.add(action); 198 menuItem.setMnemonic('a'); 199 } 200 201 private void buildUI() { 202 this.getContentPane().setLayout(new BorderLayout()); 203 this.getContentPane().add(buildControlPanel(), BorderLayout.NORTH); 204 this.getContentPane().add(buildResultPanel(), BorderLayout.CENTER); 205 this.getContentPane().add(buildStatusPanel(), BorderLayout.SOUTH); 206 } 207 208 private JComponent buildControlPanel() { 209 return toolbar; 210 } 211 212 private JComponent buildResultPanel() { 213 JPanel result = new JPanel(); 214 215 result.setLayout(new BorderLayout()); 216 result.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, buildProjectPanel(), buildChartsPanel()), BorderLayout.CENTER); 217 result.add(buildFilterPanel(), BorderLayout.SOUTH); 218 219 return result; 220 } 221 222 private JComponent buildProjectPanel() { 223 JComponent result = new JScrollPane(projectArea); 224 225 projectArea.setEditable(false); 226 227 return result; 228 } 229 230 private JComponent buildChartsPanel() { 231 JTabbedPane result = new JTabbedPane(); 232 233 result.addTab("Groups", buildGroupsChartPanel()); 235 result.addTab("Classes", buildClassesChartPanel()); 236 result.addTab("Methods", buildMethodsChartPanel()); 237 238 return result; 239 } 240 241 private JComponent buildGroupsChartPanel() { 242 return buildChartPanel(getGroupsModel()); 243 } 244 245 private JComponent buildClassesChartPanel() { 246 return buildChartPanel(getClassesModel()); 247 } 248 249 private JComponent buildMethodsChartPanel() { 250 return buildChartPanel(getMethodsModel()); 251 } 252 253 private JComponent buildChartPanel(OOMetricsTableModel model) { 254 JComponent result; 255 256 JTable table = new JTable(model); 257 258 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 259 table.setRowSelectionAllowed(true); 260 table.setDefaultRenderer(Object .class, RENDERER); 261 table.setShowHorizontalLines(false); 262 table.setShowVerticalLines(false); 263 TableHeaderListener listener = new TableHeaderListener(table, model); 264 table.getTableHeader().addMouseListener(listener); 265 table.getTableHeader().addMouseMotionListener(listener); 266 267 result = new JScrollPane(table); 268 269 return result; 270 } 271 272 private JComponent buildFilterPanel() { 273 JPanel result = new JPanel(); 274 275 result.setLayout(new BorderLayout()); 276 result.add(filterButton, BorderLayout.WEST); 277 result.add(filterField, BorderLayout.CENTER); 278 279 filterButton.addActionListener(new FilterActionListener(this)); 280 281 return result; 282 } 283 284 private JComponent buildStatusPanel() { 285 JPanel result = new JPanel(); 286 287 Dimension size = getProgressBar().getPreferredSize(); 288 size.width = 100; 289 getProgressBar().setPreferredSize(size); 290 getProgressBar().setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 291 292 result.setLayout(new BorderLayout()); 293 result.add(getStatusLine(), BorderLayout.CENTER); 294 result.add(getProgressBar(), BorderLayout.EAST); 295 296 return result; 297 } 298 299 public static void showError(CommandLineUsage clu, String msg) { 300 System.err.println(msg); 301 showError(clu); 302 } 303 304 public static void showError(CommandLineUsage clu) { 305 System.err.println(clu); 306 } 307 308 public static void main(String [] args) throws Exception { 309 CommandLine commandLine = new CommandLine(new NullParameterStrategy()); 311 commandLine.addSingleValueSwitch("default-configuration", true); 312 commandLine.addSingleValueSwitch("configuration"); 313 commandLine.addToggleSwitch("validate"); 314 commandLine.addToggleSwitch("help"); 315 316 CommandLineUsage usage = new CommandLineUsage("OOMetrics"); 317 commandLine.accept(usage); 318 319 try { 320 commandLine.parse(args); 321 } catch (IllegalArgumentException ex) { 322 showError(usage, ex.toString()); 323 System.exit(1); 324 } catch (CommandLineException ex) { 325 showError(usage, ex.toString()); 326 System.exit(1); 327 } 328 329 if (commandLine.getToggleSwitch("help")) { 330 showError(usage); 331 System.exit(1); 332 } 333 334 MetricsFactory factory; 335 336 if (commandLine.isPresent("configuration")) { 337 factory = new MetricsFactory("Project", new MetricsConfigurationLoader(commandLine.getToggleSwitch("validate")).load(commandLine.getSingleSwitch("configuration"))); 338 } else { 339 factory = new MetricsFactory("Project", new MetricsConfigurationLoader(commandLine.getToggleSwitch("validate")).load(commandLine.getSingleSwitch("default-configuration"))); 340 } 341 342 345 346 try { 347 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 348 } catch (Exception ex) { 349 } 351 352 OOMetrics model = new OOMetrics(commandLine, factory); 353 model.setVisible(true); 354 } 355 } 356 | Popular Tags |