Source Code : Utility class for preferences

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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();
        }
    }

}

   
    
    
    
  

Thank with us