1 19 20 package org.netbeans.modules.languages.studio; 21 22 import org.netbeans.api.languages.ASTItem; 23 import org.netbeans.api.languages.Highlighting; 24 import org.openide.ErrorManager; 25 import org.openide.cookies.EditorCookie; 26 import org.openide.windows.TopComponent; 27 import java.awt.Color ; 28 import java.util.Iterator ; 29 import javax.swing.JEditorPane ; 30 import javax.swing.SwingUtilities ; 31 import javax.swing.text.AttributeSet ; 32 import javax.swing.text.BadLocationException ; 33 import javax.swing.text.Document ; 34 import javax.swing.text.SimpleAttributeSet ; 35 import javax.swing.text.StyleConstants ; 36 37 38 40 public class HighlighterSupport { 41 42 private Color color; 43 private Document highlightedDocument; 44 private ASTItem highlightedItem; 45 46 47 public HighlighterSupport (Color c) { 48 color = c; 49 } 50 51 public void highlight (Document doc, ASTItem item) { 52 removeHighlightIn (); 53 Highlighting.getHighlighting (highlightedDocument = doc).highlight ( 54 highlightedItem = item, 55 getHighlightAS () 56 ); 57 refresh (doc, item.getOffset ()); 58 } 59 60 private static void refresh (final Document doc, final int offset) { 61 SwingUtilities.invokeLater(new Runnable () { 62 public void run() { 63 Iterator it = TopComponent.getRegistry ().getOpened ().iterator (); 64 while (it.hasNext ()) { 65 TopComponent tc = (TopComponent) it.next (); 66 EditorCookie ec = (EditorCookie) tc.getLookup ().lookup (EditorCookie.class); 67 if (ec == null) continue; 68 JEditorPane [] eps = ec.getOpenedPanes (); 69 if (eps == null) continue; 70 int i, k = eps.length; 71 for (i = 0; i < k; i++) { 72 if (eps [i].getDocument () == doc) { 73 final JEditorPane ep = eps [i]; 74 try { 75 ep.scrollRectToVisible (ep.modelToView (offset)); 76 } catch (BadLocationException ex) { 77 ErrorManager.getDefault ().notify (ex); 78 } 79 ep.repaint (); 80 } 81 } 82 } 83 } 84 }); 85 } 86 87 private static void refresh (final Document doc) { 88 SwingUtilities.invokeLater(new Runnable () { 89 public void run() { 90 Iterator it = TopComponent.getRegistry ().getOpened ().iterator (); 91 while (it.hasNext ()) { 92 TopComponent tc = (TopComponent) it.next (); 93 EditorCookie ec = (EditorCookie) tc.getLookup ().lookup (EditorCookie.class); 94 if (ec == null) continue; 95 JEditorPane [] eps = ec.getOpenedPanes (); 96 if (eps == null) continue; 97 int i, k = eps.length; 98 for (i = 0; i < k; i++) { 99 if (eps [i].getDocument () == doc) { 100 final JEditorPane ep = eps [i]; 101 SwingUtilities.invokeLater (new Runnable () { 102 public void run () { 103 ep.repaint (); 104 } 105 }); 106 return; 107 } 108 } 109 } 110 } 111 }); 112 } 113 114 public void removeHighlight () { 115 Document doc = highlightedDocument; 116 removeHighlightIn (); 117 if (doc != null) 118 refresh (doc); 119 } 120 121 private void removeHighlightIn () { 122 if (highlightedItem != null) { 123 Highlighting.getHighlighting (highlightedDocument).removeHighlight 124 (highlightedItem); 125 highlightedItem = null; 126 highlightedDocument = null; 127 } 128 } 129 130 private AttributeSet highlightAS = null; 131 132 private AttributeSet getHighlightAS () { 133 if (highlightAS == null) { 134 SimpleAttributeSet as = new SimpleAttributeSet (); 135 as.addAttribute (StyleConstants.Background, color); 136 highlightAS = as; 137 } 138 return highlightAS; 139 } 140 141 } 142 | Popular Tags |