ImportAction.java revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25package com.sun.hotspot.igv.coordinator.actions; 26 27import com.sun.hotspot.igv.coordinator.OutlineTopComponent; 28import com.sun.hotspot.igv.data.GraphDocument; 29import com.sun.hotspot.igv.data.serialization.Parser; 30import com.sun.hotspot.igv.settings.Settings; 31import com.sun.hotspot.igv.data.serialization.XMLParser; 32import java.awt.event.InputEvent; 33import java.awt.event.KeyEvent; 34import java.io.File; 35import java.io.FileInputStream; 36import java.io.FileNotFoundException; 37import java.io.IOException; 38import javax.swing.Action; 39import javax.swing.JFileChooser; 40import javax.swing.KeyStroke; 41import javax.swing.filechooser.FileFilter; 42import org.netbeans.api.progress.ProgressHandle; 43import org.netbeans.api.progress.ProgressHandleFactory; 44import org.openide.DialogDisplayer; 45import org.openide.NotifyDescriptor; 46import org.openide.util.HelpCtx; 47import org.openide.util.NbBundle; 48import org.openide.util.RequestProcessor; 49import org.openide.util.actions.CallableSystemAction; 50import org.openide.xml.XMLUtil; 51import org.xml.sax.InputSource; 52import org.xml.sax.SAXException; 53import org.xml.sax.XMLReader; 54 55/** 56 * 57 * @author Thomas Wuerthinger 58 */ 59public final class ImportAction extends CallableSystemAction { 60 61 public static FileFilter getFileFilter() { 62 return new FileFilter() { 63 64 public boolean accept(File f) { 65 return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory(); 66 } 67 68 public String getDescription() { 69 return "XML files (*.xml)"; 70 } 71 }; 72 } 73 74 public void performAction() { 75 76 JFileChooser fc = new JFileChooser(); 77 fc.setFileFilter(ImportAction.getFileFilter()); 78 fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT))); 79 80 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 81 File file = fc.getSelectedFile(); 82 83 File dir = file; 84 if (!dir.isDirectory()) { 85 dir = dir.getParentFile(); 86 } 87 88 Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); 89 90 try { 91 final XMLReader reader = XMLUtil.createXMLReader(); 92 final FileInputStream inputStream = new FileInputStream(file); 93 final InputSource is = new InputSource(inputStream); 94 95 final ProgressHandle handle = ProgressHandleFactory.createHandle("Opening file " + file.getName()); 96 final int basis = 1000; 97 handle.start(basis); 98 final int start = inputStream.available(); 99 100 final XMLParser.ParseMonitor parseMonitor = new XMLParser.ParseMonitor() { 101 102 public void setProgress(double d) { 103 try { 104 int curAvailable = inputStream.available(); 105 int prog = (int) (basis * (double) (start - curAvailable) / (double) start); 106 handle.progress(prog); 107 } catch (IOException ex) { 108 } 109 } 110 111 public void setState(String state) { 112 setProgress(0.0); 113 handle.progress(state); 114 } 115 }; 116 final Parser parser = new Parser(); 117 final OutlineTopComponent component = OutlineTopComponent.findInstance(); 118 119 component.requestActive(); 120 121 RequestProcessor.getDefault().post(new Runnable() { 122 123 public void run() { 124 GraphDocument document = null; 125 try { 126 document = parser.parse(reader, is, parseMonitor); 127 parseMonitor.setState("Finishing"); 128 component.getDocument().addGraphDocument(document); 129 } catch (SAXException ex) { 130 String s = "Exception during parsing the XML file, could not load document!"; 131 if (ex instanceof XMLParser.MissingAttributeException) { 132 XMLParser.MissingAttributeException e = (XMLParser.MissingAttributeException) ex; 133 s += "\nMissing attribute \"" + e.getAttributeName() + "\""; 134 } 135 ex.printStackTrace(); 136 NotifyDescriptor d = new NotifyDescriptor.Message(s, NotifyDescriptor.ERROR_MESSAGE); 137 DialogDisplayer.getDefault().notify(d); 138 } 139 handle.finish(); 140 } 141 }); 142 143 } catch (SAXException ex) { 144 ex.printStackTrace(); 145 } catch (FileNotFoundException ex) { 146 ex.printStackTrace(); 147 } catch (IOException ex) { 148 ex.printStackTrace(); 149 } 150 } 151 } 152 153 public String getName() { 154 return NbBundle.getMessage(ImportAction.class, "CTL_ImportAction"); 155 } 156 157 public ImportAction() { 158 putValue(Action.SHORT_DESCRIPTION, "Open an XML graph document"); 159 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); 160 } 161 162 @Override 163 protected String iconResource() { 164 return "com/sun/hotspot/igv/coordinator/images/import.gif"; 165 } 166 167 public HelpCtx getHelpCtx() { 168 return HelpCtx.DEFAULT_HELP; 169 } 170 171 @Override 172 protected boolean asynchronous() { 173 return false; 174 } 175} 176