001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.gui;
034:
035: import java.awt.event.*;
036: import java.io.*;
037: import java.util.*;
038: import javax.swing.*;
039:
040: import com.jeantessier.classreader.*;
041:
042: public class MetricsExtractAction extends AbstractAction implements
043: Runnable {
044: private OOMetrics model;
045: private Collection<String> filenames;
046:
047: public MetricsExtractAction(OOMetrics model) {
048: this .model = model;
049:
050: putValue(Action.LONG_DESCRIPTION,
051: "Extract metrics from compiled classes");
052: putValue(Action.NAME, "Extract");
053: putValue(Action.SMALL_ICON, new ImageIcon(getClass()
054: .getResource("icons/extract.gif")));
055: }
056:
057: public void actionPerformed(ActionEvent e) {
058: JFileChooser chooser = new JFileChooser(model.getInputFile());
059: chooser.addChoosableFileFilter(new JavaBytecodeFileFilter());
060: chooser
061: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
062: chooser.setMultiSelectionEnabled(true);
063: int returnValue = chooser.showDialog(model, "Extract");
064: if (returnValue == JFileChooser.APPROVE_OPTION) {
065: File[] selectedFiles = chooser.getSelectedFiles();
066: filenames = new LinkedList<String>();
067: for (File file : selectedFiles) {
068: filenames.add(file.toString());
069: }
070: model.setInputFile(selectedFiles[0]);
071: new Thread(this ).start();
072: }
073: }
074:
075: public void run() {
076: Date start = new Date();
077:
078: model.getStatusLine().showInfo("Scanning ...");
079: ClassfileScanner scanner = new ClassfileScanner();
080: scanner.load(filenames);
081:
082: model.getProgressBar().setMaximum(
083: scanner.getNbFiles() + scanner.getNbClasses());
084:
085: MetricsVerboseListener verboseListener = new MetricsVerboseListener(
086: model.getStatusLine(), model.getProgressBar());
087:
088: ClassfileLoader loader = new AggregatingClassfileLoader();
089: loader.addLoadListener(verboseListener);
090: loader.load(filenames);
091:
092: com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(
093: "Project", model.getMetricsFactory());
094: gatherer.addMetricsListener(verboseListener);
095: gatherer.visitClassfiles(loader.getAllClassfiles());
096:
097: // JDK 1.4 feature
098: // model.ProgressBar().setIndeterminate(true);
099:
100: model.getStatusLine().showInfo("Generating method results ...");
101: model.getMethodsModel().setMetrics(
102: model.getMetricsFactory().getMethodMetrics());
103:
104: model.getStatusLine().showInfo("Generating class results ...");
105: model.getClassesModel().setMetrics(
106: model.getMetricsFactory().getClassMetrics());
107:
108: model.getStatusLine().showInfo("Generating group results ...");
109: model.getGroupsModel().setMetrics(
110: model.getMetricsFactory().getGroupMetrics());
111:
112: model.getStatusLine()
113: .showInfo("Generating project results ...");
114: StringWriter out = new StringWriter();
115: com.jeantessier.metrics.Printer printer = new com.jeantessier.metrics.TextPrinter(
116: new PrintWriter(out), model.getMetricsFactory()
117: .getConfiguration().getProjectMeasurements());
118: printer.visitMetrics(model.getMetricsFactory()
119: .getProjectMetrics());
120: model.getProjectArea().setText(out.toString());
121:
122: Date stop = new Date();
123:
124: model
125: .getStatusLine()
126: .showInfo(
127: "Done ("
128: + ((stop.getTime() - start.getTime()) / (double) 1000)
129: + " secs).");
130: // JDK 1.4 feature
131: // model.ProgressBar().setIndeterminate(false);
132: model.setTitle("OO Metrics - Extractor");
133: }
134: }
|