1 11 package org.eclipse.ui.internal; 12 13 import java.util.Hashtable ; 14 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.jface.viewers.IPostSelectionProvider; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.ISelectionChangedListener; 19 import org.eclipse.jface.viewers.ISelectionProvider; 20 import org.eclipse.jface.viewers.SelectionChangedEvent; 21 import org.eclipse.ui.INullSelectionListener; 22 import org.eclipse.ui.ISelectionListener; 23 import org.eclipse.ui.ISelectionService; 24 import org.eclipse.ui.IWorkbenchPart; 25 26 29 public abstract class AbstractSelectionService implements ISelectionService { 30 31 34 private ListenerList listeners = new ListenerList(); 35 36 39 private ListenerList postListeners = new ListenerList(); 40 41 44 private IWorkbenchPart activePart; 45 46 50 private ISelectionProvider activeProvider; 51 52 55 private Hashtable perPartTrackers; 56 57 60 private ISelectionChangedListener selListener = new ISelectionChangedListener() { 61 public void selectionChanged(SelectionChangedEvent event) { 62 fireSelection(activePart, event.getSelection()); 63 } 64 }; 65 66 69 private ISelectionChangedListener postSelListener = new ISelectionChangedListener() { 70 public void selectionChanged(SelectionChangedEvent event) { 71 firePostSelection(activePart, event.getSelection()); 72 } 73 }; 74 75 78 protected AbstractSelectionService() { 79 } 80 81 84 public void addSelectionListener(ISelectionListener l) { 85 listeners.add(l); 86 } 87 88 91 public void addSelectionListener(String partId, ISelectionListener listener) { 92 getPerPartTracker(partId).addSelectionListener(listener); 93 } 94 95 98 public void addPostSelectionListener(ISelectionListener l) { 99 postListeners.add(l); 100 } 101 102 105 public void addPostSelectionListener(String partId, 106 ISelectionListener listener) { 107 getPerPartTracker(partId).addPostSelectionListener(listener); 108 } 109 110 113 public void removeSelectionListener(ISelectionListener l) { 114 listeners.remove(l); 115 } 116 117 121 public void removePostSelectionListener(String partId, 122 ISelectionListener listener) { 123 getPerPartTracker(partId).removePostSelectionListener(listener); 124 } 125 126 129 public void removePostSelectionListener(ISelectionListener l) { 130 postListeners.remove(l); 131 } 132 133 137 public void removeSelectionListener(String partId, 138 ISelectionListener listener) { 139 getPerPartTracker(partId).removeSelectionListener(listener); 140 } 141 142 148 protected void fireSelection(final IWorkbenchPart part, final ISelection sel) { 149 Object [] array = listeners.getListeners(); 150 for (int i = 0; i < array.length; i++) { 151 final ISelectionListener l = (ISelectionListener) array[i]; 152 if ((part != null && sel != null) 153 || l instanceof INullSelectionListener) { 154 155 try { 156 l.selectionChanged(part, sel); 157 } catch (Exception e) { 158 WorkbenchPlugin.log(e); 159 } 160 } 161 } 162 } 163 164 170 protected void firePostSelection(final IWorkbenchPart part, 171 final ISelection sel) { 172 Object [] array = postListeners.getListeners(); 173 for (int i = 0; i < array.length; i++) { 174 final ISelectionListener l = (ISelectionListener) array[i]; 175 if ((part != null && sel != null) 176 || l instanceof INullSelectionListener) { 177 178 try { 179 l.selectionChanged(part, sel); 180 } catch (Exception e) { 181 WorkbenchPlugin.log(e); 182 } 183 } 184 } 185 } 186 187 193 protected AbstractPartSelectionTracker getPerPartTracker(String partId) { 194 if (perPartTrackers == null) { 195 perPartTrackers = new Hashtable (4); 196 } 197 AbstractPartSelectionTracker tracker = (AbstractPartSelectionTracker) perPartTrackers 198 .get(partId); 199 if (tracker == null) { 200 tracker = createPartTracker(partId); 201 perPartTrackers.put(partId, tracker); 202 } 203 return tracker; 204 } 205 206 212 protected abstract AbstractPartSelectionTracker createPartTracker( 213 String partId); 214 215 218 public ISelection getSelection() { 219 if (activeProvider != null) { 220 return activeProvider.getSelection(); 221 } else { 222 return null; 223 } 224 } 225 226 229 public ISelection getSelection(String partId) { 230 return getPerPartTracker(partId).getSelection(); 231 } 232 233 240 public void setActivePart(IWorkbenchPart newPart) { 241 if (newPart == activePart) { 243 return; 244 } 245 246 ISelectionProvider selectionProvider = null; 247 248 if (newPart != null) { 249 selectionProvider = newPart.getSite().getSelectionProvider(); 250 251 if (selectionProvider == null) { 252 newPart = null; 253 } 254 } 255 256 if (newPart == activePart) { 257 return; 258 } 259 260 if (activePart != null) { 261 if (activeProvider != null) { 262 activeProvider.removeSelectionChangedListener(selListener); 263 if (activeProvider instanceof IPostSelectionProvider) { 264 ((IPostSelectionProvider) activeProvider) 265 .removePostSelectionChangedListener(postSelListener); 266 } else { 267 activeProvider 268 .removeSelectionChangedListener(postSelListener); 269 } 270 activeProvider = null; 271 } 272 activePart = null; 273 } 274 275 activePart = newPart; 276 277 if (newPart != null) { 278 activeProvider = selectionProvider; 279 activeProvider.addSelectionChangedListener(selListener); 281 ISelection sel = activeProvider.getSelection(); 282 fireSelection(newPart, sel); 283 if (activeProvider instanceof IPostSelectionProvider) { 284 ((IPostSelectionProvider) activeProvider) 285 .addPostSelectionChangedListener(postSelListener); 286 } else { 287 activeProvider.addSelectionChangedListener(postSelListener); 288 } 289 firePostSelection(newPart, sel); 290 } else { 291 fireSelection(null, null); 292 firePostSelection(null, null); 293 } 294 } 295 296 400 } 401 | Popular Tags |