KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
23 import java.lang.reflect.Modifier JavaDoc;
24
25 import javax.swing.text.Position JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 import org.openide.src.*;
29 import org.openide.text.CloneableEditorSupport;
30 import org.openide.text.PositionBounds;
31 import org.openide.text.PositionRef;
32
33 import org.netbeans.modules.java.bridge.Binding;
34
35 /**
36  *
37  * @author svata
38  * @version
39  */

40 class Method extends Member implements Binding.Method {
41     boolean constructor;
42     String JavaDoc preexistingBody;
43
44     public Method(ConstructorElement el, SourceText s) {
45         super(el, s);
46         constructor = (!(el instanceof MethodElement));
47     }
48     
49     public void create(PositionBounds b) throws SourceException {
50         super.create(b);
51         preexistingBody = null;
52     }
53     
54     protected int classifyProperty(String JavaDoc name) {
55         if (name == PROP_BODY)
56             return CLASS_BODY;
57         else
58             return CLASS_HEADER;
59     }
60     
61     private MethodElement cloneMethod() {
62         MethodElement el = new MethodElement();
63         copyProperties(el);
64         try {
65             el.setReturn(((MethodElement)getElement()).getReturn());
66         } catch (SourceException ex) {
67             // should NOT happen
68
}
69         return el;
70     }
71         
72     protected Element cloneElement() {
73         Element orig = getElement();
74         ConstructorElement el = orig instanceof MethodElement ?
75             new MethodElement() : new ConstructorElement();
76         copyProperties(el);
77         return el;
78     }
79     
80     protected void copyProperties(ConstructorElement target) {
81         ConstructorElement my = (ConstructorElement)getElement();
82         try {
83             target.setName(my.getName());
84             target.setParameters(my.getParameters());
85             target.setModifiers(my.getModifiers());
86             target.setExceptions(my.getExceptions());
87             if (my instanceof MethodElement)
88                 ((MethodElement)target).setReturn(((MethodElement)my).getReturn());
89         } catch (SourceException ex) {
90             // should NOT happen
91
}
92     }
93     
94     
95     /** Changes exception list for the method.
96      */

97     public void changeExceptions(Identifier[] exceptions) throws SourceException {
98         if (!source.isGeneratorEnabled())
99             return;
100         ConstructorElement el = (ConstructorElement)cloneElement();
101         el.setExceptions(exceptions);
102         regenerateHeader(el);
103     }
104     
105     /** Changes parameter list for the method.
106      */

107     public void changeParameters(MethodParameter[] params) throws SourceException {
108         if (!source.isGeneratorEnabled())
109             return;
110         ConstructorElement el = (ConstructorElement)cloneElement();
111         el.setParameters(params);
112         regenerateHeader(el);
113     }
114     
115     /** Changes the return type declaration.
116      */

117     public void changeReturnType(Type type) throws SourceException {
118         if (!source.isGeneratorEnabled())
119             return;
120         MethodElement el = cloneMethod();
121         el.setReturn(type);
122         regenerateHeader(el);
123     }
124
125     /**
126      * Requests change of member's modifiers.
127      */

128     public void changeModifiers(int newMods) throws SourceException {
129         if (!source.isGeneratorEnabled())
130             return;
131         
132         ConstructorElement el = (ConstructorElement)cloneElement();
133         boolean isAbstract = Modifier.isAbstract(el.getModifiers());
134         boolean newAbstract = Modifier.isAbstract(newMods);
135         el.setModifiers(newMods);
136         regenerateHeader(el);
137         if (isAbstract!=newAbstract) {
138             if (newAbstract)
139                 el.setBody(null);
140             else {
141                 if (((ConstructorElement)getElement()).getBody()!=null)
142                     return;
143                 el.setBody("");
144             }
145             regenerateBody(el);
146         }
147     }
148
149     protected void regenerateBody(Element bean) throws SourceException {
150         ConstructorElement now = (ConstructorElement)bean;
151         ConstructorElement el = (ConstructorElement)getElement();
152         boolean existed = el.getBody() != null;
153         String JavaDoc newBody = now.getBody();
154         
155         if (existed && newBody == null) {
156             makeAbstract();
157         } else if (!existed && newBody != null) {
158             createBody(newBody);
159         } else if (existed) {
160             changeBody(newBody);
161         }
162     }
163
164     /** Create a nonabstract body containing the passed text.
165      */

166     public void createBody(String JavaDoc bodyText) throws SourceException {
167         if (!source.isGeneratorEnabled())
168             return;
169         final StyledDocument JavaDoc doc = findDocument();
170         final String JavaDoc s = CodeGenerator.normalizeBody(
171             ElementBinding.convertNewlines(bodyText), true);
172         
173         source.runAtomic(getElement(), new ExceptionRunnable() {
174             public void run() throws Exception JavaDoc {
175                 int afterHeader = headerBounds.getEnd().getOffset();
176                 String JavaDoc text = CodeGenerator.formatText(doc, afterHeader,
177                     " " + s); // NOI18N
178
int end = afterHeader + text.length();
179
180                 CloneableEditorSupport supp = source.getEditorSupport();
181                 doc.insertString(afterHeader, text, null);
182                 bodyBounds = new PositionBounds(
183                     supp.createPositionRef(afterHeader + 1, Position.Bias.Forward),
184                     supp.createPositionRef(end, Position.Bias.Backward)
185                 );
186                 doc.remove(end, wholeBounds.getEnd().getOffset() - end);
187             }
188         });
189     }
190     
191     /** Retrieves the text of the body.
192      */

193     public String JavaDoc getBodyContent() throws SourceException {
194         if (preexistingBody != null)
195             return preexistingBody;
196         
197         if (bodyBounds != null) {
198             return CodeGenerator.readTextBounds(bodyBounds);
199         } else {
200             return null;
201         }
202     }
203
204     /** Make abstract means removing the body part and substituting
205      * a semicolon.
206      */

207     public void makeAbstract() throws SourceException {
208         final StyledDocument JavaDoc doc = findDocument();
209
210         if (!source.isGeneratorEnabled())
211             return;
212         source.runAtomic(getElement(), new ExceptionRunnable() {
213             public void run() throws Exception JavaDoc {
214                 int afterHeader = headerBounds.getEnd().getOffset();
215                 int end = wholeBounds.getEnd().getOffset();
216
217                 doc.insertString(afterHeader, ";", null); // NOI18N
218
doc.remove(afterHeader + 1, end - afterHeader);
219                 bodyBounds = null;
220
221                 // PENDING: register UNDO event with the document's undo manager,
222
// so that the bodyBounds are restored!!!
223
}
224         });
225     }
226     
227     /**
228      * Changes the body contents. The method assumes that there is already some body.
229      */

230     public void changeBody(String JavaDoc bodyString) throws SourceException {
231         if (!source.isGeneratorEnabled())
232             return;
233         final String JavaDoc normalized = CodeGenerator.normalizeBody(
234             ElementBinding.convertNewlines(bodyString), false)+"}"; // NOI18N
235
final ElementBinding b = this;
236         
237         source.runAtomic(getElement(), new ExceptionRunnable() {
238             public void run() throws Exception JavaDoc {
239                 String JavaDoc formatted = CodeGenerator.formatText(
240                     CodeGenerator.getDocument(b),
241                     bodyBounds.getBegin(),
242                     normalized);
243                 String JavaDoc formattedBody = formatted.substring(0,formatted.lastIndexOf('}'));
244                 CodeGenerator.fillTextBounds(bodyBounds,formattedBody);
245             }
246         });
247     }
248     
249     /**
250      * Updates the storage binding object from an external SourceText.
251      */

252     public void updateFrom(Binding other) {
253     }
254     
255     public void copyBody(String JavaDoc bodyString) {
256         preexistingBody = bodyString;
257     }
258 }
259
Popular Tags