KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > AddLoaderManuallyHid


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.openide.loaders;
21
22
23 import org.openide.filesystems.FileObject;
24 import java.lang.reflect.*;
25 import java.util.*;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28
29 import org.openide.util.Lookup;
30
31 /** Utility to add a loader to the loader pool.
32  * Should only be used in external execution mode!
33  * Requires core.jar in classpath.
34  * @author Jesse Glick
35  */

36 public final class AddLoaderManuallyHid {
37
38     private AddLoaderManuallyHid() {}
39
40     /** Add a loader to the pool (to the front of the free area). */
41     public static void addRemoveLoader(DataLoader l, boolean add) throws Exception JavaDoc {
42         // Initialize IDE:
43
// TopManager.getDefault();
44

45         // Now add the loader. Would be easy enough if we could directly access
46
// core classes, but then this test would have to be compiled with core.jar
47
// in the classpath...
48
Class JavaDoc lpnClazz = Class.forName("org.netbeans.core.LoaderPoolNode");
49         Field loadersF = lpnClazz.getDeclaredField("loaders");
50         loadersF.setAccessible(true);
51         List loaders = (List)loadersF.get(null);
52         if (add) {
53             if (loaders.contains(l)) throw new IllegalArgumentException JavaDoc();
54             loaders.add(0, l);
55         } else {
56             if (! loaders.contains(l)) throw new IllegalArgumentException JavaDoc();
57             loaders.remove(l);
58         }
59         
60         DataLoaderPool pool = DataLoaderPool.getDefault ();
61         if (add) {
62             l.addPropertyChangeListener((PropertyChangeListener JavaDoc)pool);
63         } else {
64             l.removePropertyChangeListener((PropertyChangeListener JavaDoc)pool);
65         }
66         // Simulate behavior of update(), but fire pool change immediately:
67
Field loadersArrayF = lpnClazz.getDeclaredField("loadersArray");
68         loadersArrayF.setAccessible(true);
69         loadersArrayF.set(null, null);
70         pool.fireChangeEvent(new ChangeEvent JavaDoc(pool));
71     }
72     
73 }
74
Popular Tags