KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SortJavaElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.core;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Hashtable JavaDoc;
15
16 import org.eclipse.jdt.core.JavaCore;
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.internal.core.SortElementBuilder.SortElement;
19
20 /**
21  *
22  * @since 2.1
23  */

24 public abstract class SortJavaElement implements Comparable JavaDoc {
25
26     public static final int COMPILATION_UNIT = 1;
27     public static final int TYPE = 2;
28     public static final int CLASS = 4;
29     public static final int INTERFACE = 8;
30     public static final int FIELD = 16;
31     public static final int INITIALIZER = 32;
32     public static final int METHOD = 64;
33     public static final int CONSTRUCTOR = 128;
34     public static final int MULTIPLE_FIELD = 256;
35     
36     SortElementBuilder builder;
37
38     protected static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
39
public static final String JavaDoc CORRESPONDING_ELEMENT = "corresponding_element"; //$NON-NLS-1$
40

41     Hashtable JavaDoc options;
42     
43     protected int id;
44     protected int sourceStart;
45     protected int newSourceStart;
46     protected int modifiers;
47     protected String JavaDoc superclass;
48     protected String JavaDoc[] superInterfaces;
49     
50     protected String JavaDoc[] parametersNames;
51     protected String JavaDoc[] parametersTypes;
52     protected String JavaDoc[] thrownExceptions;
53     protected String JavaDoc returnType;
54     protected String JavaDoc name;
55     protected String JavaDoc type;
56     protected int fieldCounter;
57     protected SortElementBuilder.SortFieldDeclaration[] innerFields;
58     protected ASTNode[] astNodes;
59     
60     protected int sourceEnd;
61     protected int nameSourceStart;
62     protected SortElement[] children;
63     protected int children_count;
64     protected SortElement firstChildBeforeSorting;
65     protected SortElement lastChildBeforeSorting;
66     protected int declarationStart;
67     protected int declarationSourceEnd;
68     
69     SortJavaElement(SortElementBuilder builder) {
70         this.builder = builder;
71         this.options = JavaCore.getOptions();
72     }
73     /**
74      * @see java.lang.Comparable#compareTo(java.lang.Object)
75      */

76     public int compareTo(Object JavaDoc o) {
77         return this.builder.comparator.compare(this, o);
78     }
79     
80     protected void addChild(SortElement sortElement) {
81         if (this.children_count == 0) {
82             this.children = new SortElement[3];
83         } else if (this.children_count == this.children.length) {
84             System.arraycopy(this.children, 0, this.children = new SortElement[this.children_count * 2], 0, this.children_count);
85         }
86         this.children[this.children_count++] = sortElement;
87     }
88
89     protected void closeCollections() {
90         int length = this.children_count;
91         if (length != 0 && length != this.children.length) {
92             System.arraycopy(this.children, 0, this.children= new SortElement[length], 0, length);
93         }
94     }
95
96     abstract void display(StringBuffer JavaDoc buffer, int tab);
97         
98     protected void generateSource(StringBuffer JavaDoc buffer) {
99         this.newSourceStart = buffer.length();
100     }
101
102     abstract void mapPositions();
103     
104     public String JavaDoc toString(int tab) {
105         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
106         display(buffer, tab);
107         if (this.children != null) {
108             buffer
109                 .append(tab(tab))
110                 .append("CHILDREN ------------------------------" + LINE_SEPARATOR); //$NON-NLS-1$
111
for (int i = 0; i < this.children_count; i++) {
112                 buffer.append(this.children[i].toString(tab + 1));
113                 buffer.append(LINE_SEPARATOR);
114             }
115         }
116         return buffer.toString();
117     }
118
119     protected char[] tab(int tab) {
120         char[] tabs = new char[tab];
121         Arrays.fill(tabs, '\t');
122         return tabs;
123     }
124
125     public String JavaDoc toString() {
126         return toString(0);
127     }
128
129     protected void sort() {
130         if (this.children != null) {
131             this.firstChildBeforeSorting = children[0];
132             this.lastChildBeforeSorting = children[this.children_count - 1];
133             switch(this.id) {
134                 case CLASS | TYPE :
135                 case INTERFACE | TYPE :
136                 case COMPILATION_UNIT :
137                     this.astNodes = convertChildren();
138                     Arrays.sort(astNodes, this.builder.comparator);
139             }
140             for (int i = 0, max = this.children_count; i < max; i++) {
141                 children[i].sort();
142             }
143         }
144     }
145     
146     private ASTNode[] convertChildren() {
147         ASTNode[] convertedNodes = new ASTNode[this.children_count];
148         for (int i = 0, max = this.children_count; i < max; i++) {
149             SortElementBuilder.SortElement currentElement = this.children[i];
150             ASTNode newNode = currentElement.convert();
151             newNode.setProperty(CORRESPONDING_ELEMENT, currentElement);
152             convertedNodes[i] = newNode;
153         }
154         return convertedNodes;
155     }
156 }
157
Popular Tags