1 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 , 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 (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 filename = file.getCanonicalPath(); 76 77 NodeLoader loader = new NodeLoader(model.getNodeFactory()); 78 loader.addDependencyListener(this); 79 80 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 108 if (model.getPackages() == null) { 109 model.setTitle("Dependency Finder"); 110 } 111 } 112 } 113 114 public void beginSession(DependencyEvent event) { 115 } 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 } 125 126 public void endClass(DependencyEvent event) { 127 } 129 130 public void endSession(DependencyEvent event) { 131 } 133 } 134 | Popular Tags |