001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.actions;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.action.ControlContribution;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.KeyAdapter;
016: import org.eclipse.swt.events.KeyEvent;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.widgets.Combo;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.ui.IWorkbenchWindow;
023: import org.eclipse.ui.internal.WorkbenchMessages;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025:
026: /**
027: * This is the contribution item that is used to add a help search field to
028: * the cool bar.
029: *
030: * @since 3.1
031: */
032: public class HelpSearchContributionItem extends ControlContribution {
033: private static final String ID = "org.eclipse.ui.helpSearch"; //$NON-NLS-1$
034:
035: private IWorkbenchWindow window;
036:
037: private Combo combo;
038:
039: private int MAX_ITEM_COUNT = 10;
040:
041: /**
042: * Creates the contribution item.
043: *
044: * @param window the window
045: */
046: public HelpSearchContributionItem(IWorkbenchWindow window) {
047: this (window, ID);
048: }
049:
050: /**
051: * Creates the contribution item.
052: *
053: * @param window the window
054: * @param id the contribution item id
055: */
056: public HelpSearchContributionItem(IWorkbenchWindow window, String id) {
057: super (id);
058: Assert.isNotNull(window);
059: this .window = window;
060: }
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite)
064: */
065: protected Control createControl(Composite parent) {
066: combo = new Combo(parent, SWT.NONE);
067: combo
068: .setToolTipText(WorkbenchMessages.WorkbenchWindow_searchCombo_toolTip);
069: String[] items = WorkbenchPlugin.getDefault()
070: .getDialogSettings().getArray(ID);
071: if (items != null) {
072: combo.setItems(items);
073: }
074: combo
075: .setText(WorkbenchMessages.WorkbenchWindow_searchCombo_text);
076: combo.addKeyListener(new KeyAdapter() {
077: public void keyReleased(KeyEvent e) {
078: if (e.character == SWT.CR) {
079: doSearch(combo.getText(), true);
080: }
081: }
082: });
083: combo.addSelectionListener(new SelectionAdapter() {
084: public void widgetSelected(SelectionEvent e) {
085: int index = combo.getSelectionIndex();
086: if (index != -1) {
087: doSearch(combo.getItem(index), false);
088: }
089: }
090: });
091: return combo;
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.jface.action.ControlContribution#computeWidth(org.eclipse.swt.widgets.Control)
096: */
097: protected int computeWidth(Control control) {
098: return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x;
099: }
100:
101: private void doSearch(String phrase, boolean updateList) {
102: if (phrase.length() == 0) {
103: window.getWorkbench().getHelpSystem().displaySearch();
104: return;
105: }
106: if (updateList) {
107: boolean exists = false;
108: for (int i = 0; i < combo.getItemCount(); i++) {
109: String item = combo.getItem(i);
110: if (item.equalsIgnoreCase(phrase)) {
111: exists = true;
112: break;
113: }
114: }
115: if (!exists) {
116: combo.add(phrase, 0);
117: if (combo.getItemCount() > MAX_ITEM_COUNT) {
118: combo.remove(combo.getItemCount() - 1);
119: }
120: WorkbenchPlugin.getDefault().getDialogSettings().put(
121: ID, combo.getItems());
122: }
123: }
124: window.getWorkbench().getHelpSystem().search(phrase);
125: }
126: }
|