KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > completion > CompletionSettings


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.completion;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.editor.Settings;
30 import org.netbeans.editor.SettingsChangeEvent;
31 import org.netbeans.editor.SettingsChangeListener;
32 import org.netbeans.editor.Utilities;
33 import org.netbeans.editor.ext.ExtSettingsDefaults;
34 import org.netbeans.editor.ext.ExtSettingsNames;
35
36 /**
37  * Maintenance of the editor settings related to the code completion.
38  *
39  * @author Miloslav Metelka
40  * @version 1.00
41  */

42
43 public final class CompletionSettings implements SettingsChangeListener {
44     
45     public static final CompletionSettings INSTANCE = new CompletionSettings();
46     
47     private static final Object JavaDoc NULL_VALUE = new Object JavaDoc();
48     
49     private Reference JavaDoc<JTextComponent JavaDoc> editorComponentRef;
50     
51     private Map JavaDoc<String JavaDoc, Object JavaDoc> settingName2value = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
52     
53     private CompletionSettings() {
54         Settings.addSettingsChangeListener(this);
55     }
56     
57     public boolean completionAutoPopup() {
58         return ((Boolean JavaDoc)getValue(
59                 ExtSettingsNames.COMPLETION_AUTO_POPUP,
60                 ExtSettingsDefaults.defaultCompletionAutoPopup)
61         ).booleanValue();
62     }
63     
64     public int completionAutoPopupDelay() {
65         return ((Integer JavaDoc)getValue(
66                 ExtSettingsNames.COMPLETION_AUTO_POPUP_DELAY,
67                 ExtSettingsDefaults.defaultCompletionAutoPopupDelay)
68         ).intValue();
69     }
70     
71     public boolean documentationAutoPopup() {
72         return ((Boolean JavaDoc)getValue(
73                 ExtSettingsNames.JAVADOC_AUTO_POPUP,
74                 ExtSettingsDefaults.defaultJavaDocAutoPopup)
75         ).booleanValue();
76     }
77     
78     public int documentationAutoPopupDelay() {
79         return ((Integer JavaDoc)getValue(
80                 ExtSettingsNames.JAVADOC_AUTO_POPUP_DELAY,
81                 ExtSettingsDefaults.defaultJavaDocAutoPopupDelay)
82         ).intValue();
83     }
84     
85     public Dimension JavaDoc completionPopupMaximumSize() {
86         return (Dimension JavaDoc)getValue(
87                 ExtSettingsNames.COMPLETION_PANE_MAX_SIZE,
88                 ExtSettingsDefaults.defaultCompletionPaneMaxSize);
89     }
90     
91     public Dimension JavaDoc documentationPopupPreferredSize() {
92         return (Dimension JavaDoc)getValue(
93                 ExtSettingsNames.JAVADOC_PREFERRED_SIZE,
94                 ExtSettingsDefaults.defaultJavaDocPreferredSize);
95     }
96     
97     public Color JavaDoc documentationBackgroundColor() {
98         return (Color JavaDoc)CompletionSettings.INSTANCE.getValue(
99                 ExtSettingsNames.JAVADOC_BG_COLOR,
100                 ExtSettingsDefaults.defaultJavaDocBGColor);
101     }
102
103     public boolean completionInstantSubstitution() {
104         return ((Boolean JavaDoc)getValue(
105                 ExtSettingsNames.COMPLETION_INSTANT_SUBSTITUTION,
106                 ExtSettingsDefaults.defaultCompletionInstantSubstitution)
107         ).booleanValue();
108     }
109     
110     public void notifyEditorComponentChange(JTextComponent JavaDoc newEditorComponent) {
111         this.editorComponentRef = new WeakReference JavaDoc<JTextComponent JavaDoc>(newEditorComponent);
112         clearSettingValues();
113     }
114     
115     public Object JavaDoc getValue(String JavaDoc settingName) {
116         Object JavaDoc value;
117         synchronized (this) {
118             value = settingName2value.get(settingName);
119         }
120         
121         if (value == null) {
122             JTextComponent JavaDoc c = editorComponentRef.get();
123             if (c != null) {
124                 Class JavaDoc kitClass = Utilities.getKitClass(c);
125                 if (kitClass != null) {
126                     value = Settings.getValue(kitClass, settingName);
127                     if (value == null) {
128                         value = NULL_VALUE;
129                     }
130                 }
131             }
132             
133             if (value != null) {
134                 synchronized (this) {
135                     settingName2value.put(settingName, value);
136                 }
137             }
138         }
139         
140         if (value == NULL_VALUE) {
141             value = null;
142         }
143         return value;
144     }
145     
146     public Object JavaDoc getValue(String JavaDoc settingName, Object JavaDoc defaultValue) {
147         Object JavaDoc value = getValue(settingName);
148         if (value == null) {
149             value = defaultValue;
150         }
151         return value;
152     }
153     
154     public void settingsChange(SettingsChangeEvent evt) {
155         clearSettingValues();
156     }
157     
158     private synchronized void clearSettingValues() {
159         settingName2value.clear();
160     }
161 }
162
Popular Tags