KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > syntax > EditorSettingsCopy


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
21 package org.netbeans.modules.properties.syntax;
22
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.SystemColor JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeSupport JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35
36 import org.netbeans.editor.BaseKit;
37 import org.netbeans.editor.Coloring;
38 import org.netbeans.editor.MultiKeyBinding;
39 import org.netbeans.editor.Settings;
40 import org.netbeans.editor.SettingsChangeEvent;
41 import org.netbeans.editor.SettingsChangeListener;
42 import org.netbeans.editor.SettingsNames;
43 import org.netbeans.editor.SettingsUtil;
44 import org.netbeans.modules.properties.TableViewSettings;
45
46 import org.openide.util.SharedClassObject;
47
48 /**
49  * TableViewSettings that delegates to text editor module settings.
50  *
51  * @author Peter Zavadsky, refactored by Petr Kuzel
52  * @see org.netbeans.modules.propeties.BundleEditPanel
53  */

54 public class EditorSettingsCopy extends TableViewSettings implements SettingsChangeListener {
55
56     /** Singleton instance of <code>EditorSettingsCopy</code>. */
57     private static EditorSettingsCopy editorSettingsCopy;
58     
59     /** Value of key color retrieved from settings in editor module. */
60     private Color JavaDoc keyColor;
61     /** Value of key background retrieved from settings in editor module. */
62     private Color JavaDoc keyBackground;
63     /** Value of value color retrieved from settings in editor module. */
64     private Color JavaDoc valueColor;
65     /** Value of value background retrieved from settings in editor module. */
66     private Color JavaDoc valueBackground;
67     /** Value of highlight color retrieved from settings in editor module. */
68     private Color JavaDoc highlightColor;
69     /** Value of highlight bacground retrieved from settings in editor module. */
70     private Color JavaDoc highlightBackground;
71     /** Value of shadow color retrieved from settings in editor module. */
72     private Color JavaDoc shadowColor;
73     
74     /** Key strokes for find next action rerieved from editor module. */
75     private KeyStroke JavaDoc[] keyStrokesFindNext;
76     /** Key strokes for find previous action retrieved from editor module. */
77     private KeyStroke JavaDoc[] keyStrokesFindPrevious;
78     /** Key strokes for toggle search highlight action retrieved from editor module. */
79     private KeyStroke JavaDoc[] keyStrokesToggleHighlight;
80
81     /** Support for property changes. */
82     private final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
83         
84     /** Flag indicating whether the settings are prepared. */
85     private boolean prepared = false;
86     
87     
88     /** Private constructor. */
89     private EditorSettingsCopy() {
90     }
91
92     
93     /** Implements <code>EditorSetings</code> interface method. */
94     public Color JavaDoc getKeyColor() {
95         prepareSettings();
96         if(keyColor == null) {
97             keyColor = TableViewSettings.KEY_DEFAULT_COLOR;
98         }
99         
100         return keyColor;
101     }
102     
103     /** Implements <code>EditorSetings</code> interface method. */
104     public Color JavaDoc getKeyBackground() {
105         prepareSettings();
106         if(keyBackground == null) {
107             keyBackground = TableViewSettings.KEY_DEFAULT_BACKGROUND;
108         }
109         
110         return keyBackground;
111     }
112     
113     /** Implements <code>EditorSetings</code> interface method. */
114     public Color JavaDoc getValueColor() {
115         prepareSettings();
116         if(valueColor == null) {
117             valueColor = TableViewSettings.VALUE_DEFAULT_COLOR;
118         }
119         
120         return valueColor;
121     }
122     
123     /** Implements <code>EditorSetings</code> interface method. */
124     public Color JavaDoc getValueBackground() {
125         prepareSettings();
126         if(valueBackground == null) {
127             valueBackground = TableViewSettings.VALUE_DEFAULT_BACKGROUND;
128         }
129         
130         return valueBackground;
131     }
132     
133     /** Implements <code>EditorSetings</code> interface method. */
134     public Color JavaDoc getHighlightColor() {
135         prepareSettings();
136         if(highlightColor == null) {
137             highlightColor = TableViewSettings.HIGHLIGHT_DEFAULT_COLOR;
138         }
139         
140         return highlightColor;
141     }
142     
143     /** Implements <code>EditorSetings</code> interface method. */
144     public Color JavaDoc getHighlightBackground() {
145         prepareSettings();
146         if(highlightBackground == null) {
147             highlightBackground = TableViewSettings.HIGHLIGHT_DEFAULT_BACKGROUND;
148         }
149         
150         return highlightBackground;
151     }
152     
153     /** Implements <code>EditorSetings</code> inaterface method. */
154     public Color JavaDoc getShadowColor() {
155         prepareSettings();
156         if(shadowColor == null) {
157             shadowColor = TableViewSettings.SHADOW_DEFAULT_COLOR;
158         }
159         
160         return shadowColor;
161     }
162
163     public Font JavaDoc getFont() {
164         prepareSettings();
165         Font JavaDoc font = SettingsUtil.getColoring(PropertiesKit.class,
166                                              SettingsNames.DEFAULT_COLORING,
167                                              false).getFont();
168         return font;
169     }
170
171
172     /** Implements <code>EditorSetings</code> interface method. */
173     public KeyStroke JavaDoc[] getKeyStrokesFindNext() {
174         prepareSettings();
175         if(keyStrokesFindNext == null || keyStrokesFindNext.length == 0) {
176             keyStrokesFindNext = TableViewSettings.FIND_NEXT_DEFAULT_KEYSTROKES;
177         }
178         
179         return keyStrokesFindNext;
180     }
181     
182     /** Implements <code>EditorSetings</code> interface method. */
183     public KeyStroke JavaDoc[] getKeyStrokesFindPrevious() {
184         prepareSettings();
185         if(keyStrokesFindPrevious == null || keyStrokesFindPrevious.length == 0) {
186             keyStrokesFindPrevious = TableViewSettings.FIND_PREVIOUS_DEFAULT_KEYSTROKES;
187         }
188         
189         return keyStrokesFindPrevious;
190     }
191     
192     /** Implements <code>EditorSetings</code> interface method. */
193     public KeyStroke JavaDoc[] getKeyStrokesToggleHighlight() {
194         prepareSettings();
195         if(keyStrokesToggleHighlight == null || keyStrokesToggleHighlight.length == 0) {
196             keyStrokesToggleHighlight = TableViewSettings.TOGGLE_HIGHLIGHT_DEFAULT_KEYSTROKES;
197         }
198         
199         return keyStrokesToggleHighlight;
200     }
201
202     /** Implements <code>EditorSetings</code> interface method. */
203     public void settingsUpdated() {
204         if (prepared) {
205         support.firePropertyChange(new PropertyChangeEvent JavaDoc(this, null, null, null));
206     }
207     }
208
209     /** Implements <code>EditorSetings</code> interface method. */
210     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
211         support.addPropertyChangeListener(listener);
212     }
213
214     /** Implements <code>EditorSetings</code> interface method. */
215     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
216         support.removePropertyChangeListener(listener);
217     }
218
219     /**
220      * Gets only instance of <code>EditorSettindsCopy</code> that is also
221      * registered at layer to access it declaratively.
222      */

223     public synchronized static EditorSettingsCopy getLayerInstance() {
224             if(editorSettingsCopy == null) {
225                 editorSettingsCopy = new EditorSettingsCopy();
226             }
227
228         return editorSettingsCopy;
229     }
230
231     /** Prepares settings. */
232     private void prepareSettings() {
233         if (prepared) return;
234         
235         // Set listening on changes of settings.
236
Settings.addSettingsChangeListener(this);
237
238         // Init settings.
239
updateSettings();
240         prepared = true;
241     }
242     
243     
244     /**
245      * Handle settings change.
246      */

247     public void settingsChange(SettingsChangeEvent evt) {
248         // maybe could be refined
249
updateSettings();
250     }
251     
252     /** Updates settings from properties options. Only editor module dependent code. */
253     private void updateSettings() {
254         if(updateColors())
255             updateKeyStrokes();
256     }
257
258     /** Updates colors.
259      * @return <code>true</code> if colors updated succesfully or <code>false</code> otherwise. */

260     private boolean updateColors() {
261         PropertiesOptions propertiesOptions = (PropertiesOptions)SharedClassObject.findObject(PropertiesOptions.class, false);
262         if(propertiesOptions == null) {
263             return false;
264         }
265         
266         // Update colors.
267
Map JavaDoc map = propertiesOptions.getColoringMap();
268         Coloring keyColoring = (Coloring)map.get(PropertiesTokenContext.contextPath.getFullTokenName(
269             PropertiesTokenContext.KEY));
270         keyColor = keyColoring.getForeColor();
271         keyBackground = keyColoring.getBackColor();
272         
273         Coloring valueColoring = (Coloring)map.get(PropertiesTokenContext.contextPath.getFullTokenName(
274             PropertiesTokenContext.VALUE));
275         valueColor = valueColoring.getForeColor();
276         valueBackground = valueColoring.getBackColor();
277         
278         Coloring highlightColoring = (Coloring)map.get(SettingsNames.HIGHLIGHT_SEARCH_COLORING);
279         highlightColor = highlightColoring.getForeColor();
280         highlightBackground = highlightColoring.getBackColor();
281
282         shadowColor = propertiesOptions.getShadowTableCell();
283
284         // If there is not the colors specified use default inherited colors.
285
Color JavaDoc defaultForeground = ((Coloring)map.get("default")).getForeColor(); // NOI18N
286
Color JavaDoc defaultBackground = ((Coloring)map.get("default")).getBackColor(); // NOI18N
287

288         if(keyColor == null) keyColor = defaultForeground;
289         if(keyBackground == null) keyBackground = defaultBackground;
290         if(valueColor == null) valueColor = defaultForeground;
291         if(valueBackground == null) valueBackground = defaultBackground;
292         if(highlightColor == null) highlightColor = new Color JavaDoc(SystemColor.textHighlightText.getRGB());
293         if(highlightBackground == null) highlightBackground = new Color JavaDoc(SystemColor.textHighlight.getRGB());
294         if(shadowColor == null) shadowColor = new Color JavaDoc(SystemColor.controlHighlight.getRGB());
295         
296         return true;
297     }
298
299     /** Updates keystrokes. Dependent code. */
300     private void updateKeyStrokes() {
301         // Update keyStrokes.
302
// Retrieve key bindings for Propeties Kit and super kits.
303
Settings.KitAndValue kv[] = Settings.getValueHierarchy(
304             PropertiesKit.class, SettingsNames.KEY_BINDING_LIST);
305
306         // Go through all levels (PropertiesKit and its supeclasses) and collect key bindings.
307
HashSet JavaDoc nextKS = new HashSet JavaDoc();
308         HashSet JavaDoc prevKS = new HashSet JavaDoc();
309         HashSet JavaDoc toggleKS = new HashSet JavaDoc();
310         // Loop thru each keylist for the kit class.
311
for (int i = kv.length - 1; i >= 0; i--) {
312             List JavaDoc keyList = (List JavaDoc)kv[i].value;
313             
314             JTextComponent.KeyBinding JavaDoc[] bindings = new JTextComponent.KeyBinding JavaDoc[keyList.size()];
315             
316             keyList.toArray(bindings);
317             
318             // Loop thru all bindings in the kit class.
319
for(int j=0; j<bindings.length; j++) {
320                 JTextComponent.KeyBinding JavaDoc binding = bindings[j];
321                 if(binding == null)
322                     continue;
323                 
324                 // Find key keystrokes for find next action.
325
if(binding.actionName.equals(BaseKit.findNextAction)) {
326                     if(binding instanceof MultiKeyBinding && ((MultiKeyBinding)binding).keys != null)
327                         for (int k=0; k<((MultiKeyBinding)binding).keys.length; k++)
328                             nextKS.add(((MultiKeyBinding)binding).keys[k]);
329                     else
330                         nextKS.add(binding.key);
331                 }
332                 // Find key keystrokes for find previous action.
333
if(binding.actionName.equals(BaseKit.findPreviousAction)) {
334                     if(binding instanceof MultiKeyBinding && ((MultiKeyBinding)binding).keys != null)
335                         for (int k=0; k<((MultiKeyBinding)binding).keys.length; k++)
336                             prevKS.add(((MultiKeyBinding)binding).keys[k]);
337                     else
338                         prevKS.add(binding.key);
339                 }
340                 // Find key keystrokes for toggle highlight action.
341
if(binding.actionName.equals(BaseKit.toggleHighlightSearchAction)) {
342                     if(binding instanceof MultiKeyBinding && ((MultiKeyBinding)binding).keys != null)
343                         for (int k=0; k<((MultiKeyBinding)binding).keys.length; k++)
344                             toggleKS.add(((MultiKeyBinding)binding).keys[k]);
345                     else
346                         toggleKS.add(binding.key);
347                 }
348                 
349             } // End of inner loop.
350
} // End of outer loop.
351

352         // Copy found values to our variables.
353
nextKS.toArray(keyStrokesFindNext = new KeyStroke JavaDoc[nextKS.size()]);
354         prevKS.toArray(keyStrokesFindPrevious = new KeyStroke JavaDoc[prevKS.size()]);
355         toggleKS.toArray(keyStrokesToggleHighlight = new KeyStroke JavaDoc[toggleKS.size()]);
356
357         // notify listeners about update
358
settingsUpdated();
359     }
360     
361 }
362
Popular Tags