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;
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
19 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
21 /** The name of the class. */
23 /** The superclass. */
24 public char[] superclass;
26 public int declarationSourceStart;
27 public int declarationSourceEnd;
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();
37 private Object parent;
38 /** The outlineable children (those will be in the node array too. */
39 private ArrayList children = new ArrayList();
41 private Position position;
43 * Create a class giving starting and ending offset
44 * @param sourceStart starting offset
45 * @param sourceEnd ending offset
47 public ClassDeclaration(Object parent,
52 super(sourceStart, sourceEnd);
55 this.superclass = superclass;
56 position = new Position(sourceStart, name.length);
60 * Create a class giving starting and ending offset
61 * @param sourceStart starting offset
62 * @param sourceEnd ending offset
64 public ClassDeclaration(Object parent,
68 super(sourceStart, sourceEnd);
73 public void addMethod(MethodDeclaration method) {
76 if (method.name.equals(name)) {
81 public void addVariable(FieldDeclaration var) {
82 for (int i = 0; i < var.vars.length; i++) {
83 VariableDeclaration c = var.vars[i];
90 * Tell if the class has a constructor.
93 public boolean hasConstructor() {
94 return constructor != null;
98 * Return the class as String.
99 * @param tab how many tabs before the class
100 * @return the code of this class into String
102 public String toString(int tab) {
103 return tabString(tab) + toStringHeader() + toStringBody(tab);
107 * Return the body of the class as String
108 * @param tab how many tabs before the body of the class
109 * @return the body as String
111 public String toStringBody(int tab) {
112 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
113 if (fields != null) {
114 for (int i = 0; i < fields.size(); i++) {
115 FieldDeclaration field = (FieldDeclaration) fields.get(i);
116 buff.append("\n"); //$NON-NLS-1$
117 buff.append(field.toString(tab + 1));
118 buff.append(";");//$NON-NLS-1$
121 for (int i = 0; i < methods.size(); i++) {
122 MethodDeclaration o = (MethodDeclaration) methods.get(i);
123 buff.append("\n");//$NON-NLS-1$
124 buff.append(o.toString(tab + 1));
126 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
127 return buff.toString();
131 * Return the header of the class as String.
132 * @return the header of the class
134 public String toStringHeader() {
135 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
136 if (superclass != null) {
137 buff.append(" extends "); //$NON-NLS-1$
138 buff.append(superclass);
140 return buff.toString();
144 * Get the image of a class.
145 * @return the image that represents a php class
147 public ImageDescriptor getImage() {
148 return PHPUiImages.DESC_CLASS;
151 public Object getParent() {
155 public boolean add(Outlineable o) {
156 return children.add(o);
159 public Outlineable get(int index) {
160 return (Outlineable) children.get(index);
164 PHPeclipsePlugin.log(1,"class size : "+children.size());
165 return children.size();
168 public String toString() {
169 return toStringHeader();
172 public Position getPosition() {