new version with WorkingCopy Management
[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  * Creates a new simple PACKAGE document fragment on the given range of the document.
36  *
37  * @param document - the document containing this node's original contents
38  * @param sourceRange - a two element array of integers describing the
39  *              entire inclusive source range of this node within its document.
40  *              Contents start on and include the character at the first position.
41  *              Contents end on and include the character at the last position.
42  *              An array of -1's indicates this node's contents do not exist
43  *              in the document.
44  * @param name - the identifier portion of the name of this node, or
45  *              <code>null</code> if this node does not have a name
46  */
47 DOMPackage(char[] document, int[] sourceRange, String name) {
48         super(document, sourceRange, name, new int[] {-1, -1});
49         setMask(MASK_DETAILED_SOURCE_INDEXES, false);
50 }
51 /**
52  * Creates a new detailed PACKAGE document fragment on the given range of the document.
53  *
54  * @param document - the document containing this node's original contents
55  * @param sourceRange - a two element array of integers describing the
56  *              entire inclusive source range of this node within its document.
57  *              Contents start on and include the character at the first position.
58  *              Contents end on and include the character at the last position.
59  *              An array of -1's indicates this node's contents do not exist
60  *              in the document.
61  * @param name - the identifier portion of the name of this node, or
62  *              <code>null</code> if this node does not have a name
63  * @param nameRange - a two element array of integers describing the
64  *              entire inclusive source range of this node's name within its document,
65  *              including any array qualifiers that might immediately follow the name
66  *              or -1's if this node does not have a name.
67  */
68 DOMPackage(char[] document, int[] sourceRange, String name, int[] nameRange) {
69         super(document, sourceRange, name, nameRange);
70         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
71 }
72 /**
73  * @see DOMNode#appendFragmentedContents(CharArrayBuffer)
74  */
75 protected void appendFragmentedContents(CharArrayBuffer buffer) {
76         if (fNameRange[0] < 0) {
77                 buffer
78                         .append("package ") //$NON-NLS-1$
79                         .append(fName)
80                         .append(';')
81                         .append(Util.LINE_SEPARATOR)
82                         .append(Util.LINE_SEPARATOR);
83         } else {
84                 buffer
85                         .append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0])
86                         .append(fName)
87                         .append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
88         }
89 }
90 /**
91  * @see IDOMNode#getContents()
92  */
93 public String getContents() {
94         if (fName == null) {
95                 return null;
96         } else {
97                 return super.getContents();
98         }
99 }
100 /**
101  * @see DOMNode#getDetailedNode()
102  */
103 //protected DOMNode getDetailedNode() {
104 //      return (DOMNode)getFactory().createPackage(getContents());
105 //}
106 /**
107  * @see IDOMNode#getJavaElement
108  */
109 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
110         if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
111                 return ((ICompilationUnit)parent).getPackageDeclaration(getName());
112         } else {
113                 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
114         }
115 }
116 /**
117  * @see IDOMNode#getNodeType()
118  */
119 public int getNodeType() {
120         return IDOMNode.PACKAGE;
121 }
122 /**
123  * @see DOMNode
124  */
125 protected DOMNode newDOMNode() {
126         return new DOMPackage();
127 }
128 /**
129  * @see IDOMNode#setName
130  */
131 public void setName(String name) {
132         becomeDetailed();
133         super.setName(name);
134 }
135 /**
136  * @see IDOMNode#toString()
137  */
138 public String toString() {
139         return "PACKAGE: " + getName(); //$NON-NLS-1$
140 }
141 }