2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Vincent COUVERT
4 * Copyright (C) 2011 - Scilab Enterprises - Clement DAVID
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
17 package org.scilab.modules.xcos.actions;
19 import java.awt.Toolkit;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.KeyEvent;
23 import java.io.IOException;
25 import javax.swing.JButton;
26 import javax.swing.JFileChooser;
27 import javax.swing.filechooser.FileFilter;
29 import org.scilab.modules.graph.ScilabGraph;
30 import org.scilab.modules.graph.actions.base.DefaultAction;
31 import org.scilab.modules.gui.bridge.filechooser.SwingScilabFileChooser;
32 import org.scilab.modules.gui.filechooser.ScilabFileChooser;
33 import org.scilab.modules.gui.menuitem.MenuItem;
34 import org.scilab.modules.xcos.Xcos;
35 import org.scilab.modules.xcos.configuration.ConfigurationManager;
36 import org.scilab.modules.xcos.io.XcosFileType;
37 import org.scilab.modules.xcos.utils.XcosMessages;
40 * File opening management
42 @SuppressWarnings(value = { "serial" })
43 public final class OpenAction extends DefaultAction {
44 /** Name of the action */
45 public static final String NAME = XcosMessages.OPEN;
46 /** Icon name of the action */
47 public static final String SMALL_ICON = "document-open";
48 /** Mnemonic key of the action */
49 public static final int MNEMONIC_KEY = KeyEvent.VK_O;
50 /** Accelerator key for the action */
51 public static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
57 * associated Scilab Graph
59 public OpenAction(ScilabGraph scilabGraph) {
64 * Create a menu to add in Scilab Graph menu bar
67 * associated Scilab Graph
70 public static MenuItem createMenu(ScilabGraph scilabGraph) {
71 return createMenu(scilabGraph, OpenAction.class);
75 * Create a button to add in Scilab Graph tool bar
78 * associated Scilab Graph
81 public static JButton createButton(ScilabGraph scilabGraph) {
82 return createButton(scilabGraph, OpenAction.class);
88 * @see org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
91 public void actionPerformed(ActionEvent e) {
92 final SwingScilabFileChooser fc = createFileChooser();
94 /* Configure the file chooser */
95 configureFileFilters(fc);
96 ConfigurationManager.configureCurrentDirectory(fc);
99 displayAndOpen(fc, getGraph(e).getAsComponent());
100 } catch (IOException e1) {
101 e1.printStackTrace();
106 * Helpers functions to share file chooser code
109 public static SwingScilabFileChooser createFileChooser() {
110 final SwingScilabFileChooser fc = ((SwingScilabFileChooser) ScilabFileChooser.createFileChooser().getAsSimpleFileChooser());
112 fc.setTitle(XcosMessages.OPEN);
113 fc.setUiDialogType(JFileChooser.OPEN_DIALOG);
114 fc.setMultipleSelection(true);
118 public static void configureFileFilters(final JFileChooser fc) {
119 fc.setAcceptAllFileFilterUsed(true);
121 final FileFilter[] filters = XcosFileType.getLoadingFilters();
122 for (FileFilter fileFilter : filters) {
123 fc.addChoosableFileFilter(fileFilter);
125 fc.setFileFilter(fc.getAcceptAllFileFilter());
128 protected static void displayAndOpen(final SwingScilabFileChooser fc, final java.awt.Component component) throws IOException {
129 final int status = fc.showOpenDialog(component);
130 if (status != JFileChooser.APPROVE_OPTION) {
134 final File onlySelected = fc.getSelectedFile();
135 if (onlySelected != null) {
136 Xcos.getInstance().open(onlySelected.getCanonicalPath(), 0);
139 final File[] multiSelected = fc.getSelectedFiles();
140 for (File file : multiSelected) {
141 if (file != onlySelected) {
142 Xcos.getInstance().open(file.getCanonicalPath(), 0);