SaveAsAction.java revision 222:2a1a77d3458f
1/*
2 * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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.File;
32import java.io.FileNotFoundException;
33import java.io.FileOutputStream;
34import java.io.IOException;
35import java.io.OutputStreamWriter;
36import java.io.Writer;
37import javax.swing.JFileChooser;
38import org.openide.nodes.Node;
39import org.openide.util.HelpCtx;
40import org.openide.util.NbBundle;
41import org.openide.util.actions.CookieAction;
42import org.openide.util.actions.NodeAction;
43
44/**
45 *
46 * @author Thomas Wuerthinger
47 */
48public final class SaveAsAction extends NodeAction {
49
50    protected void performAction(Node[] activatedNodes) {
51
52        GraphDocument doc = new GraphDocument();
53        for (Node n : activatedNodes) {
54            Group group = n.getLookup().lookup(Group.class);
55            doc.addGroup(group);
56        }
57
58        save(doc);
59    }
60
61    public static void save(GraphDocument doc) {
62        JFileChooser fc = new JFileChooser();
63        fc.setFileFilter(ImportAction.getFileFilter());
64        fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT)));
65
66        if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
67            File file = fc.getSelectedFile();
68            if (!file.getName().contains(".")) {
69                file = new File(file.getAbsolutePath() + ".xml");
70            }
71
72            File dir = file;
73            if (!dir.isDirectory()) {
74                dir = dir.getParentFile();
75            }
76            Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath());
77            try {
78                Writer writer = new OutputStreamWriter(new FileOutputStream(file));
79                Printer p = new Printer();
80                p.export(writer, doc);
81                writer.close();
82            } catch (FileNotFoundException e) {
83                e.printStackTrace();
84            } catch (IOException e) {
85                e.printStackTrace();
86
87            }
88        }
89    }
90
91    protected int mode() {
92        return CookieAction.MODE_SOME;
93    }
94
95    public String getName() {
96        return NbBundle.getMessage(SaveAsAction.class, "CTL_SaveAsAction");
97    }
98
99    @Override
100    protected String iconResource() {
101        return "com/sun/hotspot/igv/coordinator/images/save.gif";
102    }
103
104    public HelpCtx getHelpCtx() {
105        return HelpCtx.DEFAULT_HELP;
106    }
107
108    @Override
109    protected boolean asynchronous() {
110        return false;
111    }
112
113    protected boolean enable(Node[] nodes) {
114
115        int cnt = 0;
116        for (Node n : nodes) {
117            cnt += n.getLookup().lookupAll(Group.class).size();
118        }
119
120        return cnt > 0;
121    }
122}
123