new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / jdom / DOMImport.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.IJavaElement;
14 import net.sourceforge.phpdt.core.jdom.IDOMImport;
15 import net.sourceforge.phpdt.core.jdom.IDOMNode;
16 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
17 import net.sourceforge.phpdt.internal.compiler.util.Util;
18 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
19
20 /**
21  * DOMImport provides an implementation of IDOMImport.
22  *
23  * @see IDOMImport
24  * @see DOMNode
25  */
26 class DOMImport extends DOMNode implements IDOMImport {
27         /**
28          * Indicates if this import is an on demand type import
29          */
30         protected boolean fOnDemand;
31 /**
32  * Creates a new empty IMPORT node.
33  */
34 DOMImport() {
35         fName = "java.lang.*"; //$NON-NLS-1$
36         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
37 }
38 /**
39  * Creates a new detailed IMPORT document fragment on the given range of the document.
40  *
41  * @param document - the document containing this node's original contents
42  * @param sourceRange - a two element array of integers describing the
43  *              entire inclusive source range of this node within its document.
44  *              Contents start on and include the character at the first position.
45  *              Contents end on and include the character at the last position.
46  *              An array of -1's indicates this node's contents do not exist
47  *              in the document.
48  * @param name - the identifier portion of the name of this node, or
49  *              <code>null</code> if this node does not have a name
50  * @param nameRange - a two element array of integers describing the
51  *              entire inclusive source range of this node's name within its document,
52  *              including any array qualifiers that might immediately follow the name
53  *              or -1's if this node does not have a name.
54  * @param onDemand - indicates if this import is an on demand style import
55  */
56 DOMImport(char[] document, int[] sourceRange, String name, int[] nameRange, boolean onDemand) {
57         super(document, sourceRange, name, nameRange);
58         fOnDemand = onDemand;
59         setMask(MASK_DETAILED_SOURCE_INDEXES, true);
60 }
61 /**
62  * Creates a new simple IMPORT document fragment on the given range of the document.
63  *
64  * @param document - the document containing this node's original contents
65  * @param sourceRange - a two element array of integers describing the
66  *              entire inclusive source range of this node within its document.
67  *              Contents start on and include the character at the first position.
68  *              Contents end on and include the character at the last position.
69  *              An array of -1's indicates this node's contents do not exist
70  *              in the document.
71  * @param name - the identifier portion of the name of this node, or
72  *              <code>null</code> if this node does not have a name
73  * @param onDemand - indicates if this import is an on demand style import
74  */
75 DOMImport(char[] document, int[] sourceRange, String name, boolean onDemand) {
76         this(document, sourceRange, name, new int[] {-1, -1}, onDemand);
77         fOnDemand = onDemand;
78         setMask(MASK_DETAILED_SOURCE_INDEXES, false);
79 }
80 /**
81  * @see DOMNode#appendFragmentedContents(CharArrayBuffer)
82  */
83 protected void appendFragmentedContents(CharArrayBuffer buffer) {
84         if (fNameRange[0] < 0) {
85                 buffer
86                         .append("import ") //$NON-NLS-1$
87                         .append(fName)
88                         .append(';')
89                         .append(Util.LINE_SEPARATOR);
90         } else {
91                 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]);
92                 //buffer.append(fDocument, fNameRange[0], fNameRange[1] - fNameRange[0] + 1);
93                 buffer.append(fName);
94                 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]);
95         }
96 }
97 /** 
98  * @see IDOMNode#getContents()
99  */
100 public String getContents() {
101         if (fName == null) {
102                 return null;
103         } else {
104                 return super.getContents();
105         }
106 }
107 /**
108  * @see DOMNode#getDetailedNode()
109  */
110 //protected DOMNode getDetailedNode() {
111 //      return (DOMNode)getFactory().createImport(getContents());
112 //}
113 /**
114  * @see IDOMNode#getJavaElement
115  */
116 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
117 //      if (parent.getElementType() == IJavaElement.COMPILATION_UNIT) {
118 //              return ((ICompilationUnit)parent).getImport(getName());
119 //      } else {
120                 throw new IllegalArgumentException(Util.bind("element.illegalParent")); //$NON-NLS-1$
121 //      }
122 }
123 /**
124  * @see IDOMNode#getNodeType()
125  */
126 public int getNodeType() {
127         return IDOMNode.IMPORT;
128 }
129 /**
130  * @see IDOMImport#isOnDemand()
131  */
132 public boolean isOnDemand() {
133         return fOnDemand;       
134 }
135 /**
136  * @see DOMNode
137  */
138 protected DOMNode newDOMNode() {
139         return new DOMImport();
140 }
141 /**
142  * @see IDOMNode#setName(char[])
143  */
144 public void setName(String name) {
145         if (name == null) {
146                 throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
147         }
148         becomeDetailed();
149         super.setName(name);
150         fOnDemand = name.endsWith(".*"); //$NON-NLS-1$
151 }
152 /**
153  * @see IDOMNode#toString()
154  */
155 public String toString() {
156         return "IMPORT: " + getName(); //$NON-NLS-1$
157 }
158 }