1 30 package com.genimen.djeneric.tools.modeler.userperspective; 31 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 35 import javax.swing.ImageIcon ; 36 import javax.swing.JTree ; 37 import javax.swing.tree.DefaultMutableTreeNode ; 38 import javax.swing.tree.DefaultTreeModel ; 39 import javax.swing.tree.TreeNode ; 40 import javax.swing.tree.TreePath ; 41 42 import com.genimen.djeneric.structure.ResourceDefinition; 43 import com.genimen.djeneric.structure.ResourceDefinitionComparator; 44 import com.genimen.djeneric.tools.modeler.ModelEditor; 45 46 public class ResourceFolderNode extends DefaultMutableTreeNode 47 { 48 private static final long serialVersionUID = 1L; 49 protected JTree _tree = null; 50 protected DefaultTreeModel _treeModel = null; 51 protected String _name = null; 52 53 ArrayList _resources = new ArrayList (); 54 55 ImageIcon _icon; 56 57 public ResourceFolderNode() 58 { 59 } 60 61 public ResourceFolderNode(JTree theTree, DefaultTreeModel theModel, String name) 62 { 63 _tree = theTree; 64 _treeModel = theModel; 65 _icon = ModelEditor.getImageIcon("folder.gif"); 66 _name = name; 67 } 68 69 public void init(ResourceDefinition[] allResources) 70 { 71 String abspath = getPathString(); 72 for (int i = 0; i < allResources.length; i++) 73 { 74 if (allResources[i].getAbsolutePath().equals(abspath)) 75 { 76 _resources.add(allResources[i]); 77 } 78 } 79 } 80 81 public String getPathString() 82 { 83 if (getParent() instanceof ResourceFolderNode) 84 { 85 ResourceFolderNode p = (ResourceFolderNode) getParent(); 86 87 if (p != null && p.getParent() != null) 88 { 89 String parentPath = p.getPathString(); 90 if (!parentPath.endsWith("/")) parentPath += "/"; 91 return parentPath + toString(); 92 } 93 } 94 95 return toString(); 96 } 97 98 public void setModel(DefaultTreeModel mdl) 99 { 100 _treeModel = mdl; 101 } 102 103 public void setTree(JTree tree) 104 { 105 _tree = tree; 106 } 107 108 public JTree getTree() 109 { 110 if (_tree != null) return _tree; 111 if (getParent() instanceof ResourceFolderNode) 112 { 113 ResourceFolderNode parent = (ResourceFolderNode) getParent(); 114 if (parent != null) return parent.getTree(); 115 } 116 return null; 117 } 118 119 public DefaultTreeModel getModel() 120 { 121 return _treeModel; 122 } 123 124 public ImageIcon getImageIcon() 125 { 126 return _icon; 127 } 128 129 public void removeFromTree() 130 { 131 getModel().removeNodeFromParent(this); 132 } 133 134 public String toString() 135 { 136 if (_name != null) return _name; 137 return ""; 138 } 139 140 public void focusChild(ResourceFolderNode focusThis) 141 { 142 for (int i = 0; i < getChildCount(); i++) 143 { 144 ResourceFolderNode node = (ResourceFolderNode) getChildAt(i); 145 TreeNode parentPath[] = getPath(); 146 TreeNode childPath[] = new TreeNode [parentPath.length + 1]; 147 System.arraycopy(parentPath, 0, childPath, 0, parentPath.length); 148 149 if (node.equals(focusThis)) 150 { 151 childPath[parentPath.length] = node; 152 getTree().setSelectionPath(new TreePath (childPath)); 153 break; 154 } 155 } 156 } 157 158 public ResourceFolderNode getChild(String name) 159 { 160 for (int i = 0; i < getChildCount(); i++) 161 { 162 ResourceFolderNode node = (ResourceFolderNode) getChildAt(i); 163 if (node.getName().equals(name)) return node; 164 } 165 return null; 166 } 167 168 public String getName() 169 { 170 return _name; 171 } 172 173 public void setName(String name) 174 { 175 _name = name; 176 } 177 178 public ResourceDefinition addOrReplaceResource(ResourceDefinition def) 179 { 180 ResourceDefinition result = def; 181 boolean replaced = false; 182 for (int i = 0; i < _resources.size(); i++) 183 { 184 ResourceDefinition existingResource = (ResourceDefinition) _resources.get(i); 185 if (existingResource.getName().equals(def.getName())) 186 { 187 existingResource.copyFrom(def); 188 def.markDeleted(true); 189 replaced = true; 190 result = existingResource; 191 break; 192 } 193 } 194 if (!replaced) _resources.add(def); 195 return result; 196 } 197 198 public void removeResource(ResourceDefinition def) 199 { 200 _resources.remove(def); 201 } 202 203 public ResourceDefinition[] getResources() 204 { 205 return (ResourceDefinition[]) _resources.toArray(new ResourceDefinition[0]); 206 } 207 208 public void expandAll() 209 { 210 _tree.expandPath(new TreePath (this.getPath())); 211 int cc = getChildCount(); 212 for (int i = 0; i < cc; i++) 213 { 214 ResourceFolderNode child = (ResourceFolderNode) getChildAt(i); 215 child.expandAll(); 216 } 217 } 218 219 public void sortResources() 220 { 221 Collections.sort(_resources, new ResourceDefinitionComparator()); 222 int cc = getChildCount(); 223 for (int i = 0; i < cc; i++) 224 { 225 ResourceFolderNode child = (ResourceFolderNode) getChildAt(i); 226 child.sortResources(); 227 } 228 } 229 230 } | Popular Tags |