Mercurial > hg > ltpda
view src/MPipeline2/src/mpipeline/ltpdapreferences/LTPDAPreferences.java @ 9:fbbfcd56e449 database-connection-manager
Remove dead code
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children | 4be5f7a5316f |
line wrap: on
line source
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package mpipeline.ltpdapreferences; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Observable; import java.util.Observer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import mpipeline.utils.Utils; import mpipeline.utils.XMLUtils; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * Needs setters/getters for each preference. * Needs save/load method * */ /** * * @author hewitson */ public class LTPDAPreferences extends Observable { private File filename; private float version = 1.0f; private ArrayList<String> availableWindows; private PlotPrefGroup plotPrefs; private DisplayPrefGroup displayPrefs; private ModelsPrefGroup modelsPrefs; private ExtensionsPrefGroup extensionsPrefs; private TimePrefGroup timePrefs; private RepositoryPrefGroup repoPrefs; private ExternalPrefsGroup externalPrefs; private MiscPrefGroup miscPrefs; public LTPDAPreferences(File filepath, float version, ArrayList<String> windows) { this.availableWindows = windows; this.version = version; this.filename = filepath; initPreferences(); // observePreferences(); } private void initPreferences() { plotPrefs = new PlotPrefGroup(); displayPrefs = new DisplayPrefGroup(); modelsPrefs = new ModelsPrefGroup(); extensionsPrefs = new ExtensionsPrefGroup(); timePrefs = new TimePrefGroup(); repoPrefs = new RepositoryPrefGroup(); externalPrefs = new ExternalPrefsGroup(); miscPrefs = new MiscPrefGroup(); } private void observePreferences() { // plotPrefs.addObserver(this); // displayPrefs.addObserver(this); // modelsPrefs.addObserver(this); // extensionsPrefs.addObserver(this); // timePrefs.addObserver(this); // repoPrefs.addObserver(this); // externalPrefs.addObserver(this); // miscPrefs.addObserver(this); } private LTPDAPreferences(Node node, float version) { NamedNodeMap wbnm = node.getAttributes(); initPreferences(); // get all children of the root node NodeList nl = node.getChildNodes(); for (int ii = 0; ii < nl.getLength(); ii++) { Node n = nl.item(ii); Utils.dmsg(" reading child node: " + n.getNodeName()); if (n.getNodeName().equals("Plot")) { this.plotPrefs = new PlotPrefGroup(n, version); } else if (n.getNodeName().equals("Display")) { this.displayPrefs = new DisplayPrefGroup(n, version); } else if (n.getNodeName().equals("Models")) { this.modelsPrefs = new ModelsPrefGroup(n, version); } else if (n.getNodeName().equals("Extensions")) { this.extensionsPrefs = new ExtensionsPrefGroup(n, version); } else if (n.getNodeName().equals("Time")) { this.timePrefs = new TimePrefGroup(n, version); } else if (n.getNodeName().equals("Repository")) { this.repoPrefs = new RepositoryPrefGroup(n, version); } else if (n.getNodeName().equals("External")) { this.externalPrefs = new ExternalPrefsGroup(n, version); } else if (n.getNodeName().equals("Misc")) { this.miscPrefs = new MiscPrefGroup(n, version); } } observePreferences(); this.version = version; } public void reload() { LTPDAPreferences loadedPrefs = LTPDAPreferences.loadFromDisk(filename.getPath(), version); this.plotPrefs = loadedPrefs.plotPrefs; this.displayPrefs = loadedPrefs.displayPrefs; this.modelsPrefs = loadedPrefs.modelsPrefs; this.extensionsPrefs = loadedPrefs.extensionsPrefs; this.timePrefs = loadedPrefs.timePrefs; this.repoPrefs = loadedPrefs.repoPrefs; this.externalPrefs = loadedPrefs.externalPrefs; this.miscPrefs = loadedPrefs.miscPrefs; observePreferences(); this.display(); } public static LTPDAPreferences loadFromDisk(String filepath, float version) { LTPDAPreferences loadedPrefs = null; File filename = new File(filepath); if (filename.exists()) { try { DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); parserFactory.setValidating(false); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(filename); Element root = doc.getDocumentElement(); float fileVer = -1.0f; if (root.getNodeName().equals("LTPDAPreferences")) { // check shelf file version NamedNodeMap wbnm = root.getAttributes(); Node versionNode = wbnm.getNamedItem("version"); if (versionNode != null) { String fileVersion = versionNode.getNodeValue(); fileVer = Float.parseFloat(fileVersion); if (fileVer != version) { JOptionPane.showMessageDialog(null, "The preference file was created with a different version of LTPDA.\n" + "It may not load properly so please review the resulting preferences.", "Warning", JOptionPane.WARNING_MESSAGE); } } } // get all children of the root node to get shelf items NodeList nl = root.getChildNodes(); for (int ii = 0; ii < nl.getLength(); ii++) { Node n = nl.item(ii); Utils.dmsg(" reading child node: " + n.getNodeName()); if (n.getNodeName().equals("Preferences")) { loadedPrefs = new LTPDAPreferences(n, version); } } } catch (SAXException ex) { Logger.getLogger(LTPDAPreferences.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(LTPDAPreferences.class.getName()).log(Level.SEVERE, null, ex); } catch (ParserConfigurationException ex) { Logger.getLogger(LTPDAPreferences.class.getName()).log(Level.SEVERE, null, ex); } } else { System.err.println("Preference file not found: " + filename); } if (loadedPrefs == null) { loadedPrefs = new LTPDAPreferences(filename, version, new ArrayList<String>()); } loadedPrefs.setFilename(filename); return loadedPrefs; } public void writeToDisk() { Utils.msg("Saving preferences to " + filename); if (filename != null) { try { Document doc = XMLUtils.createDocument("LTPDAPreferences"); Element root = doc.getDocumentElement(); root.setAttribute("version", "" + version); this.attachToDom(doc, root); XMLUtils.serializeXML(doc, filename); } catch (Exception ex) { Logger.getLogger(LTPDAPreferences.class.getName()).log(Level.SEVERE, null, ex); } } } /** * Attach this prefs object to the given XML DOM node. * @param doc * @param root * @throws DOMException */ public void attachToDom(Document doc, Element root) throws DOMException { // start new document node Element tnode = doc.createElement("Preferences"); // add each preference group plotPrefs.attachToDom(doc, tnode); displayPrefs.attachToDom(doc, tnode); extensionsPrefs.attachToDom(doc, tnode); timePrefs.attachToDom(doc, tnode); repoPrefs.attachToDom(doc, tnode); externalPrefs.attachToDom(doc, tnode); miscPrefs.attachToDom(doc, tnode); root.appendChild(tnode); } public DisplayPrefGroup getDisplayPrefs() { return displayPrefs; } public void setDisplayPrefs(DisplayPrefGroup displayPrefs) { this.displayPrefs = displayPrefs; } public PlotPrefGroup getPlotPrefs() { return plotPrefs; } public void setPlotPrefs(PlotPrefGroup plotPrefs) { this.plotPrefs = plotPrefs; } public ModelsPrefGroup getModelsPrefs() { return modelsPrefs; } public void setModelsPrefs(ModelsPrefGroup modelsPrefs) { this.modelsPrefs = modelsPrefs; } public ExtensionsPrefGroup getExtensionsPrefs() { return extensionsPrefs; } public void setExtensionsPrefs(ExtensionsPrefGroup extensionsPrefs) { this.extensionsPrefs = extensionsPrefs; } public TimePrefGroup getTimePrefs() { return timePrefs; } public void setTimePrefs(TimePrefGroup timePrefs) { this.timePrefs = timePrefs; } public RepositoryPrefGroup getRepoPrefs() { return repoPrefs; } public void setRepoPrefs(RepositoryPrefGroup repoPrefs) { this.repoPrefs = repoPrefs; } public ExternalPrefsGroup getExternalPrefs() { return externalPrefs; } public void setExternalPrefs(ExternalPrefsGroup externalPrefs) { this.externalPrefs = externalPrefs; } public MiscPrefGroup getMiscPrefs() { return miscPrefs; } public void setMiscPrefs(MiscPrefGroup miscPrefs) { this.miscPrefs = miscPrefs; } public File getFilename() { return filename; } public void setFilename(File filename) { this.filename = filename; } public ArrayList<String> getAvailableWindows() { return availableWindows; } public void setAvailableWindows(ArrayList<String> availableWindows) { this.availableWindows = availableWindows; } public void display() { System.out.println("LTPDAPreferences"); System.out.println(" filename: " + filename); displayPrefs.display(); plotPrefs.display(); modelsPrefs.display(); extensionsPrefs.display(); timePrefs.display(); repoPrefs.display(); externalPrefs.display(); miscPrefs.display(); } // public void update(Observable o, Object arg) { // this.setChanged(); // this.notifyObservers(o); // } }