KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > ContextProvider


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.settings;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25 import org.openide.filesystems.*;
26 import org.openide.util.Lookup;
27 import org.openide.util.lookup.Lookups;
28
29 /** Factory producing proxy objects allowing to provide own context via Lookup.Provider.
30  *
31  * @author Jan Pokorsky
32  */

33 public final class ContextProvider {
34
35     /** Creates a new instance of ContextProvider */
36     private ContextProvider() {
37     }
38
39     /** create a proxy which delegates to original Writer w and provides
40      * source FileObject via Lookup.Provider
41      */

42     public static Writer JavaDoc createWriterContextProvider(Writer JavaDoc w, FileObject src) {
43         return new WriterProvider(w, src);
44     }
45     
46     /** create a proxy which delegates to original Reader r and provides
47      * source FileObject via Lookup.Provider
48      */

49     public static Reader JavaDoc createReaderContextProvider(Reader JavaDoc r, FileObject src) {
50         return new ReaderProvider(r, src);
51     }
52         
53     private static final class WriterProvider extends Writer JavaDoc implements Lookup.Provider {
54         private final Writer JavaDoc orig;
55         private final FileObject src;
56         private Lookup lookup;
57
58         public WriterProvider(Writer JavaDoc w, FileObject src) {
59             this.orig = w;
60             this.src = src;
61         }
62
63         public void close() throws IOException JavaDoc {
64             orig.close();
65         }
66
67         public void flush() throws IOException JavaDoc {
68             orig.flush();
69         }
70
71         public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
72             orig.write(cbuf, off, len);
73         }
74
75         public Lookup getLookup() {
76             if (lookup == null) {
77                 lookup = Lookups.singleton(new FileObjectContext(src));
78             }
79             return lookup;
80         }
81         
82     }
83     
84     private static final class ReaderProvider extends Reader JavaDoc implements Lookup.Provider {
85         private final Reader JavaDoc orig;
86         private final FileObject src;
87         private Lookup lookup;
88         
89         public ReaderProvider(Reader JavaDoc r, FileObject src) {
90             this.orig = r;
91             this.src = src;
92         }
93         
94         public void close() throws IOException JavaDoc {
95             orig.close();
96         }
97         
98         public int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
99             return orig.read(cbuf, off, len);
100         }
101         
102         public Lookup getLookup() {
103             if (lookup == null) {
104                 lookup = Lookups.singleton(new FileObjectContext(src));
105             }
106             return lookup;
107         }
108         
109     }
110     
111     /** The Restricted FileObject implementation allowing to get just
112      * read-only informations about name and location. It should prevent
113      * any manipulation with file or its content.
114      */

115     private static final class FileObjectContext extends FileObject {
116         private static final String JavaDoc UNSUPPORTED = "The Restricted FileObject" + //NOI18N
117
" implementation allowing to get just read-only informations about" + //NOI18N
118
" name and location. It should prevent any manipulation with file" + //NOI18N
119
" or its content."; //NOI18N
120
private final FileObject fo;
121         
122         public FileObjectContext(FileObject fo) {
123             this.fo = fo;
124         }
125         
126         public void addFileChangeListener(FileChangeListener fcl) {
127             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
128         }
129         
130         public FileObject createData(String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
131             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
132         }
133         
134         public FileObject createFolder(String JavaDoc name) throws IOException JavaDoc {
135             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
136         }
137         
138         public void delete(FileLock lock) throws IOException JavaDoc {
139             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
140         }
141         
142         public Object JavaDoc getAttribute(String JavaDoc attrName) {
143             return fo.getAttribute(attrName);
144         }
145         
146         public java.util.Enumeration JavaDoc<String JavaDoc> getAttributes() {
147             return fo.getAttributes();
148         }
149         
150         public FileObject[] getChildren() {
151             return new FileObject[0];
152         }
153         
154         public String JavaDoc getExt() {
155             return fo.getExt(); //NOI18N
156
}
157         
158         public FileObject getFileObject(String JavaDoc name, String JavaDoc ext) {
159             return null;
160         }
161         
162         public FileSystem getFileSystem() throws FileStateInvalidException {
163             return fo.getFileSystem();
164         }
165         
166         public java.io.InputStream JavaDoc getInputStream() throws java.io.FileNotFoundException JavaDoc {
167             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
168         }
169         
170         public String JavaDoc getName() {
171             return fo.getName();
172         }
173         
174         public java.io.OutputStream JavaDoc getOutputStream(FileLock lock) throws java.io.IOException JavaDoc {
175             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
176         }
177         
178         public FileObject getParent() {
179             return fo.getParent();
180         }
181         
182         public long getSize() {
183             return fo.getSize();
184         }
185         
186         public boolean isData() {
187             return true;
188         }
189         
190         public boolean isFolder() {
191             return false;
192         }
193         
194         public boolean isReadOnly() {
195             return fo.isReadOnly();
196         }
197         
198         public boolean isRoot() {
199             return false;
200         }
201         
202         public boolean isValid() {
203             return fo.isValid();
204         }
205         
206         public java.util.Date JavaDoc lastModified() {
207             return fo.lastModified();
208         }
209         
210         public FileLock lock() throws IOException JavaDoc {
211             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
212         }
213         
214         public void removeFileChangeListener(FileChangeListener fcl) {
215             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
216         }
217         
218         public void rename(FileLock lock, String JavaDoc name, String JavaDoc ext) throws IOException JavaDoc {
219             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
220         }
221         
222         public void setAttribute(String JavaDoc attrName, Object JavaDoc value) throws IOException JavaDoc {
223             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
224         }
225         
226         public void setImportant(boolean b) {
227             throw new UnsupportedOperationException JavaDoc(UNSUPPORTED);
228         }
229         
230     }
231     
232 }
233
Popular Tags