1 19 20 package org.netbeans.modules.tasklist.core; 21 22 import java.io.File ; 23 import javax.swing.text.*; 24 25 import java.net.URL ; 26 import java.net.MalformedURLException ; 27 import java.util.Iterator ; 28 import java.util.logging.ConsoleHandler ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import java.awt.*; 32 import java.awt.event.FocusListener ; 33 import java.awt.event.FocusEvent ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.nio.ByteBuffer ; 38 import java.nio.channels.FileChannel ; 39 40 import org.openide.cookies.LineCookie; 41 import org.openide.loaders.DataObject; 42 import org.openide.text.Line; 43 import org.openide.ErrorManager; 44 import org.openide.cookies.EditorCookie; 45 import org.openide.filesystems.URLMapper; 46 import org.openide.filesystems.FileObject; 47 import org.openide.nodes.Children; 48 import org.openide.nodes.Node; 49 50 55 public final class TLUtils { 56 57 public static final Logger LOGGER = TLUtils.getLogger(TLUtils.class); 58 59 static { 60 LOGGER.setLevel(Level.FINE); 61 } 62 63 70 public static String toHTML(String text) { 71 StringBuffer sb = new StringBuffer (2*text.length()); 72 int n = text.length(); 75 for (int i = 0; i < n; i++) { 76 switch (text.charAt(i)) { 77 case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; case '\n': sb.append("<br>"); break; default: sb.append(text.charAt(i)); break; 83 } 84 } 85 return sb.toString(); 86 } 87 88 94 public static void createBackup(File f) throws IOException { 95 File dir = f.getParentFile(); 96 File backup = new File (dir, f.getName() + "~"); if (backup.exists()) { 98 if (!backup.delete()) 99 throw new IOException ("Cannot delete the file"); } 101 102 FileInputStream fis = new FileInputStream (f); 103 try { 104 FileOutputStream fos = new FileOutputStream (backup); 105 106 try { 107 FileChannel ic = fis.getChannel(); 108 FileChannel oc = fos.getChannel(); 109 110 ByteBuffer bb = ByteBuffer.allocateDirect(16 * 1024); 111 112 bb.clear(); for (;;) { 114 if (ic.read(bb) < 0 && !bb.hasRemaining()) 115 break; bb.flip(); 117 oc.write(bb); 118 bb.compact(); } 120 } finally { 121 try { 122 fos.close(); 123 } catch (IOException e) { 124 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 125 } 126 } 127 } finally { 128 try { 129 fis.close(); 130 } catch (IOException e) { 131 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 132 } 133 } 134 135 } 136 137 139 public static Line getLineByNumber(DataObject dobj, int lineno) { 140 try { 142 LineCookie lc = (LineCookie)dobj.getCookie(LineCookie.class); 143 if (lc != null) { 144 Line.Set ls = lc.getLineSet(); 145 if (ls != null) { 146 Line l = ls.getCurrent(lineno-1); 149 return l; 150 } 151 } 152 } catch (IndexOutOfBoundsException ex) { 153 155 } catch (Exception e) { 156 ErrorManager.getDefault().log(ErrorManager.WARNING, "getLineByNumber - file " + dobj + " and lineno=" + lineno); ErrorManager.getDefault(). 158 notify(ErrorManager.INFORMATIONAL, e); 159 } 160 return null; 161 } 162 163 169 public static void appendSurroundingLine(StringBuffer sb, Line line, 170 int offset) { 171 DataObject dobj = org.openide.text.DataEditorSupport.findDataObject (line); 172 try { 173 LineCookie lc = (LineCookie)dobj.getCookie(LineCookie.class); 174 if (lc == null) { 175 return; 176 } 177 Line.Set ls = lc.getLineSet(); 178 if (ls == null) { 179 return; 180 } 181 182 int lineno = line.getLineNumber(); 183 if (lineno+offset < 0) { 184 return; 186 } 187 Line before = ls.getCurrent(lineno+offset); 188 appendHTMLString(sb, before.getText()); 189 } catch (Exception e) { 190 ErrorManager.getDefault(). 191 notify(ErrorManager.INFORMATIONAL, e); 192 } 193 } 194 195 196 public static int firstDiff(String s1, String s2) { 197 int n1 = s1.length(); 198 int n2 = s2.length(); 199 int n; 200 if (n1 < n2) { 201 n = n1; 202 } else { 203 n = n2; 204 } 205 for (int i = 0; i < n; i++) { 206 if (s1.charAt(i) != s2.charAt(i)) { 207 return i; 208 } 209 } 210 return n; 211 } 212 213 215 public static int lastDiff(String s1, String s2) { 216 int n1 = s1.length()-1; 217 int n2 = s2.length()-1; 218 int i = 0; 219 while ((n2 >= 0) && (n1 >= 0)) { 220 if (s1.charAt(n1) != s2.charAt(n2)) { 221 return i; 222 } 223 --n2; 224 --n1; 225 ++i; 226 } 227 return i; 228 } 229 230 234 public static void appendHTMLChar(StringBuffer sb, char c) { 235 switch (c) { 237 case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; case ' ': sb.append(" "); break; case '\n': sb.append("<br>"); break; default: sb.append(c); 244 } 245 } 246 247 251 public static void appendHTMLString(StringBuffer sb, String s) { 252 int n = s.length(); 253 for (int i = 0; i < n; i++) { 254 appendHTMLChar(sb, s.charAt(i)); 255 } 256 } 257 258 262 public static void appendAttributed(StringBuffer sb, 263 String text, 264 int begin, 265 int end, 266 boolean underline, 267 boolean bold) { 268 if (begin != -1) { 269 for (int i = 0; i < begin; i++) { 270 appendHTMLChar(sb, text.charAt(i)); 271 } 272 if (underline) { 273 sb.append("<u>"); } 275 if (bold) { 276 sb.append("<b>"); } 278 for (int i = begin; i < end; i++) { 279 appendHTMLChar(sb, text.charAt(i)); 280 } 281 if (underline) { 282 sb.append("</u>"); } 284 if (bold) { 285 sb.append("</b>"); } 287 int nl = text.length(); 288 for (int i = end; i < nl; i++) { 289 appendHTMLChar(sb, text.charAt(i)); 290 } 291 } else { 292 appendHTMLString(sb, text); 293 } 294 } 295 296 public static Element getElement(Document d, Line line) { 297 if (d == null) { 298 ErrorManager.getDefault().log(ErrorManager.USER, "d was null"); 299 return null; 300 } 301 302 if (!(d instanceof StyledDocument)) { 303 ErrorManager.getDefault().log(ErrorManager.USER, "Not a styleddocument"); 304 return null; 305 } 306 307 StyledDocument doc = (StyledDocument)d; 308 Element e = doc.getParagraphElement(0).getParentElement(); 309 if (e == null) { 310 e = doc.getDefaultRootElement (); 312 } 313 int lineNumber = line.getLineNumber(); 314 Element elm = e.getElement(lineNumber); 315 return elm; 316 } 317 318 public static Document getDocument(Line line) { 319 DataObject dao = org.openide.text.DataEditorSupport.findDataObject (line); 320 if (!dao.isValid()) { 321 return null; 323 } 324 return getDocument(dao); 325 } 326 327 public static Document getDocument(DataObject dao) { 328 final EditorCookie edit = (EditorCookie)dao.getCookie(EditorCookie.class); 329 if (edit == null) { 330 return null; 332 } 333 334 Document d = edit.getDocument(); return d; 336 } 337 338 342 public static boolean deleteLine(Line line, String prefix) { 343 Document doc = getDocument(line); 344 Element elm = getElement(doc, line); 345 if (elm == null) { 346 return false; 347 } 348 int offset = elm.getStartOffset(); 349 int endOffset = elm.getEndOffset(); 350 351 try { 352 String text = doc.getText(offset, endOffset-offset); 353 if (!text.startsWith(prefix)) { 354 return false; 355 } 356 doc.remove(offset, endOffset-offset); 357 } catch (BadLocationException ex) { 358 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 359 } 360 return false; 361 } 362 363 367 public static boolean commentLine(Line line, String prefix) { 368 Document doc = getDocument(line); 369 Element elm = getElement(doc, line); 370 if (elm == null) { 371 return false; 372 } 373 int offset = elm.getStartOffset(); 374 int endOffset = elm.getEndOffset(); 375 376 try { 377 String text = doc.getText(offset, endOffset-offset); 378 if (!text.startsWith(prefix)) { 379 return false; 380 } 381 doc.insertString(offset, "// ", null); } catch (BadLocationException ex) { 383 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 384 } 385 return false; 386 } 387 388 391 public static String toURL(FileObject fo) { 392 return URLMapper.findURL(fo, URLMapper.INTERNAL).toExternalForm(); 394 } 395 396 399 public static FileObject[] fromURL(String urlString) { 400 URL url; 401 try { 402 url = new URL (urlString); 403 } catch (MalformedURLException e) { 404 return null; 405 } 406 return URLMapper.findFileObjects(url); 407 } 408 409 414 public static int getChildrenCountRecursively(Node node) { 415 if (node == null) return 0; 416 417 Children children = node.getChildren(); 418 if(children.getNodesCount() == 0) return 0; 419 420 int n = 0; 421 Node[] nodes = children.getNodes(); 422 for (int i = 0; i < nodes.length; i++) { 423 n += getChildrenCountRecursively(nodes[i]) + 1; 424 } 425 return n; 426 } 427 428 435 public static Node.Property getProperty(Node n, String prop) { 436 Node.PropertySet[] propsets = n.getPropertySets(); 437 for (int j = 0; j < propsets.length; ++j) { 438 Node.Property[] props = propsets[j].getProperties(); 439 for (int k = 0; k < props.length; ++k) { 440 if (props[k].getName().equals(prop)) { 441 return props[k]; 442 } 443 } 444 } 445 return null; 446 } 447 448 449 456 public static Logger getLogger(Class clazz) { 457 Logger logger = Logger.getLogger(clazz.getName()); 460 logger.setUseParentHandlers(false); 461 462 ConsoleHandler ch = new ConsoleHandler (); 463 ch.setLevel(Level.FINE); 464 logger.addHandler(ch); 465 logger.setLevel(Level.WARNING); 466 return logger; 467 } 468 469 470 public static void traceFocus(final Container c) { 471 472 c.addFocusListener(new FocusListener () { 473 public void focusGained(FocusEvent e) { 474 System.err.println("Component " + c + " gained focus."); 475 Thread.dumpStack(); 476 } 477 public void focusLost(FocusEvent e) { 478 System.err.println("Component " + c + " lost focus."); 479 Thread.dumpStack(); 480 } 481 482 }); 483 Component[] cs = c.getComponents(); 484 if (cs != null) { 485 for (int i = 0; i<cs.length; i++) { 486 if (cs[i] instanceof Container) { 487 traceFocus((Container)cs[i]); 488 } 489 } 490 } 491 } 492 493 494 public static int recursiveCount(Iterator tasks) { 495 if (tasks == null) return 0; 496 int count = 0; 497 while (tasks.hasNext()) { 498 Task next = (Task) tasks.next(); 499 count += next.getSubtaskCountRecursively() + 1; 500 } 501 return count; 502 } 503 504 510 public static void nodeRecursivePrint(Node node, int depth) { 511 if (depth > 20) { Thread.dumpStack(); 513 return; 514 } 515 for (int i = 0; i < depth; i++) { 516 System.err.print(" "); } 518 System.err.println(node.getDisplayName()); 519 if ((node.getChildren() != null) && 520 (node.getChildren().getNodes() != null)) { 521 Node[] nodes = node.getChildren().getNodes(); 522 for (int i = 0; i < nodes.length; i++) { 523 nodeRecursivePrint(nodes[i], depth + 1); 524 } 525 } 526 } 527 } 528 | Popular Tags |