update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / tree / java / JavaFileElement.java
blob63477dd3faef3c709e88fdc37e7af160d62ea03c
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.source.tree.java;
18 import com.intellij.lang.ASTNode;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.psi.PsiJavaFile;
21 import com.intellij.psi.impl.source.Constants;
22 import com.intellij.psi.impl.source.SourceTreeToPsiMap;
23 import com.intellij.psi.impl.source.tree.ChildRole;
24 import com.intellij.psi.impl.source.tree.ElementType;
25 import com.intellij.psi.impl.source.tree.FileElement;
26 import com.intellij.psi.impl.source.tree.TreeElement;
27 import com.intellij.psi.tree.ChildRoleBase;
28 import com.intellij.psi.tree.IElementType;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 public class JavaFileElement extends FileElement implements Constants {
33 private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.java.JavaFileElement");
35 public JavaFileElement(CharSequence text) {
36 super(JAVA_FILE, text);
39 public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
40 if (before == null && first == last && first.getElementType() == ElementType.PACKAGE_STATEMENT){ //?
41 anchor = getFirstChildNode();
42 before = Boolean.TRUE;
44 return super.addInternal(first, last, anchor, before);
47 public void deleteChildInternal(@NotNull ASTNode child){
48 if (child.getElementType() == CLASS){
49 PsiJavaFile file = (PsiJavaFile)SourceTreeToPsiMap.treeElementToPsi(this);
50 if (file.getClasses().length == 1){
51 file.delete();
52 return;
55 super.deleteChildInternal(child);
58 @Nullable
59 public ASTNode findChildByRole(int role) {
60 LOG.assertTrue(ChildRole.isUnique(role));
61 switch(role){
62 default:
63 return null;
65 case ChildRole.PACKAGE_STATEMENT:
66 return findChildByType(PACKAGE_STATEMENT);
68 case ChildRole.IMPORT_LIST:
69 return findChildByType(IMPORT_LIST);
73 public int getChildRole(ASTNode child) {
74 LOG.assertTrue(child.getTreeParent() == this);
75 IElementType i = child.getElementType();
76 if (i == PACKAGE_STATEMENT) {
77 return ChildRole.PACKAGE_STATEMENT;
79 else if (i == IMPORT_LIST) {
80 return ChildRole.IMPORT_LIST;
82 else if (i == CLASS) {
83 return ChildRole.CLASS;
85 else {
86 return ChildRoleBase.NONE;
90 public void replaceChildInternal(@NotNull ASTNode child, @NotNull TreeElement newElement) {
91 if (newElement.getElementType() == ElementType.IMPORT_LIST) {
92 LOG.assertTrue(child.getElementType() == ElementType.IMPORT_LIST);
93 if (newElement.getFirstChildNode() == null) { //empty import list
94 ASTNode next = child.getTreeNext();
95 if (next != null && next.getElementType() == WHITE_SPACE) {
96 removeChild(next);
100 super.replaceChildInternal(child, newElement);