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 */
24package com.sun.hotspot.igv.view.actions;
25
26import com.sun.hotspot.igv.settings.Settings;
27import com.sun.hotspot.igv.view.ExportCookie;
28import java.awt.event.InputEvent;
29import java.awt.event.KeyEvent;
30import java.io.File;
31import javax.swing.Action;
32import javax.swing.JFileChooser;
33import javax.swing.KeyStroke;
34import javax.swing.filechooser.FileFilter;
35import org.openide.util.*;
36import org.openide.util.actions.CallableSystemAction;
37
38/**
39 *
40 * @author Thomas Wuerthinger
41 */
42public final class ExportAction extends CallableSystemAction implements LookupListener {
43
44    private final Lookup lookup;
45    private final Lookup.Result<ExportCookie> result;
46
47    public ExportAction() {
48        putValue(Action.SHORT_DESCRIPTION, "Export current graph as SVG file");
49        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
50        lookup = Utilities.actionsGlobalContext();
51        result = lookup.lookup(new Lookup.Template<>(ExportCookie.class));
52        result.addLookupListener(this);
53        resultChanged(null);
54    }
55
56    @Override
57    public void resultChanged(LookupEvent e) {
58        super.setEnabled(result.allInstances().size() > 0);
59    }
60
61    @Override
62    public void performAction() {
63
64        JFileChooser fc = new JFileChooser();
65        fc.setFileFilter(new FileFilter() {
66
67            @Override
68            public boolean accept(File f) {
69                return true;
70            }
71
72            @Override
73            public String getDescription() {
74                return "SVG files (*.svg)";
75            }
76        });
77        fc.setCurrentDirectory(new File(Settings.get().get(Settings.DIRECTORY, Settings.DIRECTORY_DEFAULT)));
78
79
80        if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
81            File file = fc.getSelectedFile();
82            if (!file.getName().contains(".")) {
83                file = new File(file.getAbsolutePath() + ".svg");
84            }
85
86            File dir = file;
87            if (!dir.isDirectory()) {
88                dir = dir.getParentFile();
89            }
90
91            Settings.get().put(Settings.DIRECTORY, dir.getAbsolutePath());
92            ExportCookie cookie = Utilities.actionsGlobalContext().lookup(ExportCookie.class);
93            if (cookie != null) {
94                cookie.export(file);
95            }
96        }
97    }
98
99    @Override
100    public String getName() {
101        return NbBundle.getMessage(ExportAction.class, "CTL_ExportAction");
102    }
103
104    @Override
105    protected String iconResource() {
106        return "com/sun/hotspot/igv/view/images/export.png";
107    }
108
109    @Override
110    public HelpCtx getHelpCtx() {
111        return HelpCtx.DEFAULT_HELP;
112    }
113
114    @Override
115    protected boolean asynchronous() {
116        return false;
117    }
118}
119