ArduinoPreferences.java
/*******************************************************************************
* jArduino: Arduino C++ Code Generation From Java
* Copyright 2020 Tony Washer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package net.sourceforge.jarduino.gui;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.sourceforge.jarduino.ArduinoException;
/**
* Preferences.
*/
public final class ArduinoPreferences {
/**
* The save error.
*/
private static final String ERROR_SAVE = "Failed to save preference";
/**
* The default DBC file variable name.
*/
private static final String DEFAULT = "DefaultDBC";
/**
* The default sketchDir variable name.
*/
private static final String SKETCHDIR = "SketchDir";
/**
* The default libraryDir variable name.
*/
private static final String LIBDIR = "LibraryDir";
/**
* The default charSet variable name.
*/
private static final String CHARSET = "CharSet";
/**
* Private constructor.
*/
private ArduinoPreferences() {
}
/**
* Obtain the default location.
* @return the default location
*/
static File getDefaultLocation() {
final Preferences myPreferences = deriveHandle();
final String myLocation = myPreferences.get(DEFAULT, null);
return myLocation == null ? null : new File(myLocation);
}
/**
* Store the default location.
* @param pLocation the default location
* @return the error (or null)
*/
static ArduinoException storeDefaultLocation(final File pLocation) {
/* Protect against exceptions */
try {
final Preferences myPreferences = deriveHandle();
myPreferences.put(DEFAULT, pLocation.getAbsolutePath());
myPreferences.flush();
return null;
} catch (BackingStoreException e) {
return new ArduinoException(ERROR_SAVE, e);
}
}
/**
* Obtain the sketch location.
* @return the sketch location
*/
static File getSketchLocation() {
final Preferences myPreferences = deriveHandle();
final String myLocation = myPreferences.get(SKETCHDIR, null);
return myLocation == null ? null : new File(myLocation);
}
/**
* Store the output location.
* @param pLocation the output location
* @return the error (or null)
*/
static ArduinoException storeSketchLocation(final File pLocation) {
/* Protect against exceptions */
try {
final Preferences myPreferences = deriveHandle();
myPreferences.put(SKETCHDIR, pLocation.getAbsolutePath());
myPreferences.flush();
return null;
} catch (BackingStoreException e) {
return new ArduinoException(ERROR_SAVE, e);
}
}
/**
* Obtain the library location.
* @return the library location
*/
static File getLibraryLocation() {
final Preferences myPreferences = deriveHandle();
final String myLocation = myPreferences.get(LIBDIR, null);
return myLocation == null ? null : new File(myLocation);
}
/**
* Store the library location.
* @param pLocation the library location
* @return the error (or null)
*/
static ArduinoException storeLibraryLocation(final File pLocation) {
/* Protect against exceptions */
try {
final Preferences myPreferences = deriveHandle();
myPreferences.put(LIBDIR, pLocation.getAbsolutePath());
myPreferences.flush();
return null;
} catch (BackingStoreException e) {
return new ArduinoException(ERROR_SAVE, e);
}
}
/**
* Obtain the charSet.
* @return the charSet
*/
static Charset getCharSet() {
final Preferences myPreferences = deriveHandle();
final String myName = myPreferences.get(CHARSET, StandardCharsets.ISO_8859_1.name());
return myName.equals(StandardCharsets.UTF_8.name())
? StandardCharsets.UTF_8
: StandardCharsets.ISO_8859_1;
}
/**
* Store the charSet.
* @param pCharSet the charSet
* @return the error (or null)
*/
static ArduinoException storeCharSet(final Charset pCharSet) {
/* Protect against exceptions */
try {
final Preferences myPreferences = deriveHandle();
myPreferences.put(CHARSET, pCharSet.name());
myPreferences.flush();
return null;
} catch (BackingStoreException e) {
return new ArduinoException(ERROR_SAVE, e);
}
}
/**
* Derive handle for node.
* @return the class name
*/
private static Preferences deriveHandle() {
/* Obtain the class name */
final Class<?> myClass = ArduinoPreferences.class;
String myName = myClass.getCanonicalName();
/* Obtain the package name */
final String myPackage = myClass.getPackage().getName();
/* Strip off the package name */
myName = myName.substring(myPackage.length() + 1);
/* Derive the handle */
final Preferences myHandle = Preferences.userNodeForPackage(myClass);
return myHandle.node(myName);
}
}