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: import javax.xml.parsers.*;
040:
041: import org.xml.sax.*;
042:
043: import com.jeantessier.dependency.*;
044:
045: public class OpenFileAction extends AbstractAction implements Runnable,
046: DependencyListener {
047: private DependencyFinder model;
048: private File file;
049:
050: public OpenFileAction(DependencyFinder model) {
051: this .model = model;
052:
053: putValue(Action.LONG_DESCRIPTION,
054: "Load dependency graph from XML file");
055: putValue(Action.NAME, "Open");
056: putValue(Action.SMALL_ICON, new ImageIcon(getClass()
057: .getResource("icons/openfile.gif")));
058: }
059:
060: public void actionPerformed(ActionEvent e) {
061: JFileChooser chooser = new JFileChooser();
062: chooser.addChoosableFileFilter(new XMLFileFilter());
063: int returnValue = chooser.showOpenDialog(model);
064: if (returnValue == JFileChooser.APPROVE_OPTION) {
065: file = chooser.getSelectedFile();
066: new Thread(this ).start();
067: }
068: }
069:
070: public void run() {
071: model.setNewDependencyGraph();
072: model.addInputFile(file);
073:
074: try {
075: Date start = new Date();
076:
077: String filename = file.getCanonicalPath();
078:
079: NodeLoader loader = new NodeLoader(model.getNodeFactory());
080: loader.addDependencyListener(this );
081:
082: // JDK 1.4 feature
083: // model.ProgressBar().setIndeterminate(true);
084:
085: model.getStatusLine().showInfo(
086: "Loading " + filename + " ...");
087: ProgressMonitorInputStream in = new ProgressMonitorInputStream(
088: model, "Reading " + filename, new FileInputStream(
089: filename));
090: in.getProgressMonitor().setMillisToDecideToPopup(0);
091: loader.load(in);
092: model.setTitle("Dependency Finder - " + filename);
093:
094: if (model.getMaximize()) {
095: model.getStatusLine().showInfo("Maximizing ...");
096: new LinkMaximizer().traverseNodes(model.getPackages());
097: } else if (model.getMinimize()) {
098: model.getStatusLine().showInfo("Minimizing ...");
099: new LinkMinimizer().traverseNodes(model.getPackages());
100: }
101:
102: Date stop = new Date();
103:
104: model
105: .getStatusLine()
106: .showInfo(
107: "Done ("
108: + ((stop.getTime() - start
109: .getTime()) / (double) 1000)
110: + " secs).");
111: } catch (SAXException ex) {
112: model.getStatusLine().showError(
113: "Cannot parse: " + ex.getClass().getName() + ": "
114: + ex.getMessage());
115: } catch (ParserConfigurationException ex) {
116: model.getStatusLine().showError(
117: "Cannot parse: " + ex.getClass().getName() + ": "
118: + ex.getMessage());
119: } catch (IOException ex) {
120: model.getStatusLine().showError(
121: "Cannot load: " + ex.getClass().getName() + ": "
122: + ex.getMessage());
123: } finally {
124: // JDK 1.4 feature
125: // model.ProgressBar().setIndeterminate(false);
126:
127: if (model.getPackages() == null) {
128: model.setTitle("Dependency Finder");
129: }
130: }
131: }
132:
133: public void beginSession(DependencyEvent event) {
134: // Do nothing
135: }
136:
137: public void beginClass(DependencyEvent event) {
138: model.getStatusLine().showInfo(
139: "Loading dependencies for " + event.getClassName()
140: + " ...");
141: }
142:
143: public void dependency(DependencyEvent event) {
144: // Do nothing
145: }
146:
147: public void endClass(DependencyEvent event) {
148: // Do nothing
149: }
150:
151: public void endSession(DependencyEvent event) {
152: // Do nothing
153: }
154: }
|