/*========================================================================= Program: FusionViewer Module: $RCSfile: Configuration.java,v $ Language: Java Date: $Date: 2007/02/02 19:26:29 $ Version: $Revision: 1.2 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.io; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.fusionviewer.ui.ViewArrangement; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /**\class Configuration *\brief Saves and loads application settings from an XML file. */ public class Configuration { private File m_configFile; // Location of the configuration file private FileHistory m_fileHistory = new FileHistory(); // List of files recently opened private int m_lastDisplayLayout = FUSION_LAYOUT; private int m_lastImageOrientation = ViewArrangement.AXIAL; public final static int FUSION_LAYOUT = 1; public final static int LINKED_CURSOR_LAYOUT = 2; private final static String DOCUMENT = "Configuration"; private final static String FILE_HISTORY_TAG = "RecentFiles"; private final static String DISPLAY_MODE_TAG = "DisplayMode"; private final static String IMAGE_ORIENTATION_TAG = "ImageOrientation"; private final static String FUSION_LAYOUT_STRING = "Fusion"; private final static String LINKED_CURSOR_LAYOUT_STRING = "Linked Cursor"; private final static String AXIAL_ORIENTATION_STRING = "Axial"; private final static String SAGITTAL_ORIENTATION_STRING = "Sagittal"; private final static String CORONAL_ORIENTATION_STRING = "Coronal"; private static Configuration m_configuration; // Singleton instance /** * Return the single instance of the Configuration object * * @return a Configuration */ public static synchronized Configuration getConfiguration() { if (m_configuration == null) { try { // Try loading an existing configuration file m_configuration = new Configuration(); } catch (IOException e) { // If the load failed, create a new default Configuration m_configuration = new Configuration(true); } } return m_configuration; } /* * Create a new Configuration, loading a configuration file if it exists. */ private Configuration() throws IOException { m_configFile = new File(Env.PREFERENCES_DIRECTORY, Env.CONFIG); if (m_configFile.exists()) load(); } /* * Create a new Configuration with the default settings. */ private Configuration(boolean noLoad) { m_configFile = new File(Env.PREFERENCES_DIRECTORY, Env.CONFIG); } /** * Saves the application configuration. * * @throws IOException */ public void save() throws IOException { Element rootElement = new Element(DOCUMENT); Document doc = new Document(rootElement); // Add display mode Element displayModeElement = new Element(DISPLAY_MODE_TAG); if (m_lastDisplayLayout == FUSION_LAYOUT) displayModeElement.addContent(FUSION_LAYOUT_STRING); else if (m_lastDisplayLayout == LINKED_CURSOR_LAYOUT) displayModeElement.addContent(LINKED_CURSOR_LAYOUT_STRING); else displayModeElement.addContent(FUSION_LAYOUT_STRING); rootElement.addContent(displayModeElement); // Add image orientation Element imageOrientationElement = new Element(IMAGE_ORIENTATION_TAG); if (m_lastImageOrientation == ViewArrangement.AXIAL) imageOrientationElement.addContent(AXIAL_ORIENTATION_STRING); else if (m_lastImageOrientation == ViewArrangement.SAGITTAL) imageOrientationElement.addContent(SAGITTAL_ORIENTATION_STRING); else if (m_lastImageOrientation == ViewArrangement.CORONAL) imageOrientationElement.addContent(CORONAL_ORIENTATION_STRING); else imageOrientationElement.addContent(AXIAL_ORIENTATION_STRING); rootElement.addContent(imageOrientationElement); // Add file history Element fileList = new Element(FILE_HISTORY_TAG); m_fileHistory.getFileHistoryAsXml(fileList); rootElement.addContent(fileList); // Write XML text to a file XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); FileWriter writer = new FileWriter(m_configFile); try { outputter.output(doc, writer); } finally { writer.close(); } } /* * Loads the application configuration. */ private void load() throws IOException { try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(m_configFile); Element root = doc.getRootElement(); String displayMode = root.getChildTextTrim(DISPLAY_MODE_TAG); if (displayMode != null) { if (displayMode.compareToIgnoreCase(FUSION_LAYOUT_STRING) == 0) m_lastDisplayLayout = FUSION_LAYOUT; else if (displayMode.compareToIgnoreCase(LINKED_CURSOR_LAYOUT_STRING) == 0) m_lastDisplayLayout = LINKED_CURSOR_LAYOUT; } String imageOrientation = root.getChildTextTrim(IMAGE_ORIENTATION_TAG); if (imageOrientation != null) { if (imageOrientation.compareToIgnoreCase(AXIAL_ORIENTATION_STRING) == 0) m_lastImageOrientation = ViewArrangement.AXIAL; else if (imageOrientation.compareToIgnoreCase(SAGITTAL_ORIENTATION_STRING) == 0) m_lastImageOrientation = ViewArrangement.SAGITTAL; else if (imageOrientation.compareToIgnoreCase(CORONAL_ORIENTATION_STRING) == 0) m_lastImageOrientation = ViewArrangement.CORONAL; } m_fileHistory.readFromXml(root.getChild(FILE_HISTORY_TAG)); } catch (JDOMException e) { throw new IOException(e.getMessage()); } } /** * Return a list of recently opened file pairs. * * @return file history list */ public FileHistory getFileHistory() { return m_fileHistory; } /** * Return the last display layout type used (fusion or linked cursor). * * @return display layout */ public int getLastDisplayLayout() { return m_lastDisplayLayout; } /** * Set the last display layout type used (fusion or linked cursor). * * @param layout display layout */ public void setLastDisplayLayout(int layout) { if ((layout != FUSION_LAYOUT) && (layout != LINKED_CURSOR_LAYOUT)) throw new IllegalArgumentException("Unknown layout type " + Integer.toString(layout)); m_lastDisplayLayout = layout; } /** * Get the last image orientation used * * @return image orientation as defined in ViewArrangement */ public int getLastImageOrientation() { return m_lastImageOrientation; } /** * Set the last image orientation used * * @param orientation image orientation as defined in ViewArrangement */ public void setLastImageOrientation(int orientation) { if ((orientation != ViewArrangement.AXIAL) && (orientation != ViewArrangement.SAGITTAL) && (orientation != ViewArrangement.CORONAL)) throw new IllegalArgumentException("Unknown orientation " + Integer.toString(orientation)); m_lastImageOrientation = orientation; } }