*** empty log message ***
[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 net.sourceforge.phpeclipse.PHPeclipsePlugin;
7 import org.eclipse.jface.resource.ImageDescriptor;
8 import org.eclipse.jface.text.Position;
9
10 import java.util.ArrayList;
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   }
72
73   public void addMethod(MethodDeclaration method) {
74     methods.add(method);
75     add(method);
76     if (method.name.equals(name)) {
77       constructor = method;
78     }
79   }
80
81   public void addVariable(FieldDeclaration var) {
82     for (int i = 0; i < var.vars.length; i++) {
83       VariableDeclaration c = var.vars[i];
84       children.add(c);
85     }
86     fields.add(var);
87   }
88
89   /**
90    * Tell if the class has a constructor.
91    * @return a boolean
92    */
93   public boolean hasConstructor() {
94     return constructor != null;
95   }
96
97   /**
98    * Return the class as String.
99    * @param tab how many tabs before the class
100    * @return the code of this class into String
101    */
102   public String toString(int tab) {
103     return tabString(tab) + toStringHeader() + toStringBody(tab);
104   }
105
106   /**
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
110    */
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$
119       }
120     }
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));
125     }
126     buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
127     return buff.toString();
128   }
129
130   /**
131    * Return the header of the class as String.
132    * @return the header of the class
133    */
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);
139     }
140     return buff.toString();
141   }
142
143   /**
144    * Get the image of a class.
145    * @return the image that represents a php class
146    */
147   public ImageDescriptor getImage() {
148     return PHPUiImages.DESC_CLASS;
149   }
150
151   public Object getParent() {
152     return parent;
153   }
154
155   public boolean add(Outlineable o) {
156     return children.add(o);
157   }
158
159   public Outlineable get(int index) {
160     return (Outlineable) children.get(index);
161   }
162
163   public int size() {
164     PHPeclipsePlugin.log(1,"class size : "+children.size());
165     return children.size();
166   }
167
168   public String toString() {
169     return toStringHeader();
170   }
171
172   public Position getPosition() {
173     return position;
174   }
175 }