KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > HelpSearchContributionItem


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ui.internal.actions;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.action.ControlContribution;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.internal.WorkbenchMessages;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26
27 /**
28  * This is the contribution item that is used to add a help search field to
29  * the cool bar.
30  *
31  * @since 3.1
32  */

33 public class HelpSearchContributionItem extends ControlContribution {
34     private static final String JavaDoc ID = "org.eclipse.ui.helpSearch"; //$NON-NLS-1$
35

36     private IWorkbenchWindow window;
37
38     private Combo combo;
39
40     private int MAX_ITEM_COUNT = 10;
41     
42     /**
43      * Creates the contribution item.
44      *
45      * @param window the window
46      */

47     public HelpSearchContributionItem(IWorkbenchWindow window) {
48         this(window, ID);
49     }
50
51     /**
52      * Creates the contribution item.
53      *
54      * @param window the window
55      * @param id the contribution item id
56      */

57     public HelpSearchContributionItem(IWorkbenchWindow window, String JavaDoc id) {
58         super(id);
59         Assert.isNotNull(window);
60         this.window = window;
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite)
65      */

66     protected Control createControl(Composite parent) {
67         combo = new Combo(parent, SWT.NONE);
68         combo.setToolTipText(WorkbenchMessages.WorkbenchWindow_searchCombo_toolTip);
69         String JavaDoc[] items = WorkbenchPlugin.getDefault().getDialogSettings()
70                 .getArray(ID);
71         if (items != null) {
72             combo.setItems(items);
73         }
74         combo.setText(WorkbenchMessages.WorkbenchWindow_searchCombo_text);
75         combo.addKeyListener(new KeyAdapter() {
76             public void keyReleased(KeyEvent e) {
77                 if (e.character == SWT.CR) {
78                     doSearch(combo.getText(), true);
79                 }
80             }
81         });
82         combo.addSelectionListener(new SelectionAdapter() {
83             public void widgetSelected(SelectionEvent e) {
84                 int index = combo.getSelectionIndex();
85                 if (index != -1) {
86                     doSearch(combo.getItem(index), false);
87                 }
88             }
89         });
90         return combo;
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.jface.action.ControlContribution#computeWidth(org.eclipse.swt.widgets.Control)
95      */

96     protected int computeWidth(Control control) {
97         return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x;
98     }
99
100     private void doSearch(String JavaDoc phrase, boolean updateList) {
101         if (phrase.length() == 0) {
102             window.getWorkbench().getHelpSystem().displaySearch();
103             return;
104         }
105         if (updateList) {
106             boolean exists = false;
107             for (int i = 0; i < combo.getItemCount(); i++) {
108                 String JavaDoc item = combo.getItem(i);
109                 if (item.equalsIgnoreCase(phrase)) {
110                     exists = true;
111                     break;
112                 }
113             }
114             if (!exists) {
115                 combo.add(phrase, 0);
116                 if (combo.getItemCount() > MAX_ITEM_COUNT) {
117                     combo.remove(combo.getItemCount() - 1);
118                 }
119                 WorkbenchPlugin.getDefault().getDialogSettings().put(ID,
120                         combo.getItems());
121             }
122         }
123         window.getWorkbench().getHelpSystem().search(phrase);
124     }
125 }
126
Popular Tags