1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.ListenerList; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.jface.util.SafeRunnable; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.ui.INullSelectionListener; 18 import org.eclipse.ui.ISelectionListener; 19 import org.eclipse.ui.IWorkbenchPart; 20 21 24 public abstract class AbstractPartSelectionTracker { 25 28 private ListenerList fListeners = new ListenerList(); 29 30 33 private ListenerList postListeners = new ListenerList(); 34 35 38 private String fPartId; 39 40 45 public AbstractPartSelectionTracker(String partId) { 46 setPartId(partId); 47 } 48 49 54 public void addSelectionListener(ISelectionListener listener) { 55 fListeners.add(listener); 56 } 57 58 63 public void addPostSelectionListener(ISelectionListener listener) { 64 postListeners.add(listener); 65 } 66 67 71 public abstract ISelection getSelection(); 72 73 78 public void removeSelectionListener(ISelectionListener listener) { 79 fListeners.remove(listener); 80 } 81 82 87 public void removePostSelectionListener(ISelectionListener listener) { 88 postListeners.remove(listener); 89 } 90 91 94 public void dispose() { 95 synchronized (fListeners) { 96 Object [] listeners = fListeners.getListeners(); 97 for (int i = 0; i < listeners.length; i++) { 98 fListeners.remove(listeners[i]); 99 postListeners.remove(listeners[i]); 100 } 101 } 102 } 103 104 111 protected void fireSelection(final IWorkbenchPart part, final ISelection sel) { 112 Object [] array = fListeners.getListeners(); 113 for (int i = 0; i < array.length; i++) { 114 final ISelectionListener l = (ISelectionListener) array[i]; 115 if ((part != null && sel != null) 116 || l instanceof INullSelectionListener) { 117 Platform.run(new SafeRunnable() { 118 public void run() { 119 l.selectionChanged(part, sel); 120 } 121 }); 122 } 123 } 124 } 125 126 133 protected void firePostSelection(final IWorkbenchPart part, 134 final ISelection sel) { 135 Object [] array = postListeners.getListeners(); 136 for (int i = 0; i < array.length; i++) { 137 final ISelectionListener l = (ISelectionListener) array[i]; 138 if ((part != null && sel != null) 139 || l instanceof INullSelectionListener) { 140 Platform.run(new SafeRunnable() { 141 public void run() { 142 l.selectionChanged(part, sel); 143 } 144 }); 145 } 146 } 147 } 148 149 154 private void setPartId(String partId) { 155 fPartId = partId; 156 } 157 158 163 protected String getPartId() { 164 return fPartId; 165 } 166 167 } 168 | Popular Tags |