KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > editor > IDEInitializer


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.options.editor;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLStreamHandler JavaDoc;
27 import java.net.URLStreamHandlerFactory JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import junit.framework.Assert;
30 import org.netbeans.junit.Manager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.LocalFileSystem;
35 import org.openide.filesystems.MultiFileSystem;
36 import org.openide.filesystems.Repository;
37 import org.openide.filesystems.XMLFileSystem;
38 import org.openide.util.Lookup;
39 import org.openide.util.lookup.Lookups;
40 import org.openide.util.lookup.ProxyLookup;
41
42
43 /**
44  * Inspired by org.netbeans.api.project.TestUtil.
45  *
46  * @author Miloslav Metelka, Jan Lahoda
47  */

48 public class IDEInitializer extends ProxyLookup {
49     
50     public static IDEInitializer DEFAULT_LOOKUP = null;
51     private static LocalFileSystem lfs;
52     
53     static {
54         IDEInitializer.class.getClassLoader ().setDefaultAssertionStatus (true);
55         System.setProperty ("org.openide.util.Lookup", IDEInitializer.class.getName ());
56         Assert.assertEquals (IDEInitializer.class, Lookup.getDefault ().getClass ());
57     }
58     
59     public IDEInitializer () {
60         Assert.assertNull (DEFAULT_LOOKUP);
61         DEFAULT_LOOKUP = this;
62         URL.setURLStreamHandlerFactory (new MyURLHandlerFactory ());
63     }
64     
65     /**
66      * Set the global default lookup with the specified content.
67      *
68      * @param layers xml-layer URLs to be present in the system filesystem.
69      * @param instances object instances to be present in the default lookup.
70      */

71     public static void setup (
72         String JavaDoc[] layers,
73         Object JavaDoc[] instances
74     ) {
75         ClassLoader JavaDoc classLoader = IDEInitializer.class.getClassLoader ();
76         File JavaDoc workDir = new File JavaDoc (Manager.getWorkDirPath ());
77         URL JavaDoc[] urls = new URL JavaDoc [layers.length];
78         int i, k = urls.length;
79         for (i = 0; i < k; i++)
80             urls [i] = classLoader.getResource (layers [i]);
81
82         // 1) create repository
83
XMLFileSystem systemFS = new XMLFileSystem ();
84         lfs = new LocalFileSystem ();
85         try {
86             systemFS.setXmlUrls (urls);
87             lfs.setRootDirectory (workDir);
88         } catch (Exception JavaDoc ex) {
89             ex.printStackTrace ();
90         }
91         MyFileSystem myFileSystem = new MyFileSystem (
92             new FileSystem [] {lfs, systemFS}
93         );
94         Repository repository = new Repository (myFileSystem);
95
96         Object JavaDoc[] lookupContent = new Object JavaDoc [instances.length + 1];
97         lookupContent [0] = repository;
98         System.arraycopy (instances, 0, lookupContent, 1, instances.length);
99         
100         DEFAULT_LOOKUP.setLookups (new Lookup[] {
101             Lookups.fixed (lookupContent),
102             Lookups.metaInfServices (classLoader),
103             Lookups.singleton (classLoader),
104         });
105         Assert.assertEquals (myFileSystem, Repository.getDefault ().getDefaultFileSystem ());
106     }
107     
108     public static void cleanWorkDir () {
109         try {
110             Enumeration JavaDoc en = lfs.getRoot ().getChildren (false);
111             while (en.hasMoreElements ())
112                 ((FileObject) en.nextElement ()).delete ();
113         } catch (IOException JavaDoc ex) {
114             ex.printStackTrace ();
115         }
116     }
117     
118     private static class MyFileSystem extends MultiFileSystem {
119         public MyFileSystem (FileSystem[] fileSystems) {
120             super (fileSystems);
121             try {
122                 setSystemName ("Yarda");
123             } catch (PropertyVetoException JavaDoc ex) {
124                 ex.printStackTrace();
125             }
126         }
127     }
128     
129     private static class MyURLHandlerFactory implements URLStreamHandlerFactory JavaDoc {
130         public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
131             if (protocol.equals ("nbfs")) {
132                 return FileUtil.nbfsURLStreamHandler ();
133             }
134             return null;
135         }
136     }
137 }
138
Popular Tags