KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > rebind > ClassSourceFileComposerFactory


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.rebind;
17
18 import com.google.gwt.core.ext.GeneratorContext;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Factory clas to create <code>ClassSourceFileComposer</code> instances.
28  *
29  */

30 public class ClassSourceFileComposerFactory {
31   /**
32    * Represents a java source file category. Right now support interface and
33    * class, later should support abstract class, static class, etc.
34    */

35   public static class JavaSourceCategory extends Enum JavaDoc {
36     /**
37      * This type is a class.
38      */

39     public static final JavaSourceCategory CLASS;
40
41     /**
42      * This type is a interface.
43      */

44     public static final JavaSourceCategory INTERFACE;
45     static Map JavaDoc pool = new HashMap JavaDoc();
46
47     static {
48       CLASS = new JavaSourceCategory("class");
49       INTERFACE = new JavaSourceCategory("interface");
50     }
51
52     public static JavaSourceCategory require(String JavaDoc key) {
53       return (JavaSourceCategory) Enum.require(key, pool);
54     }
55
56     protected JavaSourceCategory(String JavaDoc key) {
57       super(key, pool);
58     }
59   }
60
61   private JavaSourceCategory classCategory = JavaSourceCategory.CLASS;
62
63   private String JavaDoc classComment;
64
65   private String JavaDoc className;
66
67   private Set JavaDoc imports = new HashSet JavaDoc();
68
69   private Set JavaDoc interfaceNames = new HashSet JavaDoc();
70
71   private String JavaDoc packageName;
72
73   private String JavaDoc superClassName;
74
75   public ClassSourceFileComposerFactory(String JavaDoc packageName, String JavaDoc className) {
76     this.packageName = packageName;
77     this.className = className;
78   }
79
80   public void addImplementedInterface(String JavaDoc intfName) {
81     interfaceNames.add(intfName);
82   }
83
84   public void addImport(String JavaDoc typeName) {
85     imports.add(typeName);
86   }
87
88   /**
89    * Creates an implementation of {@link SourceWriter} that can be used to write
90    * the innards of a class. Note that the subsequent changes to this factory do
91    * not affect the returned instance.
92    *
93    * @throws RuntimeException If the settings on this factory are inconsistent
94    * or invalid
95    */

96   public SourceWriter createSourceWriter(GeneratorContext ctx,
97       PrintWriter JavaDoc printWriter) {
98     return new ClassSourceFileComposer(ctx, printWriter, getCreatedPackage(),
99         getCreatedClassShortName(), getSuperclassName(), getInterfaceNames(),
100         getImports(), classCategory, classComment);
101   }
102
103   /**
104    * Creates an implementation of {@link SourceWriter} that can be used to write
105    * the innards of a class. Note that the subsequent changes to this factory do
106    * not affect the returned instance.
107    *
108    * @param printWriter underlying writer
109    * @return the source writer
110    * @throws RuntimeException If the settings on this factory are inconsistent
111    * or invalid
112    */

113   public SourceWriter createSourceWriter(PrintWriter JavaDoc printWriter) {
114     return new ClassSourceFileComposer(null, printWriter, getCreatedPackage(),
115         getCreatedClassShortName(), getSuperclassName(), getInterfaceNames(),
116         getImports(), classCategory, classComment);
117   }
118
119   public String JavaDoc getCreatedClassName() {
120     return getCreatedPackage() + "." + getCreatedClassShortName();
121   }
122
123   public String JavaDoc getCreatedClassShortName() {
124     return className;
125   }
126
127   public String JavaDoc getCreatedPackage() {
128     return packageName;
129   }
130
131   public String JavaDoc[] getInterfaceNames() {
132     return (String JavaDoc[]) interfaceNames.toArray(new String JavaDoc[interfaceNames.size()]);
133   }
134
135   public String JavaDoc getSuperclassName() {
136     return superClassName;
137   }
138
139   /**
140    * This class is an interface.
141    */

142   public void makeInterface() {
143     classCategory = JavaSourceCategory.INTERFACE;
144   }
145
146   /**
147    * Sets the java doc comment for <code>this</code>.
148    *
149    * @param comment java doc comment.
150    */

151   public void setJavaDocCommentForClass(String JavaDoc comment) {
152     classComment = comment;
153   }
154
155   public void setSuperclass(String JavaDoc superclassName) {
156     superClassName = superclassName;
157   }
158
159   private String JavaDoc[] getImports() {
160     return (String JavaDoc[]) imports.toArray(new String JavaDoc[imports.size()]);
161   }
162 }
163
Popular Tags