Utility class for preferences : Preference Properties « Development Class « Java






Utility class for preferences

    
/*
 * Copyright Javelin Software, All rights reserved.
 */


import java.util.*;
import java.util.prefs.*;


/**
 * Utility class for prefs
 *
 * @author Robin Sharp
 */

public class PrefsUtil 
{   
    /**
     * Clear all the node
     */
    public static void clear( Preferences preferences, String key )
    {
        try
        {
            if( preferences.nodeExists( key ) )
            {
                preferences.node( key ).clear();
            }
        }
        catch( BackingStoreException bse )
        {
            bse.printStackTrace();
        }
    }

    /**
     * Remove the node
     */
    public static void remove( Preferences preferences, String key )
    {
        try
        {
            if( preferences.nodeExists( key ) )
            {
                preferences.node( key ).removeNode();
            }
        }
        catch( BackingStoreException bse )
        {
            bse.printStackTrace();
        }
    }

    
    /**
     * Puts a list into the preferences.
     */
    public static void putMap( Preferences preferences, Map map, String key )
    {
        putMap( preferences.node( key ), map );
    }
    
    /**
     * Puts a list into the preferences.
     */
    public static void putMap( Preferences preferences, Map map )
    {
        if( preferences == null )
        {
            throw new IllegalArgumentException( "Preferences not set." );
        }
    
        for( Iterator iter = map.entrySet().iterator(); iter.hasNext(); )
        {
            Map.Entry entry = (Map.Entry)iter.next();
            Object value = entry.getValue();
            preferences.put( entry.getKey().toString(), value == null ? null : value.toString() );
        }
    }
       

    /**
     * Gets a Map from the preferences.
     */
    public static Map getMap( Preferences preferences, String key )
    {
        return getMap( preferences.node( key ) );
    }
    
    /**
     * Gets a Map from the preferences.
     */
    public static Map getMap( Preferences preferences )
    {
        if( preferences == null )
        {
            throw new IllegalArgumentException( "Preferences not set." );
        }

        Map map = new HashMap();
        
        try
        {
            String[] keys = preferences.keys();

            for( int index = 0; index < keys.length; index++ )
            {
                map.put( keys[index], preferences.get( keys[index], null ) );
            }
        }
        catch( BackingStoreException bse )
        {
            bse.printStackTrace();
        }
        
       return map;
    }
    
    /**
     * Puts a list into the preferences starting with "0" then "1"
     */
    public static void putList( Preferences preferences, List list, String key )
    {
        putList( preferences.node( key ), list );
    }
    
    /**
     * Puts a list into the preferences starting with "0" then "1"
     */
    public static void putList( Preferences preferences, List list )
    {
        if( preferences == null )
        {
            throw new IllegalArgumentException( "Preferences not set." );
        }
        //System.out.println( "LIST=" + list );
        for( int index = 0; list != null && index < list.size(); index++ )
        {
            Object value = list.get( index );
            preferences.put( ""+index, value == null ? null : value.toString() );
        }
    }

    /**
     * Gets a List from the preferences, starting with "0", then "1" etc
     */
    public static List getList( Preferences preferences, String key )
    {
        return getList( preferences.node( key ) );
    }
    
    /**
     * Gets a List from the preferences, starting with "0", then "1" etc
     */
    public static List getList( Preferences preferences )
    {
        if( preferences == null )
        {
            throw new IllegalArgumentException( "Preferences not set." );
        }

        List list = new ArrayList();
    
        for( int index = 0; index < 1000; index++ )
        {
            String value = preferences.get( ""+index, null );
            if( value == null ) break;
            //System.out.println( ""+index+ " " + value );
            list.add( value );
        }

        return list;
    }
    

    public static void main( String[] args )
    {
        try
        {
            Map map = new HashMap();
            map.put( "0", "A" );
            map.put( "1", "B" );
            map.put( "2", "C" );
            map.put( "3", "D" );
            map.put( "5", "f" );

            Preferences prefs = Preferences.userNodeForPackage( String.class );
            
            String RECENT_FILES = "XXX";
            
        List recentFiles = PrefsUtil.getList( prefs, RECENT_FILES );
        PrefsUtil.clear( prefs, RECENT_FILES );
     
        PrefsUtil.putList( prefs, recentFiles, RECENT_FILES );

        //System.out.println( PrefsUtil.getList( prefs, RECENT_FILES ) );

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }

}

   
    
    
    
  








Related examples in the same category

1.Put key value pair to PreferencePut key value pair to Preference
2.Get childrenNames from PreferencesGet childrenNames from Preferences
3.Get keys from PreferencesGet keys from Preferences
4.Get name and parent from PreferenceGet name and parent from Preference
5.Get node from PreferenceGet node from Preference
6.Get value from PreferencesGet value from Preferences
7.Export Preferences to XML fileExport Preferences to XML file
8.Passing references aroundPassing references around
9.Retrieve the preference node using a Class object and saves and retrieves a preference in the node.
10.Determining If a Preference Node Contains a Specific Key
11.Determining If a Preference Node Contains a Specific Value
12.Removing a Preference from a Preference Node
13.Getting and Setting Java Type Values in a Preference
14.Getting the Maximum Size of a Preference Key and Value
15.Getting the Roots of the Preference Trees
16.Retrieving a Preference Node
17.Removing a Preference Node
18.Determining If a Preference Node Exists
19.Retrieving the Parent and Child Nodes of a Preference Node
20.Exporting the Preferences in a Preference Node
21.Exporting the Preferences in a Subtree of Preference Nodes
22.Determining When a Preference Node Is Added or Removed
23.Read / write data in Windows registry
24.Listening for Changes to Preference Values in a Preference Node
25.Saving Data with the Preferences APISaving Data with the Preferences API
26.Preference Example:: export To FilePreference Example:: export To File
27.Properties load
28.Properties TreeMap and StreamProperties TreeMap and Stream
29.Parse Properties FilesParse Properties Files
30.Preferences DemoPreferences Demo
31.Properties TestProperties Test
32.Sort Properties when saving
33.Loading configuration parameters from text file based properties
34.To read a Properties file via an Applet
35.A Properties file stored in a JAR can be loaded this way
36.Load a properties file in the startup directory
37.Have a multi-line value in a properties file
38.Convert a Properties list into a map.
39.Listing All System Properties
40.Getting and Setting Properties
41.Use XML with Properties
42.Store properties as XML file
43.Reading and Writing a Properties File
44.Read system property as an integer
45.Read a configuration file using java.util.Properties
46.Load properties from XML file
47.Here is an example of the contents of a properties file:
48.Use the Registry to store informations (Preferences API)
49.Load a properties file in the classpath
50.Listing the system properties
51.Properties Demo
52.PropsToXML takes a standard Java properties file, and converts it into an XML file
53.Static methods for retrieving and system properties and converting them to various types
54.Store recent items (e.g. recent file in a menu or recent search text in a search dialog)
55.Storing/loading Preferences
56.A frame that restores position and size from user preferences and updates the preferences upon exit
57.A frame that restores position and size from a properties file and updates the properties upon exitA frame that restores position and size from a properties file and updates the properties upon exit
58.A simple Properties extension easing the loading and saving of data