1 /* 2 * Copyright (c) 2008, 2015, 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 25 package com.sun.hotspot.igv.coordinator.actions; 26 27 import com.sun.hotspot.igv.coordinator.OutlineTopComponent; 28 import com.sun.hotspot.igv.data.GraphDocument; 29 import com.sun.hotspot.igv.data.serialization.BinaryParser; 30 import com.sun.hotspot.igv.data.serialization.GraphParser; 31 import com.sun.hotspot.igv.data.serialization.ParseMonitor; 32 import com.sun.hotspot.igv.data.serialization.Parser; 33 import com.sun.hotspot.igv.settings.Settings; 34 import java.awt.event.InputEvent; 35 import java.awt.event.KeyEvent; 36 import java.io.File; 37 import java.io.FileNotFoundException; 38 import java.io.IOException; 39 import java.nio.channels.FileChannel; 40 import java.nio.file.StandardOpenOption; 41 import javax.swing.Action; 42 import javax.swing.JFileChooser; 43 import javax.swing.KeyStroke; 44 import javax.swing.SwingUtilities; 45 import javax.swing.filechooser.FileFilter; 46 import org.netbeans.api.progress.ProgressHandle; 47 import org.netbeans.api.progress.ProgressHandleFactory; 48 import org.openide.util.Exceptions; 49 import org.openide.util.HelpCtx; 50 import org.openide.util.NbBundle; 51 import org.openide.util.RequestProcessor; 52 import org.openide.util.actions.CallableSystemAction; 53 54 /** 55 * 56 * @author Thomas Wuerthinger 57 */ 58 public final class ImportAction extends CallableSystemAction { 59 private static final int WORKUNITS = 10000; 60 61 public static FileFilter getFileFilter() { 62 return new FileFilter() { 63 64 @Override 65 public boolean accept(File f) { 66 return f.getName().toLowerCase().endsWith(".xml") || f.getName().toLowerCase().endsWith(".bgv") || f.isDirectory(); 67 } 68 69 @Override 70 public String getDescription() { 71 return "Graph files (*.xml, *.bgv)"; 72 } 73 }; 74 } 75 76 @Override 77 public void performAction() { 78 79 JFileChooser fc = new JFileChooser(); 80 fc.setFileFilter(ImportAction.getFileFilter()); 81 fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT))); 82 83 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 84 File file = fc.getSelectedFile(); 85 86 File dir = file; 87 if (!dir.isDirectory()) { 88 dir = dir.getParentFile(); 89 } 90 91 Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); 92 try { 93 final FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ); 94 final ProgressHandle handle = ProgressHandleFactory.createHandle("Opening file " + file.getName()); 95 handle.start(WORKUNITS); 96 final long start = channel.size(); 97 ParseMonitor monitor = new ParseMonitor() { 98 @Override 99 public void updateProgress() { 100 try { 101 int prog = (int) (WORKUNITS * (double) channel.position() / (double) start); 102 handle.progress(prog); 103 } catch (IOException ex) { 104 } 105 } 106 @Override 107 public void setState(String state) { 108 updateProgress(); 109 handle.progress(state); 110 } 111 }; 112 final GraphParser parser; 113 final OutlineTopComponent component = OutlineTopComponent.findInstance(); 114 if (file.getName().endsWith(".xml")) { 115 parser = new Parser(channel, monitor, null); 119 parser = null; 120 } 121 RequestProcessor.getDefault().post(new Runnable() { 122 @Override 123 public void run() { 124 try { 125 final GraphDocument document = parser.parse(); 126 if (document != null) { 127 SwingUtilities.invokeLater(new Runnable(){ 128 @Override 129 public void run() { 130 component.requestActive(); 131 component.getDocument().addGraphDocument(document); 132 } 133 }); 134 } 135 } catch (IOException ex) { 136 Exceptions.printStackTrace(ex); 137 } 138 handle.finish(); 139 } 140 }); 141 } catch (FileNotFoundException ex) { 142 Exceptions.printStackTrace(ex); 143 } catch (IOException ex) { 144 Exceptions.printStackTrace(ex); 145 } 146 } 147 } 148 149 @Override 150 public String getName() { 151 return NbBundle.getMessage(ImportAction.class, "CTL_ImportAction"); 152 } 153 154 public ImportAction() { 155 putValue(Action.SHORT_DESCRIPTION, "Open XML graph document..."); 156 putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); 157 } 158 159 @Override 160 protected String iconResource() { 161 return "com/sun/hotspot/igv/coordinator/images/import.png"; 162 } 163 164 @Override 165 public HelpCtx getHelpCtx() { 166 return HelpCtx.DEFAULT_HELP; 167 } 168 169 @Override 170 protected boolean asynchronous() { 171 return false; 172 } 173 } | 1 /* 2 * Copyright (c) 2008, 2016, 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 25 package com.sun.hotspot.igv.coordinator.actions; 26 27 import com.sun.hotspot.igv.coordinator.OutlineTopComponent; 28 import com.sun.hotspot.igv.data.GraphDocument; 29 import com.sun.hotspot.igv.data.serialization.BinaryParser; 30 import com.sun.hotspot.igv.data.serialization.GraphParser; 31 import com.sun.hotspot.igv.data.serialization.ParseMonitor; 32 import com.sun.hotspot.igv.data.serialization.Parser; 33 import com.sun.hotspot.igv.settings.Settings; 34 import java.awt.event.ActionEvent; 35 import java.io.File; 36 import java.io.FileNotFoundException; 37 import java.io.IOException; 38 import java.nio.channels.FileChannel; 39 import java.nio.file.StandardOpenOption; 40 import java.util.logging.Level; 41 import java.util.logging.Logger; 42 import javax.swing.JFileChooser; 43 import javax.swing.SwingUtilities; 44 import javax.swing.filechooser.FileFilter; 45 import org.netbeans.api.progress.ProgressHandle; 46 import org.netbeans.api.progress.ProgressHandleFactory; 47 import org.openide.util.Exceptions; 48 import org.openide.util.RequestProcessor; 49 import org.openide.awt.ActionID; 50 import org.openide.awt.ActionReference; 51 import org.openide.awt.ActionReferences; 52 import org.openide.awt.ActionRegistration; 53 import org.openide.util.HelpCtx; 54 import org.openide.util.NbBundle; 55 import org.openide.util.actions.SystemAction; 56 57 /** 58 * 59 * @author Thomas Wuerthinger 60 */ 61 62 @ActionID( 63 category = "File", 64 id = "com.sun.hotspot.igv.coordinator.actions.ImportAction" 65 ) 66 @ActionRegistration( 67 iconBase = "com/sun/hotspot/igv/coordinator/images/import.png", 68 displayName = "#CTL_ImportAction" 69 ) 70 @ActionReferences({ 71 @ActionReference(path = "Menu/File", position = 0), 72 @ActionReference(path = "Shortcuts", name = "C-O") 73 }) 74 public final class ImportAction extends SystemAction { 75 private static final int WORKUNITS = 10000; 76 77 public static FileFilter getFileFilter() { 78 return new FileFilter() { 79 80 @Override 81 public boolean accept(File f) { 82 return f.getName().toLowerCase().endsWith(".xml") || f.getName().toLowerCase().endsWith(".bgv") || f.isDirectory(); 83 } 84 85 @Override 86 public String getDescription() { 87 return "Graph files (*.xml, *.bgv)"; 88 } 89 }; 90 } 91 92 @Override 93 public void actionPerformed(ActionEvent e) { 94 JFileChooser fc = new JFileChooser(); 95 fc.setFileFilter(ImportAction.getFileFilter()); 96 fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT))); 97 fc.setMultiSelectionEnabled(true); 98 99 if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 100 for (final File file : fc.getSelectedFiles()) { 101 File dir = file; 102 if (!dir.isDirectory()) { 103 dir = dir.getParentFile(); 104 } 105 106 Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); 107 try { 108 final FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ); 109 final ProgressHandle handle = ProgressHandleFactory.createHandle("Opening file " + file.getName()); 110 handle.start(WORKUNITS); 111 final long startTime = System.currentTimeMillis(); 112 final long start = channel.size(); 113 ParseMonitor monitor = new ParseMonitor() { 114 @Override 115 public void updateProgress() { 116 try { 117 int prog = (int) (WORKUNITS * (double) channel.position() / (double) start); 118 handle.progress(prog); 119 } catch (IOException ex) { 120 } 121 } 122 @Override 123 public void setState(String state) { 124 updateProgress(); 125 handle.progress(state); 126 } 127 }; 128 final GraphParser parser; 129 final OutlineTopComponent component = OutlineTopComponent.findInstance(); 130 if (file.getName().endsWith(".xml")) { 131 parser = new Parser(channel, monitor, null); 135 parser = null; 136 } 137 RequestProcessor.getDefault().post(new Runnable() { 138 @Override 139 public void run() { 140 try { 141 final GraphDocument document = parser.parse(); 142 if (document != null) { 143 SwingUtilities.invokeLater(new Runnable(){ 144 @Override 145 public void run() { 146 component.requestActive(); 147 component.getDocument().addGraphDocument(document); 148 } 149 }); 150 } 151 } catch (IOException ex) { 152 Exceptions.printStackTrace(ex); 153 } 154 handle.finish(); 155 long stop = System.currentTimeMillis(); 156 Logger.getLogger(getClass().getName()).log(Level.INFO, "Loaded in " + file + " in " + ((stop - startTime) / 1000.0) + " seconds"); 157 } 158 }); 159 } catch (FileNotFoundException ex) { 160 Exceptions.printStackTrace(ex); 161 } catch (IOException ex) { 162 Exceptions.printStackTrace(ex); 163 } 164 } 165 } 166 } 167 168 @Override 169 public String getName() { 170 return NbBundle.getMessage(ImportAction.class, "CTL_ImportAction"); 171 } 172 173 @Override 174 protected String iconResource() { 175 return "com/sun/hotspot/igv/coordinator/images/import.png"; 176 } 177 178 @Override 179 public HelpCtx getHelpCtx() { 180 return HelpCtx.DEFAULT_HELP; 181 } 182 } |