intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.core / src / net / sourceforge / phpeclipse / js / core / model / JSElement.java
1 /*
2  * $RCSfile: JSElement.java,v $
3  *
4  * Copyright 2002
5  * CH-1700 Fribourg, Switzerland
6  * All rights reserved.
7  *
8  *========================================================================
9  * Modifications history
10  *========================================================================
11  * $Log: not supported by cvs2svn $
12  * Revision 1.2  2004/02/27 18:28:10  cell
13  * Make model elements platform objects so they are automatically adapted
14  *
15  * Revision 1.1  2004/02/26 02:25:42  agfitzp
16  * renamed packages to match xml & css
17  *
18  * Revision 1.1  2004/02/05 03:10:08  agfitzp
19  * Initial Submission
20  *
21  * Revision 1.1.2.1  2003/12/12 21:37:24  agfitzp
22  * Experimental work for Classes view
23  *
24  * Revision 1.2  2003/05/30 20:53:08  agfitzp
25  * 0.0.2 : Outlining is now done as the user types. Some other bug fixes.
26  *
27  * Revision 1.1  2003/05/28 15:17:12  agfitzp
28  * net.sourceforge.phpeclipse.js.core 0.0.1 code base
29  *
30  *========================================================================
31 */
32
33 package net.sourceforge.phpeclipse.js.core.model;
34
35 import java.util.List;
36 import java.util.LinkedList;
37
38 import org.eclipse.core.resources.IFile;
39 import org.eclipse.core.runtime.PlatformObject;
40
41 /**
42  * DOCUMENT ME!
43  * 
44  * @author Addi 
45  */
46 abstract public class JSElement extends PlatformObject
47         implements JSElementCategories
48 {
49         protected IFile file;
50         protected String name;
51         protected int offset;
52         protected int numberOfLines;
53         protected int length;
54
55         protected JSElement parent;
56         protected List children;
57
58         /**
59          * Creates a new JSElement and stores parent element and location in the text.
60          * 
61          * @param aName text corresponding to the func
62          * @param offset  the offset into the text
63          * @param length  the length of the element
64          */
65         public JSElement(IFile aFile, String aName, int offset, int length)
66         {
67                 this.file = aFile;
68                 this.name = aName;
69                 this.offset = offset;
70                 this.length = length;
71                 this.children = new LinkedList();
72         }
73
74         /**
75          * Method declared on IWorkbenchAdapter
76          * @param o
77          * 
78          * @return
79          */
80         public String getLabel(Object o)
81         {
82                 return name;
83         }
84
85         /**
86          * Returns the number of characters in this section.
87          * @return
88          */
89         public int getLength()
90         {
91                 return length;
92         }
93
94         /**
95          * Returns the number of lines in the element.
96          * 
97          * @return the number of lines in the element
98          */
99         public int getNumberOfLines()
100         {
101                 return numberOfLines;
102         }
103
104         /**
105          * Returns the offset of this section in the file.
106          * @return
107          */
108         public int getStart()
109         {
110                 return offset;
111         }
112
113         /**
114          * Sets the number of lines in the element
115          * 
116          * @param newNumberOfLines  the number of lines in the element
117          */
118         public void setNumberOfLines(int newNumberOfLines)
119         {
120                 numberOfLines = newNumberOfLines;
121         }
122
123         /**
124          * @see java.lang.Object#toString()
125          */
126         public String toString()
127         {
128                 return getLabel(this);
129         }
130
131         /**
132          * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(Object)
133          */
134         public Object[] getChildren(Object o)
135         {
136                 Object[] result = new Object[children.size()];
137                 return children.toArray(result);
138         }
139
140         /**
141          * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(Object)
142          */
143         public Object getParent(Object o)
144         {
145                 return null;
146         }
147         
148         /**
149          * 
150          * @return A category enumeration for sub-types.
151          */
152         abstract public int category();
153
154         /**
155          * @return
156          */
157         public String getName()
158         {
159                 return name;
160         }
161
162         /**
163          * @return
164          */
165         public int getOffset()
166         {
167                 return offset;
168         }
169
170         /**
171          * @return
172          */
173         public JSElement getParent()
174         {
175                 return parent;
176         }
177
178         /**
179          * @param element
180          */
181         protected void setParent(JSElement element)
182         {
183                 parent = element;
184         }
185
186         /**
187          * @param anElement
188          * @return
189          */
190         public boolean sharesParentWith(JSElement anElement)
191         {
192                 if(parent == null) {
193                         return anElement.getParent() == null;
194                 }
195                 
196                 return parent.equals(anElement.getParent());
197         }
198
199         /**
200          * @param anElement
201          * @return
202          */
203         public boolean equals(JSElement anElement)
204         {
205                 return sharesParentWith(anElement) && name.equals(anElement.getName());
206         }
207
208         /**
209          * @return Returns the file.
210          */
211         public IFile getFile() {
212                 return file;
213         }
214
215         /**
216          * @param file The file to set.
217          */
218         protected void setFile(IFile file) {
219                 this.file = file;
220         }
221
222 }