KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > palette > HTMLPaletteUtilities


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.html.palette;
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import javax.swing.JTree JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Caret JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.editor.BaseDocument;
35 import org.netbeans.editor.Formatter;
36 import org.netbeans.editor.TokenItem;
37 import org.netbeans.editor.ext.html.HTMLSyntaxSupport;
38 import org.netbeans.editor.ext.html.HTMLTokenContext;
39 import org.openide.filesystems.FileObject;
40
41
42 /**
43  *
44  * @author Libor Kotouc
45  */

46 public final class HTMLPaletteUtilities {
47     
48     public static int wrapTags(HTMLSyntaxSupport sup, int start, int end, BaseDocument doc) {
49         
50         try {
51             TokenItem token = sup.getTokenChain(start, start + 1);
52             
53             if (token == null)
54                 return end;
55             
56             while (token.getOffset() < end) { // interested only in the tokens inside the body
57
token = token.getNext();
58                 if (token.getTokenID() == HTMLTokenContext.TAG_OPEN_SYMBOL) { // it's '<' token
59
int offset = token.getOffset();
60                     doc.insertString(offset, "\n", null); // insert a new-line before '<'
61
end++; // remember new body end
62
token = sup.getTokenChain(offset + 1, offset + 2); // create new token chain reflecting changed document
63
}
64             }
65             
66         } catch (IllegalStateException JavaDoc ise) {
67         } catch (BadLocationException JavaDoc ble) {
68         }
69         
70         return end;
71     }
72
73     public static SourceGroup[] getSourceGroups(FileObject fObj) {
74     
75         Project proj = FileOwnerQuery.getOwner(fObj);
76         SourceGroup[] sg = new SourceGroup[] {};
77         if (proj != null) {
78             Sources sources = ProjectUtils.getSources(proj);
79             sg = sources.getSourceGroups("doc_root");
80 // if (sg.length == 0)
81
// sg = sources.getSourceGroups(Sources.TYPE_GENERIC);
82
}
83         
84         return sg;
85     }
86
87     public static JTree JavaDoc findTreeComponent(Component JavaDoc component) {
88         if (component instanceof JTree JavaDoc) {
89             return (JTree JavaDoc) component;
90         }
91         if (component instanceof Container JavaDoc) {
92             Component JavaDoc[] components = ((Container JavaDoc) component).getComponents();
93             for (int i = 0; i < components.length; i++) {
94                 JTree JavaDoc tree = findTreeComponent(components[i]);
95                 if (tree != null) {
96                     return tree;
97                 }
98             }
99         }
100         return null;
101     }
102
103     public static String JavaDoc getRelativePath(FileObject base, FileObject target) {
104         
105         final String JavaDoc DELIM = "/";
106         final String JavaDoc PARENT = ".." + DELIM;
107         
108         String JavaDoc targetPath = target.getPath();
109         String JavaDoc basePath = base.getPath();
110
111         //paths begin either with '/' or with '<letter>:/' - ensure that in the latter case the <letter>s equal
112
String JavaDoc baseDisc = basePath.substring(0, basePath.indexOf(DELIM));
113         String JavaDoc targetDisc = targetPath.substring(0, targetPath.indexOf(DELIM));
114         if (!baseDisc.equals(targetDisc))
115             return ""; //different disc letters, thus returning an empty string to signalize this fact
116

117         //cut a filename at the end taking last index for case of the same dir name as file name, really obscure but possible ;)
118
basePath = basePath.substring(0, basePath.lastIndexOf(base.getNameExt()));
119         targetPath = targetPath.substring(0, targetPath.lastIndexOf(target.getNameExt()));
120
121         //iterate through prefix dirs until difference occurres
122
StringTokenizer JavaDoc baseST = new StringTokenizer JavaDoc(basePath, DELIM);
123         StringTokenizer JavaDoc targetST = new StringTokenizer JavaDoc(targetPath, DELIM);
124         String JavaDoc baseDir = "";
125         String JavaDoc targetDir = "";
126         while (baseST.hasMoreTokens() && targetST.hasMoreTokens() && baseDir.equals(targetDir)) {
127             baseDir = baseST.nextToken();
128             targetDir = targetST.nextToken();
129         }
130         //create prefix consisting of parent dirs ("..")
131
StringBuffer JavaDoc parentPrefix = new StringBuffer JavaDoc(!baseDir.equals(targetDir) ? PARENT : "");
132         while (baseST.hasMoreTokens()) {
133             parentPrefix.append(PARENT);
134             baseST.nextToken();
135         }
136         //append remaining dirs with delimiter ("/")
137
StringBuffer JavaDoc targetSB = new StringBuffer JavaDoc(!baseDir.equals(targetDir) ? targetDir + DELIM : "");
138         while (targetST.hasMoreTokens())
139             targetSB.append(targetST.nextToken() + DELIM);
140
141         //resulting path
142
targetPath = parentPrefix.toString() + targetSB.toString() + target.getNameExt();
143         
144         return targetPath;
145     }
146
147     public static void insert(String JavaDoc s, JTextComponent JavaDoc target)
148     throws BadLocationException JavaDoc
149     {
150         insert(s, target, true);
151     }
152     
153     public static void insert(String JavaDoc s, JTextComponent JavaDoc target, boolean reformat)
154     throws BadLocationException JavaDoc
155     {
156
157         if (s == null)
158             s = "";
159         
160         Document JavaDoc doc = target.getDocument();
161         if (doc == null)
162             return;
163         
164         if (doc instanceof BaseDocument)
165             ((BaseDocument)doc).atomicLock();
166         
167         int start = insert(s, target, doc);
168         
169         if (reformat && start >= 0 && doc instanceof BaseDocument) { // format the inserted text
170
int end = start + s.length();
171             Formatter f = ((BaseDocument)doc).getFormatter();
172             f.reformat((BaseDocument)doc, start, end);
173         }
174
175 // if (select && start >= 0) { // select the inserted text
176
// Caret caret = target.getCaret();
177
// int current = caret.getDot();
178
// caret.setDot(start);
179
// caret.moveDot(current);
180
// caret.setSelectionVisible(true);
181
// }
182

183         if (doc instanceof BaseDocument)
184             ((BaseDocument)doc).atomicUnlock();
185         
186     }
187     
188     private static int insert(String JavaDoc s, JTextComponent JavaDoc target, Document JavaDoc doc)
189     throws BadLocationException JavaDoc
190     {
191
192         int start = -1;
193         try {
194             //at first, find selected text range
195
Caret JavaDoc caret = target.getCaret();
196             int p0 = Math.min(caret.getDot(), caret.getMark());
197             int p1 = Math.max(caret.getDot(), caret.getMark());
198             doc.remove(p0, p1 - p0);
199             
200             //replace selected text by the inserted one
201
start = caret.getDot();
202             doc.insertString(start, s, null);
203         }
204         catch (BadLocationException JavaDoc ble) {}
205         
206         return start;
207     }
208     
209 }
210
Popular Tags