KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > OpenFileAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.openfile;
21
22 import java.io.File JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import org.netbeans.modules.utilities.Manager;
25 import org.openide.filesystems.FileUtil;
26 import org.openide.loaders.DataObject;
27 import org.openide.nodes.Node;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.NbBundle;
30 import org.openide.util.UserCancelException;
31 import org.openide.util.actions.CallableSystemAction;
32 import org.openide.windows.TopComponent;
33 import org.openide.windows.WindowManager;
34
35 /**
36  * Action which allows user open file from disk. It is installed
37  * in Menu | File | Open file... .
38  *
39  * @author Jesse Glick
40  * @author Marian Petras
41  */

42 public class OpenFileAction extends CallableSystemAction {
43
44     public OpenFileAction() {
45         putValue("noIconInMenu", Boolean.TRUE);
46     }
47     
48     public String JavaDoc getName() {
49         return NbBundle.getMessage(OpenFileAction.class, "LBL_openFile");
50     }
51
52     public HelpCtx getHelpCtx() {
53         return new HelpCtx(OpenFileAction.class);
54     }
55
56     protected String JavaDoc iconResource() {
57         return "org/netbeans/modules/openfile/openFile.png"; // NOI18N
58
}
59
60     /**
61      * Creates and initializes a file chooser.
62      *
63      * @return the initialized file chooser
64      */

65     protected JFileChooser JavaDoc prepareFileChooser() {
66         JFileChooser JavaDoc chooser = new FileChooser();
67         File JavaDoc currDir = findStartingDirectory();
68         FileUtil.preventFileChooserSymlinkTraversal(chooser, currDir);
69         HelpCtx.setHelpIDString(chooser, getHelpCtx().getHelpID());
70         
71         return chooser;
72     }
73     
74     /**
75      * Displays the specified file chooser and returns a list of selected files.
76      *
77      * @param chooser file chooser to display
78      * @return array of selected files,
79      * @exception org.openide.util.UserCancelException
80      * if the user cancelled the operation
81      */

82     public static File JavaDoc[] chooseFilesToOpen(JFileChooser JavaDoc chooser)
83             throws UserCancelException {
84         File JavaDoc[] files;
85         do {
86             int selectedOption = chooser.showOpenDialog(
87                 WindowManager.getDefault().getMainWindow());
88             
89             if (selectedOption != JFileChooser.APPROVE_OPTION) {
90                 throw new UserCancelException();
91             }
92             files = chooser.getSelectedFiles();
93         } while (files.length == 0);
94         return files;
95     }
96     
97     /**
98      * {@inheritDoc} Displays a file chooser dialog
99      * and opens the selected files.
100      */

101     public void performAction() {
102         if (!Manager.actionActivated(this)) {
103             return;
104         }
105         try {
106             JFileChooser JavaDoc chooser = prepareFileChooser();
107             File JavaDoc[] files;
108             try {
109                 files = chooseFilesToOpen(chooser);
110             } catch (UserCancelException ex) {
111                 return;
112             }
113             for (int i = 0; i < files.length; i++) {
114                 OpenFile.openFile(files[i], -1);
115             }
116         } finally {
117             Manager.actionFinished(this);
118         }
119     }
120     
121     protected boolean asynchronous() {
122         return false;
123     }
124
125     /**
126      * Try to find a directory to open the chooser open.
127      * If there is a file among selected nodes (e.g. open editor windows),
128      * use that directory; else just stick to the user's home directory.
129      */

130     private static File JavaDoc findStartingDirectory() {
131         Node[] nodes = TopComponent.getRegistry().getActivatedNodes();
132         for (int i = 0; i < nodes.length; i++) {
133             DataObject d = (DataObject) nodes[i].getCookie(DataObject.class);
134             if (d != null) {
135                 File JavaDoc f = FileUtil.toFile(d.getPrimaryFile());
136                 if (f != null) {
137                     if (f.isFile()) {
138                         f = f.getParentFile();
139                     }
140                     return f;
141                 }
142             }
143         }
144         // Backup:
145
return new File JavaDoc(System.getProperty("user.home"));
146     }
147
148 }
149
Popular Tags