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:
046: import org.openide.DialogDisplayer;
047: import org.openide.NotifyDescriptor;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileUtil;
050: import org.openide.util.Lookup;
051: import org.openide.util.NbBundle;
052:
053: /**
054: * Opens files when requested. Main functionality.
055: * @author Jaroslav Tulach, Jesse Glick
056: * @author Marian Petras
057: */
058: public final class OpenFile {
059:
060: /** do not instantiate */
061: private OpenFile() {
062: }
063:
064: /**
065: * Open a file (object) at the beginning.
066: * @param fileObject the file to open
067: * @param line
068: * @return error message or null on success
069: * @usecase API
070: */
071: public static String open(FileObject fileObject, int line) {
072: for (OpenFileImpl impl : Lookup.getDefault().lookupAll(
073: OpenFileImpl.class)) {
074: if (impl.open(fileObject, line)) {
075: return null;
076: }
077: }
078: return NbBundle.getMessage(OpenFile.class,
079: "MSG_FileIsNotPlainFile", fileObject);
080: }
081:
082: /**
083: * Opens a file.
084: *
085: * @param file file to open (must exist)
086: * @param line line number to try to open to (starting at zero),
087: * or <code>-1</code> to ignore
088: * @return null on success, otherwise the error message
089: * @usecase CallbackImpl, OpenFileAction
090: */
091: static String openFile(File file, int line) {
092: String msg = checkFileExists(file);
093: if (msg != null) {
094: return msg;
095: }
096:
097: FileObject fileObject;
098: fileObject = FileUtil
099: .toFileObject(FileUtil.normalizeFile(file));
100: if (fileObject != null) {
101: return open(fileObject, line);
102: }
103: return NbBundle.getMessage(OpenFile.class,
104: "MSG_FileDoesNotExist", file);
105: }
106:
107: /**
108: * Checks whether the specified file exists.
109: * If the file doesn't exists, displays a message.
110: * <p>
111: * The code for displaying the message is running in a separate thread
112: * so that it does not block the current thread.
113: *
114: * @param file file to check for existence
115: * @return null on success, otherwise the error message
116: */
117: private static String checkFileExists(File file) {
118: final String errMsgKey;
119: if (!file.exists()) {
120: errMsgKey = "MSG_fileNotFound"; //NOI18N
121: } else if (isSpecifiedByUNCPath(file)) {
122: errMsgKey = "MSG_UncNotSupported"; //NOI18N
123: } else if (!file.isFile() && !file.isDirectory()) {
124: errMsgKey = "MSG_fileNotFound"; //NOI18N
125: } else {
126: return null;
127: }
128:
129: final String fileName = file.toString();
130: final String msg = NbBundle.getMessage(OpenFile.class,
131: errMsgKey, fileName);
132: return msg;
133: }
134:
135: /**
136: * Checks whether a given file is specified by an UNC path.
137: *
138: * @param file existing file to check
139: * @return <code>true</code> if the file is specified by UNC path;
140: * <code>false</code> otherwise
141: */
142: static boolean isSpecifiedByUNCPath(File file) {
143: assert file != null && file.exists();
144:
145: file = FileUtil.normalizeFile(file);
146: return file.getPath().startsWith("\\\\"); //NOI18N
147: }
148:
149: }
|