25ae67768090f328cc11bd2f85e663a56cbfe221
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / VariableDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import net.sourceforge.phpdt.internal.compiler.ast.Expression;
4 import net.sourceforge.phpdt.internal.compiler.ast.AbstractVariableDeclaration;
5 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7 import org.eclipse.jface.resource.ImageDescriptor;
8
9 /**
10  * A variable declaration.
11  * @author Matthieu Casanova
12  */
13 public class VariableDeclaration extends AbstractVariableDeclaration implements Outlineable {
14
15   /** The value for variable initialization. */
16   public Expression initialization;
17
18   private Object parent;
19   private boolean reference;
20
21   /**
22    * Create a variable.
23    * @param initialization the initialization
24    * @param name the name of the variable
25    * @param sourceStart the start point
26    */
27   public VariableDeclaration(Object parent,
28                              char[] name,
29                              Expression initialization,
30                              int sourceStart) {
31     super(name, sourceStart, initialization.sourceEnd);
32     this.initialization = initialization;
33     this.parent = parent;
34   }
35
36   /**
37    * Create a variable.
38    * @param initialization the initialization
39    * @param name the name of the variable
40    * @param sourceStart the start point
41    */
42   public VariableDeclaration(Object parent,
43                              char[] name,
44                              int sourceStart,
45                              int sourceEnd) {
46     super(name, sourceStart, sourceEnd);
47     this.initialization = initialization;
48     this.parent = parent;
49   }
50
51   public void setReference(boolean reference) {
52     this.reference = reference;
53   }
54
55   /**
56    * Create a variable.
57    * @param initialization the initialization
58    * @param name the name of the variable
59    * @param sourceStart the start point
60    */
61   public VariableDeclaration(char[] name,
62                              Expression initialization,
63                              int sourceStart) {
64     super(name, sourceStart, initialization.sourceEnd);
65     this.initialization = initialization;
66   }
67   /**
68    * Return the variable into String.
69    * @return a String
70    */
71   public String toStringExpression() {
72     final StringBuffer buff;
73     if (reference) {
74       buff = new StringBuffer('&');
75     } else {
76       buff = new StringBuffer();
77     }
78     buff.append(name);
79     if (initialization != null) {
80       buff.append(" = "); //$NON-NLS-1$
81       buff.append(initialization.toStringExpression());
82     }
83     return buff.toString();
84   }
85
86   public Object getParent() {
87     return parent;
88   }
89
90     /**
91      * Get the image of a variable.
92      * @return the image that represents a php variable
93      */
94     public ImageDescriptor getImage() {
95         return PHPUiImages.DESC_VAR;
96     }
97 }