View Javadoc

1   package com.explosion.utilities.preferences.impl.sun;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.util.Vector;
24  import java.util.prefs.BackingStoreException;
25  import java.util.prefs.Preferences;
26  
27  import com.explosion.utilities.preferences.Preference;
28  import com.explosion.utilities.preferences.groups.PreferenceGroup;
29  import com.explosion.utilities.preferences.groups.PreferencePersistenceException;
30  import com.explosion.utilities.preferences.persist.PreferenceGroupPersister;
31  
32  /***
33   * @author: Stephen Cowx
34   *
35   * A persister to allow preference groups to be persisted using the Java Preferences API
36   * 
37   */
38  
39  public class SunPreferenceGroupPersister implements PreferenceGroupPersister
40  {
41      private Preferences node;
42  
43      /***
44       * Hidden constructor, want to use the factory method
45       */
46      private SunPreferenceGroupPersister()    {        
47      }
48      
49      /***
50       * Constructs an instance of a SunPreferencePersistor
51       * @param referenceToStore
52       * @return
53       */
54      public static PreferenceGroupPersister createPreferenceGroupPersistor(Object referenceToStore)
55      {
56          SunPreferenceGroupPersister persister = new SunPreferenceGroupPersister();
57          persister.node = (Preferences) referenceToStore;
58          return persister;
59      }
60      
61      /***
62       * Returns a reference to the backing store as understood by the PreferencePersister
63       * @return
64       */
65      public Object getReferenceToStore()
66      {
67          return node;
68      }
69      
70      /***
71       * Sets the reference to the backing store as understood by the PreferencePersister
72       * Effectively moving it from one store to another or moving it from one location in the same store to another
73       * @return
74       */
75      public void setReferenceToStore(Object referenceToStore)
76      {
77          this.node = (Preferences) referenceToStore;
78      }
79      
80      /***
81       * This method deletes the preferenceGroup from the backing store
82       * @param group
83       * @throws PreferencePersistenceException
84       */
85      public void deleteGroup(PreferenceGroup group) throws PreferencePersistenceException
86  	{
87      	try {
88  			/* Remove the node */
89  			node.removeNode();
90  			Preferences.userRoot().flush();
91  		} catch (BackingStoreException e) {
92  			throw new PreferencePersistenceException("Unable to delete group", e);
93  		}
94  	}
95      
96      /***
97       * This method saves a preference group in the store
98       * @param group
99       * @throws PreferencePersistenceException
100      */
101     public void saveGroup(PreferenceGroup group) throws PreferencePersistenceException
102 	{
103     	Vector attributesVector = group.getPreferencesVector();
104     	for (int i = 0; i < attributesVector.size(); i++)
105         {
106            Preference pref = (Preference) attributesVector.elementAt(i);
107            pref.setReferenceToStore(node);
108         }
109 	}
110     
111     public void renameGroup(PreferenceGroup group, String newName) throws PreferencePersistenceException
112     {
113         /* ParentNode of parentNode */
114     	Preferences parentNode = node.parent();
115     	
116     	/* Assign the old preferences to the new node */
117         deleteGroup(group);
118         
119         /* set the store to the new parent + the new name */
120         this.node = parentNode.node(parentNode.absolutePath() + "/" + newName);
121           
122         /* Save everything */
123         saveGroup(group);
124     }
125 
126     
127 }