/* * Copyright 2015 Sudipto Chandra. * * 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 org.sandsoft.cymric.util; import org.sandsoft.cymric.Cymric; import java.io.File; import java.util.prefs.Preferences; /** * Contains various preference data. All data are stored in default preference * storage. * * @author Sudipto Chandra */ public class AppSettings { //prefernces data private final Preferences mPreference; /** * Creates a new AppSettings with default preference path. */ public AppSettings() { mPreference = Preferences.userRoot().node(Cymric.DEFAULT_SETTINGS); } /** * Creates a new AppSettings and returns it. * * @return AppSettings object to get or set preferences. */ public static AppSettings defaultSettings() { return new AppSettings(); } /** * Gets the current preferences object. * * @return Current preferences object. */ public Preferences getPreference() { return mPreference; } /** * Sets the current working directory. * * @param path Full directory path. */ public void setWorkingPath(String path) { mPreference.put("Working Path", path); } /** * Sets the current working directory. * * @return Full directory path if valid, null otherwise. */ public String getWorkingPath() { String path = mPreference.get("Working Path", null); if (isWorkingPathValid(path)) { return path; } else { return null; } } /** * Checks whether the working directory specified is valid. * * @param path Full directory path * @return True if valid, false otherwise. */ private boolean isWorkingPathValid(String path) { if (path == null || path.isEmpty()) { return false; } File file = new File(path); return (file.exists() && file.isDirectory()); } }