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