KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > editor > GsfFormatSupport


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.retouche.editor;
21
22 import javax.swing.text.Document JavaDoc;
23 import org.netbeans.editor.BaseImageTokenID;
24 import org.netbeans.editor.TokenContext;
25 import org.netbeans.editor.TokenID;
26 import org.netbeans.editor.TokenItem;
27 import org.netbeans.editor.TokenContextPath;
28 import org.netbeans.editor.TokenItem;
29 import org.netbeans.editor.ext.FormatTokenPosition;
30 import org.netbeans.editor.ext.ExtFormatSupport;
31 import org.netbeans.editor.ext.FormatWriter;
32 import org.netbeans.modules.gsf.Language;
33
34 /**
35  * Java indentation services are located here
36  * This file is originally from Retouche, the Java Support
37  * infrastructure in NetBeans. I have modified the file as little
38  * as possible to make merging Retouche fixes back as simple as
39  * possible.
40  *
41 *
42 * @author Miloslav Metelka
43 * @version 1.00
44 */

45
46 public class GsfFormatSupport extends ExtFormatSupport {
47
48     private TokenContextPath tokenContextPath;
49     private Language language;
50     private Document JavaDoc doc;
51
52     public GsfFormatSupport(FormatWriter formatWriter, Language language, Document JavaDoc doc) {
53         this(formatWriter, null/*GsfTokenContext.contextPath*/, language, doc);
54         assert doc != null;
55     }
56
57     public GsfFormatSupport(FormatWriter formatWriter, TokenContextPath tokenContextPath, Language language, Document JavaDoc doc) {
58         super(formatWriter);
59         assert doc != null;
60         if (tokenContextPath == null) {
61             TokenContext context = new TokenContext("gsf");
62             tokenContextPath = context.getContextPath();
63         }
64         this.tokenContextPath = tokenContextPath;
65         this.language = language;
66         this.doc = doc;
67     }
68
69     public TokenContextPath getTokenContextPath() {
70         return tokenContextPath;
71     }
72
73     public boolean isComment(TokenItem token, int offset) {
74         TokenID tokenID = token.getTokenID();
75 // return (token.getTokenContextPath() == tokenContextPath
76
// && (tokenID == GsfTokenId.LINE_COMMENT
77
// || tokenID == GsfTokenId.COMMENT));
78
return false;
79     }
80
81     public boolean isMultiLineComment(TokenItem token) {
82 // return (token.getTokenID() == GsfTokenId.COMMENT);
83
return false;
84     }
85
86     public boolean isMultiLineComment(FormatTokenPosition pos) {
87         TokenItem token = pos.getToken();
88         return (token == null) ? false : isMultiLineComment(token);
89     }
90
91     /** Check whether the given token is multi-line comment
92      * that starts with slash and two stars.
93      */

94     public boolean isJavaDocComment(TokenItem token) {
95         return isMultiLineComment(token)
96             && token.getImage().startsWith("/**");
97     }
98
99     public TokenID getWhitespaceTokenID() {
100 // return GsfTokenId.WHITESPACE;
101
// return null;
102
return new BaseImageTokenID(" ");
103     }
104
105     public TokenContextPath getWhitespaceTokenContextPath() {
106         return tokenContextPath;
107     }
108
109     public boolean canModifyWhitespace(TokenItem inToken) {
110 // if (inToken.getTokenContextPath() == GsfTokenContext.contextPath) {
111
// switch (inToken.getTokenID().getNumericID()) {
112
// case GsfTokenId.COMMENT_ID:
113
// case GsfTokenId.WHITESPACE_ID:
114
// return true;
115
// }
116
// }
117
//
118
return false;
119     }
120
121
122     /** Get the indentation for the given token.
123      * It first searches whether there's an non-whitespace and a non-leftbrace
124      * character on the line with the token and if so,
125      * it takes indent of the non-ws char instead.
126      * @param token token for which the indent is being searched.
127      * The token itself is ignored and the previous token
128      * is used as a base for the search.
129      * @param forceFirstNonWhitespace set true to ignore leftbrace and search
130      * directly for first non-whitespace
131      */

132     public int getTokenIndent(TokenItem token, boolean forceFirstNonWhitespace) {
133         FormatTokenPosition tp = getPosition(token, 0);
134         // this is fix for bugs: 7980 and 9111
135
// see the findLineFirstNonWhitespaceAndNonLeftBrace definition
136
// for more info about the fix
137
FormatTokenPosition fnw;
138         if (forceFirstNonWhitespace)
139             fnw = findLineFirstNonWhitespace(tp);
140         else
141             fnw = findLineFirstNonWhitespaceAndNonLeftBrace(tp);
142         
143         if (fnw != null) { // valid first non-whitespace
144
tp = fnw;
145         }
146         return getVisualColumnOffset(tp);
147     }
148
149     public int getTokenIndent(TokenItem token) {
150         return getTokenIndent(token, false);
151     }
152     
153     public int findIndent(TokenItem token) {
154         int indent = -1; // assign invalid indent
155

156         // First check the given token
157
if (token != null) {
158         }
159
160         // If indent not found, search back for the first important token
161
if (indent < 0) { // if not yet resolved
162
TokenItem t = findImportantToken(token, null, true);
163             if (t != null) { // valid important token
164
if (indent < 0) { // no indent found yet
165
indent = getTokenIndent(t);
166                 }
167             }
168         }
169         
170         if (indent < 0) { // no important token found
171
indent = 0;
172         }
173
174         return indent;
175     }
176     
177     
178     public FormatTokenPosition indentLine(FormatTokenPosition pos) {
179         if (language.getFormatter() != null) {
180             // For indent-only always indent
181
int offset = getFormatWriter().getOffset();
182             return changeLineIndent(pos, language.getFormatter().getLineIndent(doc, offset,
183                     new GsfFormatter.GenericFormattingPreferences(language.getFormatter().indentSize())));
184         }
185         
186         int indent = 0; // Desired indent
187

188         // Get the first non-whitespace position on the line
189
FormatTokenPosition firstNWS = findLineFirstNonWhitespace(pos);
190         if (firstNWS != null) { // some non-WS on the line
191
if (isComment(firstNWS)) { // comment is first on the line
192
} else { // first non-WS char is not comment
193
indent = findIndent(firstNWS.getToken());
194             }
195         } else { // whole line is WS
196
// Can be empty line inside multi-line comment
197
TokenItem token = pos.getToken();
198             if (token == null) {
199                 token = findLineStart(pos).getToken();
200                 if (token == null) { // empty line
201
token = getLastToken();
202                 }
203             }
204             indent = findIndent(pos.getToken());
205         }
206
207         // For indent-only always indent
208
return changeLineIndent(pos, indent);
209     }
210
211     /* this is fix for bugs: 7980 and 9111. if user enters
212      * { foo();
213      * and press enter at the end of the line, she wants
214      * to be indented just under "f" in "foo();" and not under the "{"
215      * as it happens now. and this is what findLineFirstNonWhitespaceAndNonLeftBrace checks
216      */

217     public FormatTokenPosition findLineFirstNonWhitespaceAndNonLeftBrace(FormatTokenPosition pos) {
218         // first call the findLineFirstNonWhitespace
219
FormatTokenPosition ftp = super.findLineFirstNonWhitespace(pos);
220         if (ftp == null) { // no line start, no WS
221
return null;
222         }
223
224         System.err.println("Bogus findLineFirstNonWhitespaceAndNonLeftBrace implementation");
225         
226             return ftp;
227     }
228 }
229
Popular Tags