/*========================================================================= Program: FusionViewer Module: $RCSfile: WindowLevelPresets.java,v $ Language: Java Date: $Date: 2007/02/02 19:26:29 $ Version: $Revision: 1.3 $ 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.IOException; import java.util.ArrayList; import java.util.Iterator; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; /**\class WindowLevelPresets *\brief A list of presets for window and level image constrast settings that * can be loaded from an XML file. */ public class WindowLevelPresets { private final static String SETTING_TAG = "setting"; private final static String NAME_ATTRIBUTE = "name"; private final static String WINDOW_TAG = "window"; private final static String LEVEL_TAG = "level"; private ArrayList m_settings = new ArrayList(); /** * Read a list of window and level settings from an XML file. * * @param presetsFile file to read settings from * @throws IOException if there is an error reading presetsFile */ public WindowLevelPresets(File presetsFile) throws IOException { if (presetsFile.exists()) load(presetsFile); } /* * Read the XML file. */ private void load(File presetsFile) throws IOException { try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(presetsFile); Element root = doc.getRootElement(); Iterator it = root.getChildren(SETTING_TAG).iterator(); while (it.hasNext()) { Element setting = (Element) it.next(); m_settings.add(new WindowLevel( setting.getAttribute(NAME_ATTRIBUTE).getValue(), Float.parseFloat(setting.getChildText(WINDOW_TAG)), Float.parseFloat(setting.getChildText(LEVEL_TAG)))); } } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } catch (JDOMException e) { throw new IOException(e.getMessage()); } } /** * Get the number of window and level settings * * @return number of window and level settings */ public int getNumSettings() { return m_settings.size(); } /** * Get a window and level setting * * @param i index of the window and level to get * @return the window and level */ public WindowLevel getWindowLevel(int i) { return (WindowLevel) m_settings.get(i); } /** * A named window and level pair. */ public static final class WindowLevel { private final String name; private final float window; private final float level; private final int hash; /** * Create a named window and level pair. * * @param name name of the pair * @param window see ImageDisplaySettings * @param level see ImageDisplaySettings */ public WindowLevel(String name, float window, float level) { this.name = name; this.window = window; this.level = level; // Hash code computation from Effective Java by Joshua Block int result = 17; result = 37 * result + name.hashCode(); result = 37 * result + Float.floatToIntBits(window); result = 37 * result + Float.floatToIntBits(level); hash = result; } /** * Determine if this object is equivalent to another FilePair. */ public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof WindowLevel)) return false; WindowLevel p = (WindowLevel) o; return name.equals(p.name) && (window == p.window) && (level == p.level); } /** * Compute the hash code for this object. */ public int hashCode() { return hash; } /** * @return Returns the name. */ public String getName() { return name; } /** * @return Returns the level. */ public float getLevel() { return level; } /** * @return Returns the window. */ public float getWindow() { return window; } } }