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 25package com.sun.hotspot.igv.coordinator.actions; 26 27import com.sun.hotspot.igv.data.GraphDocument; 28import com.sun.hotspot.igv.data.Group; 29import com.sun.hotspot.igv.data.serialization.Printer; 30import com.sun.hotspot.igv.settings.Settings; 31import java.io.*; 32import javax.swing.Action; 33import javax.swing.JFileChooser; 34import org.openide.nodes.Node; 35import org.openide.util.HelpCtx; 36import org.openide.util.NbBundle; 37import org.openide.util.actions.CookieAction; 38import org.openide.util.actions.NodeAction; 39 40/** 41 * 42 * @author Thomas Wuerthinger 43 */ 44public final class SaveAsAction extends NodeAction { 45 46 public SaveAsAction() { 47 putValue(Action.SHORT_DESCRIPTION, "Save selected groups to XML file..."); 48 } 49 50 @Override 51 protected void performAction(Node[] activatedNodes) { 52 53 GraphDocument doc = new GraphDocument(); 54 for (Node n : activatedNodes) { 55 Group group = n.getLookup().lookup(Group.class); 56 doc.addElement(group); 57 } 58 59 save(doc); 60 } 61 62 public static void save(GraphDocument doc) { 63 JFileChooser fc = new JFileChooser(); 64 fc.setFileFilter(ImportAction.getFileFilter()); 65 fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT))); 66 67 if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { 68 File file = fc.getSelectedFile(); 69 if (!file.getName().contains(".")) { 70 file = new File(file.getAbsolutePath() + ".xml"); 71 } 72 73 File dir = file; 74 if (!dir.isDirectory()) { 75 dir = dir.getParentFile(); 76 } 77 Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath()); 78 try { 79 try (Writer writer = new OutputStreamWriter(new FileOutputStream(file))) { 80 Printer p = new Printer(); 81 p.export(writer, doc); 82 } 83 } catch (FileNotFoundException e) { 84 e.printStackTrace(); 85 } catch (IOException e) { 86 e.printStackTrace(); 87 88 } 89 } 90 } 91 92 protected int mode() { 93 return CookieAction.MODE_SOME; 94 } 95 96 @Override 97 public String getName() { 98 return NbBundle.getMessage(SaveAsAction.class, "CTL_SaveAsAction"); 99 } 100 101 @Override 102 protected String iconResource() { 103 return "com/sun/hotspot/igv/coordinator/images/save.png"; 104 } 105 106 @Override 107 public HelpCtx getHelpCtx() { 108 return HelpCtx.DEFAULT_HELP; 109 } 110 111 @Override 112 protected boolean asynchronous() { 113 return false; 114 } 115 116 @Override 117 protected boolean enable(Node[] nodes) { 118 119 int cnt = 0; 120 for (Node n : nodes) { 121 cnt += n.getLookup().lookupAll(Group.class).size(); 122 } 123 124 return cnt > 0; 125 } 126} 127