KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > java > JavaImport


1 /*
2  * JavaImport.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.PrintWriter JavaDoc;
37
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeStyle;
40
41 /**
42  * A class generating a Java import declaration.
43  *
44  * @author Per Cederberg, <per at percederberg dot net>
45  * @version 1.0
46  */

47 public class JavaImport extends CodeElement {
48
49     /**
50      * The imported package name.
51      */

52     private String JavaDoc packageName;
53
54     /**
55      * The imported class name.
56      */

57     private String JavaDoc className;
58
59     /**
60      * Creates a new import declaration, importing all classes of a
61      * specified package.
62      *
63      * @param pkg the package to import
64      */

65     public JavaImport(JavaPackage pkg) {
66         this(pkg.toString());
67     }
68
69     /**
70      * Creates a new import declaration, importing all classes of a
71      * specified package.
72      *
73      * @param packageName the fully qualified package name
74      */

75     public JavaImport(String JavaDoc packageName) {
76         this(packageName, "");
77     }
78
79     /**
80      * Creates a new import declaration, importing the selected class
81      * from the specified package.
82      *
83      * @param pkg the package containing the specified class
84      * @param cls the class to import
85      */

86     public JavaImport(JavaPackage pkg, JavaClass cls) {
87         this(pkg.toString(), cls.toString());
88     }
89
90     /**
91      * Creates a new import declaration, importing the selected class
92      * from the specified package.
93      *
94      * @param packageName the fully qualified package name
95      * @param className the class name
96      */

97     public JavaImport(String JavaDoc packageName, String JavaDoc className) {
98         this.packageName = packageName;
99         this.className = className;
100     }
101
102     /**
103      * Compares this object to another one. The comparison is based
104      * primarily on the code element category, and secondarily on the
105      * package name.
106      *
107      * @param obj the object to compare to
108      *
109      * @return negative if this object preceeds the other one,
110      * zero (0) if the objects are equal, or
111      * positive if this object succeeds the other one
112      */

113     public int compareTo(Object JavaDoc obj) {
114         int value = super.compareTo(obj);
115
116         if (value == 0) {
117             return toString().compareTo(obj.toString());
118         } else {
119             return value;
120         }
121     }
122
123     /**
124      * Returns true if this object is equal to another import.
125      *
126      * @param obj the object to compare to
127      *
128      * @return true if the objects are equal, or
129      * false otherwise
130      */

131     public boolean equals(Object JavaDoc obj) {
132         return compareTo(obj) == 0;
133     }
134
135     /**
136      * Returns a string description of the imported packages.
137      *
138      * @return the string representation of this import
139      */

140     public String JavaDoc toString() {
141         if (className.equals("")) {
142             return packageName + ".*";
143         } else {
144             return packageName + "." + className;
145         }
146     }
147
148     /**
149      * Returns a numeric category number for the code element. A lower
150      * category number implies that the code element should be placed
151      * before code elements with a higher category number within a
152      * declaration.
153      *
154      * @return the category number
155      */

156     public int category() {
157         return packageName.startsWith("java") ? 2 : 3;
158     }
159
160     /**
161      * Prints the code element to the specified output stream.
162      *
163      * @param out the output stream
164      * @param style the code style to use
165      * @param indent the indentation level
166      */

167     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
168         out.println("import " + toString() + ";");
169     }
170 }
171
Popular Tags