1 19 20 package org.netbeans.modules.editor.completion; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.lang.ref.Reference ; 25 import java.lang.ref.WeakReference ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import javax.swing.text.JTextComponent ; 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 42 43 public final class CompletionSettings implements SettingsChangeListener { 44 45 public static final CompletionSettings INSTANCE = new CompletionSettings(); 46 47 private static final Object NULL_VALUE = new Object (); 48 49 private Reference <JTextComponent > editorComponentRef; 50 51 private Map <String , Object > settingName2value = new HashMap <String , Object >(); 52 53 private CompletionSettings() { 54 Settings.addSettingsChangeListener(this); 55 } 56 57 public boolean completionAutoPopup() { 58 return ((Boolean )getValue( 59 ExtSettingsNames.COMPLETION_AUTO_POPUP, 60 ExtSettingsDefaults.defaultCompletionAutoPopup) 61 ).booleanValue(); 62 } 63 64 public int completionAutoPopupDelay() { 65 return ((Integer )getValue( 66 ExtSettingsNames.COMPLETION_AUTO_POPUP_DELAY, 67 ExtSettingsDefaults.defaultCompletionAutoPopupDelay) 68 ).intValue(); 69 } 70 71 public boolean documentationAutoPopup() { 72 return ((Boolean )getValue( 73 ExtSettingsNames.JAVADOC_AUTO_POPUP, 74 ExtSettingsDefaults.defaultJavaDocAutoPopup) 75 ).booleanValue(); 76 } 77 78 public int documentationAutoPopupDelay() { 79 return ((Integer )getValue( 80 ExtSettingsNames.JAVADOC_AUTO_POPUP_DELAY, 81 ExtSettingsDefaults.defaultJavaDocAutoPopupDelay) 82 ).intValue(); 83 } 84 85 public Dimension completionPopupMaximumSize() { 86 return (Dimension )getValue( 87 ExtSettingsNames.COMPLETION_PANE_MAX_SIZE, 88 ExtSettingsDefaults.defaultCompletionPaneMaxSize); 89 } 90 91 public Dimension documentationPopupPreferredSize() { 92 return (Dimension )getValue( 93 ExtSettingsNames.JAVADOC_PREFERRED_SIZE, 94 ExtSettingsDefaults.defaultJavaDocPreferredSize); 95 } 96 97 public Color documentationBackgroundColor() { 98 return (Color )CompletionSettings.INSTANCE.getValue( 99 ExtSettingsNames.JAVADOC_BG_COLOR, 100 ExtSettingsDefaults.defaultJavaDocBGColor); 101 } 102 103 public boolean completionInstantSubstitution() { 104 return ((Boolean )getValue( 105 ExtSettingsNames.COMPLETION_INSTANT_SUBSTITUTION, 106 ExtSettingsDefaults.defaultCompletionInstantSubstitution) 107 ).booleanValue(); 108 } 109 110 public void notifyEditorComponentChange(JTextComponent newEditorComponent) { 111 this.editorComponentRef = new WeakReference <JTextComponent >(newEditorComponent); 112 clearSettingValues(); 113 } 114 115 public Object getValue(String settingName) { 116 Object value; 117 synchronized (this) { 118 value = settingName2value.get(settingName); 119 } 120 121 if (value == null) { 122 JTextComponent c = editorComponentRef.get(); 123 if (c != null) { 124 Class 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 getValue(String settingName, Object defaultValue) { 147 Object 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 |