1 /*******************************************************************************
2 * jhunters: Pool League
3 * Copyright 2015 Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ********************************************************************************/
17 package net.sourceforge.jhunters.pool.ui;
18
19 import java.io.File;
20 import java.util.prefs.BackingStoreException;
21 import java.util.prefs.Preferences;
22
23 import net.sourceforge.jhunters.pool.PoolException;
24
25 /**
26 * Pool Preferences.
27 */
28 public class PoolPreferences {
29 /**
30 * The FileName preference name.
31 */
32 private static final String PREF_FILENAME = "filename";
33
34 /**
35 * The Preference node for this set.
36 */
37 private final Preferences theHandle;
38
39 /**
40 * The Current file name.
41 */
42 private File theFileName;
43
44 /**
45 * Constructor.
46 * @throws PoolException on error
47 */
48 public PoolPreferences() throws PoolException {
49 /* Access the handle */
50 Preferences myHandle = Preferences.userNodeForPackage(this.getClass());
51 theHandle = myHandle.node(this.getClass().getSimpleName());
52 }
53
54 /**
55 * Obtain the file name.
56 * @return the file name
57 */
58 public File getFileName() {
59 /* Load file name if currently null */
60 if (theFileName == null) {
61 String myValue = readStringValue(PREF_FILENAME);
62 theFileName = (myValue == null)
63 ? null
64 : new File(myValue);
65 }
66
67 /* return the filename */
68 return theFileName;
69 }
70
71 /**
72 * Set the filename.
73 * @param pValue the file name
74 * @throws PoolException on error
75 */
76 public void setFileName(final File pValue) throws PoolException {
77 /* Set the value */
78 setStringValue(PREF_FILENAME, pValue.getAbsolutePath());
79
80 /* Adjust cache */
81 theFileName = pValue;
82 }
83
84 /**
85 * Read the value.
86 * @param pName the name of the value
87 * @return the existing value
88 */
89 private String readStringValue(final String pName) {
90 return theHandle.get(pName, null);
91 }
92
93 /**
94 * Set the value.
95 * @param pName the name of the value
96 * @param pValue the value
97 * @throws PoolException on error
98 */
99 private void setStringValue(final String pName,
100 final String pValue) throws PoolException {
101 /* If we are setting a null value */
102 if (pName == null) {
103 /* remove the value */
104 removeStringValue(PREF_FILENAME);
105
106 /* else just set the value */
107 } else {
108 theHandle.put(pName, pValue);
109 }
110
111 /* Flush the output */
112 flushChanges();
113 }
114
115 /**
116 * Remove the value.
117 * @param pName the name of the value
118 */
119 private void removeStringValue(final String pName) {
120 /* Look up existing value */
121 String myCurr = readStringValue(pName);
122 if (myCurr != null) {
123 theHandle.remove(pName);
124 }
125 }
126
127 /**
128 * Flush changes.
129 * @throws PoolException on error
130 */
131 private void flushChanges() throws PoolException {
132 /* Flush changes */
133 try {
134 theHandle.flush();
135 } catch (BackingStoreException e) {
136 throw new PoolException("Failed to flush preferences", e);
137 }
138 }
139 }