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 add(MethodDeclaration method) {
78 if (method.name.equals(name)) {
83 public void add(FieldDeclaration var) {
84 for (int i = 0; i < var.vars.length; i++) {
85 VariableDeclaration c = var.vars[i];
91 public boolean add(Outlineable o) {
92 return children.add(o);
96 * Tell if the class has a constructor.
99 public boolean hasConstructor() {
100 return constructor != null;
104 * Return the class as String.
105 * @param tab how many tabs before the class
106 * @return the code of this class into String
108 public String toString(int tab) {
109 return tabString(tab) + toStringHeader() + toStringBody(tab);
113 * Return the body of the class as String
114 * @param tab how many tabs before the body of the class
115 * @return the body as String
117 public String toStringBody(int tab) {
118 final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
119 if (fields != null) {
120 for (int i = 0; i < fields.size(); i++) {
121 FieldDeclaration field = (FieldDeclaration) fields.get(i);
122 buff.append("\n"); //$NON-NLS-1$
123 buff.append(field.toString(tab + 1));
124 buff.append(";");//$NON-NLS-1$
127 for (int i = 0; i < methods.size(); i++) {
128 MethodDeclaration o = (MethodDeclaration) methods.get(i);
129 buff.append("\n");//$NON-NLS-1$
130 buff.append(o.toString(tab + 1));
132 buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
133 return buff.toString();
137 * Return the header of the class as String.
138 * @return the header of the class
140 public String toStringHeader() {
141 final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
142 if (superclass != null) {
143 buff.append(" extends "); //$NON-NLS-1$
144 buff.append(superclass);
146 return buff.toString();
150 * Get the image of a class.
151 * @return the image that represents a php class
153 public ImageDescriptor getImage() {
154 return PHPUiImages.DESC_CLASS;
157 public Object getParent() {
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() {