1 11 package org.eclipse.help.ui.internal.views; 12 13 import org.eclipse.core.runtime.Platform; 14 import org.eclipse.help.HelpSystem; 15 import org.eclipse.help.ICommandLink; 16 import org.eclipse.help.IContext; 17 import org.eclipse.help.IContext2; 18 import org.eclipse.help.IContext3; 19 import org.eclipse.help.IContextProvider; 20 import org.eclipse.help.IHelpResource; 21 import org.eclipse.help.IToc; 22 import org.eclipse.help.ITopic; 23 import org.eclipse.help.UAContentFilter; 24 import org.eclipse.help.internal.HelpPlugin; 25 import org.eclipse.help.internal.base.HelpEvaluationContext; 26 import org.eclipse.help.ui.internal.ExecuteCommandAction; 27 import org.eclipse.help.ui.internal.HelpUIResources; 28 import org.eclipse.help.ui.internal.IHelpUIConstants; 29 import org.eclipse.help.ui.internal.Messages; 30 import org.eclipse.jface.action.IAction; 31 import org.eclipse.jface.action.IMenuManager; 32 import org.eclipse.jface.dialogs.IDialogPage; 33 import org.eclipse.jface.dialogs.IPageChangeProvider; 34 import org.eclipse.jface.resource.JFaceResources; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.jface.wizard.IWizardContainer; 37 import org.eclipse.osgi.util.NLS; 38 import org.eclipse.swt.custom.CTabFolder; 39 import org.eclipse.swt.custom.CTabItem; 40 import org.eclipse.swt.graphics.Font; 41 import org.eclipse.swt.graphics.FontData; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Display; 45 import org.eclipse.swt.widgets.TabFolder; 46 import org.eclipse.swt.widgets.TabItem; 47 import org.eclipse.ui.IMemento; 48 import org.eclipse.ui.IPerspectiveDescriptor; 49 import org.eclipse.ui.IViewPart; 50 import org.eclipse.ui.IWorkbenchPage; 51 import org.eclipse.ui.IWorkbenchPart; 52 import org.eclipse.ui.IWorkbenchWindow; 53 import org.eclipse.ui.actions.ActionFactory; 54 import org.eclipse.ui.forms.IFormColors; 55 import org.eclipse.ui.forms.SectionPart; 56 import org.eclipse.ui.forms.events.ExpansionAdapter; 57 import org.eclipse.ui.forms.events.ExpansionEvent; 58 import org.eclipse.ui.forms.events.HyperlinkEvent; 59 import org.eclipse.ui.forms.events.IHyperlinkListener; 60 import org.eclipse.ui.forms.widgets.FormText; 61 import org.eclipse.ui.forms.widgets.FormToolkit; 62 import org.eclipse.ui.forms.widgets.Section; 63 import org.eclipse.ui.forms.widgets.TableWrapData; 64 import org.eclipse.ui.forms.widgets.TableWrapLayout; 65 66 public class ContextHelpPart extends SectionPart implements IHelpPart { 67 private ReusableHelpPart parent; 68 69 private static final String HELP_KEY = "org.eclipse.ui.help"; 71 private FormText text; 72 73 private Control lastControl; 74 75 private IContextProvider lastProvider; 76 77 private IContext lastContext; 78 79 private IWorkbenchPart lastPart; 80 81 private String defaultText = ""; 83 private String id; 84 85 private Font codeFont; 86 87 92 public ContextHelpPart(Composite parent, FormToolkit toolkit) { 93 super(parent, toolkit, Section.EXPANDED | Section.TWISTIE 94 | Section.TITLE_BAR); 95 Section section = getSection(); 96 section.marginWidth = 5; 97 section.setText(Messages.ContextHelpPart_about); 98 Composite container = toolkit.createComposite(section); 99 section.setClient(container); 100 section.addExpansionListener(new ExpansionAdapter() { 101 public void expansionStateChanged(ExpansionEvent e) { 102 if (e.getState() 103 && (lastProvider != null || lastControl != null)) { 104 String helpText = createContextHelp(lastProvider, 105 lastControl); 106 updateText(helpText); 107 } 108 } 109 }); 110 TableWrapLayout layout = new TableWrapLayout(); 111 layout.topMargin = layout.bottomMargin = 0; 112 layout.leftMargin = layout.rightMargin = 0; 113 layout.verticalSpacing = 10; 114 container.setLayout(layout); 115 text = toolkit.createFormText(container, false); 116 text.setWhitespaceNormalized(false); 117 text.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB)); 118 text.setColor(IFormColors.TITLE, toolkit.getColors().getColor( 119 IFormColors.TITLE)); 120 codeFont = createCodeFont(parent.getDisplay(), parent.getFont(), JFaceResources.getTextFont()); 121 text.setFont("code", codeFont); String key = IHelpUIConstants.IMAGE_FILE_F1TOPIC; 123 text.setImage(key, HelpUIResources.getImage(key)); 124 key = IHelpUIConstants.IMAGE_COMMAND_F1TOPIC; 125 text.setImage(key, HelpUIResources.getImage(key)); 126 text.addHyperlinkListener(new IHyperlinkListener() { 127 public void linkActivated(HyperlinkEvent e) { 128 doOpenLink(e.getHref()); 129 } 130 131 public void linkEntered(HyperlinkEvent e) { 132 ContextHelpPart.this.parent.handleLinkEntered(e); 133 } 134 135 public void linkExited(HyperlinkEvent e) { 136 ContextHelpPart.this.parent.handleLinkExited(e); 137 } 138 }); 139 text.setText(defaultText, false, false); 140 } 141 142 private static Font createCodeFont(Display display, Font regularFont, Font textFont) { 143 FontData[] rfontData = regularFont.getFontData(); 144 FontData[] tfontData = textFont.getFontData(); 145 int height = 0; 146 147 for (int i=0; i<rfontData.length; i++) { 148 FontData data = rfontData[i]; 149 height = Math.max(height, data.getHeight()); 150 } 151 for (int i = 0; i < tfontData.length; i++) { 152 tfontData[i].setHeight(height); 153 } 154 return new Font(display, tfontData); 155 } 156 157 public void dispose() { 158 if (codeFont!=null) 159 codeFont.dispose(); 160 codeFont = null; 161 super.dispose(); 162 } 163 164 169 public Control getControl() { 170 return getSection(); 171 } 172 173 178 public void init(ReusableHelpPart parent, String id, IMemento memento) { 179 this.parent = parent; 180 this.id = id; 181 parent.hookFormText(text); 182 } 183 184 public String getId() { 185 return id; 186 } 187 188 193 public void setVisible(boolean visible) { 194 getSection().setVisible(visible); 195 } 196 197 200 public String getDefaultText() { 201 return defaultText; 202 } 203 204 208 public void setDefaultText(String defaultText) { 209 this.defaultText = defaultText; 210 if (text != null) 211 text.setText(defaultText, false, false); 212 } 213 214 private void doOpenLink(Object href) { 215 String sHref = (String )href; 216 if (sHref.startsWith("command://")) { doRunCommand(sHref.substring(10)); 218 } 219 else { 220 parent.showURL(sHref); 221 } 222 } 223 224 private void doRunCommand(String serialization) { 225 ExecuteCommandAction action = new ExecuteCommandAction(); 226 action.setInitializationString(serialization); 227 action.run(); 228 } 229 230 public void handleActivation(Control c, IWorkbenchPart part) { 231 if (text.isDisposed()) 232 return; 233 lastControl = c; 234 lastPart = part; 235 lastProvider = null; 236 String helpText = createContextHelp(c); 237 if (getSection().isExpanded()) 238 updateText(helpText); 239 updateDynamicHelp(false); 240 } 241 242 private void updateDynamicHelp(boolean explicitContext) { 243 if (explicitContext && lastContext instanceof IContext2) { 244 String title = ((IContext2)lastContext).getTitle(); 245 if (title!=null) { 246 updateDynamicHelp(stripMnemonic(title), lastControl); 247 return; 248 } 249 } 250 if (lastProvider != null || lastControl != null) 251 updateDynamicHelp(lastProvider != null ? lastProvider 252 .getSearchExpression(lastControl) : null, lastControl); 253 } 254 255 public void handleActivation(IContextProvider provider, IContext context, 256 Control c, 257 IWorkbenchPart part) { 258 if (text.isDisposed()) 259 return; 260 lastControl = c; 261 lastProvider = provider; 262 lastContext = context; 263 lastPart = part; 264 if (context==null && provider!=null) { 265 lastContext = provider.getContext(c); 266 } 267 String helpText; 268 if (lastContext!=null) 269 helpText = formatHelpContext(lastContext); 270 else 271 helpText = createContextHelp(c); 272 updateTitle(); 273 if (getSection().isExpanded()) 274 updateText(helpText); 275 updateDynamicHelp(context!=null); 276 } 277 278 private void updateTitle() { 279 String title = null; 280 if (lastContext != null && lastContext instanceof IContext2) { 281 IContext2 c2 = (IContext2)lastContext; 282 title = c2.getTitle(); 283 } 284 if (title==null && lastPart != null) 285 title = NLS.bind(Messages.ContextHelpPart_aboutP, lastPart 286 .getSite().getRegisteredName()); 287 if (title==null) 288 title = Messages.ContextHelpPart_about; 289 getSection().setText(title); 290 } 291 292 private void updateText(String helpText) { 293 text.setText(helpText != null ? helpText : defaultText, 294 helpText != null, 295 false); 296 getSection().layout(); 297 getManagedForm().reflow(true); 298 } 299 300 private void updateDynamicHelp(String expression, Control c) { 301 if (expression == null) { 302 expression = computeDefaultSearchExpression(c); 303 } 304 RelatedTopicsPart part = (RelatedTopicsPart) parent 305 .findPart(IHelpUIConstants.HV_RELATED_TOPICS); 306 if (part != null) { 307 if (expression != null) 308 part.startSearch(expression, lastContext); 309 } 310 } 311 312 private String computeDefaultSearchExpression(Control c) { 313 StringBuffer buff = new StringBuffer (); 314 Composite parent = c.getParent(); 315 316 while (parent != null) { 317 Object data = parent.getData(); 318 if (data instanceof IWizardContainer) { 319 IWizardContainer wc = (IWizardContainer) data; 320 buff.append("\""); buff.append(wc.getCurrentPage().getTitle()); 322 buff.append("\" OR \""); buff.append(wc.getCurrentPage().getWizard().getWindowTitle()); 324 buff.append("\""); break; 326 } else if (data instanceof IWorkbenchWindow) { 327 IWorkbenchWindow window = (IWorkbenchWindow) data; 328 IWorkbenchPage page = window.getActivePage(); 329 if (page != null) { 330 IWorkbenchPart part = lastPart; 331 if (part != null) { 332 buff.append("\""); if (part instanceof IViewPart) 334 buff.append(NLS.bind( 335 Messages.ContextHelpPart_query_view, part 336 .getSite().getRegisteredName())); 337 buff.append("\" "); } 339 IPerspectiveDescriptor persp = page.getPerspective(); 340 if (persp != null) { 341 if (buff.length() > 0) 342 buff.append("OR "); buff.append("\""); buff.append(NLS.bind( 345 Messages.ContextHelpPart_query_perspective, 346 persp.getLabel())); 347 buff.append("\""); } 349 } 350 break; 351 } else if (data instanceof Window) { 352 Window w = (Window) data; 353 if (w instanceof IPageChangeProvider) { 354 Object page = ((IPageChangeProvider) w).getSelectedPage(); 355 String pageName = getPageName(c, page); 356 if (pageName != null) { 357 buff.append("\""); buff.append(pageName); 359 buff.append("\" "); } 361 } 362 if (buff.length() > 0) 363 buff.append("OR "); buff.append("\""); buff.append(w.getShell().getText()); 366 buff.append("\""); break; 368 } 369 parent = parent.getParent(); 370 } 371 return buff.length() > 0 ? buff.toString().trim() : null; 372 } 373 374 private String getPageName(Control focusControl, Object page) { 375 if (page instanceof IDialogPage) 376 return ((IDialogPage) page).getTitle(); 377 if (focusControl == null) 378 return null; 379 380 Composite parent = focusControl.getParent(); 381 while (parent != null) { 382 if (parent instanceof TabFolder) { 383 TabItem[] selection = ((TabFolder) parent).getSelection(); 384 if (selection.length == 1) 385 return stripMnemonic(selection[0].getText()); 386 } else if (parent instanceof CTabFolder) { 387 CTabItem selection = ((CTabFolder) parent).getSelection(); 388 return stripMnemonic(selection.getText()); 389 } 390 parent = parent.getParent(); 391 } 392 return null; 393 } 394 395 private String stripMnemonic(String name) { 396 int loc = name.indexOf('&'); 397 if (loc!= -1) 398 return name.substring(0, loc)+name.substring(loc+1); 399 return name; 400 } 401 402 private String createContextHelp(IContextProvider provider, Control c) { 403 if (provider == null) 404 return createContextHelp(c); 405 lastContext = provider.getContext(c); 406 if (lastContext != null) { 407 return formatHelpContext(lastContext); 408 } 409 return null; 410 } 411 412 private String createContextHelp(Control page) { 413 String text = null; 414 lastContext = null; 415 if (page != null) { 416 if (page != null && !page.isDisposed()) { 417 IContext helpContext = findHelpContext(page); 418 if (helpContext != null) { 419 text = formatHelpContext(helpContext); 420 lastContext = helpContext; 421 } 422 } 423 } 424 return text; 425 } 426 427 public static IContext findHelpContext(Control c) { 428 String contextId = null; 429 Control node = c; 430 do { 431 contextId = (String ) node.getData(HELP_KEY); 432 if (contextId != null) 433 break; 434 node = node.getParent(); 435 } while (node != null); 436 if (contextId != null) { 437 return HelpSystem.getContext(contextId); 438 } 439 return null; 440 } 441 442 private String formatHelpContext(IContext context) { 443 String locale = Platform.getNL(); 444 StringBuffer sbuf = new StringBuffer (); 445 sbuf.append("<form>"); sbuf.append("<p>"); sbuf.append(decodeContextBoldTags(context)); 448 sbuf.append("</p>"); 450 ICommandLink[] commands = null; 451 if (context instanceof IContext3) { 452 commands = ((IContext3)context).getRelatedCommands(); 453 } 454 455 String category = new String (); 456 if (commands != null && commands.length > 0) { 457 for (int i=0;i<commands.length;++i) { 458 if (!UAContentFilter.isFiltered(commands[i], HelpEvaluationContext.getContext())) { 459 if (category != null) { 460 addCategory(sbuf, null); 461 } 462 category = null; 463 sbuf.append("<li style=\"image\" value=\""); sbuf.append(IHelpUIConstants.IMAGE_COMMAND_F1TOPIC); 465 sbuf.append("\" indent=\"21\">"); sbuf.append("<a HREF=\"command://"); sbuf.append(commands[i].getSerialization()); 468 sbuf.append("\">"); sbuf.append(parent.escapeSpecialChars(commands[i].getLabel())); 470 sbuf.append("</a>"); sbuf.append("</li>"); } 473 } 474 } 475 476 IHelpResource[] links = context.getRelatedTopics(); 477 if (links != null && context instanceof IContext2) { 478 ContextHelpSorter sorter = new ContextHelpSorter((IContext2)context); 479 sorter.sort(null, links); 480 } 481 if (links != null && links.length > 0) { 482 for (int i = 0; i < links.length; i++) { 483 IHelpResource link = links[i]; 484 if (!UAContentFilter.isFiltered(link, HelpEvaluationContext.getContext())) { 485 String cat = null; 486 if (context instanceof IContext2) { 487 cat = ((IContext2)context).getCategory(link); 488 } 489 if (cat == null && category != null || cat != null 490 && category == null || cat != null 491 && category != null && !cat.equals(category)) { 492 addCategory(sbuf, cat); 493 } 494 category = cat; 495 sbuf.append("<li style=\"image\" value=\""); sbuf.append(IHelpUIConstants.IMAGE_FILE_F1TOPIC); 497 sbuf.append("\" indent=\"21\">"); sbuf.append("<a HREF=\""); sbuf.append(link.getHref()); 500 String tcat = getTopicCategory(link.getHref(), locale); 501 if (tcat != null && !Platform.getWS().equals(Platform.WS_GTK)) { 502 sbuf.append("\" alt=\""); sbuf.append(parent.escapeSpecialChars(tcat)); 504 } 505 sbuf.append("\">"); sbuf.append(parent.escapeSpecialChars(link.getLabel())); 507 sbuf.append("</a>"); sbuf.append("</li>"); } 510 } 511 } 512 sbuf.append("</form>"); return sbuf.toString(); 514 } 515 516 private void addCategory(StringBuffer sbuf, String category) { 517 if (category == null) 518 category = Messages.ContextHelpPart_seeAlso; 519 sbuf.append("<p><span color=\""); sbuf.append(IFormColors.TITLE); 521 sbuf.append("\">"); sbuf.append(category); 523 sbuf.append("</span></p>"); } 525 526 private String getTopicCategory(String href, String locale) { 527 IToc[] tocs = HelpPlugin.getTocManager().getTocs(locale); 528 for (int i = 0; i < tocs.length; i++) { 529 ITopic topic = tocs[i].getTopic(href); 530 if (topic != null) 531 return tocs[i].getLabel(); 532 } 533 return null; 534 } 535 536 543 private String decodeContextBoldTags(IContext context) { 544 String styledText; 545 if (context instanceof IContext2) { 546 styledText = ((IContext2) context).getStyledText(); 547 if (styledText == null) { 548 styledText = context.getText(); 549 } 550 } else { 551 styledText = context.getText(); 552 } 553 if (styledText == null) { 554 return Messages.ContextHelpPart_noDescription; 555 } 556 String decodedString = styledText.replaceAll("<@#\\$b>", "<b>"); decodedString = decodedString.replaceAll("</@#\\$b>", "</b>"); decodedString = parent.escapeSpecialChars(decodedString, true); 559 decodedString = decodedString.replaceAll("\r\n|\n|\r", "<br/>"); return decodedString; 561 } 562 563 public boolean setFormInput(Object input) { 564 if (input instanceof ContextHelpProviderInput) { 565 ContextHelpProviderInput chinput = (ContextHelpProviderInput) input; 566 handleActivation(chinput.getProvider(), chinput.getContext(), chinput.getControl(), 568 chinput.getPart()); 569 return true; 573 } 574 return false; 575 } 576 577 public void setFocus() { 578 if (text != null) 579 text.setFocus(); 580 } 581 582 587 public boolean fillContextMenu(IMenuManager manager) { 588 return parent.fillFormContextMenu(text, manager); 589 } 590 591 596 public boolean hasFocusControl(Control control) { 597 return text.equals(control); 598 } 599 600 public IAction getGlobalAction(String id) { 601 if (id.equals(ActionFactory.COPY.getId())) 602 return parent.getCopyAction(); 603 return null; 604 } 605 606 public void stop() { 607 } 608 609 public void toggleRoleFilter() { 610 } 611 612 public void refilter() { 613 } 614 615 public void saveState(IMemento memento) { 616 } 617 } 618 | Popular Tags |