A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / jdom / DOMPackage.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
12
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.jdom.IDOMNode;
16 import net.sourceforge.phpdt.core.jdom.IDOMPackage;
17 import net.sourceforge.phpdt.internal.compiler.util.Util;
18 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
19
20 /**
21  * DOMPackage provides an implementation of IDOMPackage.
22  * 
23  * @see IDOMPackage
24  * @see DOMNode
25  */
26 class DOMPackage extends DOMNode implements IDOMPackage {
27
28         /**
29          * Creates an empty PACKAGE node.
30          */
31         DOMPackage() {
32                 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
33         }
34
35         /**
36          * Creates a new simple PACKAGE document fragment on the given range of the
37          * document.
38          * 
39          * @param document -
40          *            the document containing this node's original contents
41          * @param sourceRange -
42          *            a two element array of integers describing the entire
43          *            inclusive source range of this node within its document.
44          *            Contents start on and include the character at the first
45          *            position. Contents end on and include the character at the
46          *            last position. An array of -1's indicates this node's contents
47          *            do not exist in the document.
48          * @param name -
49          *            the identifier portion of the name of this node, or
50          *            <code>null</code> if this node does not have a name
51          */
52         DOMPackage(char[] document, int[] sourceRange, String name) {
53                 super(document, sourceRange, name, new int[] { -1, -1 });
54                 setMask(MASK_DETAILED_SOURCE_INDEXES, false);
55         }
56
57         /**
58          * Creates a new detailed PACKAGE document fragment on the given range of
59          * the document.
60          * 
61          * @param document -
62          *            the document containing this node's original contents
63          * @param sourceRange -
64          *            a two element array of integers describing the entire
65          *            inclusive source range of this node within its document.
66          *            Contents start on and include the character at the first
67          *            position. Contents end on and include the character at the
68          *            last position. An array of -1's indicates this node's contents
69          *            do not exist in the document.
70          * @param name -
71          *            the identifier portion of the name of this node, or
72          *            <code>null</code> if this node does not have a name
73          * @param nameRange -
74          *            a two element array of integers describing the entire
75          *            inclusive source range of this node's name within its
76          *            document, including any array qualifiers that might
77          *            immediately follow the name or -1's if this node does not have
78          *            a name.
79          */
80         DOMPackage(char[] document, int[] sourceRange, String name, int[] nameRange) {
81                 super(document, sourceRange, name, nameRange);
82                 setMask(MASK_DETAILED_SOURCE_INDEXES, true);
83         }
84
85         /**
86          * @see DOMNode#appendFragmentedContents(CharArrayBuffer)
87          */
88         protected void appendFragmentedContents(CharArrayBuffer buffer) {
89                 if (fNameRange[0] < 0) {
90                         buffer.append("package ") //$NON-NLS-1$
91                                         .append(fName).append(';').append(Util.LINE_SEPARATOR)
92                                         .append(Util.LINE_SEPARATOR);
93                 } else {
94                         buffer.append(fDocument, fSourceRange[0],
95                                         fNameRange[0] - fSourceRange[0]).append(fName).append(
96                                         fDocument, fNameRange[1] + 1,
97                                         fSourceRange[1] - fNameRange[1]);
98                 }
99         }
100
101         /**
102          * @see IDOMNode#getContents()
103          */
104         public String getContents() {
105                 if (fName == null) {
106                         return null;
107                 } else {
108                         return super.getContents();
109                 }
110         }
111
112         /**
113          * @see DOMNode#getDetailedNode()
114          */
115         // protected DOMNode getDetailedNode() {
116         // return (DOMNode)getFactory().createPackage(getContents());
117         // }
118         /**
119          * @see IDOMNode#getJavaElement
120          */
121         public IJavaElement getJavaElement(IJavaElement parent)
122                         throws IllegalArgumentException {
123                 if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
124                         return ((ICompilationUnit) parent).getPackageDeclaration(getName());
125                 } else {
126                         throw new IllegalArgumentException(Util
127                                         .bind("element.illegalParent")); //$NON-NLS-1$
128                 }
129         }
130
131         /**
132          * @see IDOMNode#getNodeType()
133          */
134         public int getNodeType() {
135                 return IDOMNode.PACKAGE;
136         }
137
138         /**
139          * @see DOMNode
140          */
141         protected DOMNode newDOMNode() {
142                 return new DOMPackage();
143         }
144
145         /**
146          * @see IDOMNode#setName
147          */
148         public void setName(String name) {
149                 becomeDetailed();
150                 super.setName(name);
151         }
152
153         /**
154          * @see IDOMNode#toString()
155          */
156         public String toString() {
157                 return "PACKAGE: " + getName(); //$NON-NLS-1$
158         }
159 }