KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.openide.DialogDisplayer;
25 import org.openide.NotifyDescriptor;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.util.Lookup;
29 import org.openide.util.NbBundle;
30
31 /**
32  * Opens files when requested. Main functionality.
33  * @author Jaroslav Tulach, Jesse Glick
34  * @author Marian Petras
35  */

36 public final class OpenFile {
37
38     /** do not instantiate */
39     private OpenFile() {}
40
41     /**
42      * Open a file (object) at the beginning.
43      * @param fileObject the file to open
44      * @usecase API
45      */

46     public static boolean open(FileObject fileObject, int line) {
47         for (OpenFileImpl impl : Lookup.getDefault().lookupAll(OpenFileImpl.class)) {
48             if (impl.open(fileObject, line)) {
49                 return true;
50             }
51         }
52         return false;
53     }
54     
55     /**
56      * Opens a file.
57      *
58      * @param file file to open (must exist)
59      * @param line line number to try to open to (starting at zero),
60      * or <code>-1</code> to ignore
61      * @return true on success, false on failure
62      * @usecase CallbackImpl, OpenFileAction
63      */

64     static boolean openFile(File JavaDoc file, int line) {
65         if (!checkFileExists(file)) {
66             return false;
67         }
68                               
69         FileObject fileObject;
70         fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(file));
71         if (fileObject != null) {
72             return open(fileObject, line);
73         }
74         return false;
75     }
76     
77     /**
78      * Checks whether the specified file exists.
79      * If the file doesn't exists, displays a message.
80      * <p>
81      * The code for displaying the message is running in a separate thread
82      * so that it does not block the current thread.
83      *
84      * @param file file to check for existence
85      * @return <code>true</code> if the file exists and is a plain file,
86      * <code>false</code> otherwise
87      */

88     private static boolean checkFileExists(File JavaDoc file) {
89         final String JavaDoc errMsgKey;
90         if (!file.exists()) {
91             errMsgKey = "MSG_fileNotFound"; //NOI18N
92
} else if (isSpecifiedByUNCPath(file)) {
93             errMsgKey = "MSG_UncNotSupported"; //NOI18N
94
} else if (!file.isFile() && !file.isDirectory()) {
95             errMsgKey = "MSG_fileNotFound"; //NOI18N
96
} else {
97             return true;
98         }
99         
100         final String JavaDoc fileName = file.toString();
101         final String JavaDoc msg = NbBundle.getMessage(OpenFile.class,
102                                                errMsgKey,
103                                                fileName);
104         DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message(msg));
105         return false;
106     }
107
108     /**
109      * Checks whether a given file is specified by an UNC path.
110      *
111      * @param file existing file to check
112      * @return <code>true</code> if the file is specified by UNC path;
113      * <code>false</code> otherwise
114      */

115     static boolean isSpecifiedByUNCPath(File JavaDoc file) {
116         assert file != null && file.exists();
117
118         file = FileUtil.normalizeFile(file);
119         return file.getPath().startsWith("\\\\"); //NOI18N
120
}
121     
122 }
123
Popular Tags