KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > palette > JSPPaletteUtilities


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.web.core.palette;
21
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Caret JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import org.netbeans.api.html.lexer.HTMLTokenId;
27 import org.netbeans.api.jsp.lexer.JspTokenId;
28 import org.netbeans.api.lexer.Token;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenSequence;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.editor.Formatter;
33
34 /**
35  *
36  * @author Libor Kotouc
37  */

38 public final class JSPPaletteUtilities {
39     
40     //marekf: who calls this???? It seems the method is not used and was the only reason of having impl. dep. on web/jspsyntax, grrrr.
41
public static int wrapTags(int start, int end, BaseDocument doc) {
42         try {
43             TokenHierarchy th = TokenHierarchy.get(doc);
44             TokenSequence jspTs = th.tokenSequence();
45             if(jspTs.move(start) == Integer.MAX_VALUE) {
46                 return end; //no token in sequence
47
}
48             Token token = jspTs.token();
49             //wrap JSP tags
50
while (token.offset(th) < end && jspTs.moveNext()) { // interested only in the tokens inside the body
51
token = jspTs.token();
52                 if (token.text().toString().startsWith("<") && token.id() == JspTokenId.SYMBOL) {
53                     // it's '<' token
54
int offset = token.offset(th);
55                     doc.insertString(offset, "\n", null); // insert a new-line before '<'
56
end++; // remember new body end
57
}
58             }
59             //wrap HTML tags
60
TokenSequence htmlTs = th.tokenSequence(HTMLTokenId.language());
61             if(htmlTs == null || htmlTs.move(start) == Integer.MAX_VALUE) {
62                 return end; //no token in sequence
63
}
64             while (token.offset(th) < end && htmlTs.moveNext()) { // interested only in the tokens inside the body
65
token = htmlTs.token();
66                 if (token.text().toString().startsWith("<") && token.id() == HTMLTokenId.TAG_OPEN_SYMBOL) {
67                     // it's '<' token
68
int offset = token.offset(th);
69                     doc.insertString(offset, "\n", null); // insert a new-line before '<'
70
end++; // remember new body end
71
}
72             }
73         } catch (IllegalStateException JavaDoc ise) {
74             //ignore
75
} catch (BadLocationException JavaDoc ble) {
76             //ignore
77
}
78         
79         return end;
80     }
81
82     public static void insert(String JavaDoc s, JTextComponent JavaDoc target)
83     throws BadLocationException JavaDoc
84     {
85         insert(s, target, true);
86     }
87     
88     public static void insert(String JavaDoc s, JTextComponent JavaDoc target, boolean reformat)
89     throws BadLocationException JavaDoc
90     {
91         Document JavaDoc doc = target.getDocument();
92         if (doc == null)
93             return;
94         
95         //check whether we are not in a scriptlet
96
// JspSyntaxSupport sup = (JspSyntaxSupport)(doc.getSyntaxSupport().get(JspSyntaxSupport.class));
97
// int start = target.getCaret().getDot();
98
// TokenItem token = sup.getTokenChain(start, start + 1);
99
// if (token != null && token.getTokenContextPath().contains(JavaTokenContext.contextPath)) // we are in a scriptlet
100
// return false;
101

102         if (s == null)
103             s = "";
104         
105         if (doc instanceof BaseDocument)
106             ((BaseDocument)doc).atomicLock();
107         
108         int start = insert(s, target, doc);
109         
110         if (reformat && start >= 0 && doc instanceof BaseDocument) { // format the inserted text
111
int end = start + s.length();
112             Formatter f = ((BaseDocument)doc).getFormatter();
113             f.reformat((BaseDocument)doc, start, end);
114         }
115
116 // if (select && start >= 0) { // select the inserted text
117
// Caret caret = target.getCaret();
118
// int current = caret.getDot();
119
// caret.setDot(start);
120
// caret.moveDot(current);
121
// caret.setSelectionVisible(true);
122
// }
123

124         if (doc instanceof BaseDocument)
125             ((BaseDocument)doc).atomicUnlock();
126         
127     }
128     
129     private static int insert(String JavaDoc s, JTextComponent JavaDoc target, Document JavaDoc doc)
130     throws BadLocationException JavaDoc
131     {
132
133         int start = -1;
134         try {
135             //at first, find selected text range
136
Caret JavaDoc caret = target.getCaret();
137             int p0 = Math.min(caret.getDot(), caret.getMark());
138             int p1 = Math.max(caret.getDot(), caret.getMark());
139             doc.remove(p0, p1 - p0);
140             
141             //replace selected text by the inserted one
142
start = caret.getDot();
143             doc.insertString(start, s, null);
144         }
145         catch (BadLocationException JavaDoc ble) {}
146         
147         return start;
148     }
149
150 }
151
Popular Tags