View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 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.jarduino.gui;
18  
19  import java.io.File;
20  import java.nio.charset.Charset;
21  import java.nio.charset.StandardCharsets;
22  import java.util.prefs.BackingStoreException;
23  import java.util.prefs.Preferences;
24  
25  import net.sourceforge.jarduino.ArduinoException;
26  
27  /**
28   * Preferences.
29   */
30  public final class ArduinoPreferences {
31      /**
32       * The save error.
33       */
34      private static final String ERROR_SAVE = "Failed to save preference";
35  
36      /**
37       * The default DBC file variable name.
38       */
39      private static final String DEFAULT = "DefaultDBC";
40  
41      /**
42       * The default sketchDir variable name.
43       */
44      private static final String SKETCHDIR = "SketchDir";
45  
46      /**
47       * The default libraryDir variable name.
48       */
49      private static final String LIBDIR = "LibraryDir";
50  
51      /**
52       * The default charSet variable name.
53       */
54      private static final String CHARSET = "CharSet";
55  
56      /**
57       * Private constructor.
58       */
59      private ArduinoPreferences() {
60      }
61  
62      /**
63       * Obtain the default location.
64       * @return the default location
65       */
66      static File getDefaultLocation() {
67          final Preferences myPreferences = deriveHandle();
68          final String myLocation = myPreferences.get(DEFAULT, null);
69          return myLocation == null ? null : new File(myLocation);
70      }
71  
72      /**
73       * Store the default location.
74       * @param pLocation the default location
75       * @return the error (or null)
76       */
77      static ArduinoException storeDefaultLocation(final File pLocation) {
78          /* Protect against exceptions */
79          try {
80              final Preferences myPreferences = deriveHandle();
81              myPreferences.put(DEFAULT, pLocation.getAbsolutePath());
82              myPreferences.flush();
83              return null;
84  
85          } catch (BackingStoreException e) {
86              return new ArduinoException(ERROR_SAVE, e);
87          }
88      }
89  
90      /**
91       * Obtain the sketch location.
92       * @return the sketch location
93       */
94      static File getSketchLocation() {
95          final Preferences myPreferences = deriveHandle();
96          final String myLocation = myPreferences.get(SKETCHDIR, null);
97          return myLocation == null ? null : new File(myLocation);
98      }
99  
100     /**
101      * Store the output location.
102      * @param pLocation the output location
103      * @return the error (or null)
104      */
105     static ArduinoException storeSketchLocation(final File pLocation) {
106         /* Protect against exceptions */
107         try {
108             final Preferences myPreferences = deriveHandle();
109             myPreferences.put(SKETCHDIR, pLocation.getAbsolutePath());
110             myPreferences.flush();
111             return null;
112 
113         } catch (BackingStoreException e) {
114             return new ArduinoException(ERROR_SAVE, e);
115         }
116     }
117 
118     /**
119      * Obtain the library location.
120      * @return the library location
121      */
122     static File getLibraryLocation() {
123         final Preferences myPreferences = deriveHandle();
124         final String myLocation = myPreferences.get(LIBDIR, null);
125         return myLocation == null ? null : new File(myLocation);
126     }
127 
128     /**
129      * Store the library location.
130      * @param pLocation the library location
131      * @return the error (or null)
132      */
133     static ArduinoException storeLibraryLocation(final File pLocation) {
134         /* Protect against exceptions */
135         try {
136             final Preferences myPreferences = deriveHandle();
137             myPreferences.put(LIBDIR, pLocation.getAbsolutePath());
138             myPreferences.flush();
139             return null;
140 
141         } catch (BackingStoreException e) {
142             return new ArduinoException(ERROR_SAVE, e);
143         }
144     }
145 
146     /**
147      * Obtain the charSet.
148      * @return the charSet
149      */
150     static Charset getCharSet() {
151         final Preferences myPreferences = deriveHandle();
152         final String myName = myPreferences.get(CHARSET, StandardCharsets.ISO_8859_1.name());
153         return myName.equals(StandardCharsets.UTF_8.name())
154                ? StandardCharsets.UTF_8
155                : StandardCharsets.ISO_8859_1;
156      }
157 
158     /**
159      * Store the charSet.
160      * @param pCharSet the charSet
161      * @return the error (or null)
162      */
163     static ArduinoException storeCharSet(final Charset pCharSet) {
164         /* Protect against exceptions */
165         try {
166             final Preferences myPreferences = deriveHandle();
167             myPreferences.put(CHARSET, pCharSet.name());
168             myPreferences.flush();
169             return null;
170 
171         } catch (BackingStoreException e) {
172             return new ArduinoException(ERROR_SAVE, e);
173         }
174     }
175 
176     /**
177      * Derive handle for node.
178      * @return the class name
179      */
180     private static Preferences deriveHandle() {
181         /* Obtain the class name */
182         final Class<?> myClass = ArduinoPreferences.class;
183         String myName = myClass.getCanonicalName();
184 
185         /* Obtain the package name */
186         final String myPackage = myClass.getPackage().getName();
187 
188         /* Strip off the package name */
189         myName = myName.substring(myPackage.length() + 1);
190 
191         /* Derive the handle */
192         final Preferences myHandle = Preferences.userNodeForPackage(myClass);
193         return myHandle.node(myName);
194     }
195 }