*** 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
9 import java.util.ArrayList;
10 import java.util.Enumeration;
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   /**
42    * Create a class giving starting and ending offset
43    * @param sourceStart starting offset
44    * @param sourceEnd ending offset
45    */
46   public ClassDeclaration(Object parent,
47                           char[] name,
48                           char[] superclass,
49                           int sourceStart,
50                           int sourceEnd) {
51     super(sourceStart, sourceEnd);
52     this.parent = parent;
53     this.name = name;
54     this.superclass = superclass;
55   }
56
57   /**
58    * Create a class giving starting and ending offset
59    * @param sourceStart starting offset
60    * @param sourceEnd ending offset
61    */
62   public ClassDeclaration(Object parent,
63                           char[] name,
64                           int sourceStart,
65                           int sourceEnd) {
66     super(sourceStart, sourceEnd);
67     this.parent = parent;
68     this.name = name;
69   }
70
71   public void addMethod(MethodDeclaration method) {
72     methods.add(method);
73     children.add(method);
74     if (method.name.equals(name)) {
75       constructor = method;
76     }
77   }
78
79   public void addVariable(FieldDeclaration var) {
80     for (int i = 0; i < var.vars.length; i++) {
81       VariableDeclaration c = var.vars[i];
82       children.add(c);
83     }
84     fields.add(var);
85   }
86
87   /**
88    * Tell if the class has a constructor.
89    * @return a boolean
90    */
91   public boolean hasConstructor() {
92     return constructor != null;
93   }
94
95   /**
96    * Return the class as String.
97    * @param tab how many tabs before the class
98    * @return the code of this class into String
99    */
100   public String toString(int tab) {
101     return tabString(tab) + toStringHeader() + toStringBody(tab);
102   }
103
104   /**
105    * Return the body of the class as String
106    * @param tab how many tabs before the body of the class
107    * @return the body as String
108    */
109   public String toStringBody(int tab) {
110     final StringBuffer buff = new StringBuffer(" {");//$NON-NLS-1$
111     if (fields != null) {
112       for (int i = 0; i < fields.size(); i++) {
113         FieldDeclaration field = (FieldDeclaration) fields.get(i);
114         buff.append("\n"); //$NON-NLS-1$
115         buff.append(field.toString(tab + 1));
116         buff.append(";");//$NON-NLS-1$
117       }
118     }
119     for (int i = 0; i < methods.size(); i++) {
120       MethodDeclaration o = (MethodDeclaration) methods.get(i);
121       buff.append("\n");//$NON-NLS-1$
122       buff.append(o.toString(tab + 1));
123     }
124     buff.append("\n").append(tabString(tab)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
125     return buff.toString();
126   }
127
128   /**
129    * Return the header of the class as String.
130    * @return the header of the class
131    */
132   public String toStringHeader() {
133     final StringBuffer buff = new StringBuffer("class ").append(name);//$NON-NLS-1$
134     if (superclass != null) {
135       buff.append(" extends "); //$NON-NLS-1$
136       buff.append(superclass);
137     }
138     return buff.toString();
139   }
140
141   /**
142    * Get the image of a class.
143    * @return the image that represents a php class
144    */
145   public ImageDescriptor getImage() {
146     return PHPUiImages.DESC_CLASS;
147   }
148
149   public Object getParent() {
150     return parent;
151   }
152
153   public boolean add(Outlineable o) {
154     return children.add(o);
155   }
156
157   public Outlineable get(int index) {
158     return (Outlineable) children.get(index);
159   }
160
161   public int size() {
162     return children.size();
163   }
164 }