KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.help.HelpSystem;
17 import org.eclipse.help.IHelpResource;
18 import org.eclipse.help.IToc;
19 import org.eclipse.help.search.ISearchEngineResult;
20 import org.eclipse.help.ui.internal.HelpUIPlugin;
21 import org.eclipse.help.ui.internal.HelpUIResources;
22 import org.eclipse.help.ui.internal.IHelpUIConstants;
23 import org.eclipse.help.ui.internal.Messages;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.Separator;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.ScrollBar;
39 import org.eclipse.ui.IMemento;
40 import org.eclipse.ui.actions.ActionFactory;
41 import org.eclipse.ui.forms.AbstractFormPart;
42 import org.eclipse.ui.forms.widgets.FormText;
43 import org.eclipse.ui.forms.widgets.FormToolkit;
44 import org.eclipse.ui.forms.widgets.ScrolledForm;
45 import org.eclipse.ui.forms.widgets.TableWrapData;
46 import org.eclipse.ui.forms.widgets.TableWrapLayout;
47
48 public class SearchResultsPart extends AbstractFormPart implements IHelpPart {
49     private static final String JavaDoc S_DESCRIPTION_OFF = "no-description"; //$NON-NLS-1$
50
private static final String JavaDoc S_SHOW_CATEGORIES = "show-categories"; //$NON-NLS-1$
51
ReusableHelpPart parent;
52
53     private Composite separator;
54
55     private Composite container;
56
57     private ScrolledForm innerForm;
58
59     private String JavaDoc id;
60
61     //private Action removeAllAction;
62

63     private Action showCategoriesAction;
64
65     private Action showDescriptionAction;
66
67     private ArrayList JavaDoc results;
68
69     //private String phrase;
70

71     private FormToolkit innerToolkit;
72
73     /**
74      * @param parent
75      * @param toolkit
76      * @param style
77      */

78     public SearchResultsPart(Composite parent, FormToolkit toolkit,
79             IToolBarManager tbm) {
80         GridLayout layout = new GridLayout();
81         layout.marginWidth = layout.marginHeight = 0;
82         layout.verticalSpacing = 0;
83         innerToolkit = new FormToolkit(parent.getDisplay());
84         innerToolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(
85                 toolkit.getHyperlinkGroup().getHyperlinkUnderlineMode());
86         container = innerToolkit.createComposite(parent);
87         container.setLayout(layout);
88         separator = innerToolkit.createCompositeSeparator(container);
89         separator.setVisible(false);
90         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
91         gd.heightHint = 1;
92         separator.setLayoutData(gd);
93         innerForm = innerToolkit.createScrolledForm(container);
94         innerForm.setDelayedReflow(true);
95         innerForm.setLayoutData(new GridData(GridData.FILL_BOTH));
96         final ScrollBar scrollBar = innerForm.getVerticalBar();
97         scrollBar.addSelectionListener(new SelectionAdapter() {
98             public void widgetSelected(SelectionEvent e) {
99                 updateSeparatorVisibility();
100             }
101         });
102         TableWrapLayout tlayout = new TableWrapLayout();
103         tlayout.topMargin = 0;
104         tlayout.bottomMargin = 0;
105         innerForm.getBody().setLayout(tlayout);
106         results = new ArrayList JavaDoc();
107         contributeToToolBar(tbm);
108     }
109     
110     void updateSeparatorVisibility() {
111         ScrollBar scrollBar = innerForm.getVerticalBar();
112         separator.setVisible(scrollBar.getSelection()>0);
113     }
114
115     private void contributeToToolBar(IToolBarManager tbm) {
116         /*
117          * removeAllAction = new Action() { public void run() { clearResults(); } };
118          * removeAllAction.setImageDescriptor(HelpUIResources
119          * .getImageDescriptor(IHelpUIConstants.IMAGE_REMOVE_ALL));
120          * removeAllAction.setToolTipText("Remove all hits");
121          * removeAllAction.setId("removeAll"); tbm.insertBefore("back",
122          * removeAllAction); tbm.insertAfter("removeAll", new Separator());
123          */

124
125         IDialogSettings settings = HelpUIPlugin.getDefault().getDialogSettings();
126         boolean descOff = settings.getBoolean(S_DESCRIPTION_OFF);
127         boolean showCategories = settings.getBoolean(S_SHOW_CATEGORIES);
128         showCategoriesAction = new Action() {
129             public void run() {
130                 updateResultSections();
131                 HelpUIPlugin.getDefault().getDialogSettings().put(S_SHOW_CATEGORIES, showCategoriesAction.isChecked());
132             }
133         };
134         showCategoriesAction.setImageDescriptor(HelpUIResources
135                 .getImageDescriptor(IHelpUIConstants.IMAGE_SHOW_CATEGORIES));
136         showCategoriesAction.setChecked(showCategories);
137         showCategoriesAction.setToolTipText(Messages.SearchResultsPart_showCategoriesAction_tooltip);
138         showCategoriesAction.setId("categories"); //$NON-NLS-1$
139
tbm.insertBefore("back", showCategoriesAction); //$NON-NLS-1$
140

141         showDescriptionAction = new Action() {
142             public void run() {
143                 updateResultSections();
144                 HelpUIPlugin.getDefault().getDialogSettings().put(S_DESCRIPTION_OFF, !showDescriptionAction.isChecked());
145             }
146         };
147         showDescriptionAction.setImageDescriptor(HelpUIResources
148                 .getImageDescriptor(IHelpUIConstants.IMAGE_SHOW_DESC));
149         showDescriptionAction.setChecked(!descOff);
150         showDescriptionAction.setToolTipText(Messages.SearchResultsPart_showDescriptionAction_tooltip);
151         showDescriptionAction.setId("description"); //$NON-NLS-1$
152
tbm.insertAfter("categories", showDescriptionAction); //$NON-NLS-1$
153
tbm.insertAfter("description", new Separator()); //$NON-NLS-1$
154
}
155
156     public void dispose() {
157         innerToolkit.dispose();
158         super.dispose();
159     }
160
161     private void updateResultSections() {
162         BusyIndicator.showWhile(container.getDisplay(), new Runnable JavaDoc() {
163             public void run() {
164                 for (int i = 0; i < results.size(); i++) {
165                     EngineResultSection section = (EngineResultSection) results
166                             .get(i);
167                     section.updateResults(false);
168                 }
169                 reflow();
170             }
171         });
172     }
173
174     boolean getShowCategories() {
175         return showCategoriesAction.isChecked();
176     }
177
178     boolean getShowDescription() {
179         return showDescriptionAction.isChecked();
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see org.eclipse.help.ui.internal.views.IHelpPart#getControl()
186      */

187     public Control getControl() {
188         return container;
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see org.eclipse.help.ui.internal.views.IHelpPart#init(org.eclipse.help.ui.internal.views.NewReusableHelpPart)
195      */

196     public void init(ReusableHelpPart parent, String JavaDoc id, IMemento memento) {
197         this.parent = parent;
198         this.id = id;
199     }
200
201     public String JavaDoc getId() {
202         return id;
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see org.eclipse.help.ui.internal.views.IHelpPart#setVisible(boolean)
209      */

210     public void setVisible(boolean visible) {
211         container.setVisible(visible);
212     }
213     
214     void clearResults() {
215         clearResultSections();
216         separator.setVisible(false);
217         reflow();
218     }
219
220     void clearResultSections() {
221         for (int i = 0; i < results.size(); i++) {
222             EngineResultSection section = (EngineResultSection) results.get(i);
223             section.dispose();
224         }
225         results.clear();
226     }
227
228     void startNewSearch(String JavaDoc phrase, ArrayList JavaDoc eds) {
229         //this.phrase = phrase;
230
//separator.setVisible(true);
231
// locate local help engine and add it first
232
EngineDescriptor localHelp = findLocalHelp(eds);
233         if (localHelp!=null)
234             add(localHelp);
235         // add engines other than local help
236
for (int i = 0; i < eds.size(); i++) {
237             EngineDescriptor ed = (EngineDescriptor) eds.get(i);
238             if (ed==localHelp)
239                 continue;
240             add(ed);
241         }
242         reflow();
243     }
244     
245     private EngineDescriptor findLocalHelp(ArrayList JavaDoc eds) {
246         for (int i=0; i<eds.size(); i++) {
247             EngineDescriptor ed = (EngineDescriptor)eds.get(i);
248             if (ed.getEngineTypeId().equals(IHelpUIConstants.INTERNAL_HELP_ID))
249                 return ed;
250         }
251         return null;
252     }
253     
254     void completed() {
255         for (int i = 0; i < results.size(); i++) {
256             EngineResultSection section = (EngineResultSection) results.get(i);
257             section.completed();
258         }
259     }
260     
261     void canceling() {
262         for (int i = 0; i < results.size(); i++) {
263             EngineResultSection section = (EngineResultSection) results.get(i);
264             section.canceling();
265         }
266     }
267
268     void doOpenLink(Object JavaDoc href) {
269         String JavaDoc url = (String JavaDoc) href;
270
271         if (url.startsWith("nw:")) { //$NON-NLS-1$
272
parent.showExternalURL(url.substring(3));
273         } else
274             parent.showURL(url);
275     }
276     
277     void doCategoryLink(String JavaDoc href) {
278         parent.showPage(IHelpUIConstants.HV_ALL_TOPICS_PAGE);
279         AllTopicsPart part = (AllTopicsPart) parent
280                 .findPart(IHelpUIConstants.HV_TOPIC_TREE);
281         if (part != null) {
282             IToc[] tocs = HelpSystem.getTocs();
283             IHelpResource target = null;
284             for (int i = 0; i < tocs.length; i++) {
285                 if (tocs[i].getHref().equals(href))
286                     target = tocs[i];
287             }
288             if (target != null) {
289                 part.selectReveal(target);
290             }
291         }
292     }
293
294     /*
295      * (non-Javadoc)
296      *
297      * @see org.eclipse.help.ui.internal.views.IHelpPart#fillContextMenu(org.eclipse.jface.action.IMenuManager)
298      */

299     public boolean fillContextMenu(IMenuManager manager) {
300         Control focusControl = container.getDisplay().getFocusControl();
301         if (focusControl != null && focusControl instanceof FormText) {
302             return parent.fillFormContextMenu((FormText) focusControl, manager);
303         }
304         return false;
305     }
306
307     /*
308      * (non-Javadoc)
309      *
310      * @see org.eclipse.help.ui.internal.views.IHelpPart#hasFocusControl(org.eclipse.swt.widgets.Control)
311      */

312     public boolean hasFocusControl(Control control) {
313         for (int i = 0; i < results.size(); i++) {
314             EngineResultSection er = (EngineResultSection) results.get(i);
315             if (er.hasControl(control))
316                 return true;
317         }
318         return false;
319     }
320
321     public synchronized void add(EngineDescriptor ed, ISearchEngineResult match) {
322         EngineResultSection ers = findEngineResult(ed);
323         if (match != null)
324             ers.add(match);
325     }
326
327     public synchronized void add(EngineDescriptor ed,
328             ISearchEngineResult[] matches) {
329         EngineResultSection ers = findEngineResult(ed);
330         ers.add(matches);
331     }
332
333     public synchronized void error(EngineDescriptor ed, IStatus status) {
334         EngineResultSection ers = findEngineResult(ed);
335         ers.error(status);
336     }
337
338     private synchronized EngineResultSection findEngineResult(
339             EngineDescriptor ed) {
340         for (int i = 0; i < results.size(); i++) {
341             EngineResultSection er = (EngineResultSection) results.get(i);
342             if (er.matches(ed))
343                 return er;
344         }
345         final EngineResultSection er = new EngineResultSection(this, ed);
346         Display display = parent.getForm().getToolkit().getColors()
347                 .getDisplay();
348         display.syncExec(new Runnable JavaDoc() {
349             public void run() {
350                 Control c = er.createControl(innerForm.getBody(), innerToolkit);
351                 c.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
352             }
353         });
354         results.add(er);
355         return er;
356     }
357
358     private void add(EngineDescriptor ed) {
359         final EngineResultSection er = new EngineResultSection(this, ed);
360         Control c = er.createControl(innerForm.getBody(), innerToolkit);
361         c.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
362         results.add(er);
363     }
364
365     void reflow() {
366         innerForm.reflow(true);
367         parent.reflow();
368     }
369
370     public boolean setFormInput(Object JavaDoc input) {
371         return false;
372     }
373
374     void scrollToBeginning() {
375         innerForm.setOrigin(0, 0);
376     }
377
378     public IAction getGlobalAction(String JavaDoc id) {
379         if (id.equals(ActionFactory.COPY.getId()))
380             return parent.getCopyAction();
381         return null;
382     }
383
384     FormToolkit getToolkit() {
385         return innerToolkit;
386     }
387     public void stop() {
388     }
389
390     public void toggleRoleFilter() {
391         updateResultSections();
392     }
393
394     public void refilter() {
395         updateResultSections();
396     }
397
398     public void saveState(IMemento memento) {
399     }
400 }
Popular Tags