1 19 package org.netbeans.modules.xml.text.syntax; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.Panel ; 23 import java.util.*; 24 25 import javax.swing.text.Caret ; 27 import javax.swing.text.Document ; 28 import javax.swing.text.TextAction ; 29 import javax.swing.text.JTextComponent ; 30 import javax.swing.text.BadLocationException ; 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 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 55 public class XMLKit extends NbEditorKit implements org.openide.util.HelpCtx.Provider { 56 57 58 private static final long serialVersionUID =5326735092324267367L; 59 60 public static final String xmlCommentAction = "xml-comment"; 62 63 public static final String xmlUncommentAction = "xml-uncomment"; 65 66 public static final String xmlTestAction = "xml-dump"; 68 69 public static Map settings; 71 72 private static final boolean J2EE_LEXER_COLORING = Boolean.getBoolean("j2ee_lexer_coloring"); 75 public org.openide.util.HelpCtx getHelpCtx() { 76 return new org.openide.util.HelpCtx(XMLKit.class); 77 } 78 79 80 public Syntax createSyntax(Document doc) { 81 return new XMLDefaultSyntax(); 82 } 88 89 public Document createDefaultDocument() { 90 if(J2EE_LEXER_COLORING) { 91 Document doc = new XMLEditorDocument(this.getClass()); 92 Object mimeType = doc.getProperty("mimeType"); if (mimeType == null){ 94 doc.putProperty("mimeType", getContentType()); } 96 doc.putProperty(Language.class, XMLTokenId.language()); 97 return doc; 98 } else { 99 return new NbEditorDocument (this.getClass()); 100 } 101 } 102 103 104 105 public SyntaxSupport createSyntaxSupport(BaseDocument doc) { 106 return new XMLSyntaxSupport(doc); 107 } 108 109 110 public Completion createCompletion(ExtEditorUI extEditorUI) { 111 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")) { new NodeSelector(c); 123 } 124 } 125 126 public static void setMap(Map map) { 128 settings = map; 129 } 130 131 public Map getMap() { 133 return settings; 134 } 135 136 public String getContentType() { 138 return XMLDataObject.MIME_TYPE; 139 } 140 141 144 protected Action [] createActions() { 145 Action [] actions = new Action [] { 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 id) { 157 super(id); 158 String desc = org.openide.util.NbBundle.getMessage(XMLKit.class,id); if (desc != null) { 160 putValue(SHORT_DESCRIPTION, desc); 161 } 162 } 163 164 167 protected void problem(String reason) { 168 if (reason != null) StatusDisplayer.getDefault().setStatusText("Cannot proceed: " + reason); 169 new Panel ().getToolkit().beep(); 170 } 171 } 172 173 176 public static class XMLCommentAction extends XMLEditorAction { 177 178 private static final long serialVersionUID =4004056745446061L; 179 180 private static final String commentStartString = "<!--"; private static final String commentEndString = "-->"; 183 public XMLCommentAction() { 184 super( xmlCommentAction); 185 } 186 187 public void actionPerformed(ActionEvent evt, JTextComponent target) { 188 if (target == null) return; 189 if (!target.isEditable() || !target.isEnabled()) { 190 problem(null); 191 return; 192 } 193 Caret 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 { 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 e) { 225 problem(null); 226 } 227 } 228 229 } 230 231 235 public static class XMLUncommentAction extends XMLEditorAction { 236 private static final String commentStartString = "<!--"; private static final String commentEndString = "-->"; 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 evt, JTextComponent target) { 248 if (target == null) return; 249 if (!target.isEditable() || !target.isEnabled()) { 250 problem(null); 251 return; 252 } 253 Caret 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 { 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 e) { 298 problem(null); 299 } 300 } 301 } 302 303 304 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 evt, JTextComponent target) { 316 if (target == null) return; 317 if (!target.isEditable() || !target.isEnabled()) { 318 problem(null); 319 return; 320 } 321 Caret caret = target.getCaret(); 322 BaseDocument doc = (BaseDocument)target.getDocument(); 323 try { 324 doc.dump(System.out); 325 if (target == null) throw new BadLocationException (null,0); } catch (BadLocationException e) { 327 problem(null); 328 } 329 } 330 331 } 332 333 public class XMLEditorDocument extends NbEditorDocument { 334 public XMLEditorDocument(Class kitClass) { 335 super(kitClass); 336 } 337 338 public boolean addLayer(DrawLayer layer, int visibility) { 339 if(!(layer instanceof DrawLayerFactory.SyntaxLayer)) { 341 return super.addLayer(layer, visibility); 342 } else { 343 return false; 344 } 345 } 346 } 347 } 348 | Popular Tags |