1 19 package org.netbeans.modules.editor.highlights; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.SortedSet ; 28 import java.util.TreeSet ; 29 import javax.swing.text.Document ; 30 import org.netbeans.editor.Coloring; 31 import org.netbeans.editor.DrawContext; 32 import org.netbeans.editor.DrawLayer; 33 import org.netbeans.editor.MarkFactory; 34 import org.netbeans.modules.editor.highlights.spi.*; 35 import org.openide.ErrorManager; 36 37 41 final class HighlightLayer extends DrawLayer.AbstractLayer { 42 43 44 public HighlightLayer() { 45 super("highlight-layer"); this.initialized = false; 47 type2Highlights = new HashMap (); 48 fakeHighlight = new FakeHighlight(); 49 } 50 51 private Map type2Highlights; 52 53 private Highlight fakeHighlight; 54 private int fakePosition; 55 56 public static final int VISIBILITY = 3000; 57 58 private boolean initialized = false; 59 60 private void checkDocument(Document doc) { 61 } 62 63 public boolean extendsEOL() { 64 return true; 65 } 66 67 public synchronized void init(final DrawContext ctx) { 68 if (!initialized) { 69 Document doc = ctx.getEditorUI().getDocument(); 70 71 initialized = true; 72 checkDocument(doc); 73 } 74 75 if (isActive()) 76 setNextActivityChangeOffset(0); 77 } 78 79 private boolean isActive() { 80 return true; 81 } 82 83 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 84 if (isActive()) { 85 int currentOffset = ctx.getFragmentOffset(); 86 List highlights = getHighlightsForOffset(currentOffset); 87 int nextActivity = Integer.MAX_VALUE; 88 89 for (Iterator i = highlights.iterator(); i.hasNext(); ) { 91 Highlight h = (Highlight) i.next(); 92 93 if (h.getStart() <= currentOffset) { 94 if (nextActivity > h.getEnd()) { 95 nextActivity = h.getEnd(); 96 } 97 } else { 98 if (nextActivity > h.getStart()) { 99 nextActivity = h.getStart(); 100 } 101 } 102 } 103 104 if (nextActivity == currentOffset) { 105 nextActivity++; 106 } 107 108 setNextActivityChangeOffset(nextActivity); 109 return true; 110 } 111 112 return false; 113 } 114 115 public void updateContext(DrawContext ctx) { 116 if (!isActive()) 117 return ; 118 119 int currentOffset = ctx.getFragmentOffset(); 120 List highlights = getHighlightsForOffset(currentOffset); 121 int nextActivity = Integer.MAX_VALUE; 122 int starts = Integer.MAX_VALUE; 123 boolean applied = false; 124 125 for (Iterator i = highlights.iterator(); i.hasNext(); ) { 127 Highlight h = (Highlight) i.next(); 128 129 if (h.getStart() <= currentOffset && currentOffset < h.getEnd()) { 130 h.getColoring().apply(ctx); 131 applied = true; 132 } 133 134 if (nextActivity > h.getEnd()) { 135 nextActivity = h.getEnd(); 136 } 137 138 if (starts > h.getStart()) { 139 starts = h.getStart(); 140 } 141 } 142 143 if (!applied && starts < nextActivity) 144 nextActivity = starts; 145 146 setNextActivityChangeOffset(nextActivity); 147 } 148 149 public synchronized void setHighlights(String type, Collection highlights) { 150 SortedSet target = (SortedSet ) type2Highlights.get(type); 151 152 if (target == null) { 153 type2Highlights.put(type, target = new TreeSet (new HighlightComparator())); 154 } else { 155 target.clear(); 156 } 157 158 for (Iterator i = highlights.iterator(); i.hasNext(); ) { 160 Highlight h = (Highlight) i.next(); 161 162 if (h != null) { 163 target.add(h); 164 } else { 165 ErrorManager.getDefault().log(ErrorManager.WARNING, "Got a null highlight, type: " + type); } 167 } 168 } 169 170 private synchronized List getHighlightsForOffset(int offset) { 171 List highlights = new ArrayList (); 172 173 fakePosition = offset; 174 175 for (Iterator i = type2Highlights.keySet().iterator(); i.hasNext(); ) { 177 String type = (String ) i.next(); 178 179 SortedSet tail = ((SortedSet ) type2Highlights.get(type)).tailSet(fakeHighlight); 180 181 if (!tail.isEmpty()) { 182 Highlight h = (Highlight) tail.first(); 183 184 highlights.add(h); 185 } 186 } 187 188 return highlights; 189 } 190 191 private final class FakeHighlight implements Highlight { 192 193 public int getStart() { 194 return -1; 195 } 196 197 public int getEnd() { 198 return fakePosition; 199 } 200 201 public Coloring getColoring() { 202 return null; 203 } 204 205 } 206 } 207 | Popular Tags |