KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > TableViewSettings


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.properties;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Event JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.SystemColor JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import org.openide.util.Lookup;
32 import org.openide.util.LookupEvent;
33 import org.openide.util.LookupListener;
34
35 /**
36  * Template for table editor setting registered in Lookup.
37  *
38  * @author Petr Kuzel
39  */

40 public abstract class TableViewSettings {
41     
42     /** Default key color. */
43     public static final Color JavaDoc KEY_DEFAULT_COLOR = new Color JavaDoc(0, 0, 153);
44     /** Default key background. */
45     public static final Color JavaDoc KEY_DEFAULT_BACKGROUND = Color.white;
46     /** Default value color. */
47     public static final Color JavaDoc VALUE_DEFAULT_COLOR = new Color JavaDoc(153, 0, 107);
48     /** Default value background. */
49     public static final Color JavaDoc VALUE_DEFAULT_BACKGROUND = Color.white;
50     /** Default highlight color. */
51     public static final Color JavaDoc HIGHLIGHT_DEFAULT_COLOR = Color.black;
52     /** Default highlight background. */
53     public static final Color JavaDoc HIGHLIGHT_DEFAULT_BACKGROUND = Color.yellow;
54     /** Default shadow color. */
55     public static final Color JavaDoc SHADOW_DEFAULT_COLOR = new Color JavaDoc(SystemColor.controlHighlight.getRGB());
56     /** Default keystrokes for find next action. */
57     public static final KeyStroke JavaDoc[] FIND_NEXT_DEFAULT_KEYSTROKES = new KeyStroke JavaDoc[] {KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0)};
58     /** Default keystrokes for find previous action. */
59     public static final KeyStroke JavaDoc[] FIND_PREVIOUS_DEFAULT_KEYSTROKES = new KeyStroke JavaDoc[] {KeyStroke.getKeyStroke(KeyEvent.VK_F3, Event.SHIFT_MASK)};
60     /** Default keystrokes for toggle highliht action. */
61     public static final KeyStroke JavaDoc[] TOGGLE_HIGHLIGHT_DEFAULT_KEYSTROKES = new KeyStroke JavaDoc[] {KeyStroke.getKeyStroke(KeyEvent.VK_H, Event.SHIFT_MASK | Event.ALT_MASK)};
62     
63     // the only instance
64
private static DelegatingSettings delegatingSettings;
65
66     // active registrations
67
private static Lookup.Result<TableViewSettings> registrations;
68     
69     /**
70      * Reserved for subclasses
71      */

72     protected TableViewSettings() {
73     }
74     
75     /**
76      * Query lookup for settings and watch them translating
77      * registration changes to firing PCEs on returned
78      * instance.
79      */

80     public synchronized static TableViewSettings getDefault() {
81         
82         if (delegatingSettings == null) {
83             Lookup lookup = Lookup.getDefault();
84             Lookup.Template<TableViewSettings> template = new Lookup.Template<TableViewSettings>(TableViewSettings.class);
85             registrations = lookup.lookup(template);
86             registrations.addLookupListener(new LookupListener() {
87                 public void resultChanged(LookupEvent e) {
88                     TableViewSettings peer = null;
89                     if (registrations.allInstances().isEmpty()) {
90                         peer = new HardcodedSettings();
91                     } else {
92                         peer = registrations.allInstances().iterator().next();
93                     }
94                     delegatingSettings.setPeer(peer);
95                 }
96             });
97             TableViewSettings peer = null;
98             if (registrations.allInstances().isEmpty()) {
99                 peer = new HardcodedSettings();
100             } else {
101                 peer = registrations.allInstances().iterator().next();
102             }
103             delegatingSettings = new DelegatingSettings(peer);
104         }
105         
106         return delegatingSettings;
107     }
108     
109     // Settings accessors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110

111     public abstract Color JavaDoc getKeyColor();
112     public abstract Color JavaDoc getKeyBackground();
113     public abstract Color JavaDoc getValueColor();
114     public abstract Color JavaDoc getValueBackground();
115     public abstract Color JavaDoc getHighlightColor();
116     public abstract Color JavaDoc getHighlightBackground();
117     public abstract Color JavaDoc getShadowColor();
118     public abstract Font JavaDoc getFont();
119
120     public abstract KeyStroke JavaDoc[] getKeyStrokesFindNext();
121     public abstract KeyStroke JavaDoc[] getKeyStrokesFindPrevious();
122     public abstract KeyStroke JavaDoc[] getKeyStrokesToggleHighlight();
123
124     public abstract void addPropertyChangeListener(PropertyChangeListener JavaDoc listener);
125     public abstract void removePropertyChangeListener(PropertyChangeListener JavaDoc listener);
126
127     /**
128      * Returned by getDefault, allows to change impl as
129      * reaction to registration changes.
130      */

131     private static class DelegatingSettings extends TableViewSettings implements PropertyChangeListener JavaDoc {
132         
133         private PropertyChangeSupport JavaDoc events = new PropertyChangeSupport JavaDoc(this);
134         
135         private TableViewSettings peer;
136         
137         DelegatingSettings(TableViewSettings peer) {
138             this.peer = peer;
139             peer.addPropertyChangeListener(this);
140         }
141         
142         void setPeer(TableViewSettings peer) {
143             this.peer.removePropertyChangeListener(this);
144             this.peer = peer;
145             this.peer.addPropertyChangeListener(this);
146             // everything has possibly changed
147
events.firePropertyChange(null, null, null);
148         }
149         
150         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
151             events.addPropertyChangeListener(listener);
152         }
153         
154         public Color JavaDoc getHighlightBackground() {
155             return peer.getHighlightBackground();
156         }
157         
158         public Color JavaDoc getHighlightColor() {
159             return peer.getHighlightColor();
160         }
161         
162         public Color JavaDoc getKeyBackground() {
163             return peer.getKeyBackground();
164         }
165         
166         public Color JavaDoc getKeyColor() {
167             return peer.getKeyColor();
168         }
169         
170         public Font JavaDoc getFont() {
171             return peer.getFont();
172         }
173         
174         public KeyStroke JavaDoc[] getKeyStrokesFindNext() {
175             return peer.getKeyStrokesFindNext();
176         }
177         
178         public KeyStroke JavaDoc[] getKeyStrokesFindPrevious() {
179             return peer.getKeyStrokesFindPrevious();
180         }
181         
182         public KeyStroke JavaDoc[] getKeyStrokesToggleHighlight() {
183             return peer.getKeyStrokesToggleHighlight();
184         }
185         
186         public Color JavaDoc getShadowColor() {
187             return peer.getShadowColor();
188         }
189         
190         public Color JavaDoc getValueBackground() {
191             return peer.getValueBackground();
192         }
193         
194         public Color JavaDoc getValueColor() {
195             return peer.getValueColor();
196         }
197         
198         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
199             events.removePropertyChangeListener(listener);
200         }
201         
202         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
203             events.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
204         }
205         
206     }
207     
208     /**
209      * If no setting is registered return following one.
210      */

211     private static class HardcodedSettings extends TableViewSettings {
212         public Color JavaDoc getKeyColor() {return KEY_DEFAULT_COLOR;}
213         public Color JavaDoc getKeyBackground() {return KEY_DEFAULT_BACKGROUND;}
214         public Color JavaDoc getValueColor() {return VALUE_DEFAULT_COLOR;}
215         public Color JavaDoc getValueBackground() {return VALUE_DEFAULT_BACKGROUND;}
216         public Color JavaDoc getHighlightColor() {return HIGHLIGHT_DEFAULT_COLOR;}
217         public Color JavaDoc getHighlightBackground() {return HIGHLIGHT_DEFAULT_BACKGROUND;}
218         public Color JavaDoc getShadowColor() { return SHADOW_DEFAULT_COLOR;}
219         public Font JavaDoc getFont() { return UIManager.getFont("TextField.font"); }
220         
221         public KeyStroke JavaDoc[] getKeyStrokesFindNext() {return FIND_NEXT_DEFAULT_KEYSTROKES;}
222         public KeyStroke JavaDoc[] getKeyStrokesFindPrevious() {return FIND_PREVIOUS_DEFAULT_KEYSTROKES;}
223         public KeyStroke JavaDoc[] getKeyStrokesToggleHighlight() {return TOGGLE_HIGHLIGHT_DEFAULT_KEYSTROKES;}
224
225         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
226         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {}
227     }
228 }
229
Popular Tags