001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.openfile;
043:
044: import java.io.File;
045: import javax.swing.JFileChooser;
046: import org.netbeans.modules.utilities.Manager;
047: import org.openide.filesystems.FileUtil;
048: import org.openide.loaders.DataObject;
049: import org.openide.nodes.Node;
050: import org.openide.util.HelpCtx;
051: import org.openide.util.NbBundle;
052: import org.openide.util.UserCancelException;
053: import org.openide.util.actions.CallableSystemAction;
054: import org.openide.windows.TopComponent;
055: import org.openide.windows.WindowManager;
056:
057: /**
058: * Action which allows user open file from disk. It is installed
059: * in Menu | File | Open file... .
060: *
061: * @author Jesse Glick
062: * @author Marian Petras
063: */
064: public class OpenFileAction extends CallableSystemAction {
065:
066: public OpenFileAction() {
067: putValue("noIconInMenu", Boolean.TRUE);
068: }
069:
070: public String getName() {
071: return NbBundle
072: .getMessage(OpenFileAction.class, "LBL_openFile");
073: }
074:
075: public HelpCtx getHelpCtx() {
076: return new HelpCtx(OpenFileAction.class);
077: }
078:
079: protected String iconResource() {
080: return "org/netbeans/modules/openfile/openFile.png"; // NOI18N
081: }
082:
083: /**
084: * Creates and initializes a file chooser.
085: *
086: * @return the initialized file chooser
087: */
088: protected JFileChooser prepareFileChooser() {
089: JFileChooser chooser = new FileChooser();
090: File currDir = findStartingDirectory();
091: FileUtil.preventFileChooserSymlinkTraversal(chooser, currDir);
092: HelpCtx.setHelpIDString(chooser, getHelpCtx().getHelpID());
093:
094: return chooser;
095: }
096:
097: /**
098: * Displays the specified file chooser and returns a list of selected files.
099: *
100: * @param chooser file chooser to display
101: * @return array of selected files,
102: * @exception org.openide.util.UserCancelException
103: * if the user cancelled the operation
104: */
105: public static File[] chooseFilesToOpen(JFileChooser chooser)
106: throws UserCancelException {
107: File[] files;
108: do {
109: int selectedOption = chooser.showOpenDialog(WindowManager
110: .getDefault().getMainWindow());
111:
112: if (selectedOption != JFileChooser.APPROVE_OPTION) {
113: throw new UserCancelException();
114: }
115: files = chooser.getSelectedFiles();
116: } while (files.length == 0);
117: return files;
118: }
119:
120: /**
121: * {@inheritDoc} Displays a file chooser dialog
122: * and opens the selected files.
123: */
124: public void performAction() {
125: if (!Manager.actionActivated(this )) {
126: return;
127: }
128: try {
129: JFileChooser chooser = prepareFileChooser();
130: File[] files;
131: try {
132: files = chooseFilesToOpen(chooser);
133: } catch (UserCancelException ex) {
134: return;
135: }
136: for (int i = 0; i < files.length; i++) {
137: OpenFile.openFile(files[i], -1);
138: }
139: } finally {
140: Manager.actionFinished(this );
141: }
142: }
143:
144: protected boolean asynchronous() {
145: return false;
146: }
147:
148: /**
149: * Try to find a directory to open the chooser open.
150: * If there is a file among selected nodes (e.g. open editor windows),
151: * use that directory; else just stick to the user's home directory.
152: */
153: private static File findStartingDirectory() {
154: Node[] nodes = TopComponent.getRegistry().getActivatedNodes();
155: for (int i = 0; i < nodes.length; i++) {
156: DataObject d = (DataObject) nodes[i]
157: .getCookie(DataObject.class);
158: if (d != null) {
159: File f = FileUtil.toFile(d.getPrimaryFile());
160: if (f != null) {
161: if (f.isFile()) {
162: f = f.getParentFile();
163: }
164: return f;
165: }
166: }
167: }
168: // Backup:
169: return new File(System.getProperty("user.home"));
170: }
171:
172: }
|