KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > parts > ContextHelpStandbyPart


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

11 package org.eclipse.ui.internal.intro.impl.parts;
12
13 import java.io.*;
14 import java.net.*;
15
16 import org.eclipse.help.*;
17 import org.eclipse.help.internal.context.*;
18 import org.eclipse.jface.resource.*;
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.events.*;
21 import org.eclipse.swt.layout.*;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.ui.*;
24 import org.eclipse.ui.forms.*;
25 import org.eclipse.ui.forms.events.*;
26 import org.eclipse.ui.forms.widgets.*;
27 import org.eclipse.ui.help.*;
28 import org.eclipse.ui.internal.intro.impl.*;
29 import org.eclipse.ui.internal.intro.impl.util.*;
30 import org.eclipse.ui.intro.*;
31 import org.eclipse.ui.intro.config.*;
32
33 /**
34  *
35  */

36 public class ContextHelpStandbyPart implements IStandbyContentPart {
37
38     private ScrolledForm form;
39     private IPartListener2 partListener;
40     private Label title;
41     private Text phraseText;
42     private FormText text;
43     private String JavaDoc defaultText;
44     private static final String JavaDoc HELP_KEY = "org.eclipse.ui.help"; //$NON-NLS-1$
45

46     // private StandbyPart standbyPart;
47
class PartListener implements IPartListener2 {
48
49         /*
50          * (non-Javadoc)
51          *
52          * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
53          */

54         public void partActivated(IWorkbenchPartReference ref) {
55             handlePartActivation(ref, true);
56         }
57
58         /*
59          * (non-Javadoc)
60          *
61          * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
62          */

63         public void partBroughtToTop(IWorkbenchPartReference ref) {
64         }
65
66         /*
67          * (non-Javadoc)
68          *
69          * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
70          */

71         public void partClosed(IWorkbenchPartReference ref) {
72             handlePartActivation(ref, false);
73         }
74
75         /*
76          * (non-Javadoc)
77          *
78          * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
79          */

80         public void partDeactivated(IWorkbenchPartReference ref) {
81         }
82
83         /*
84          * (non-Javadoc)
85          *
86          * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
87          */

88         public void partHidden(IWorkbenchPartReference ref) {
89         }
90
91         /*
92          * (non-Javadoc)
93          *
94          * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
95          */

96         public void partInputChanged(IWorkbenchPartReference ref) {
97         }
98
99         /*
100          * (non-Javadoc)
101          *
102          * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
103          */

104         public void partOpened(IWorkbenchPartReference ref) {
105         }
106
107         /*
108          * (non-Javadoc)
109          *
110          * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
111          */

112         public void partVisible(IWorkbenchPartReference ref) {
113         }
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.eclipse.ui.internal.intro.impl.parts.IStandbyContentPart#init(org.eclipse.ui.intro.IIntroPart)
120      */

121     public void init(IIntroPart introPart, IMemento memento) {
122         partListener = new PartListener();
123         defaultText = IntroPlugin
124                 .getString("ContextHelpStandbyPart.defaultText"); //$NON-NLS-1$
125
ImageUtil.registerImage(ImageUtil.HELP_TOPIC, "help_topic.gif"); //$NON-NLS-1$
126
}
127
128     public void createPartControl(Composite parent, FormToolkit toolkit) {
129         // parent form
130
form = toolkit.createScrolledForm(parent);
131         TableWrapLayout layout = new TableWrapLayout();
132         form.getBody().setLayout(layout);
133         //Util.highlight(form.getBody(), SWT.COLOR_YELLOW);
134
// help container. Has three colums (search, text, go)
135
Composite helpContainer = toolkit.createComposite(form.getBody());
136         GridLayout glayout = new GridLayout();
137         glayout.numColumns = 3;
138         glayout.marginWidth = glayout.marginHeight = 1;
139         helpContainer.setLayout(glayout);
140         helpContainer.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
141         toolkit.paintBordersFor(helpContainer);
142         Label label = toolkit.createLabel(helpContainer, IntroPlugin
143                 .getString("ContextHelpStandbyPart.search")); //$NON-NLS-1$
144
label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
145         phraseText = toolkit.createText(helpContainer, ""); //$NON-NLS-1$
146
phraseText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
147         final Button button = toolkit.createButton(helpContainer, IntroPlugin
148                 .getString("ContextHelpStandbyPart.button.go"), SWT.PUSH); //$NON-NLS-1$
149
button.addSelectionListener(new SelectionAdapter() {
150
151             public void widgetSelected(SelectionEvent e) {
152                 doSearch(phraseText.getText());
153             }
154         });
155         button.setEnabled(false);
156         phraseText.addModifyListener(new ModifyListener() {
157
158             public void modifyText(ModifyEvent e) {
159                 String JavaDoc text = phraseText.getText();
160                 button.setEnabled(text.length() > 0);
161             }
162         });
163         phraseText.addKeyListener(new KeyAdapter() {
164
165             public void keyReleased(KeyEvent e) {
166                 if (e.character == '\r') {
167                     if (button.isEnabled())
168                         doSearch(phraseText.getText());
169                 }
170             }
171         });
172         title = toolkit.createLabel(form.getBody(), null, SWT.WRAP);
173         title.setText(IntroPlugin
174                 .getString("ContextHelpStandbyPart.contextHelpArea.Title")); //$NON-NLS-1$
175
title.setFont(JFaceResources.getHeaderFont());
176         title.setForeground(toolkit.getColors().getColor(FormColors.TITLE));
177         text = toolkit.createFormText(form.getBody(), true);
178         text.setImage(ImageUtil.HELP_TOPIC, ImageUtil
179                 .getImage(ImageUtil.HELP_TOPIC));
180         text.addHyperlinkListener(new HyperlinkAdapter() {
181
182             public void linkActivated(HyperlinkEvent e) {
183                 openLink(e.getHref());
184             }
185         });
186         text.setLayoutData(new TableWrapData(TableWrapData.FILL,
187                 TableWrapData.FILL));
188         text.setText(defaultText, false, false);
189         IWorkbenchWindow window = PlatformUI.getWorkbench()
190                 .getActiveWorkbenchWindow();
191         IPartService service = window.getPartService();
192         service.addPartListener(partListener);
193         toolkit.paintBordersFor(form.getBody());
194     }
195
196     public Control getControl() {
197         return form;
198     }
199
200     private void doSearch(String JavaDoc phrase) {
201         try {
202             String JavaDoc ephrase = URLEncoder.encode(phrase, "UTF-8"); //$NON-NLS-1$
203
String JavaDoc query = "tab=search&searchWord=" + ephrase; //$NON-NLS-1$
204
WorkbenchHelp.displayHelpResource(query);
205         } catch (UnsupportedEncodingException e) {
206             System.out.println(e);
207         }
208     }
209
210     private void handlePartActivation(IWorkbenchPartReference ref,
211             boolean activated) {
212         if (text.isDisposed())
213             return;
214         IWorkbenchPart part = ref.getPart(false);
215         String JavaDoc partId = part.getSite().getId();
216         // Ignore ourselves
217
if (partId.equals("org.eclipse.ui.internal.introview")) //$NON-NLS-1$
218
return;
219         if (activated) {
220             title.setText(IntroPlugin
221                     .getString("ContextHelpStandbyPart.whatIsArea.Title") //$NON-NLS-1$
222
+ " \"" + part.getSite().getRegisteredName() + "\"?"); //$NON-NLS-1$ //$NON-NLS-2$
223
String JavaDoc helpText = createContextHelp(part);
224             text.setText(helpText != null ? helpText : "", helpText != null, //$NON-NLS-1$
225
false);
226         } else {
227             title.setText(IntroPlugin
228                     .getString("ContextHelpStandbyPart.contextHelpArea.Title")); //$NON-NLS-1$
229
text.setText(defaultText, false, false);
230         }
231         form.getBody().layout();
232         form.reflow(true);
233     }
234
235     private String JavaDoc createContextHelp(IWorkbenchPart part) {
236         String JavaDoc text = null;
237         if (part != null) {
238             Display display = part.getSite().getShell().getDisplay();
239             Control c = display.getFocusControl();
240             if (c != null && c.isVisible() && !c.isDisposed()) {
241                 IContext helpContext = findHelpContext(c);
242                 if (helpContext != null) {
243                     text = formatHelpContext(helpContext);
244                 }
245             }
246         }
247         return text;
248     }
249
250     private IContext findHelpContext(Control c) {
251         String JavaDoc contextId = null;
252         Control node = c;
253         do {
254             contextId = (String JavaDoc) node.getData(HELP_KEY);
255             if (contextId != null)
256                 break;
257             node = node.getParent();
258         } while (node != null);
259         if (contextId != null) { return HelpSystem.getContext(contextId); }
260         return null;
261     }
262
263     private String JavaDoc formatHelpContext(IContext context) {
264         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
265         sbuf.append("<form>"); //$NON-NLS-1$
266
sbuf.append("<p>"); //$NON-NLS-1$
267
sbuf.append(decodeContextBoldTags(context));
268         sbuf.append("</p>"); //$NON-NLS-1$
269
IHelpResource[] links = context.getRelatedTopics();
270         if (links.length > 0) {
271             for (int i = 0; i < links.length; i++) {
272                 IHelpResource link = links[i];
273                 sbuf.append("<li style=\"text\" indent=\"2\">"); //$NON-NLS-1$
274
sbuf.append("<img HREF=\""); //$NON-NLS-1$
275
sbuf.append(ImageUtil.HELP_TOPIC);
276                 sbuf.append("\"/> "); //$NON-NLS-1$
277
sbuf.append("<a HREF=\""); //$NON-NLS-1$
278
sbuf.append(link.getHref());
279                 sbuf.append("\">"); //$NON-NLS-1$
280
sbuf.append(link.getLabel());
281                 sbuf.append("</a>"); //$NON-NLS-1$
282
sbuf.append("</li>"); //$NON-NLS-1$
283
}
284         }
285         sbuf.append("</form>"); //$NON-NLS-1$
286
return sbuf.toString();
287     }
288
289     /**
290      * Make sure to support the Help system bold tag. Help systen returns a
291      * regular string for getText(). Use internal apis for now to get bold.
292      *
293      * @param context
294      * @return
295      */

296     private String JavaDoc decodeContextBoldTags(IContext context) {
297         String JavaDoc styledText;
298         if (context instanceof IStyledContext) {
299             styledText = ((IStyledContext) context).getStyledText();
300         } else {
301             styledText = context.getText();
302         }
303         String JavaDoc decodedString = styledText.replaceAll("<@#\\$b>", "<b>"); //$NON-NLS-1$ //$NON-NLS-2$
304
decodedString = decodedString.replaceAll("</@#\\$b>", "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
305
return decodedString;
306     }
307
308     private void openLink(Object JavaDoc href) {
309         String JavaDoc url = (String JavaDoc) href;
310         if (url != null)
311             WorkbenchHelp.displayHelpResource(url);
312     }
313
314     public void dispose() {
315         IWorkbenchWindow window = PlatformUI.getWorkbench()
316                 .getActiveWorkbenchWindow();
317         if (window == null)
318             return;
319         IPartService service = window.getPartService();
320         if (service == null)
321             return;
322         service.removePartListener(partListener);
323     }
324
325     /*
326      * (non-Javadoc)
327      *
328      * @see org.eclipse.ui.internal.intro.impl.parts.IStandbyContentPart#setFocus()
329      */

330     public void setFocus() {
331         phraseText.setFocus();
332     }
333
334     /*
335      * (non-Javadoc)
336      *
337      * @see org.eclipse.ui.internal.intro.impl.parts.IStandbyContentPart#setInput(java.lang.Object)
338      */

339     public void setInput(Object JavaDoc input) {
340         // does nothing.
341
}
342
343     public void saveState(IMemento memento) {
344     }
345 }
Popular Tags