1 11 package org.eclipse.help.ui.internal.views; 12 13 import org.eclipse.help.*; 14 import org.eclipse.help.ui.internal.*; 15 import org.eclipse.jface.dialogs.IPageChangeProvider; 16 import org.eclipse.jface.dialogs.IPageChangedListener; 17 import org.eclipse.jface.dialogs.PageChangedEvent; 18 import org.eclipse.jface.viewers.*; 19 import org.eclipse.swt.widgets.*; 20 import org.eclipse.ui.*; 21 import org.eclipse.ui.forms.HyperlinkGroup; 22 import org.eclipse.ui.forms.widgets.FormToolkit; 23 import org.eclipse.ui.part.ViewPart; 24 25 public class HelpView extends ViewPart implements IPartListener2, 26 ISelectionChangedListener, IPageChangedListener { 27 private FormToolkit toolkit; 28 29 private String firstPageId; 30 31 private ReusableHelpPart reusableHelpPart; 32 33 35 private IWorkbenchPart monitoredPart; 36 37 private boolean visible; 38 39 42 public HelpView() { 43 } 44 45 50 public void createPartControl(Composite parent) { 51 toolkit = new FormToolkit(parent.getDisplay()); 52 toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode( 53 HyperlinkGroup.UNDERLINE_HOVER); 54 toolkit.getColors().initializeSectionToolBarColors(); 56 reusableHelpPart.createControl(parent, toolkit); 57 reusableHelpPart.setDefaultContextHelpText(Messages.HelpView_defaultText); 58 reusableHelpPart.showPage(getFirstPage()); 59 IWorkbenchWindow window = PlatformUI.getWorkbench() 60 .getActiveWorkbenchWindow(); 61 if (window == null) 62 return; 63 IWorkbenchPage page = window.getActivePage(); 64 if (page == null) 65 return; 66 IWorkbenchPartReference aref = page.getActivePartReference(); 67 if (aref != null) 68 handlePartActivation(aref); 69 } 70 71 public void dispose() { 72 IWorkbenchWindow window = PlatformUI.getWorkbench() 73 .getActiveWorkbenchWindow(); 74 IPartService service = window.getPartService(); 75 if (monitoredPart != null) { 76 uninstallSelectionListener(monitoredPart); 77 uninstallPageListener(monitoredPart); 78 } 79 service.removePartListener(this); 80 if (reusableHelpPart != null) { 81 reusableHelpPart.dispose(); 82 reusableHelpPart = null; 83 } 84 if (toolkit != null) { 85 toolkit.dispose(); 86 toolkit = null; 87 } 88 super.dispose(); 89 } 90 91 public void init(IViewSite site, IMemento memento) throws PartInitException { 92 if (memento!=null) 93 this.firstPageId = memento.getString("pageId"); init(site); 95 reusableHelpPart = new ReusableHelpPart(site.getWorkbenchWindow(), 96 getHelpPartStyle()); 97 IActionBars actionBars = site.getActionBars(); 98 reusableHelpPart.init(actionBars, actionBars.getToolBarManager(), 99 actionBars.getStatusLineManager(), memento); 100 IWorkbenchWindow window = PlatformUI.getWorkbench() 101 .getActiveWorkbenchWindow(); 102 IPartService service = window.getPartService(); 103 service.addPartListener(this); 104 } 105 106 public void saveState(IMemento memento) { 107 if (reusableHelpPart!=null && memento!=null) { 108 String pageId = reusableHelpPart.getCurrentPageId(); 109 if (pageId!=null) 110 memento.putString("pageId", pageId); reusableHelpPart.saveState(memento); 112 } 113 } 114 115 private void handlePartActivation(IWorkbenchPartReference ref) { 116 if (reusableHelpPart == null) 117 return; 118 if (!visible || !reusableHelpPart.isMonitoringContextHelp()) 119 return; 120 if (isThisPart(ref)) 121 return; 122 IWorkbenchPart part = ref.getPart(false); 123 Display display = part.getSite().getShell().getDisplay(); 124 Control c = display.getFocusControl(); 125 if (c != null && c.isVisible() && !c.isDisposed()) { 126 IContextProvider provider = (IContextProvider) part 127 .getAdapter(IContextProvider.class); 128 if (provider != null) { 129 reusableHelpPart.update(provider, null, part, c); 130 if ((provider.getContextChangeMask() & IContextProvider.SELECTION) != 0) { 131 installSelectionListener(part); 133 } 134 } else 135 reusableHelpPart.update(part, c); 136 if (part instanceof IPageChangeProvider) 137 installPageListener(part); 138 } 139 } 140 141 private void installPageListener(IWorkbenchPart part) { 142 if (part instanceof IPageChangeProvider) 143 ((IPageChangeProvider)part).addPageChangedListener(this); 144 monitoredPart = part; 145 } 146 147 private void uninstallPageListener(IWorkbenchPart part) { 148 if (part instanceof IPageChangeProvider) 149 ((IPageChangeProvider)part).removePageChangedListener(this); 150 monitoredPart = null; 151 } 152 153 private void installSelectionListener(IWorkbenchPart part) { 154 ISelectionProvider provider = part.getSite().getSelectionProvider(); 155 if (provider instanceof IPostSelectionProvider) 156 ((IPostSelectionProvider) provider) 157 .addPostSelectionChangedListener(this); 158 else 159 provider.addSelectionChangedListener(this); 160 monitoredPart = part; 161 } 162 163 private void uninstallSelectionListener(IWorkbenchPart part) { 164 ISelectionProvider provider = part.getSite().getSelectionProvider(); 165 if (provider instanceof IPostSelectionProvider) 166 ((IPostSelectionProvider) provider) 167 .removePostSelectionChangedListener(this); 168 else if (provider != null) 169 provider.removeSelectionChangedListener(this); 170 monitoredPart = null; 171 } 172 173 private boolean isThisPart(IWorkbenchPartReference ref) { 174 IWorkbenchPart part = ref.getPart(false); 175 return part != null && part.equals(this); 176 } 177 178 private void updateActivePart() { 179 if (reusableHelpPart == null) 180 return; 181 if (!reusableHelpPart.isMonitoringContextHelp()) 182 return; 183 if (monitoredPart == null) 184 return; 185 Control c = monitoredPart.getSite().getShell().getDisplay() 186 .getFocusControl(); 187 if (c != null && c.isDisposed() == false && visible) { 188 IContextProvider provider = (IContextProvider) monitoredPart 189 .getAdapter(IContextProvider.class); 190 if (provider != null) 191 reusableHelpPart.update(provider, null, monitoredPart, c); 192 else 193 reusableHelpPart.update(monitoredPart, c); 194 } 195 } 196 197 private void handlePartDeactivation(IWorkbenchPartReference ref) { 198 IWorkbenchPart part = ref.getPart(false); 199 if (monitoredPart != null && part != null && part.equals(monitoredPart)) { 200 uninstallSelectionListener(part); 201 uninstallPageListener(part); 202 } 203 } 204 205 210 public void partActivated(final IWorkbenchPartReference partRef) { 211 if (isThisPart(partRef)) { 212 visible = true; 213 hook(true); 214 selectionChanged(null); 215 } else { 216 handlePartActivation(partRef); 217 } 218 } 219 220 225 public void partBroughtToTop(IWorkbenchPartReference partRef) { 226 if (isThisPart(partRef)) { 227 visible = true; 228 hook(true); 229 selectionChanged(null); 230 } 231 } 232 233 238 public void partClosed(IWorkbenchPartReference partRef) { 239 handlePartDeactivation(partRef); 240 } 241 242 247 public void partDeactivated(IWorkbenchPartReference partRef) { 248 handlePartDeactivation(partRef); 249 } 250 251 256 public void partHidden(IWorkbenchPartReference partRef) { 257 if (isThisPart(partRef)) { 258 visible = false; 259 hook(false); 260 } 261 } 262 263 268 public void partInputChanged(IWorkbenchPartReference partRef) { 269 } 270 271 276 public void partOpened(IWorkbenchPartReference partRef) { 277 if (isThisPart(partRef)) { 278 visible = true; 279 hook(true); 280 selectionChanged(null); 281 } 282 } 283 284 289 public void partVisible(IWorkbenchPartReference partRef) { 290 if (isThisPart(partRef)) { 291 visible = true; 292 hook(true); 293 selectionChanged(null); 294 } 295 } 296 297 private void hook(boolean doHook) { 298 if (doHook) { 299 IWorkbenchWindow window = PlatformUI.getWorkbench() 300 .getActiveWorkbenchWindow(); 301 IPartService service = window.getPartService(); 302 IWorkbenchPartReference aref = service.getActivePartReference(); 303 if (aref != null) 304 handlePartActivation(aref); 305 } else { 306 if (monitoredPart != null) { 307 uninstallSelectionListener(monitoredPart); 308 uninstallPageListener(monitoredPart); 309 } 310 } 311 } 312 313 318 public void selectionChanged(SelectionChangedEvent event) { 319 if (!visible) 320 return; 321 getSite().getShell().getDisplay().asyncExec(new Runnable () { 322 public void run() { 323 updateActivePart(); 324 } 325 }); 326 } 327 328 333 protected String getFirstPage() { 334 if (firstPageId!=null) 335 return firstPageId; 336 return IHelpUIConstants.HV_CONTEXT_HELP_PAGE; 337 } 338 339 public void displayContext(IContext context, IWorkbenchPart part, 340 Control control) { 341 if (reusableHelpPart != null) { 342 346 IHelpResource[] topics = context.getRelatedTopics(); 347 if (context.getText() != null || topics.length != 1) { 348 reusableHelpPart.showPage(IHelpUIConstants.HV_CONTEXT_HELP_PAGE); 350 IContextProvider provider = null; 352 if (part!=null) 353 provider = (IContextProvider) part 354 .getAdapter(IContextProvider.class); 355 if (provider != null) 356 reusableHelpPart.update(provider, context, part, control); 357 else 358 reusableHelpPart.update(context, part, control); 359 } 360 else { 361 reusableHelpPart.showURL(topics[0].getHref()); 362 } 363 } 364 } 365 366 371 protected int getHelpPartStyle() { 372 return ReusableHelpPart.getDefaultStyle(); 373 } 374 375 public void setFocus() { 376 if (reusableHelpPart != null) 377 reusableHelpPart.setFocus(); 378 } 379 380 public void startSearch(String phrase) { 381 if (reusableHelpPart != null) 382 reusableHelpPart.startSearch(phrase); 383 } 384 public void showDynamicHelp(IWorkbenchPart part, Control c) { 385 if (reusableHelpPart != null) 386 reusableHelpPart.showDynamicHelp(part, c); 387 } 388 389 public void pageChanged(PageChangedEvent event) { 390 if (!visible) 391 return; 392 updateActivePart(); 393 } 394 } 395 | Popular Tags |