KickJava   Java API By Example, From Geeks To Geeks.

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


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.editor.options;
21
22 import java.beans.beancontext.BeanContextChild JavaDoc;
23 import java.beans.beancontext.BeanContext JavaDoc;
24 import java.beans.beancontext.BeanContextMembershipListener JavaDoc;
25 import java.beans.beancontext.BeanContextMembershipEvent JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import org.netbeans.editor.Settings;
29 import org.openide.options.ContextSystemOption;
30 import org.openide.options.SystemOption;
31
32 /**
33  * Listener that adds/removes the initializers corresponding
34  * to the options to the Settings and performs the resetting
35  * of the Settings.
36  *
37  * @author Miloslav Metelka
38  * @version 1.00
39  */

40 class ContextOptionsListener implements BeanContextMembershipListener JavaDoc {
41     
42     /** Whether debug messages should be displayed */
43     private static final boolean debug
44         = Boolean.getBoolean("netbeans.debug.editor.options"); // NOI18N
45

46     /** Only one shared instance is used. The side-effect advantage is
47      * that BeanContextSupport will not add the same listener twice.
48      */

49     private static final ContextOptionsListener sharedListener
50         = new ContextOptionsListener();
51
52     /** Process the existing options added to the context system option
53      * and start listening on the changes.
54      */

55     static void processExistingAndListen(ContextSystemOption cso) {
56         BeanContextChild JavaDoc bcc = cso.getBeanContextProxy();
57         
58         // Start listening first
59
if (bcc instanceof BeanContext JavaDoc) {
60             ((BeanContext JavaDoc)bcc).addBeanContextMembershipListener(sharedListener);
61         }
62         
63         // Process all the currently added options
64
SystemOption[] sos = cso.getOptions();
65         if (sos != null) {
66             sharedListener.processInitializers(Arrays.asList(sos).iterator(), false);
67         }
68     }
69     
70     private ContextOptionsListener() {
71     }
72
73     public void childrenAdded(BeanContextMembershipEvent JavaDoc bcme) {
74         processInitializers(bcme.iterator(), false);
75     }
76
77     public void childrenRemoved(BeanContextMembershipEvent JavaDoc bcme) {
78         processInitializers(bcme.iterator(), true);
79     }
80
81     private void processInitializers(Iterator JavaDoc it, boolean remove) {
82         while (it.hasNext()) {
83             Object JavaDoc o = it.next();
84             if (o instanceof OptionSupport) {
85                 OptionSupport os = (OptionSupport)o;
86                 Settings.Initializer si = os.getSettingsInitializer();
87                 // Remove the old one
88
Settings.removeInitializer(si.getName());
89                 if (!remove) { // add the new one
90
Settings.addInitializer(si, Settings.OPTION_LEVEL);
91                 }
92
93                 if (debug) {
94                     System.err.println((remove ? "Removed" : "Refreshed") // NOI18N
95
+ " initializer=" + si.getName()); // NOI18N
96
}
97             }
98         }
99         
100         /* Reset the settings so that the new initializers take effect
101          * or the old are removed.
102          */

103         Settings.reset();
104     }
105
106 }
107
Popular Tags