KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > codegen > SourceB


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.codegen;
21
22 import javax.swing.text.Position JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24
25 import org.openide.src.*;
26 import org.openide.text.*;
27
28 import org.netbeans.modules.java.bridge.Binding;
29
30 /**
31  *
32  * @author svata
33  * @version
34  */

35 class SourceB extends ElementBinding implements Binding.Source {
36     /** Bounds for the package statement, or null if the statement is not present.
37      */

38     PositionBounds packageBounds;
39
40     /** Support for containment of classes.
41      */

42     ContainerSupport classContainer;
43     
44     /** Support for containment of interfaces.
45      */

46     ContainerSupport importContainer;
47     
48     public SourceB(SourceElement src, SourceText s) {
49         super(src, s);
50         classContainer = new ContainerSupport(s, this);
51         importContainer = new ContainerSupport(s, this);
52         importContainer.separateMembers = false;
53     }
54     
55     protected Element cloneElement() {
56         return null;
57     }
58     
59     public void changePackage(final Identifier id) throws SourceException {
60         if (!source.isGeneratorEnabled())
61             return;
62         
63         source.runAtomic(getElement(), new ExceptionRunnable() {
64             public void run() throws Exception JavaDoc {
65                 PositionBounds bounds;
66                 
67                 if (packageBounds == null)
68                     bounds = findPackageBounds();
69                 else
70                     bounds = packageBounds;
71                 if (id == null) {
72                     CodeGenerator.clearBounds(bounds, true);
73                     packageBounds = null;
74                 } else {
75                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76                     
77                     sb.append("package "); // NOI18N
78
sb.append(id.getSourceName());
79                     sb.append(";"); // NOI18N
80
CodeGenerator.fillTextBounds(bounds, sb.toString());
81                     if (packageBounds == null) {
82                         // add a newline:
83
StyledDocument JavaDoc d = source.getDocument();
84                         d.insertString(bounds.getEnd().getOffset(),
85                             "\n", null); // NOI18N
86
}
87                     packageBounds = bounds;
88                 }
89             }
90         });
91     }
92     
93     public Binding.Container getImportsSection() {
94         return importContainer;
95     }
96     
97     public Binding.Container getClassSection() {
98         return classContainer;
99     }
100
101     public ElementBinding findBindingAt(int pos) {
102         return classContainer.findBindingAt(pos);
103     }
104
105     /**
106      * The operation does nothing, because the source cannot be removed ;-)
107      */

108     public void remove() throws SourceException {
109     }
110     
111     public void updateBounds(int kind, PositionBounds b) {
112         super.updateBounds(kind, b);
113         // update package bounds - if there is some package declaration.
114
if (kind == BOUNDS_PACKAGE) {
115             packageBounds = b;
116         }
117     }
118     
119     /**
120      * This method should return PositionBounds of the maximum possible
121      * extent for a container.
122      */

123     PositionBounds findContainerBounds(TextBinding.Container cont) {
124         int begin = -1, end = -1;
125         
126         if (cont == importContainer) {
127             if (packageBounds != null) {
128                 begin = packageBounds.getEnd().getOffset();
129             } else if (importContainer == null || importContainer.isEmpty()) {
130                 // beginning of the text.
131
begin = 0;
132             } else {
133                 begin = importContainer.getFirstPosition().getOffset();
134             }
135             if (classContainer == null || classContainer.isEmpty()) {
136                 end = -1;
137             } else {
138                 end = classContainer.getFirstPosition().getOffset();
139             }
140         } else if (cont == classContainer) {
141             if (importContainer == null || importContainer.isEmpty()) {
142                 if (packageBounds == null) {
143                     begin = 0;
144                 } else {
145                     begin = packageBounds.getEnd().getOffset();
146                 }
147             } else {
148                 begin = importContainer.getLastPosition().getOffset();
149             }
150         } else
151             throw new IllegalArgumentException JavaDoc(cont.toString());
152
153         if (end == -1) {
154             StyledDocument JavaDoc doc = source.getEditorSupport().getDocument();
155             if (doc == null)
156                 end = begin;
157             else
158                 end = doc.getLength();
159         }
160
161         PositionBounds b = new PositionBounds(
162             source.createPos(begin, Position.Bias.Forward),
163             source.createPos(end, Position.Bias.Backward));
164         return b;
165     }
166     
167     private PositionBounds findPackageBounds() throws SourceException {
168         PositionBounds bounds = null;
169         
170         if (importContainer != null && !importContainer.isEmpty())
171             bounds = importContainer.getContainerBounds();
172         
173         if (bounds == null && !classContainer.isEmpty())
174             bounds = classContainer.getContainerBounds();
175         
176         if (bounds != null)
177             return CodeGenerator.createNewLineBounds(bounds.getBegin(),
178                 CodeGenerator.BOUNDS_PACKAGE);
179         
180         return CodeGenerator.createNewLineBounds(
181             source.createPos(0, Position.Bias.Forward),
182             CodeGenerator.BOUNDS_PACKAGE);
183     }
184     
185     public void updatePackageBounds(PositionBounds b) {
186         this.packageBounds = b;
187     }
188 }
189
Popular Tags