*** 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 import java.util.List;
12
13
14 /**
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
19  */
20 public class ClassDeclaration extends Statement implements OutlineableWithChildren {
21
22   /** The name of the class. */
23   public char[] name;
24   /** The superclass. */
25   public char[] superclass;
26
27   public int declarationSourceStart;
28   public int declarationSourceEnd;
29   public int bodyStart;
30   public int bodyEnd;
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();
37
38   private Object parent;
39   /** The outlineable children (those will be in the node array too. */
40   private ArrayList children = new ArrayList();
41
42   private Position position;
43   /**
44    * Create a class giving starting and ending offset
45    * @param sourceStart starting offset
46    * @param sourceEnd ending offset
47    */
48   public ClassDeclaration(Object parent,
49                           char[] name,
50                           char[] superclass,
51                           int sourceStart,
52                           int sourceEnd) {
53     super(sourceStart, sourceEnd);
54     this.parent = parent;
55     this.name = name;
56     this.superclass = superclass;
57     position = new Position(sourceStart, name.length);
58   }
59
60   /**
61    * Create a class giving starting and ending offset
62    * @param sourceStart starting offset
63    * @param sourceEnd ending offset
64    */
65   public ClassDeclaration(Object parent,
66                           char[] name,
67                           int sourceStart,
68                           int sourceEnd) {
69     super(sourceStart, sourceEnd);
70     this.parent = parent;
71     this.name = name;
72     position = new Position(sourceStart, name.length);
73   }
74
75   public void addMethod(MethodDeclaration method) {
76     methods.add(method);
77     add(method);
78     if (method.name.equals(name)) {
79       constructor = method;
80     }
81   }
82
83   public void addVariable(FieldDeclaration var) {
84     for (int i = 0; i < var.vars.length; i++) {
85       VariableDeclaration c = var.vars[i];
86       children.add(c);
87     }
88     fields.add(var);
89   }
90
91   /**
92    * Tell if the class has a constructor.
93    * @return a boolean
94    */
95   public boolean hasConstructor() {
96     return constructor != null;
97   }
98
99   /**
100    * Return the class as String.
101    * @param tab how many tabs before the class
102    * @return the code of this class into String
103    */
104   public String toString(int tab) {
105     return tabString(tab) + toStringHeader() + toStringBody(tab);
106   }
107
108   /**
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
112    */
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$
121       }
122     }
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));
127     }
128     buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
129     return buff.toString();
130   }
131
132   /**
133    * Return the header of the class as String.
134    * @return the header of the class
135    */
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);
141     }
142     return buff.toString();
143   }
144
145   /**
146    * Get the image of a class.
147    * @return the image that represents a php class
148    */
149   public ImageDescriptor getImage() {
150     return PHPUiImages.DESC_CLASS;
151   }
152
153   public Object getParent() {
154     return parent;
155   }
156
157   public boolean add(Outlineable o) {
158     return children.add(o);
159   }
160
161   public Outlineable get(int index) {
162     return (Outlineable) children.get(index);
163   }
164
165   public int size() {
166     PHPeclipsePlugin.log(1,"class size : "+children.size());
167     return children.size();
168   }
169
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);
175     }
176     return buff.toString();
177   }
178
179   public Position getPosition() {
180     return position;
181   }
182
183   public List getList() {
184     return children;
185   }
186 }