1 7 8 package org.jdesktop.swing.decorator; 9 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 import java.util.EventListener ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import java.awt.Component ; 17 18 import javax.swing.event.EventListenerList ; 19 import javax.swing.event.TableModelEvent ; 20 import javax.swing.event.TableModelListener ; 21 22 29 public class HighlighterPipeline { 30 protected EventListenerList listenerList = new EventListenerList (); 31 32 private List highlighters; 33 private final static Highlighter nullHighlighter = new Highlighter(null, null); 34 35 public HighlighterPipeline(Highlighter[] inList) { 36 List copy = Arrays.asList((Highlighter[])inList.clone()); 39 highlighters = new ArrayList (copy.size()); 40 highlighters.addAll(copy); 41 42 reindexHighlighters(); 43 } 44 45 private void reindexHighlighters() { 46 Iterator iter = highlighters.iterator(); 47 int i = 0; 48 while (iter.hasNext()) { 49 Highlighter hl = (Highlighter)iter.next(); 50 hl.order = i++; 51 } 52 } 53 54 59 public void addHighlighter(Highlighter hl) { 60 addHighlighter(hl, false); 61 } 62 63 69 public void addHighlighter(Highlighter hl, boolean prepend) { 70 if (highlighters == null) { 71 highlighters = new ArrayList (); 72 } 73 if (prepend) { 74 highlighters.add(0, hl); 75 } else { 76 highlighters.add(highlighters.size(), hl); 77 } 78 reindexHighlighters(); 79 } 80 81 86 public void removeHighlighter(Highlighter hl) { 87 boolean success = highlighters.remove(hl); 88 } 90 91 public Highlighter[] getHighlighters() { 92 return (Highlighter[])highlighters.toArray(new Highlighter[highlighters.size()]); 93 } 94 95 public EventListener [] getListeners(Class listenerType) { 96 return listenerList.getListeners(listenerType); 97 } 98 99 105 public void addPipelineListener(PipelineListener l) { 106 listenerList.add(PipelineListener.class, l); 107 } 108 109 115 public void removePipelineListener(PipelineListener l) { 116 listenerList.remove(PipelineListener.class, l); 117 } 118 119 130 public PipelineListener[] getPipelineListeners() { 131 return (PipelineListener[]) listenerList.getListeners( 132 PipelineListener.class); 133 } 134 135 protected void fireContentsChanged(Object source) { 136 Object [] listeners = listenerList.getListenerList(); 137 PipelineEvent e = null; 138 139 for (int i = listeners.length - 2; i >= 0; i -= 2) { 140 if (listeners[i] == PipelineListener.class) { 141 if (e == null) { 142 e = new PipelineEvent(source, PipelineEvent.CONTENTS_CHANGED); 143 } 144 ( (PipelineListener) listeners[i + 1]).contentsChanged(e); 145 } 146 } 147 } 148 149 152 public Component apply(Component stamp, ComponentAdapter adapter) { 153 154 stamp = nullHighlighter.highlight(stamp, adapter); 156 Iterator iter = highlighters.iterator(); 157 while (iter.hasNext()) { 158 Highlighter hl = (Highlighter)iter.next(); 159 stamp = hl.highlight(stamp, adapter); 160 } 161 return stamp; 162 } 163 } 164 | Popular Tags |