1 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 33 public class HelpSearchContributionItem extends ControlContribution { 34 private static final String ID = "org.eclipse.ui.helpSearch"; 36 private IWorkbenchWindow window; 37 38 private Combo combo; 39 40 private int MAX_ITEM_COUNT = 10; 41 42 47 public HelpSearchContributionItem(IWorkbenchWindow window) { 48 this(window, ID); 49 } 50 51 57 public HelpSearchContributionItem(IWorkbenchWindow window, String id) { 58 super(id); 59 Assert.isNotNull(window); 60 this.window = window; 61 } 62 63 66 protected Control createControl(Composite parent) { 67 combo = new Combo(parent, SWT.NONE); 68 combo.setToolTipText(WorkbenchMessages.WorkbenchWindow_searchCombo_toolTip); 69 String [] 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 96 protected int computeWidth(Control control) { 97 return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x; 98 } 99 100 private void doSearch(String 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 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 |