1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.action;
21
22 import org.apache.log4j.Logger;
23
24 import java.awt.event.ActionEvent;
25 import java.awt.event.KeyEvent;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28 import java.io.File;
29 import java.io.IOException;
30 import javax.swing.AbstractAction;
31 import javax.swing.Action;
32 import javax.swing.KeyStroke;
33 import org.xnap.XNap;
34 import org.xnap.action.AbstractXNapAction;
35 import org.xnap.gui.FileProvider;
36 import org.xnap.gui.util.IconHelper;
37 import org.xnap.util.Preferences;
38 import org.xnap.util.launcher.LauncherManager;
39
40 /***
41 * Opens the files provided by {@link FileProvider} using the
42 * {@link LauncherManager} for actually opening them. OpenFileAction can be
43 * disabled/enabled.
44 */
45 public class OpenFileAction extends AbstractAction
46 implements PropertyChangeListener
47 {
48
49
50
51
52
53 private FileProvider fp;
54
55 private Preferences prefs = Preferences.getInstance();
56
57 private static Logger logger = Logger.getLogger(OpenFileAction.class);
58
59
60
61 public OpenFileAction(FileProvider fp)
62 {
63 this.fp = fp;
64
65 putValue(Action.NAME, XNap.tr("Open"));
66 putValue(Action.SHORT_DESCRIPTION, XNap.tr("Open selected files"));
67 putValue(IconHelper.XNAP_ICON, "fileopen.png");
68 putValue(AbstractXNapAction.DEFAULT_KEYSTROKE,
69 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
70 putValue(Action.ACTION_COMMAND_KEY, "openFileAction");
71
72 prefs.addPropertyChangeListener("fileLauncherCmd", this);
73 prefs.addPropertyChangeListener("fileLauncherType", this);
74
75 setEnabled(LauncherManager.isEnabled());
76 }
77
78
79
80 /***
81 * Collects the files and opens them using the {@link LauncherManager}.
82 */
83 public void actionPerformed(ActionEvent event)
84 {
85 File[] files = fp.getFiles();
86
87 if (files == null)
88 return;
89
90 for (int i = 0; i < files.length; i++) {
91 if (files[i] == null || files[i].length() == 0) {
92 continue;
93 }
94
95 try {
96 LauncherManager.open(files[i]);
97 }
98 catch (IOException e) {
99
100 logger.debug("error opening file", e);
101 }
102 }
103 }
104
105 public void propertyChange(PropertyChangeEvent e)
106 {
107 setEnabled(LauncherManager.isEnabled());
108 }
109
110 }