1 19 20 package org.netbeans.modules.palette; 21 22 import java.beans.BeanInfo ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import java.util.prefs.BackingStoreException ; 28 import java.util.prefs.Preferences ; 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 44 public final class DefaultSettings implements Settings, ModelListener, CategoryListener { 45 46 private static final String NODE_ATTR_PREFIX = "psa_"; 47 48 private static final String NULL_VALUE = "null"; 49 50 private static final String XML_ROOT = "root"; 51 private static final String XML_CATEGORY = "category"; 52 private static final String XML_ITEM = "item"; 53 54 private static final String XML_ATTR_NAME = "name"; 55 56 private static final String [] KNOWN_PROPERTIES = new String [] { 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 propertySupport = new PropertyChangeSupport ( this ); 70 71 private String prefsName; 72 73 private static Logger ERR = Logger.getLogger("org.netbeans.modules.palette"); 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 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 getPreferences() { 100 return NbPreferences.forModule( DefaultSettings.class ).node( "CommonPaletteSettings" ).node( prefsName ); } 102 103 public void addPropertyChangeListener( PropertyChangeListener l ) { 104 propertySupport.addPropertyChangeListener( l ); 105 } 106 107 public void removePropertyChangeListener( PropertyChangeListener 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 attrName, boolean defaultValue ) { 174 Object 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 attrName, int defaultValue ) { 179 Object value = get( node, attrName, Integer.valueOf( defaultValue ) ); 180 try { 181 if( null != value ) 182 return Integer.parseInt( value.toString() ); 183 } catch( NumberFormatException nfE ) { 184 } 186 return defaultValue; 187 } 188 189 private Object get( Node node, String attrName, Object defaultValue ) { 190 Object 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 getNodeDefaultValue( Node node, String attrName ) { 204 Object 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 attrName, boolean newValue, boolean defaultValue ) { 215 set( node, attrName, Boolean.valueOf( newValue ), Boolean.valueOf( defaultValue ) ); 216 } 217 218 private void set( Node node, String attrName, int newValue, int defaultValue ) { 219 set( node, attrName, Integer.valueOf( newValue ), Integer.valueOf( defaultValue ) ); 220 } 221 222 private void set( Node node, String attrName, Object newValue, Object defaultValue ) { 223 if( null == node ) 224 return; 225 Object 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 evt) { 253 } 255 256 public void categoriesReordered() { 257 } 259 260 private boolean isLoading = false; 261 private void load() { 262 try { 263 isLoading = true; 264 Preferences 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 pref = getPreferences(); 285 try { 286 pref.clear(); 287 } catch( BackingStoreException bsE ) { 288 ERR.log( Level.INFO, Utils.getBundleString("Err_StoreSettings"), bsE ); } 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 bsE ) { 324 ERR.log( Level.INFO, Utils.getBundleString("Err_StoreSettings"), bsE ); } 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 |