KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > palette > JSFPaletteUtilities


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.jsf.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.editor.BaseDocument;
27 import org.netbeans.editor.Formatter;
28 import org.netbeans.editor.TokenItem;
29
30 /**
31  *
32  * @author Libor Kotouc
33  */

34 public final class JSFPaletteUtilities {
35     
36     public static void insert(String JavaDoc s, JTextComponent JavaDoc target)
37     throws BadLocationException JavaDoc
38     {
39         insert(s, target, true);
40     }
41     
42     public static void insert(String JavaDoc s, JTextComponent JavaDoc target, boolean reformat)
43     throws BadLocationException JavaDoc
44     {
45         Document JavaDoc doc = target.getDocument();
46         if (doc == null)
47             return;
48         
49         //check whether we are not in a scriptlet
50
// JspSyntaxSupport sup = (JspSyntaxSupport)(doc.getSyntaxSupport().get(JspSyntaxSupport.class));
51
// int start = target.getCaret().getDot();
52
// TokenItem token = sup.getTokenChain(start, start + 1);
53
// if (token != null && token.getTokenContextPath().contains(JavaTokenContext.contextPath)) // we are in a scriptlet
54
// return false;
55

56         if (s == null)
57             s = "";
58         
59         if (doc instanceof BaseDocument)
60             ((BaseDocument)doc).atomicLock();
61         
62         int start = insert(s, target, doc);
63         
64         if (reformat && start >= 0 && doc instanceof BaseDocument) { // format the inserted text
65
int end = start + s.length();
66             Formatter f = ((BaseDocument)doc).getFormatter();
67             f.reformat((BaseDocument)doc, start, end);
68         }
69
70 // if (select && start >= 0) { // select the inserted text
71
// Caret caret = target.getCaret();
72
// int current = caret.getDot();
73
// caret.setDot(start);
74
// caret.moveDot(current);
75
// caret.setSelectionVisible(true);
76
// }
77

78         if (doc instanceof BaseDocument)
79             ((BaseDocument)doc).atomicUnlock();
80         
81     }
82     
83     private static int insert(String JavaDoc s, JTextComponent JavaDoc target, Document JavaDoc doc)
84     throws BadLocationException JavaDoc
85     {
86
87         int start = -1;
88         try {
89             //at first, find selected text range
90
Caret JavaDoc caret = target.getCaret();
91             int p0 = Math.min(caret.getDot(), caret.getMark());
92             int p1 = Math.max(caret.getDot(), caret.getMark());
93             doc.remove(p0, p1 - p0);
94             
95             //replace selected text by the inserted one
96
start = caret.getDot();
97             doc.insertString(start, s, null);
98         }
99         catch (BadLocationException JavaDoc ble) {}
100         
101         return start;
102     }
103
104 }
105
Popular Tags