KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > DefaultSettings


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.palette;
21
22 import java.beans.BeanInfo JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.prefs.BackingStoreException JavaDoc;
28 import java.util.prefs.Preferences JavaDoc;
29 import org.netbeans.spi.palette.PaletteController;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.Node;
34 import org.openide.util.Lookup;
35 import org.openide.util.NbPreferences;
36 import org.openide.util.Utilities;
37
38 /**
39  * Palette settings to be remembered over IDE restarts.
40  * There's an instance of these settings for each palette model instance.
41  *
42  * @author S. Aubrecht
43  */

44 public final class DefaultSettings implements Settings, ModelListener, CategoryListener {
45     
46     private static final String JavaDoc NODE_ATTR_PREFIX = "psa_";
47     
48     private static final String JavaDoc NULL_VALUE = "null";
49     
50     private static final String JavaDoc XML_ROOT = "root";
51     private static final String JavaDoc XML_CATEGORY = "category";
52     private static final String JavaDoc XML_ITEM = "item";
53     
54     private static final String JavaDoc XML_ATTR_NAME = "name";
55     
56     private static final String JavaDoc[] KNOWN_PROPERTIES = new String JavaDoc[] {
57         NODE_ATTR_PREFIX + PaletteController.ATTR_ICON_SIZE,
58         NODE_ATTR_PREFIX + PaletteController.ATTR_IS_EXPANDED,
59         NODE_ATTR_PREFIX + PaletteController.ATTR_IS_VISIBLE,
60         NODE_ATTR_PREFIX + PaletteController.ATTR_SHOW_ITEM_NAMES
61     };
62     
63     private static final int ICON_SIZE_ATTR_INDEX = 0;
64     private static final int IS_EXPANDED_ATTR_INDEX = 1;
65     private static final int IS_VISIBLE_ATTR_INDEX = 2;
66     private static final int SHOW_ITEM_NAMES_ATTR_INDEX = 3;
67     
68     private Model model;
69     private PropertyChangeSupport JavaDoc propertySupport = new PropertyChangeSupport JavaDoc( this );
70     
71     private String JavaDoc prefsName;
72     
73     private static Logger JavaDoc ERR = Logger.getLogger("org.netbeans.modules.palette"); // NOI18N
74

75     public DefaultSettings( Model model ) {
76         this.model = model;
77         prefsName = constructPrefsFileName( model );
78         if( Utilities.isWindows() )
79             prefsName = prefsName.toLowerCase();
80         model.addModelListener( this );
81         Category[] categories = model.getCategories();
82         for( int i=0; i<categories.length; i++ ) {
83             categories[i].addCategoryListener( this );
84         }
85         load();
86     }
87     
88     private String JavaDoc constructPrefsFileName( Model model ) {
89         DataFolder dof = (DataFolder)model.getRoot().lookup( DataFolder.class );
90         if( null != dof ) {
91             FileObject fo = dof.getPrimaryFile();
92             if( null != fo ) {
93                 return fo.getPath();
94             }
95         }
96         return model.getName();
97     }
98     
99     private Preferences JavaDoc getPreferences() {
100         return NbPreferences.forModule( DefaultSettings.class ).node( "CommonPaletteSettings" ).node( prefsName ); //NOI18N
101
}
102
103     public void addPropertyChangeListener( PropertyChangeListener JavaDoc l ) {
104         propertySupport.addPropertyChangeListener( l );
105     }
106
107     public void removePropertyChangeListener( PropertyChangeListener JavaDoc l ) {
108         propertySupport.removePropertyChangeListener( l );
109     }
110
111     public boolean isVisible(Item item) {
112         Node node = getNode( item.getLookup() );
113         return get( node, PaletteController.ATTR_IS_VISIBLE, true );
114     }
115
116     public void setVisible(Item item, boolean visible ) {
117         Node node = getNode( item.getLookup() );
118         set( node, PaletteController.ATTR_IS_VISIBLE, visible, true );
119     }
120
121     public boolean isVisible( Category category ) {
122         Node node = getNode( category.getLookup() );
123         return get( node, PaletteController.ATTR_IS_VISIBLE, true );
124     }
125
126     public void setVisible( Category category, boolean visible ) {
127         Node node = getNode( category.getLookup() );
128         set( node, PaletteController.ATTR_IS_VISIBLE, visible, true );
129     }
130     
131     public boolean isNodeVisible( Node node ) {
132         return get( node, PaletteController.ATTR_IS_VISIBLE, true );
133     }
134     
135     public void setNodeVisible( Node node, boolean visible ) {
136         set( node, PaletteController.ATTR_IS_VISIBLE, visible, true );
137     }
138
139     public boolean isExpanded( Category category ) {
140         Node node = getNode( category.getLookup() );
141         return get( node, PaletteController.ATTR_IS_EXPANDED, false );
142     }
143
144     public void setExpanded( Category category, boolean expanded ) {
145         Node node = getNode( category.getLookup() );
146         set( node, PaletteController.ATTR_IS_EXPANDED, expanded, false );
147     }
148
149     public int getIconSize() {
150         Node node = getNode( model.getRoot() );
151         return get( node, PaletteController.ATTR_ICON_SIZE, BeanInfo.ICON_COLOR_16x16 );
152     }
153
154     public void setIconSize( int iconSize ) {
155         Node node = getNode( model.getRoot() );
156         set( node, PaletteController.ATTR_ICON_SIZE, iconSize, BeanInfo.ICON_COLOR_16x16 );
157     }
158
159     public void setShowItemNames( boolean showNames ) {
160         Node node = getNode( model.getRoot() );
161         set( node, PaletteController.ATTR_SHOW_ITEM_NAMES, showNames, true );
162     }
163
164     public boolean getShowItemNames() {
165         Node node = getNode( model.getRoot() );
166         return get( node, PaletteController.ATTR_SHOW_ITEM_NAMES, true );
167     }
168     
169     private Node getNode( Lookup lkp ) {
170         return (Node)lkp.lookup( Node.class );
171     }
172     
173     private boolean get( Node node, String JavaDoc attrName, boolean defaultValue ) {
174         Object JavaDoc value = get( node, attrName, Boolean.valueOf( defaultValue ) );
175         return null == value ? defaultValue : Boolean.valueOf( value.toString() ).booleanValue();
176     }
177
178     private int get( Node node, String JavaDoc attrName, int defaultValue ) {
179         Object JavaDoc value = get( node, attrName, Integer.valueOf( defaultValue ) );
180         try {
181             if( null != value )
182                 return Integer.parseInt( value.toString() );
183         } catch( NumberFormatException JavaDoc nfE ) {
184             //ignore
185
}
186         return defaultValue;
187     }
188     
189     private Object JavaDoc get( Node node, String JavaDoc attrName, Object JavaDoc defaultValue ) {
190         Object JavaDoc res = null;
191         if( null != node ) {
192             res = node.getValue( NODE_ATTR_PREFIX+attrName );
193             if( null == res || NULL_VALUE.equals( res ) ) {
194                 res = getNodeDefaultValue( node, attrName );
195             }
196         }
197         if( null == res ) {
198             res = defaultValue;
199         }
200         return res;
201     }
202     
203     private Object JavaDoc getNodeDefaultValue( Node node, String JavaDoc attrName ) {
204         Object JavaDoc res = node.getValue( attrName );
205         if( null == res ) {
206             DataObject dobj = (DataObject)node.getCookie( DataObject.class );
207             if( null != dobj ) {
208                 res = dobj.getPrimaryFile().getAttribute( attrName );
209             }
210         }
211         return res;
212     }
213     
214     private void set( Node node, String JavaDoc attrName, boolean newValue, boolean defaultValue ) {
215         set( node, attrName, Boolean.valueOf( newValue ), Boolean.valueOf( defaultValue ) );
216     }
217     
218     private void set( Node node, String JavaDoc attrName, int newValue, int defaultValue ) {
219         set( node, attrName, Integer.valueOf( newValue ), Integer.valueOf( defaultValue ) );
220     }
221     
222     private void set( Node node, String JavaDoc attrName, Object JavaDoc newValue, Object JavaDoc defaultValue ) {
223         if( null == node )
224             return;
225         Object JavaDoc oldValue = get( node, attrName, defaultValue );
226         if( oldValue.equals( newValue ) ) {
227             return;
228         }
229         node.setValue( NODE_ATTR_PREFIX+attrName, newValue );
230         store();
231         propertySupport.firePropertyChange( attrName, oldValue, newValue );
232     }
233
234     public void categoryModified( Category src ) {
235         store();
236     }
237     
238     public void categoriesRemoved( Category[] removedCategories ) {
239         for( int i=0; i<removedCategories.length; i++ ) {
240             removedCategories[i].removeCategoryListener( this );
241         }
242         store();
243     }
244
245     public void categoriesAdded( Category[] addedCategories ) {
246         for( int i=0; i<addedCategories.length; i++ ) {
247             addedCategories[i].addCategoryListener( this );
248         }
249         store();
250     }
251
252     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
253         //not interested
254
}
255
256     public void categoriesReordered() {
257         //not interested
258
}
259
260     private boolean isLoading = false;
261     private void load() {
262         try {
263             isLoading = true;
264             Preferences JavaDoc pref = getPreferences();
265             setIconSize( pref.getInt( PaletteController.ATTR_ICON_SIZE, getIconSize() ) );
266             setShowItemNames( pref.getBoolean( PaletteController.ATTR_SHOW_ITEM_NAMES, getShowItemNames() ) );
267
268             for( Category category : model.getCategories() ) {
269                 setVisible( category, pref.getBoolean( category.getName()+'-'+PaletteController.ATTR_IS_VISIBLE, isVisible( category ) ) );
270                 setExpanded( category, pref.getBoolean( category.getName()+'-'+PaletteController.ATTR_IS_EXPANDED, isExpanded( category ) ) );
271
272                 for( Item item : category.getItems() ) {
273                     setVisible( item, pref.getBoolean( category.getName()+'-'+item.getName()+'-'+PaletteController.ATTR_IS_VISIBLE, isVisible( item ) ) );
274                 }
275             }
276         } finally {
277             isLoading = false;
278         }
279     }
280     
281     private void store() {
282         if( isLoading )
283             return;
284         Preferences JavaDoc pref = getPreferences();
285         try {
286             pref.clear();
287         } catch( BackingStoreException JavaDoc bsE ) {
288             ERR.log( Level.INFO, Utils.getBundleString("Err_StoreSettings"), bsE ); //NOI18N
289
}
290         pref.putInt( PaletteController.ATTR_ICON_SIZE, getIconSize() );
291         pref.putBoolean( PaletteController.ATTR_SHOW_ITEM_NAMES, getShowItemNames() );
292
293         for( Category category : model.getCategories() ) {
294             pref.putBoolean( category.getName()+'-'+PaletteController.ATTR_IS_VISIBLE, isVisible( category ) );
295             pref.putBoolean( category.getName()+'-'+PaletteController.ATTR_IS_EXPANDED, isExpanded( category ) );
296             
297             for( Item item : category.getItems() ) {
298                 pref.putBoolean( category.getName()+'-'+item.getName()+'-'+PaletteController.ATTR_IS_VISIBLE, isVisible( item ) );
299             }
300         }
301     }
302
303     public int getItemWidth() {
304         Node node = getNode( model.getRoot() );
305         return get( node, PaletteController.ATTR_ITEM_WIDTH, -1 );
306     }
307
308     public void reset() {
309         Node root = (Node)model.getRoot().lookup( Node.class );
310         clearAttributes( root );
311         Category[] categories = model.getCategories();
312         for( int i=0; i<categories.length; i++ ) {
313             Node cat = (Node)categories[i].getLookup().lookup( Node.class );
314             clearAttributes( cat );
315             Item[] items = categories[i].getItems();
316             for( int j=0; j<items.length; j++ ) {
317                 Node it = (Node)items[j].getLookup().lookup( Node.class );
318                 clearAttributes( it );
319             }
320         }
321         try {
322             getPreferences().removeNode();
323         } catch( BackingStoreException JavaDoc bsE ) {
324             ERR.log( Level.INFO, Utils.getBundleString("Err_StoreSettings"), bsE ); //NOI18N
325
}
326     }
327
328     private void clearAttributes( Node node ) {
329         for( int i=0; i<KNOWN_PROPERTIES.length; i++ ) {
330             node.setValue( KNOWN_PROPERTIES[i], NULL_VALUE );
331         }
332     }
333 }
334
Popular Tags