1 19 20 package org.netbeans.modules.xml.xam.ui.highlight; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.WeakHashMap ; 30 import org.netbeans.modules.xml.xam.Component; 31 import org.openide.util.WeakListeners; 32 33 39 public abstract class HighlightManager { 40 41 private static final HighlightManager defaultInstance; 42 43 private List <Highlighted> listeners; 44 45 private List <HighlightGroup> groups; 46 47 private Map <Component, List <Highlighted>> componentListenerMap; 48 49 static { 50 defaultInstance = new DefaultHighlightManager(); 51 } 52 53 56 public HighlightManager() { 57 listeners = new ArrayList <Highlighted>(); 58 groups = new ArrayList <HighlightGroup>(); 59 componentListenerMap = new WeakHashMap <Component, List <Highlighted>>(); 60 } 61 62 67 public void addHighlightGroup(HighlightGroup group) { 68 synchronized (groups) { 69 groups.add(group); 70 } 71 showHighlights(group); 72 } 73 74 80 public void addHighlighted(Highlighted l) { 81 Highlighted wl = (Highlighted) 82 WeakListeners.create(Highlighted.class, l, this); 83 synchronized (listeners) { 84 listeners.add(wl); 85 } 86 synchronized (componentListenerMap) { 87 Set <Component> comps = wl.getComponents(); 88 if (comps != null) { 89 for (Component comp : comps) { 90 List <Highlighted> list = componentListenerMap.get(comp); 91 if (list == null) { 92 list = new LinkedList <Highlighted>(); 93 componentListenerMap.put(comp, list); 94 } 95 list.add(wl); 96 } 97 } 98 } 99 highlight(l); 100 } 101 102 109 protected Map <Highlighted, List <Highlight>> findListeners( 110 HighlightGroup group) { 111 Map <Highlighted, List <Highlight>> map = 112 new HashMap <Highlighted, List <Highlight>>(); 113 synchronized (componentListenerMap) { 114 Iterator <Highlight> iter = group.highlights().iterator(); 115 while (iter.hasNext()) { 116 Highlight h = iter.next(); 117 Component comp = h.getComponent(); 118 List <Highlighted> list = componentListenerMap.get(comp); 119 if (list != null) { 120 for (Highlighted l : list) { 121 List <Highlight> lights = map.get(l); 122 if (lights == null) { 123 lights = new ArrayList <Highlight>(); 124 map.put(l, lights); 125 } 126 lights.add(h); 127 } 128 } 129 } 130 } 131 return map; 132 } 133 134 139 public static HighlightManager getDefault(){ 140 return defaultInstance; 141 } 142 143 148 public List <HighlightGroup> getHighlightGroups(String type) { 149 List <HighlightGroup> list = new ArrayList <HighlightGroup>(); 150 synchronized (groups) { 151 if (groups.size() > 0) { 152 for (HighlightGroup group : groups) { 153 if (group.getType().equals(type)) { 154 list.add(group); 155 } 156 } 157 } 158 } 159 return list; 160 } 161 162 167 protected abstract void hideHighlights(HighlightGroup group); 168 169 175 protected void highlight(Highlighted listener) { 176 Set <Component> comps = listener.getComponents(); 177 if (comps != null && !comps.isEmpty()) { 178 synchronized (groups) { 179 for (HighlightGroup group : groups) { 180 if (group.isShowing()) { 181 Iterator <Highlight> iter = group.highlights().iterator(); 182 while (iter.hasNext()) { 183 Highlight h = iter.next(); 184 Component comp = h.getComponent(); 185 if (comps.contains(comp)) { 186 listener.highlightAdded(h); 187 } 188 } 189 } 190 } 191 } 192 } 193 } 194 195 200 public void removeHighlightGroup(HighlightGroup group) { 201 hideHighlights(group); 202 synchronized (groups) { 203 groups.remove(group); 204 } 205 } 206 207 216 public void removeHighlighted(Highlighted l) { 217 synchronized (listeners) { 219 listeners.remove(l); 220 } 221 synchronized (componentListenerMap) { 222 Set <Component> comps = l.getComponents(); 223 if (comps != null) { 224 for (Component comp : comps) { 225 List <Highlighted> list = componentListenerMap.get(comp); 226 if (list != null) { 227 list.remove(l); 228 } 229 } 230 } 231 } 232 } 233 234 239 protected abstract void showHighlights(HighlightGroup group); 240 } 241 | Popular Tags |