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.ArrayList;
023    import java.util.List;
024    
025    import org.mactor.framework.MactorException;
026    
027    public abstract class ProjectTreeNode {
028            protected ProjectNodeType nodeType;
029            protected ArrayList<ProjectTreeNode> childNodes = new ArrayList<ProjectTreeNode>();
030            protected ProjectTreeNode parentNode;
031            protected Object modelObject;
032            protected ProjectTreeNode(ProjectNodeType nodeType) {
033                    this.nodeType = nodeType;
034            }
035            public ProjectTreeNode(ProjectNodeType nodeType, Object modelObject) {
036                    this.nodeType = nodeType;
037                    this.modelObject = modelObject;
038            }
039            public String getName() {
040                    if (modelObject == null)
041                            return nodeType.name();
042                    return model_getName();
043            }
044            public String getCaption() {
045                    return getName();
046            }
047            public List<ProjectTreeNode> getChildNodes() {
048                    return childNodes;
049            }
050            public Object getModelObject() {
051                    return modelObject;
052            }
053            public ProjectNodeType getNodeType() {
054                    return nodeType;
055            }
056            public ProjectTreeNode getParentNode() {
057                    return parentNode;
058            }
059            public ProjectTreeNode getChildNode(int index) {
060                    return childNodes.get(index);
061            }
062            public void detach() throws MactorException {
063                    model_detatch();
064            }
065            public void removeChild(ProjectTreeNode child) throws MactorException {
066                    int index = childNodes.indexOf(child);
067                    childNodes.remove(child);
068                    model_remove_child(index);
069                    child.model_detatch();
070                    model_save();
071            }
072            public void addChild(ProjectTreeNode newChild) throws MactorException {
073                    newChild.setParentNode(this);
074                    childNodes.add(0, newChild);
075                    model_insert_child(0, newChild.getModelObject());
076                    model_save();
077            }
078            public void addChildAfter(ProjectTreeNode node, ProjectTreeNode newChild) throws MactorException {
079                    newChild.setParentNode(this);
080                    int index = childNodes.indexOf(node);
081                    childNodes.add(index + 1, newChild);
082                    model_insert_child(index + 1, newChild.getModelObject());
083                    model_save();
084            }
085            public boolean rename(String newName) throws MactorException {
086                    if (model_rename(newName)) {
087                            model_save();
088                            return true;
089                    }
090                    return false;
091            }
092            public void save() throws MactorException {
093                    model_save();
094            }
095            public int getIndexOfChild(ProjectTreeNode child) {
096                    return childNodes.indexOf(child);
097            }
098            public int getChildCount() {
099                    return childNodes.size();
100            }
101            private void setParentNode(ProjectTreeNode parentNode) {
102                    this.parentNode = parentNode;
103            }
104            @Override
105            public String toString() {
106                    return getCaption();
107            }
108            protected abstract ProjectTreeNode copy() throws MactorException;
109            protected abstract void model_remove_child(int index);
110            protected abstract void model_insert_child(int index, Object newChild);
111            protected abstract void model_delete() throws MactorException;
112            protected abstract void model_detatch() throws MactorException;
113            protected abstract boolean model_rename(String newName) throws MactorException;
114            protected abstract String model_getName();
115            protected abstract void model_save() throws MactorException;
116    }