001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project.editors;
021    
022    import java.net.URL;
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    import org.dom4j.Document;
028    import org.dom4j.Element;
029    import org.dom4j.io.SAXReader;
030    import org.mactor.ui.gui.project.ProjectNodeType;
031    
032    public class NodeEditorConfigManager {
033            private static NodeEditorConfigManager instance;
034            private static Object lock = new Object();
035            private Map<String, NodeEditorConfig> nodeEditorConfigMap = new HashMap<String, NodeEditorConfig>();
036            public static NodeEditorConfigManager getInstance() {
037                    if (instance == null) {
038                            synchronized (lock) {
039                                    if (instance == null) {
040                                            instance = new NodeEditorConfigManager();
041                                    }
042                            }
043                    }
044                    return instance;
045            }
046            public NodeEditorConfig getNodeEditorConfig(ProjectNodeType nodeType) {
047                    if(nodeType==null)
048                            return null;
049                    return nodeEditorConfigMap.get(nodeType.name());
050            }
051            private NodeEditorConfigManager() {
052                    try {
053                            loadConfig("node_editors.xml");
054                    } catch (Exception e) {
055                            e.printStackTrace();
056                    }
057                    try {
058                            loadConfig("node_editors_ex.xml");
059                            System.out.print("node_editors_ex.xml was loaded");
060                    } catch (Exception e) {
061                            System.out.print("node_editors_ex.xml was not loaded. Reason:" + e.getMessage());
062                    }
063            }
064            private void loadConfig(String fn) throws Exception {
065                    URL u = Thread.currentThread().getContextClassLoader().getResource(fn);
066                    if (u == null)
067                            throw new RuntimeException("Resource '" + fn + "' was not found");
068                    Document doc = new SAXReader().read(u);
069                    Iterator typeIt = doc.getRootElement().elementIterator("node_type");
070                    while (typeIt.hasNext()) {
071                            Element e = (Element) typeIt.next();
072                            String name = e.attributeValue("name");
073                            ProjectNodeType nodeType = ProjectNodeType.valueOf(name);
074                            if (nodeType == null) {
075                                    System.out.println("Unsupported node type '" + name + "' ignoring...");
076                                    continue;
077                            }
078                            NodeEditorConfig config = nodeEditorConfigMap.get(name);
079                            if (config == null) {
080                                    config = new NodeEditorConfig(nodeType);
081                                    nodeEditorConfigMap.put(name, config);
082                            }
083                            config.load(e);
084                    }
085            }
086    }