KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > XMLKit


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 package org.netbeans.modules.xml.text.syntax;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.Panel JavaDoc;
23 import java.util.*;
24
25 // prevent ambiguous reference to Utilities
26
import javax.swing.text.Caret JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.TextAction JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.*;
32 import org.netbeans.api.lexer.Language;
33 import org.netbeans.api.xml.lexer.XMLTokenId;
34 import org.openide.awt.StatusDisplayer;
35
36 // we depend on NetBeans editor stuff
37
import org.netbeans.editor.*;
38 import org.netbeans.editor.ext.*;
39 import org.netbeans.modules.editor.*;
40
41 import org.netbeans.modules.xml.core.XMLDataObject;
42 import org.netbeans.modules.xml.text.completion.NodeSelector;
43 import org.netbeans.modules.xml.text.completion.XMLCompletion;
44
45
46 /**
47  * NetBeans editor kit implementation for xml content type.
48  * <p>
49  * It provides syntax coloring, code completion, actions, abbrevirations, ...
50  *
51  * @author Libor Kramolis
52  * @author Petr Kuzel
53  * @author Sandeep
54  */

55 public class XMLKit extends NbEditorKit implements org.openide.util.HelpCtx.Provider {
56
57     /** Serial Version UID */
58     private static final long serialVersionUID =5326735092324267367L;
59     
60     // comment action name
61
public static final String JavaDoc xmlCommentAction = "xml-comment";
62     
63     // uncomment action name
64
public static final String JavaDoc xmlUncommentAction = "xml-uncomment";
65
66     // dump XML sysntax
67
public static final String JavaDoc xmlTestAction = "xml-dump";
68     
69     // hack to be settings browseable //??? more info needed
70
public static Map settings;
71     
72     //temporary - will be removed when lexer is stabilized
73
private static final boolean J2EE_LEXER_COLORING = Boolean.getBoolean("j2ee_lexer_coloring"); //NOI18N
74

75     public org.openide.util.HelpCtx getHelpCtx() {
76         return new org.openide.util.HelpCtx(XMLKit.class);
77     }
78     
79     /** Create new instance of syntax coloring parser */
80     public Syntax createSyntax(Document JavaDoc doc) {
81         return new XMLDefaultSyntax();
82 // return new JJEditorSyntax(
83
// new XMLSyntaxTokenManager(null).new Bridge(),
84
// new XMLSyntaxTokenMapper(),
85
// XMLTokenContext.contextPath
86
// );
87
}
88
89     public Document JavaDoc createDefaultDocument() {
90         if(J2EE_LEXER_COLORING) {
91             Document JavaDoc doc = new XMLEditorDocument(this.getClass());
92             Object JavaDoc mimeType = doc.getProperty("mimeType"); //NOI18N
93
if (mimeType == null){
94                 doc.putProperty("mimeType", getContentType()); //NOI18N
95
}
96             doc.putProperty(Language.class, XMLTokenId.language());
97             return doc;
98         } else {
99             return new NbEditorDocument (this.getClass());
100         }
101     }
102
103
104     /** Create syntax support */
105     public SyntaxSupport createSyntaxSupport(BaseDocument doc) {
106         return new XMLSyntaxSupport(doc);
107     }
108     
109
110     public Completion createCompletion(ExtEditorUI extEditorUI) {
111         //return new org.netbeans.modules.xml.text.completion.XMLCompletion(extEditorUI);
112
return null;
113     }
114     
115     public Completion createCompletionForProvider(ExtEditorUI extEditorUI) {
116         return new XMLCompletion(extEditorUI);
117     }
118     
119     public void install(JEditorPane c) {
120         super.install(c);
121         if (Boolean.getBoolean("netbeans.experimental.xml.nodeselectors")) { // NOI18N
122
new NodeSelector(c);
123         }
124     }
125
126     // hack to be settings browseable //??? more info needed
127
public static void setMap(Map map) {
128         settings = map;
129     }
130
131     // hack to be settings browseable //??? more info needed
132
public Map getMap() {
133         return settings;
134     }
135
136     //??? +xml handling
137
public String JavaDoc getContentType() {
138         return XMLDataObject.MIME_TYPE;
139     }
140
141     /**
142      * Provide XML related actions.
143      */

144     protected Action JavaDoc[] createActions() {
145         Action JavaDoc[] actions = new Action JavaDoc[] {
146             new XMLCommentAction(),
147             new XMLUncommentAction(),
148             new TestAction(),
149         };
150         return TextAction.augmentList(super.createActions(), actions);
151     }
152     
153     
154     public abstract static class XMLEditorAction extends BaseAction {
155         
156         public XMLEditorAction (String JavaDoc id) {
157             super(id);
158             String JavaDoc desc = org.openide.util.NbBundle.getMessage(XMLKit.class,id); // NOI18N
159
if (desc != null) {
160                 putValue(SHORT_DESCRIPTION, desc);
161             }
162         }
163         
164         /**
165          * Uniform way of reporting problem while action executing #15589
166          */

167         protected void problem(String JavaDoc reason) {
168             if (reason != null) StatusDisplayer.getDefault().setStatusText("Cannot proceed: " + reason);
169             new Panel JavaDoc().getToolkit().beep();
170         }
171     }
172     
173     /**
174      * Comment out editor selection.
175      */

176     public static class XMLCommentAction extends XMLEditorAction {
177         
178         private static final long serialVersionUID =4004056745446061L;
179
180         private static final String JavaDoc commentStartString = "<!--"; //NOI18N
181
private static final String JavaDoc commentEndString = "-->"; //NOI18N
182

183         public XMLCommentAction() {
184             super( xmlCommentAction);
185         }
186         
187         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
188             if (target == null) return;
189             if (!target.isEditable() || !target.isEnabled()) {
190                 problem(null);
191                 return;
192             }
193             Caret JavaDoc caret = target.getCaret();
194             BaseDocument doc = (BaseDocument)target.getDocument();
195             try {
196                 if (caret.isSelectionVisible()) {
197                     int startPos = Utilities.getRowStart(doc, target.getSelectionStart());
198                     int endPos = target.getSelectionEnd();
199                     doc.atomicLock();
200                     try {
201
202                         if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
203                             endPos--;
204                         }
205
206                         int pos = startPos;
207                         int lineCnt = Utilities.getRowCount(doc, startPos, endPos);
208
209                         for (;lineCnt > 0; lineCnt--) {
210                             doc.insertString(pos, commentStartString, null);
211                             doc.insertString(Utilities.getRowEnd(doc,pos), commentEndString, null);
212                             pos = Utilities.getRowStart(doc, pos, +1);
213                         }
214
215                     } finally {
216                         doc.atomicUnlock();
217                     }
218                 } else { // selection not visible
219
doc.insertString(Utilities.getRowStart(doc, target.getSelectionStart()),
220                         commentStartString, null);
221                     doc.insertString(Utilities.getRowEnd(doc, target.getSelectionStart()),
222                         commentEndString, null);
223                 }
224             } catch (BadLocationException JavaDoc e) {
225                 problem(null);
226             }
227         }
228         
229     }
230
231     /**
232      * Uncomment selected text
233      *
234      */

235     public static class XMLUncommentAction extends XMLEditorAction {
236         private static final String JavaDoc commentStartString = "<!--"; //NOI18N
237
private static final String JavaDoc commentEndString = "-->"; //NOI18N
238
private static final char[] commentStart = {'<','!','-','-'};
239         private static final char[] commentEnd = {'-','-','>'};
240         
241         static final long serialVersionUID = 40040567454546061L;
242         
243         public XMLUncommentAction() {
244             super( xmlUncommentAction);
245         }
246         
247         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
248             if (target == null) return;
249             if (!target.isEditable() || !target.isEnabled()) {
250                 problem(null);
251                 return;
252             }
253             Caret JavaDoc caret = target.getCaret();
254             BaseDocument doc = (BaseDocument)target.getDocument();
255             try {
256                 if (caret.isSelectionVisible()) {
257                     int startPos = Utilities.getRowStart(doc, target.getSelectionStart());
258                     int endPos = target.getSelectionEnd();
259                     doc.atomicLock();
260                     try {
261
262                         if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
263                             endPos--;
264                         }
265
266                         int pos = startPos;
267                         int lineCnt = Utilities.getRowCount(doc, startPos, endPos);
268                         char[] startChars, endChars;
269
270                         for (; lineCnt > 0; lineCnt-- ) {
271                             startChars = doc.getChars(pos, 4 );
272                             endChars = doc.getChars(Utilities.getRowEnd(doc,pos)-3, 3 );
273
274                             if(startChars[0] == commentStart[0] && startChars[1] == commentStart[1] &&
275                                 startChars[2] == commentStart[2] && startChars[3] == commentStart[3] &&
276                                 endChars[0] == commentEnd[0] && endChars[1] == commentEnd[1] && endChars[2] == commentEnd[2] ){
277
278                                 doc.remove(pos,4);
279                                 doc.remove(Utilities.getRowEnd(doc,pos)-3,3);
280                             }
281                             pos = Utilities.getRowStart(doc, pos, +1);
282                         }
283
284                     } finally {
285                         doc.atomicUnlock();
286                     }
287                 } else { // selection not visible
288
char[] startChars = doc.getChars(target.getSelectionStart(), 4 );
289                   char[] endChars = doc.getChars(Utilities.getRowEnd(doc,target.getSelectionStart())-3, 3 );
290                   if(startChars[0] == commentStart[0] && startChars[1] == commentStart[1] &&
291                                 startChars[2] == commentStart[2] && startChars[3] == commentStart[3] &&
292                                 endChars[0] == commentEnd[0] && endChars[1] == commentEnd[1] && endChars[2] == commentEnd[2] ){
293                         doc.remove(target.getSelectionStart(),4);
294                         doc.remove(Utilities.getRowEnd(doc,target.getSelectionStart())-3,3);
295                     }
296                 }
297             } catch (BadLocationException JavaDoc e) {
298                 problem(null);
299             }
300         }
301     }
302
303     
304     /**
305      * Dump it.
306      */

307     public static class TestAction extends XMLEditorAction {
308         
309         private static final long serialVersionUID =4004056745446099L;
310
311         public TestAction() {
312             super( xmlTestAction);
313         }
314         
315         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
316             if (target == null) return;
317             if (!target.isEditable() || !target.isEnabled()) {
318                 problem(null);
319                 return;
320             }
321             Caret JavaDoc caret = target.getCaret();
322             BaseDocument doc = (BaseDocument)target.getDocument();
323             try {
324                 doc.dump(System.out);
325                 if (target == null) throw new BadLocationException JavaDoc(null,0); // folish compiler
326
} catch (BadLocationException JavaDoc e) {
327                 problem(null);
328             }
329         }
330         
331     }
332     
333     public class XMLEditorDocument extends NbEditorDocument {
334         public XMLEditorDocument(Class JavaDoc kitClass) {
335             super(kitClass);
336         }
337         
338         public boolean addLayer(DrawLayer layer, int visibility) {
339             //filter out the syntax layer adding
340
if(!(layer instanceof DrawLayerFactory.SyntaxLayer)) {
341                 return super.addLayer(layer, visibility);
342             } else {
343                 return false;
344             }
345         }
346     }
347 }
348
Popular Tags