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