KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > ContextHelpPart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc HELP_KEY = "org.eclipse.ui.help"; //$NON-NLS-1$
70

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 JavaDoc defaultText = ""; //$NON-NLS-1$
82

83     private String JavaDoc id;
84     
85     private Font codeFont;
86
87     /**
88      * @param parent
89      * @param toolkit
90      * @param style
91      */

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 JavaDoc 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); //$NON-NLS-1$
122
String JavaDoc 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     /*
165      * (non-Javadoc)
166      *
167      * @see org.eclipse.help.ui.internal.views.IHelpPart#getControl()
168      */

169     public Control getControl() {
170         return getSection();
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.eclipse.help.ui.internal.views.IHelpPart#init(org.eclipse.help.ui.internal.views.NewReusableHelpPart)
177      */

178     public void init(ReusableHelpPart parent, String JavaDoc id, IMemento memento) {
179         this.parent = parent;
180         this.id = id;
181         parent.hookFormText(text);
182     }
183
184     public String JavaDoc getId() {
185         return id;
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see org.eclipse.help.ui.internal.views.IHelpPart#setVisible(boolean)
192      */

193     public void setVisible(boolean visible) {
194         getSection().setVisible(visible);
195     }
196
197     /**
198      * @return Returns the defaultText.
199      */

200     public String JavaDoc getDefaultText() {
201         return defaultText;
202     }
203
204     /**
205      * @param defaultText
206      * The defaultText to set.
207      */

208     public void setDefaultText(String JavaDoc defaultText) {
209         this.defaultText = defaultText;
210         if (text != null)
211             text.setText(defaultText, false, false);
212     }
213
214     private void doOpenLink(Object JavaDoc href) {
215         String JavaDoc sHref = (String JavaDoc)href;
216         if (sHref.startsWith("command://")) { //$NON-NLS-1$
217
doRunCommand(sHref.substring(10));
218         }
219         else {
220             parent.showURL(sHref);
221         }
222     }
223     
224     private void doRunCommand(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc computeDefaultSearchExpression(Control c) {
313         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
314         Composite parent = c.getParent();
315
316         while (parent != null) {
317             Object JavaDoc data = parent.getData();
318             if (data instanceof IWizardContainer) {
319                 IWizardContainer wc = (IWizardContainer) data;
320                 buff.append("\""); //$NON-NLS-1$
321
buff.append(wc.getCurrentPage().getTitle());
322                 buff.append("\" OR \""); //$NON-NLS-1$
323
buff.append(wc.getCurrentPage().getWizard().getWindowTitle());
324                 buff.append("\""); //$NON-NLS-1$
325
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("\""); //$NON-NLS-1$
333
if (part instanceof IViewPart)
334                             buff.append(NLS.bind(
335                                     Messages.ContextHelpPart_query_view, part
336                                             .getSite().getRegisteredName()));
337                         buff.append("\" "); //$NON-NLS-1$
338
}
339                     IPerspectiveDescriptor persp = page.getPerspective();
340                     if (persp != null) {
341                         if (buff.length() > 0)
342                             buff.append("OR "); //$NON-NLS-1$
343
buff.append("\""); //$NON-NLS-1$
344
buff.append(NLS.bind(
345                                 Messages.ContextHelpPart_query_perspective,
346                                 persp.getLabel()));
347                         buff.append("\""); //$NON-NLS-1$
348
}
349                 }
350                 break;
351             } else if (data instanceof Window) {
352                 Window w = (Window) data;
353                 if (w instanceof IPageChangeProvider) {
354                     Object JavaDoc page = ((IPageChangeProvider) w).getSelectedPage();
355                     String JavaDoc pageName = getPageName(c, page);
356                     if (pageName != null) {
357                         buff.append("\""); //$NON-NLS-1$
358
buff.append(pageName);
359                         buff.append("\" "); //$NON-NLS-1$
360
}
361                 }
362                 if (buff.length() > 0)
363                     buff.append("OR "); //$NON-NLS-1$
364
buff.append("\""); //$NON-NLS-1$
365
buff.append(w.getShell().getText());
366                 buff.append("\""); //$NON-NLS-1$
367
break;
368             }
369             parent = parent.getParent();
370         }
371         return buff.length() > 0 ? buff.toString().trim() : null;
372     }
373
374     private String JavaDoc getPageName(Control focusControl, Object JavaDoc 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 JavaDoc stripMnemonic(String JavaDoc 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 JavaDoc 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 JavaDoc createContextHelp(Control page) {
413         String JavaDoc text = null;
414         lastContext = null;
415         if (page != null) {
416             if (page != null /* && page.isVisible() */&& !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 JavaDoc contextId = null;
429         Control node = c;
430         do {
431             contextId = (String JavaDoc) 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 JavaDoc formatHelpContext(IContext context) {
443         String JavaDoc locale = Platform.getNL();
444         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
445         sbuf.append("<form>"); //$NON-NLS-1$
446
sbuf.append("<p>"); //$NON-NLS-1$
447
sbuf.append(decodeContextBoldTags(context));
448         sbuf.append("</p>"); //$NON-NLS-1$
449

450         ICommandLink[] commands = null;
451         if (context instanceof IContext3) {
452             commands = ((IContext3)context).getRelatedCommands();
453         }
454                 
455         String JavaDoc category = new String JavaDoc();
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=\""); //$NON-NLS-1$
464
sbuf.append(IHelpUIConstants.IMAGE_COMMAND_F1TOPIC);
465                     sbuf.append("\" indent=\"21\">"); //$NON-NLS-1$
466
sbuf.append("<a HREF=\"command://"); //$NON-NLS-1$
467
sbuf.append(commands[i].getSerialization());
468                     sbuf.append("\">"); //$NON-NLS-1$
469
sbuf.append(parent.escapeSpecialChars(commands[i].getLabel()));
470                     sbuf.append("</a>"); //$NON-NLS-1$
471
sbuf.append("</li>"); //$NON-NLS-1$
472
}
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 JavaDoc 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=\""); //$NON-NLS-1$
496
sbuf.append(IHelpUIConstants.IMAGE_FILE_F1TOPIC);
497                     sbuf.append("\" indent=\"21\">"); //$NON-NLS-1$
498
sbuf.append("<a HREF=\""); //$NON-NLS-1$
499
sbuf.append(link.getHref());
500                     String JavaDoc tcat = getTopicCategory(link.getHref(), locale);
501                     if (tcat != null && !Platform.getWS().equals(Platform.WS_GTK)) {
502                         sbuf.append("\" alt=\""); //$NON-NLS-1$
503
sbuf.append(parent.escapeSpecialChars(tcat));
504                     }
505                     sbuf.append("\">"); //$NON-NLS-1$
506
sbuf.append(parent.escapeSpecialChars(link.getLabel()));
507                     sbuf.append("</a>"); //$NON-NLS-1$
508
sbuf.append("</li>"); //$NON-NLS-1$
509
}
510             }
511         }
512         sbuf.append("</form>"); //$NON-NLS-1$
513
return sbuf.toString();
514     }
515
516     private void addCategory(StringBuffer JavaDoc sbuf, String JavaDoc category) {
517         if (category == null)
518             category = Messages.ContextHelpPart_seeAlso;
519         sbuf.append("<p><span color=\""); //$NON-NLS-1$
520
sbuf.append(IFormColors.TITLE);
521         sbuf.append("\">"); //$NON-NLS-1$
522
sbuf.append(category);
523         sbuf.append("</span></p>"); //$NON-NLS-1$
524
}
525
526     private String JavaDoc getTopicCategory(String JavaDoc href, String JavaDoc 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     /**
537      * Make sure to support the Help system bold tag. Help systen returns a
538      * regular string for getText(). Use internal apis for now to get bold.
539      *
540      * @param context
541      * @return
542      */

543     private String JavaDoc decodeContextBoldTags(IContext context) {
544         String JavaDoc 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 JavaDoc decodedString = styledText.replaceAll("<@#\\$b>", "<b>"); //$NON-NLS-1$ //$NON-NLS-2$
557
decodedString = decodedString.replaceAll("</@#\\$b>", "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
558
decodedString = parent.escapeSpecialChars(decodedString, true);
559         decodedString = decodedString.replaceAll("\r\n|\n|\r", "<br/>"); //$NON-NLS-1$ //$NON-NLS-2$
560
return decodedString;
561     }
562
563     public boolean setFormInput(Object JavaDoc input) {
564         if (input instanceof ContextHelpProviderInput) {
565             ContextHelpProviderInput chinput = (ContextHelpProviderInput) input;
566             //if (chinput.getContext() != null)
567
handleActivation(chinput.getProvider(), chinput.getContext(), chinput.getControl(),
568                         chinput.getPart());
569             //else
570
//handleActivation(chinput.getProvider(), chinput.getControl(),
571
// chinput.getPart());
572
return true;
573         }
574         return false;
575     }
576
577     public void setFocus() {
578         if (text != null)
579             text.setFocus();
580     }
581
582     /*
583      * (non-Javadoc)
584      *
585      * @see org.eclipse.help.ui.internal.views.IHelpPart#fillContextMenu(org.eclipse.jface.action.IMenuManager)
586      */

587     public boolean fillContextMenu(IMenuManager manager) {
588         return parent.fillFormContextMenu(text, manager);
589     }
590
591     /*
592      * (non-Javadoc)
593      *
594      * @see org.eclipse.help.ui.internal.views.IHelpPart#hasFocusControl(org.eclipse.swt.widgets.Control)
595      */

596     public boolean hasFocusControl(Control control) {
597         return text.equals(control);
598     }
599
600     public IAction getGlobalAction(String JavaDoc 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