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