001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.settings.j2seprefs; 017 018 import java.io.IOException; 019 import java.util.Arrays; 020 import java.util.prefs.BackingStoreException; 021 import java.util.prefs.Preferences; 022 023 import org.springframework.richclient.settings.AbstractSettings; 024 import org.springframework.richclient.settings.Settings; 025 026 /** 027 * Settings implementation using the J2SE Preferences API. <br> 028 * Not using the PreferenceChangeListener to implement PropertyChangeListener 029 * support, because we also need the old value. 030 * 031 * @author Peter De Bruycker 032 */ 033 public class PreferencesSettings extends AbstractSettings { 034 035 private Preferences prefs; 036 037 /** 038 * Create the root. 039 */ 040 public PreferencesSettings(String name) { 041 this(null, name); 042 } 043 044 /** 045 * Create a child with the given name. 046 */ 047 public PreferencesSettings(PreferencesSettings parent, String name) { 048 super(parent, name); 049 if (parent != null) { 050 prefs = parent.getPreferences().node(name); 051 } 052 else { 053 prefs = Preferences.userRoot().node(name); 054 } 055 } 056 057 /** 058 * @param preferences 059 */ 060 public PreferencesSettings(Preferences preferences) { 061 super(null, preferences.name()); 062 this.prefs = preferences; 063 } 064 065 public Preferences getPreferences() { 066 return prefs; 067 } 068 069 public void save() throws IOException { 070 try { 071 prefs.flush(); 072 } 073 catch (BackingStoreException e) { 074 IOException ioe = new IOException("Unable to save settings"); 075 ioe.initCause(e); 076 throw ioe; 077 } 078 } 079 080 public void load() throws IOException { 081 try { 082 prefs.sync(); 083 } 084 catch (BackingStoreException e) { 085 IOException ioe = new IOException("Unable to save settings"); 086 ioe.initCause(e); 087 throw ioe; 088 } 089 } 090 091 protected String internalGet(String key) { 092 if (prefs.get(key, "").equals("")) { 093 return null; 094 } 095 return prefs.get(key, ""); 096 } 097 098 protected Settings internalCreateChild(String key) { 099 return new PreferencesSettings(this, key); 100 } 101 102 /* 103 * (non-Javadoc) 104 * 105 * @see org.springframework.richclient.settings.AbstractSettings#internalSet(java.lang.String, 106 * java.lang.String) 107 */ 108 protected void internalSet(String key, String value) { 109 prefs.put(key, value); 110 } 111 112 /* 113 * (non-Javadoc) 114 * 115 * @see org.springframework.richclient.settings.AbstractSettings#getKeys() 116 */ 117 public String[] getKeys() { 118 try { 119 return prefs.keys(); 120 } 121 catch (BackingStoreException e) { 122 throw new RuntimeException(e); 123 } 124 } 125 126 /* 127 * (non-Javadoc) 128 * 129 * @see org.springframework.richclient.settings.AbstractSettings#internalRemove(java.lang.String) 130 */ 131 protected void internalRemove(String key) { 132 prefs.remove(key); 133 } 134 135 public boolean internalContains(String key) { 136 try { 137 return Arrays.asList(prefs.keys()).contains(key); 138 } 139 catch (BackingStoreException e) { 140 throw new RuntimeException(e); 141 } 142 } 143 144 protected String[] internalGetChildSettings() { 145 try { 146 return prefs.childrenNames(); 147 } 148 catch (BackingStoreException e) { 149 throw new RuntimeException(e); 150 } 151 } 152 153 protected void internalRemoveSettings() { 154 try { 155 prefs.removeNode(); 156 } catch( BackingStoreException e ) { 157 throw new RuntimeException(e); 158 } 159 } 160 }