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.io.File; 023 import java.util.LinkedList; 024 import java.util.List; 025 026 import javax.swing.event.TreeModelEvent; 027 import javax.swing.event.TreeModelListener; 028 import javax.swing.tree.TreeModel; 029 import javax.swing.tree.TreePath; 030 031 import org.mactor.framework.MactorException; 032 033 public class ProjectModel implements TreeModel { 034 ProjectTreeNode root;// = new 035 // OrganizationProjectTreeNode(ProjectNodeType.PROJECT_ROOT); 036 public ProjectModel(File projectPath) throws MactorException { 037 if (projectPath == null) 038 root = null; 039 else 040 root = ProjectTreeNodeBuilder.buildProjectTree(); 041 } 042 public void reload(File projectPath) throws MactorException { 043 root = ProjectTreeNodeBuilder.buildProjectTree(); 044 notifyTreeStructureChanged(); 045 } 046 public Object getChild(Object node, int index) { 047 return ((ProjectTreeNode) node).getChildNode(index); 048 } 049 public int getChildCount(Object node) { 050 return ((ProjectTreeNode) node).getChildCount(); 051 } 052 public int getIndexOfChild(Object node, Object child) { 053 return ((ProjectTreeNode) node).getIndexOfChild((ProjectTreeNode) child); 054 } 055 public ProjectTreeNode getRoot() { 056 return root; 057 } 058 public boolean isLeaf(Object node) { 059 return ((ProjectTreeNode) node).getChildCount() == 0; 060 } 061 private void notifyTreeStructureChanged() { 062 TreeModelEvent e = new TreeModelEvent(this, new Object[] { root }); 063 for (TreeModelListener l : listeners) 064 l.treeStructureChanged(e); 065 } 066 public void notifyTreeStructureChanged(ProjectTreeNode node) { 067 TreeModelEvent e = new TreeModelEvent(this, new Object[] { getPath(node) }); 068 for (TreeModelListener l : listeners) 069 l.treeStructureChanged(e); 070 } 071 private void notifyTreeNodesChanged(Object[] path, int index, Object changedNode) { 072 TreeModelEvent e = new TreeModelEvent(this, path, new int[] { index }, new Object[] { changedNode }); 073 for (TreeModelListener l : listeners) 074 l.treeNodesChanged(e); 075 } 076 public void notifyNodeNameChanged(ProjectTreeNode tn) { 077 int index = tn.getParentNode().getIndexOfChild(tn); 078 notifyTreeNodesChanged(getPath(tn), index, tn); 079 } 080 /* 081 * private void notifyTreeNodeRemoved(Object[] path, int index, Object 082 * removedNode) { TreeModelEvent e = new TreeModelEvent(this, path, new 083 * int[] { index }, new Object[] { removedNode }); for (TreeModelListener l : 084 * listeners) l.treeNodesRemoved(e); } 085 */ 086 private void notifyTreeNodeInserted(Object[] path, int index, Object insertedNode) { 087 TreeModelEvent e = new TreeModelEvent(this, path, new int[] { index }, new Object[] { insertedNode }); 088 for (TreeModelListener l : listeners) 089 l.treeNodesInserted(e); 090 } 091 private void notifyTreeNodesRemoved(Object[] path, int index) { 092 TreeModelEvent e = new TreeModelEvent(this, path, new int[] { index }, new Object[] { path[path.length - 1] }); 093 for (TreeModelListener l : listeners) 094 l.treeNodesRemoved(e); 095 } 096 public static Object[] getPath(ProjectTreeNode node) { 097 LinkedList<ProjectTreeNode> path = new LinkedList<ProjectTreeNode>(); 098 while (node != null) { 099 path.addFirst(node); 100 node = node.getParentNode(); 101 } 102 return path.toArray(); 103 } 104 public void moveAfter(ProjectTreeNode destNode, ProjectTreeNode nodeToMove) throws MactorException { 105 Object[] path = getPath(nodeToMove.getParentNode()); 106 int index = nodeToMove.getParentNode().getIndexOfChild(nodeToMove); 107 nodeToMove.getParentNode().removeChild(nodeToMove); 108 notifyTreeNodesRemoved(path, index); 109 ProjectTreeNode copyOfNodeToMove = nodeToMove.copy(); 110 destNode.getParentNode().addChildAfter(destNode, copyOfNodeToMove); 111 index = copyOfNodeToMove.getParentNode().getIndexOfChild(copyOfNodeToMove); 112 notifyTreeNodeInserted(getPath(destNode.getParentNode()), index, copyOfNodeToMove); 113 } 114 public void delete(ProjectTreeNode node) throws MactorException { 115 int index = node.getParentNode().getIndexOfChild(node); 116 node.getParentNode().removeChild(node); 117 notifyTreeNodesRemoved(getPath(node.getParentNode()), index); 118 } 119 public void insertAfter(ProjectTreeNode destNode, ProjectTreeNode nodeToInsert) throws MactorException { 120 destNode.getParentNode().addChildAfter(destNode, nodeToInsert); 121 int index = nodeToInsert.getParentNode().getIndexOfChild(nodeToInsert); 122 notifyTreeNodeInserted(getPath(destNode.getParentNode()), index, nodeToInsert); 123 } 124 public void copyAfter(ProjectTreeNode destNode, ProjectTreeNode nodeToCopy) throws MactorException { 125 nodeToCopy = nodeToCopy.copy(); 126 destNode.getParentNode().addChildAfter(destNode, nodeToCopy); 127 int index = nodeToCopy.getParentNode().getIndexOfChild(nodeToCopy); 128 notifyTreeNodeInserted(getPath(destNode.getParentNode()), index, nodeToCopy); 129 } 130 public void moveInto(ProjectTreeNode destNode, ProjectTreeNode nodeToMove) throws MactorException { 131 Object[] path = getPath(nodeToMove.getParentNode()); 132 int index = nodeToMove.getParentNode().getIndexOfChild(nodeToMove); 133 nodeToMove.getParentNode().removeChild(nodeToMove); 134 notifyTreeNodesRemoved(path, index); 135 ProjectTreeNode copyOfNodeToMove = nodeToMove.copy(); 136 destNode.addChild(copyOfNodeToMove); 137 index = copyOfNodeToMove.getParentNode().getIndexOfChild(copyOfNodeToMove); 138 notifyTreeNodeInserted(getPath(destNode), index, copyOfNodeToMove); 139 } 140 public void insertInto(ProjectTreeNode destNode, ProjectTreeNode nodeToInsert) throws MactorException { 141 destNode.addChild(nodeToInsert); 142 int index = nodeToInsert.getParentNode().getIndexOfChild(nodeToInsert); 143 notifyTreeNodeInserted(getPath(destNode), index, nodeToInsert); 144 } 145 public void copyInto(ProjectTreeNode destNode, ProjectTreeNode nodeToCopy) throws MactorException { 146 nodeToCopy = nodeToCopy.copy(); 147 destNode.addChildAfter(destNode, nodeToCopy); 148 int index = nodeToCopy.getParentNode().getIndexOfChild(nodeToCopy); 149 notifyTreeNodeInserted(getPath(destNode), index, nodeToCopy); 150 } 151 List<TreeModelListener> listeners = new LinkedList<TreeModelListener>(); 152 public void addTreeModelListener(TreeModelListener listener) { 153 listeners.add(listener); 154 } 155 public void removeTreeModelListener(TreeModelListener listener) { 156 listeners.remove(listener); 157 } 158 public void valueForPathChanged(TreePath path, Object node) { 159 if (path != null) { 160 ProjectTreeNode tn = (ProjectTreeNode) path.getLastPathComponent(); 161 try { 162 if (tn.rename((String) node)) { 163 int index = tn.getParentNode().getIndexOfChild(tn); 164 notifyTreeNodesChanged(getPath(tn), index, tn); 165 } 166 } catch (MactorException me) { 167 me.printStackTrace(); 168 } 169 } 170 } 171 }