1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.jface.viewers.IPostSelectionProvider; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 import org.eclipse.jface.viewers.SelectionChangedEvent; 18 import org.eclipse.ui.IPartListener; 19 import org.eclipse.ui.IViewPart; 20 import org.eclipse.ui.IWorkbenchPage; 21 import org.eclipse.ui.IWorkbenchPart; 22 23 30 public class PagePartSelectionTracker extends AbstractPartSelectionTracker 31 implements IPartListener, ISelectionChangedListener { 32 33 36 private IWorkbenchPage fPage; 37 38 41 private IWorkbenchPart fPart; 42 43 private ISelectionChangedListener selectionListener = new ISelectionChangedListener() { 44 public void selectionChanged(SelectionChangedEvent event) { 45 fireSelection(getPart(), event.getSelection()); 46 } 47 }; 48 49 private ISelectionChangedListener postSelectionListener = new ISelectionChangedListener() { 50 public void selectionChanged(SelectionChangedEvent event) { 51 firePostSelection(getPart(), event.getSelection()); 52 } 53 }; 54 55 public PagePartSelectionTracker(IWorkbenchPage page, String partId) { 56 super(partId); 57 setPage(page); 58 page.addPartListener(this); 59 IViewPart part = page.findView(partId); 60 if (part != null) { 61 setPart(part, false); 62 } 63 } 64 65 69 public void dispose() { 70 setPart(null, false); 71 setPage(null); 72 super.dispose(); 73 } 74 75 78 public void partActivated(IWorkbenchPart part) { 79 } 80 81 84 public void partBroughtToTop(IWorkbenchPart part) { 85 } 86 87 90 public void partClosed(IWorkbenchPart part) { 91 if (getPartId(part).equals(getPartId())) { 92 setPart(null, true); 93 } 94 } 95 96 99 public void partDeactivated(IWorkbenchPart part) { 100 } 101 102 105 public void partOpened(IWorkbenchPart part) { 106 if (getPartId(part).equals(getPartId())) { 107 setPart(part, true); 108 } 109 } 110 111 117 private Object getPartId(IWorkbenchPart part) { 118 String id = part.getSite().getId(); 119 if (part instanceof IViewPart) { 120 String secondaryId = ((IViewPart) part).getViewSite() 121 .getSecondaryId(); 122 if (secondaryId != null) { 123 id = id + ':' + secondaryId; 124 } 125 } 126 return id; 127 } 128 129 135 public void selectionChanged(SelectionChangedEvent event) { 136 fireSelection(getPart(), event.getSelection()); 137 } 138 139 144 private void setPage(IWorkbenchPage page) { 145 fPage = page; 146 } 147 148 153 protected IWorkbenchPage getPage() { 154 return fPage; 155 } 156 157 163 protected IWorkbenchPart getPart() { 164 return fPart; 165 } 166 167 170 public ISelection getSelection() { 171 IWorkbenchPart part = getPart(); 172 if (part != null) { 173 ISelectionProvider sp = part.getSite().getSelectionProvider(); 174 if (sp != null) { 175 return sp.getSelection(); 176 } 177 } 178 return null; 179 } 180 181 184 protected ISelectionProvider getSelectionProvider() { 185 IWorkbenchPart part = getPart(); 186 if (part != null) { 187 return part.getSite().getSelectionProvider(); 188 } 189 return null; 190 } 191 192 198 private void setPart(IWorkbenchPart part, boolean notify) { 199 if (fPart != null) { 200 ISelectionProvider sp = fPart.getSite().getSelectionProvider(); 202 if (sp != null) { 203 sp.removeSelectionChangedListener(selectionListener); 204 if (sp instanceof IPostSelectionProvider) { 205 ((IPostSelectionProvider) sp) 206 .removePostSelectionChangedListener(postSelectionListener); 207 } else { 208 sp.removeSelectionChangedListener(postSelectionListener); 209 } 210 } 211 } 212 fPart = part; 213 ISelection sel = null; 214 if (part != null) { 215 ISelectionProvider sp = part.getSite().getSelectionProvider(); 216 if (sp != null) { 217 sp.addSelectionChangedListener(selectionListener); 218 if (sp instanceof IPostSelectionProvider) { 219 ((IPostSelectionProvider) sp) 220 .addPostSelectionChangedListener(postSelectionListener); 221 } else { 222 sp.addSelectionChangedListener(postSelectionListener); 223 } 224 if (notify) { 225 sel = sp.getSelection(); 227 } 228 } 229 } 230 if (notify) { 231 fireSelection(part, sel); 232 firePostSelection(part, sel); 233 } 234 } 235 } 236 | Popular Tags |