Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ClassDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
7 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
8 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
9
10 import org.eclipse.jface.resource.ImageDescriptor;
11 import org.eclipse.jface.text.Position;
12
13
14 /**
15  * This class is my ClassDeclaration declaration for php.
16  * It is similar to org.eclipse.jdt.internal.compiler.ast.TypeDeclaration
17  * It directly extends AstNode because a class cannot appear anywhere in php
18  * @author Matthieu Casanova
19  */
20 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
21
22   /** The name of the class. */
23   public String name;
24   /** The superclass. */
25   public String superclass;
26
27   public int declarationSourceStart;
28   public int declarationSourceEnd;
29   public int bodyStart;
30   public int bodyEnd;
31   /** The methods of the class. */
32   private final ArrayList methods = new ArrayList();
33   /** The constructor of the class. */
34   public MethodDeclaration constructor;
35   /** The fields of the class. */
36   private ArrayList fields = new ArrayList();
37
38   private Object parent;
39   /** The outlineable children (those will be in the node array too. */
40   private ArrayList children = new ArrayList();
41
42   private Position position;
43
44   /**
45    * Create a class giving starting and ending offset
46    * @param sourceStart starting offset
47    * @param sourceEnd ending offset
48    */
49   public ClassDeclaration(final Object parent,
50                           final String name,
51                           final String superclass,
52                           final int sourceStart,
53                           final int sourceEnd) {
54     super(sourceStart, sourceEnd);
55     this.parent = parent;
56     this.name = name;
57     this.superclass = superclass;
58     position = new Position(sourceStart, name.length());
59   }
60
61   /**
62    * Create a class giving starting and ending offset
63    * @param sourceStart starting offset
64    * @param sourceEnd ending offset
65    */
66   public ClassDeclaration(final Object parent,
67                           final String name,
68                           final int sourceStart,
69                           final int sourceEnd) {
70     super(sourceStart, sourceEnd);
71     this.parent = parent;
72     this.name = name;
73     position = new Position(sourceStart, name.length());
74   }
75
76   /**
77    * Add a method to the class.
78    * @param method the method declaration
79    */
80   public void addMethod(final MethodDeclaration method) {
81     methods.add(method);
82     add(method);
83     if (method.name.equals(name)) {
84       constructor = method;
85     }
86   }
87
88   public void addField(final FieldDeclaration var) {
89     for (int i = 0; i < var.vars.length; i++) {
90       final VariableDeclaration c = var.vars[i];
91       children.add(c);
92     }
93     fields.add(var);
94   }
95
96   public boolean add(final Outlineable o) {
97     return children.add(o);
98   }
99
100   /**
101    * Tell if the class has a constructor.
102    * @return a boolean
103    */
104   public boolean hasConstructor() {
105     return constructor != null;
106   }
107
108   /**
109    * Return the class as String.
110    * @param tab how many tabs before the class
111    * @return the code of this class into String
112    */
113   public String toString(final int tab) {
114     return tabString(tab) + toStringHeader() + toStringBody(tab);
115   }
116
117   /**
118    * Return the body of the class as String.
119    * @param tab how many tabs before the body of the class
120    * @return the body as String
121    */
122   public String toStringBody(final int tab) {
123     final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
124     if (fields != null) {
125       for (int i = 0; i < fields.size(); i++) {
126         final FieldDeclaration field = (FieldDeclaration) fields.get(i);
127         buff.append("\n"); //$NON-NLS-1$
128         buff.append(field.toString(tab + 1));
129         buff.append(";");//$NON-NLS-1$
130       }
131     }
132     for (int i = 0; i < methods.size(); i++) {
133       final MethodDeclaration o = (MethodDeclaration) methods.get(i);
134       buff.append("\n");//$NON-NLS-1$
135       buff.append(o.toString(tab + 1));
136     }
137     buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
138     return buff.toString();
139   }
140
141   /**
142    * Return the header of the class as String.
143    * @return the header of the class
144    */
145   public String toStringHeader() {
146     final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
147     if (superclass != null) {
148       buff.append(" extends "); //$NON-NLS-1$
149       buff.append(superclass);
150     }
151     return buff.toString();
152   }
153
154   /**
155    * Get the image of a class.
156    * @return the image that represents a php class
157    */
158   public ImageDescriptor getImage() {
159     return PHPUiImages.DESC_CLASS;
160   }
161
162   public Object getParent() {
163     return parent;
164   }
165
166   public Outlineable get(final int index) {
167     return (Outlineable) children.get(index);
168   }
169
170   public int size() {
171     return children.size();
172   }
173
174   public String toString() {
175     final StringBuffer buff = new StringBuffer(name);
176     if (superclass != null) {
177       buff.append(":"); //$NON-NLS-1$
178       buff.append(superclass);
179     }
180     return buff.toString();
181   }
182
183   public Position getPosition() {
184     return position;
185   }
186
187   public List getList() {
188     return children;
189   }
190
191   /**
192    * Get the variables from outside (parameters, globals ...)
193    */
194   public void getOutsideVariable(final List list) {
195   }
196
197   /**
198    * get the modified variables.
199    */
200   public void getModifiedVariable(final List list) {
201   }
202
203   /**
204    * Get the variables used.
205    */
206   public void getUsedVariable(final List list) {
207   }
208 }