View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
50      
51      //--- Data field(s) ---
52      
53      private FileProvider fp;
54  
55      private Preferences prefs = Preferences.getInstance();
56  
57  	private static Logger logger = Logger.getLogger(OpenFileAction.class);
58          
59      //--- Constructor(s) ---
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      //--- Method(s) ---
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  				//  Console.getInstance().println(e.getMessage());
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 }