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