KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > userperspective > ResourceFolderNode


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.userperspective;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JTree JavaDoc;
37 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
38 import javax.swing.tree.DefaultTreeModel JavaDoc;
39 import javax.swing.tree.TreeNode JavaDoc;
40 import javax.swing.tree.TreePath JavaDoc;
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 JavaDoc
47 {
48   private static final long serialVersionUID = 1L;
49   protected JTree JavaDoc _tree = null;
50   protected DefaultTreeModel JavaDoc _treeModel = null;
51   protected String JavaDoc _name = null;
52
53   ArrayList JavaDoc _resources = new ArrayList JavaDoc();
54
55   ImageIcon JavaDoc _icon;
56
57   public ResourceFolderNode()
58   {
59   }
60
61   public ResourceFolderNode(JTree JavaDoc theTree, DefaultTreeModel JavaDoc theModel, String JavaDoc 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 JavaDoc 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 JavaDoc getPathString()
82   {
83     if (getParent() instanceof ResourceFolderNode)
84     {
85       ResourceFolderNode p = (ResourceFolderNode) getParent();
86
87       if (p != null && p.getParent() != null)
88       {
89         String JavaDoc 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 JavaDoc mdl)
99   {
100     _treeModel = mdl;
101   }
102
103   public void setTree(JTree JavaDoc tree)
104   {
105     _tree = tree;
106   }
107
108   public JTree JavaDoc 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 JavaDoc getModel()
120   {
121     return _treeModel;
122   }
123
124   public ImageIcon JavaDoc getImageIcon()
125   {
126     return _icon;
127   }
128
129   public void removeFromTree()
130   {
131     getModel().removeNodeFromParent(this);
132   }
133
134   public String JavaDoc 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 JavaDoc parentPath[] = getPath();
146       TreeNode JavaDoc childPath[] = new TreeNode JavaDoc[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 JavaDoc(childPath));
153         break;
154       }
155     }
156   }
157
158   public ResourceFolderNode getChild(String JavaDoc 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 JavaDoc getName()
169   {
170     return _name;
171   }
172
173   public void setName(String JavaDoc 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 JavaDoc(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