1 19 20 package org.netbeans.modules.tasklist.usertasks.util; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.net.URL ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import javax.swing.Action ; 34 import javax.swing.JButton ; 35 36 import javax.swing.JEditorPane ; 37 import javax.swing.JToolBar ; 38 import org.netbeans.modules.tasklist.core.TLUtils; 39 40 import org.openide.cookies.EditorCookie; 41 import org.openide.cookies.LineCookie; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.filesystems.URLMapper; 45 import org.openide.loaders.DataObject; 46 import org.openide.loaders.DataObjectNotFoundException; 47 import org.openide.nodes.Node; 48 import org.openide.text.CloneableEditorSupport; 49 import org.openide.text.Line; 50 import org.openide.text.NbDocument; 51 import org.openide.util.actions.Presenter; 52 import org.openide.windows.Mode; 53 import org.openide.windows.TopComponent; 54 import org.openide.windows.WindowManager; 55 import org.w3c.dom.Element ; 56 import org.w3c.dom.Text ; 57 58 63 public final class UTUtils { 64 public static final Logger LOGGER = TLUtils.getLogger(UTUtils.class); 65 66 static { 67 LOGGER.setLevel(Level.OFF); 68 } 69 70 76 public static void copyStream(InputStream is, OutputStream os) 77 throws IOException { 78 byte[] buffer = new byte[1024]; 79 int read; 80 while ((read = is.read(buffer)) != -1) { 81 os.write(buffer, 0, read); 82 } 83 } 84 85 92 public static Element appendElement(Element el, String tagName) { 93 Element r = el.getOwnerDocument().createElement(tagName); 94 el.appendChild(r); 95 return r; 96 } 97 98 106 public static Element appendElement(Element el, String tagName, 107 String content) { 108 Element r = el.getOwnerDocument().createElement(tagName); 109 el.appendChild(r); 110 Text txt = el.getOwnerDocument().createTextNode(content); 111 r.appendChild(txt); 112 return r; 113 } 114 115 122 public static Element appendText(Element el, String content) { 123 Text txt = el.getOwnerDocument().createTextNode(content); 124 el.appendChild(txt); 125 return el; 126 } 127 128 135 public static String prepareForTooltip(String text) { 136 int index = text.indexOf('\n'); 137 if (index == -1) 138 return text; 139 140 StringBuffer sb = new StringBuffer ("<html>"); while (index >= 0) { 142 sb.append(text.substring(0, index)); 143 sb.append("<br>"); text = text.substring(index + 1); 145 index = text.indexOf('\n'); 146 }; 147 sb.append(text); 148 sb.append("</html>"); return sb.toString(); 150 } 151 152 159 public static JToolBar createToolbarPresenter(Action [] actions) { 160 JToolBar p = new JToolBar (); 161 int i; 162 int k = actions.length; 163 164 for (i = 0; i < k; i++) { 165 if (actions[i] == null) { 166 p.addSeparator(new Dimension (3, 3)); 167 } else if (actions[i] instanceof Presenter.Toolbar) { 168 p.add(((Presenter.Toolbar) actions[i]).getToolbarPresenter()); 169 } else { 170 p.add(actions[i]); 171 } 172 } 173 174 final Dimension D = new Dimension (24, 24); 175 for (int j = 0; j < p.getComponentCount(); j++) { 176 Component c = p.getComponent(j); 177 if (c instanceof JButton ) { 178 ((JButton ) c).setPreferredSize(D); 179 ((JButton ) c).setMinimumSize(D); 180 ((JButton ) c).setMaximumSize(D); 181 } 182 } 183 184 return p; 185 } 186 187 194 public static boolean objectsEquals(Object obj1, Object obj2) { 195 if (obj1 == null && obj2 == null) 196 return true; 197 if (obj1 != null && obj2 == null) 198 return false; 199 if (obj1 == null && obj2 != null) 200 return false; 201 return obj1.equals(obj2); 202 } 203 204 210 public static Node[] getEditorNodes() { 211 Node[] nodes = null; 219 WindowManager wm = WindowManager.getDefault(); 220 221 Mode mode = wm.findMode(CloneableEditorSupport.EDITOR_MODE); 226 if (mode == null) { 227 return null; 228 } 229 TopComponent [] tcs = mode.getTopComponents(); 230 for (int j = 0; j < tcs.length; j++) { 231 if (tcs[j].isShowing()) { 233 nodes = tcs[j].getActivatedNodes(); 234 break; 235 } 236 } 237 return nodes; 238 } 239 240 246 public static Line findCursorPosition(Node[] nodes) { 247 if (nodes == null) { 248 return null; 249 } 250 251 for (int i = 0; i < nodes.length; i++) { 252 EditorCookie ec = (EditorCookie) nodes[i].getCookie(EditorCookie.class); 253 254 if (ec != null) { 255 JEditorPane [] editorPanes = ec.getOpenedPanes(); 256 if ((editorPanes != null) && (editorPanes.length > 0)) { 257 int line = NbDocument.findLineNumber( 258 ec.getDocument(), 259 editorPanes[0].getCaret().getDot()); 260 LineCookie lc = (LineCookie) nodes[i]. 261 getCookie(LineCookie.class); 262 if (lc != null) { 263 Line l = lc.getLineSet().getCurrent(line); 264 if (l != null) 265 return l; 266 } 267 } 268 } 269 } 270 271 return null; 272 } 273 274 280 public static FileObject getFileObjectForFile(String filename) { 281 return FileUtil.toFileObject(FileUtil.normalizeFile(new File (filename))); 282 } 283 284 291 public static Line getLineByFile(FileObject fo, int lineno) { 292 DataObject dobj = null; 293 try { 294 dobj = DataObject.find(fo); 295 } catch (DataObjectNotFoundException e) { 296 LOGGER.log(Level.WARNING, 297 "No data object could be found for file object " + fo, e); 299 } 300 301 if (dobj == null) 302 return null; 303 304 try { 306 LineCookie lc = (LineCookie)dobj.getCookie(LineCookie.class); 307 if (lc != null) { 308 Line.Set ls = lc.getLineSet(); 309 if (ls != null) { 310 Line l = ls.getCurrent(lineno); 313 return l; 314 } 315 } 316 } catch (Exception e) { 317 LOGGER.log(Level.INFO, "failed", e); } 319 return null; 320 } 321 322 329 public static URL getExternalURLForLine(Line line) { 330 DataObject dobj = (DataObject) line.getLookup(). 331 lookup(DataObject.class); 332 URL url = null; 333 if (dobj != null) { 334 FileObject fo = dobj.getPrimaryFile(); 335 url = URLMapper.findURL(fo, URLMapper.EXTERNAL); 336 337 347 } 348 349 return url; 350 } 351 352 358 public static <T> List <T> filter(TreeAbstraction<T> t, UnaryFunction filter) { 359 List <T> r = new ArrayList <T>(); 360 filter(t, t.getRoot(), filter, r); 361 return r; 362 } 363 364 371 private static <T> void filter(TreeAbstraction<T> t, T node, 372 UnaryFunction filter, List <T> result) { 373 if (((Boolean ) filter.compute(node)).booleanValue()) 374 result.add(node); 375 for (int i = 0; i < t.getChildCount(node); i++) { 376 filter(t, t.getChild(node, i), filter, result); 377 } 378 } 379 380 386 public static <T> void processDepthFirst(TreeAbstraction<T> tree, 387 UnaryFunction f) { 388 processDepthFirst(tree, tree.getRoot(), f); 389 } 390 391 397 private static <T> void processDepthFirst(TreeAbstraction<T> tree, 398 T object, UnaryFunction f) { 399 for (int i = 0; i < tree.getChildCount(object); i++) { 400 processDepthFirst(tree, tree.getChild(object, i), f); 401 } 402 f.compute(object) ; 403 } 404 405 411 public static <T> void processBreadthFirst(TreeAbstraction<T> tree, 412 UnaryFunction f) { 413 processBreadthFirst(tree, tree.getRoot(), f); 414 } 415 416 422 private static <T> void processBreadthFirst(TreeAbstraction<T> tree, 423 T object, UnaryFunction f) { 424 f.compute(object); 425 for (int i = 0; i < tree.getChildCount(object); i++) { 426 processBreadthFirst(tree, tree.getChild(object, i), f); 427 } 428 } 429 430 436 public static long sum(long[] values) { 437 long r = 0; 438 for (int i = 0; i < values.length; i++) { 439 r += values[i]; 440 } 441 return r; 442 } 443 444 445 452 public static String toString(Object [] objs) { 453 StringBuilder sb = new StringBuilder (); 454 sb.append("["); 455 for (Object a: objs) { 456 if (sb.length() != 1) 457 sb.append(", "); 458 sb.append(a); 459 } 460 sb.append("]"); 461 return sb.toString(); 462 } 463 464 471 public static<T> int identityIndexOf(List <T> values, T value) { 472 for (int i = 0; i < values.size(); i++) { 473 if (values.get(i) == value) 474 return i; 475 } 476 return -1; 477 } 478 479 493 } 494 | Popular Tags |