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;
021
022 import java.util.Iterator;
023 import java.util.LinkedList;
024 import java.util.List;
025 import java.util.Map;
026
027 import org.dom4j.Attribute;
028 import org.dom4j.Element;
029 import org.mactor.framework.MactorException;
030
031 public class XmlProjectTreeNode extends ProjectTreeNode {
032 protected Map<String, ProjectNodeType> nodeTypeDictionary;
033 public XmlProjectTreeNode(ProjectNodeType nodeType, Element modelElement, Map<String, ProjectNodeType> nodeTypeDictionary) {
034 super(nodeType, modelElement);
035 this.nodeTypeDictionary = nodeTypeDictionary;
036 Iterator it = modelElement.elementIterator();
037 while (it.hasNext()) {
038 Element el = (Element) it.next();
039 ProjectNodeType elType = nodeTypeDictionary.get(el.getName());
040 if (elType == null)
041 continue;
042 XmlProjectTreeNode child = new XmlProjectTreeNode(elType, el, nodeTypeDictionary);
043 if (child != null) {
044 child.parentNode = this;
045 childNodes.add(child);
046 }
047 }
048 }
049 protected Element getModelElement() {
050 return (Element) super.getModelObject();
051 }
052 @Override
053 protected ProjectTreeNode copy() throws MactorException {
054 return new XmlProjectTreeNode(getNodeType(), (getModelElement().createCopy()), nodeTypeDictionary);
055 }
056 @Override
057 protected void model_delete() throws MactorException {
058 getModelElement().detach();
059 }
060 @Override
061 protected void model_detatch() throws MactorException {
062 getModelElement().detach();
063 }
064 @Override
065 protected String model_getName() {
066 return getModelElement().attributeValue("name");
067 }
068 @Override
069 protected void model_insert_child(int index, Object modelObject) {
070 if (index == getModelElement().elements().size()) {
071 getModelElement().add((Element) modelObject);
072 } else {
073 List elemements = getModelElement().elements();
074 List newElements = new LinkedList();
075 Iterator it = elemements.iterator();
076 while (it.hasNext()) {
077 newElements.add(((Element) it.next()).detach());
078 }
079 newElements.add(index, modelObject);
080 it = newElements.iterator();
081 while (it.hasNext()) {
082 getModelElement().add((Element) it.next());
083 }
084 }
085 }
086 @Override
087 protected void model_remove_child(int index) {
088 Iterator it = getModelElement().elementIterator();
089 Element el = (Element) it.next();
090 for (int i = 0; i < index; i++)
091 el = (Element) it.next();
092 el.detach();
093 }
094 @Override
095 protected boolean model_rename(String newName) throws MactorException {
096 Attribute a = getModelElement().attribute("name");
097 if (a == null)
098 return false;
099 a.setValue(newName);
100 return true;
101 }
102 @Override
103 protected void model_save() throws MactorException {
104 parentNode.model_save();
105 }
106 }