KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > EditorTestLookup


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 the LaTeX module.
16  * The Initial Developer of the Original Software is Jan Lahoda.
17  * Portions created by Jan Lahoda_ are Copyright (C) 2002-2004.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Jan Lahoda.
21  */

22 package org.netbeans.modules.editor;
23
24
25 import java.beans.PropertyVetoException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import junit.framework.Assert;
29 import org.openide.filesystems.Repository;
30 import org.openide.filesystems.XMLFileSystem;
31 import org.openide.util.Lookup;
32 import org.openide.util.lookup.Lookups;
33 import org.openide.util.lookup.ProxyLookup;
34
35
36 /**
37  * Inspired by org.netbeans.api.project.TestUtil.
38  *
39  * @author Miloslav Metelka, Jan Lahoda
40  */

41 public class EditorTestLookup extends ProxyLookup {
42     
43     public static EditorTestLookup DEFAULT_LOOKUP = null;
44     
45     static {
46         EditorTestLookup.class.getClassLoader().setDefaultAssertionStatus(true);
47         System.setProperty("org.openide.util.Lookup", EditorTestLookup.class.getName());
48         Assert.assertEquals(EditorTestLookup.class, Lookup.getDefault().getClass());
49     }
50     
51     public EditorTestLookup() {
52         Assert.assertNull(DEFAULT_LOOKUP);
53         DEFAULT_LOOKUP = this;
54     }
55     
56     /**
57      * Set the global default lookup with some fixed instances including META-INF/services/*.
58      */

59     public static void setLookup(Object JavaDoc[] instances, ClassLoader JavaDoc cl) {
60         DEFAULT_LOOKUP.setLookups(new Lookup[] {
61             Lookups.fixed(instances),
62             Lookups.metaInfServices(cl),
63             Lookups.singleton(cl),
64         });
65     }
66     
67     /**
68      * Set the global default lookup with the specified content.
69      *
70      * @param layers xml-layer URLs to be present in the system filesystem.
71      * @param instances object instances to be present in the default lookup.
72      */

73     public static void setLookup(URL JavaDoc[] layers, Object JavaDoc[] instances, ClassLoader JavaDoc cl)
74     throws IOException JavaDoc, PropertyVetoException JavaDoc {
75         
76         XMLFileSystem system = new XMLFileSystem();
77         system.setXmlUrls(layers);
78         Repository repository = new Repository(system);
79
80         Object JavaDoc[] lookupContent = new Object JavaDoc[instances.length + 1];
81         lookupContent[0] = repository;
82         System.arraycopy(instances, 0, lookupContent, 1, instances.length);
83         
84         DEFAULT_LOOKUP.setLookup(lookupContent, cl);
85     }
86     
87 }
88
Popular Tags