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);
 132                     } else if (file.getName().endsWith(".bgv")) {
 133                         parser = new BinaryParser(channel, monitor, component.getDocument(), null);
 134                     } else {
 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 }