/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.project;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javolution37.javolution.xml.ObjectReader;
import javolution37.javolution.xml.ObjectWriter;

import org.apache.log4j.Logger;

import com.jstatcom.component.TopFrame;
import com.jstatcom.io.FileSupport;
import com.jstatcom.model.JSCConstants;

/**
 * Singleton class that manages storing and retrieving of settings that should
 * be kept permanently between two program runs. The methods
 * saveSettings and restoreSettings are
 * automatically being invoked from the TopFrame class when the
 * application is quit or started, respectively.
 * 

* This class also loads the last state from a special project file. This way * the application always restores the project settings that were used when it * was quit. *

* The last state and the settings are store in two different XML files in the * users home directory under a name starting with a ".", followed by the title * of the application, and the suffix "_settings" and "_laststate", * respectively. * * @author Markus Kraetzig */ public final class SettingsManager { private static final Logger log = Logger.getLogger(SettingsManager.class); private final String settingsFileName = "." + TopFrame.TITLE.toLowerCase() + "_settings"; private final String lastStateFileName = "." + TopFrame.TITLE.toLowerCase() + "_laststate"; private final String settingsPath = JSCConstants.getUserHome() + File.separator + settingsFileName; public final String LAST_STATE_PATH = JSCConstants.getUserHome() + File.separator + lastStateFileName; private static final SettingsManager INSTANCE = new SettingsManager(); private SettingsManager() { // not to be invoked } public static SettingsManager getInstance() { return INSTANCE; } /** * Invokes deserialization from the settings file. All settings elements are * restored and the setElement method of each instance is * called. *

* Typically this method is invoked at program start. */ public void restoreSettings() { // restore settings ObjectReader constructor = new ObjectReader(); File file = new File(settingsPath); if (!file.canRead()) return; Settings settings = null; try { settings = (Settings) constructor.read(new FileInputStream(file)); } catch (Throwable t) { log.error("Failed to read settings from " + file.getAbsolutePath(), t); return; } for (Iterator iter = settings.getSettingsIterator(); iter.hasNext();) { SettingsElement element = (SettingsElement) iter.next(); element.setElement(); } } /** * Saves all settings elements to an XML. */ public void saveSettings() { Settings settings = new Settings(); // add project nodes for (Iterator iter = ProjectTree.getInstance() .getProjectNodesIterator(); iter.hasNext();) { ProjectTreeNode element = (ProjectTreeNode) iter.next(); if (!element.isLastState()) settings.addSettingsElement(element); } // could as well be automated with SettingsHandler // registering, but probably not needed // get TopFrameSettings from TopFrame TopFrame topFrame = ProjectManager.getInstance().getTopFrame(); if (topFrame != null) { SettingsElement element = topFrame.getTopFrameSettings(); settings.addSettingsElement(element); } // get AppSettings from TopFrame if (topFrame != null) { SettingsElement element = topFrame.getAppSettings(); settings.addSettingsElement(element); } // get FileSupportSettings from FileSupport singleton instance FileSupport fSupport = FileSupport.getInstance(); if (fSupport != null) { SettingsElement element = fSupport.getFileSupportSettings(); settings.addSettingsElement(element); } // invoke serialization try { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(settingsPath)); ObjectWriter objectWriter = new ObjectWriter(); objectWriter.setPackagePrefix("", "com.jstatcom.model"); objectWriter.setPackagePrefix("jstatcom", "com.jstatcom"); objectWriter.write(settings, out); // Uses UTF-8 } catch (IOException ivjExc) { log.error("XML serialization failed to file " + settingsPath, ivjExc); } } /** * Stores the current state to a special project last state . * */ public void saveLastState() { // save last state ProjectManager.getInstance().serializeProject( new File(LAST_STATE_PATH), "LAST STATE", null); } /** * The last state is retrieved which is just a project. */ public void restoreLastState() { // restore last state File lastStateFile = new File(LAST_STATE_PATH); if (lastStateFile.canRead()) { ProjectTreeNode node = ProjectManager.getInstance() .deSerializeProject(lastStateFile); node.setLastState(true); } } }

Ads help maintain this website.