1 package net.sourceforge.phpdt.internal.compiler.ast;
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 net.sourceforge.phpeclipse.PHPeclipsePlugin;
7 import org.eclipse.jface.resource.ImageDescriptor;
8 import org.eclipse.jface.text.Position;
10 import java.util.ArrayList;
11 import java.util.List;
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
20 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
22 /** The name of the class. */
24 /** The superclass. */
25 public char[] superclass;
27 public int declarationSourceStart;
28 public int declarationSourceEnd;
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();
38 private Object parent;
39 /** The outlineable children (those will be in the node array too. */
40 private ArrayList children = new ArrayList();
42 private Position position;
44 * Create a class giving starting and ending offset
45 * @param sourceStart starting offset
46 * @param sourceEnd ending offset
48 public ClassDeclaration(Object parent,
53 super(sourceStart, sourceEnd);
56 this.superclass = superclass;
57 position = new Position(sourceStart, name.length);
61 * Create a class giving starting and ending offset
62 * @param sourceStart starting offset
63 * @param sourceEnd ending offset
65 public ClassDeclaration(Object parent,
69 super(sourceStart, sourceEnd);
72 position = new Position(sourceStart, name.length);
75 public void addMethod(MethodDeclaration method) {
78 if (method.name.equals(name)) {
83 public void addVariable(FieldDeclaration var) {
84 for (int i = 0; i < var.vars.length; i++) {
85 VariableDeclaration c = var.vars[i];
92 * Tell if the class has a constructor.
95 public boolean hasConstructor() {
96 return constructor != null;
100 * Return the class as String.
101 * @param tab how many tabs before the class
102 * @return the code of this class into String
104 public String toString(int tab) {
105 return tabString(tab) + toStringHeader() + toStringBody(tab);
109 * Return the body of the class as String
110 * @param tab how many tabs before the body of the class
111 * @return the body as String
113 public String toStringBody(int tab) {
114 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
115 if (fields != null) {
116 for (int i = 0; i < fields.size(); i++) {
117 FieldDeclaration field = (FieldDeclaration) fields.get(i);
118 buff.append("\n"); //$NON-NLS-1$
119 buff.append(field.toString(tab + 1));
120 buff.append(";");//$NON-NLS-1$
123 for (int i = 0; i < methods.size(); i++) {
124 MethodDeclaration o = (MethodDeclaration) methods.get(i);
125 buff.append("\n");//$NON-NLS-1$
126 buff.append(o.toString(tab + 1));
128 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
129 return buff.toString();
133 * Return the header of the class as String.
134 * @return the header of the class
136 public String toStringHeader() {
137 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
138 if (superclass != null) {
139 buff.append(" extends "); //$NON-NLS-1$
140 buff.append(superclass);
142 return buff.toString();
146 * Get the image of a class.
147 * @return the image that represents a php class
149 public ImageDescriptor getImage() {
150 return PHPUiImages.DESC_CLASS;
153 public Object getParent() {
157 public boolean add(Outlineable o) {
158 return children.add(o);
161 public Outlineable get(int index) {
162 return (Outlineable) children.get(index);
166 PHPeclipsePlugin.log(1,"class size : "+children.size());
167 return children.size();
170 public String toString() {
171 final StringBuffer buff = new StringBuffer(new String(name));//$NON-NLS-1$
172 if (superclass != null) {
173 buff.append(":"); //$NON-NLS-1$
174 buff.append(superclass);
176 return buff.toString();
179 public Position getPosition() {
183 public List getList() {