KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > gui > OpenFileAction


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependencyfinder.gui;
34
35 import java.awt.event.*;
36 import java.io.*;
37 import java.util.*;
38
39 import javax.swing.*;
40
41 import org.xml.sax.*;
42
43 import com.jeantessier.classreader.*;
44 import com.jeantessier.dependency.*;
45
46 public class OpenFileAction extends AbstractAction implements Runnable JavaDoc, DependencyListener {
47     private DependencyFinder model;
48     private File file;
49     
50     public OpenFileAction(DependencyFinder model) {
51         this.model = model;
52
53         putValue(Action.LONG_DESCRIPTION, "Load dependency graph from XML file");
54         putValue(Action.NAME, "Open");
55         putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("icons/openfile.gif")));
56     }
57
58     public void actionPerformed(ActionEvent e) {
59         JFileChooser chooser = new JFileChooser();
60         chooser.addChoosableFileFilter(new XMLFileFilter());
61         int returnValue = chooser.showOpenDialog(model);
62         if (returnValue == JFileChooser.APPROVE_OPTION) {
63             file = chooser.getSelectedFile();
64             new Thread JavaDoc(this).start();
65         }
66     }
67
68     public void run() {
69         model.setNewDependencyGraph();
70         model.addInputFiles(Collections.singleton(file));
71         
72         try {
73             Date start = new Date();
74
75             String JavaDoc filename = file.getCanonicalPath();
76
77             NodeLoader loader = new NodeLoader(model.getNodeFactory());
78             loader.addDependencyListener(this);
79
80             // JDK 1.4 feature
81
// model.ProgressBar().setIndeterminate(true);
82

83             model.getStatusLine().showInfo("Loading " + filename + " ...");
84             ProgressMonitorInputStream in = new ProgressMonitorInputStream(model, "Reading " + filename, new FileInputStream(filename));
85             in.getProgressMonitor().setMillisToDecideToPopup(0);
86             loader.load(in);
87             model.setTitle("Dependency Finder - " + filename);
88
89             if (model.getMaximize()) {
90                 model.getStatusLine().showInfo("Maximizing ...");
91                 new LinkMaximizer().traverseNodes(model.getPackages());
92             } else if (model.getMinimize()) {
93                 model.getStatusLine().showInfo("Minimizing ...");
94                 new LinkMinimizer().traverseNodes(model.getPackages());
95             }
96
97             Date stop = new Date();
98
99             model.getStatusLine().showInfo("Done (" + ((stop.getTime() - start.getTime()) / (double) 1000) + " secs).");
100         } catch (SAXException ex) {
101             model.getStatusLine().showError("Cannot parse: " + ex.getClass().getName() + ": " + ex.getMessage());
102         } catch (IOException ex) {
103             model.getStatusLine().showError("Cannot load: " + ex.getClass().getName() + ": " + ex.getMessage());
104         } finally {
105             // JDK 1.4 feature
106
// model.ProgressBar().setIndeterminate(false);
107

108             if (model.getPackages() == null) {
109                 model.setTitle("Dependency Finder");
110             }
111         }
112     }
113     
114     public void beginSession(DependencyEvent event) {
115         // Do nothing
116
}
117
118     public void beginClass(DependencyEvent event) {
119         model.getStatusLine().showInfo("Loading dependencies for " + event.getClassName() + " ...");
120     }
121     
122     public void dependency(DependencyEvent event) {
123         // Do nothing
124
}
125     
126     public void endClass(DependencyEvent event) {
127         // Do nothing
128
}
129     
130     public void endSession(DependencyEvent event) {
131         // Do nothing
132
}
133 }
134
Popular Tags