1 11 12 package org.eclipse.ui.contentassist; 13 14 import org.eclipse.core.commands.AbstractHandler; 15 import org.eclipse.core.commands.ExecutionEvent; 16 import org.eclipse.core.commands.ExecutionException; 17 import org.eclipse.core.commands.IHandler; 18 19 import org.eclipse.swt.events.DisposeEvent; 20 import org.eclipse.swt.events.DisposeListener; 21 import org.eclipse.swt.events.FocusEvent; 22 import org.eclipse.swt.events.FocusListener; 23 import org.eclipse.swt.widgets.Combo; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Text; 26 27 import org.eclipse.jface.bindings.TriggerSequence; 28 import org.eclipse.jface.contentassist.AbstractControlContentAssistSubjectAdapter; 29 import org.eclipse.jface.contentassist.ComboContentAssistSubjectAdapter; 30 import org.eclipse.jface.contentassist.SubjectControlContentAssistant; 31 import org.eclipse.jface.contentassist.TextContentAssistSubjectAdapter; 32 import org.eclipse.jface.viewers.ILabelProvider; 33 import org.eclipse.jface.viewers.LabelProvider; 34 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.handlers.IHandlerActivation; 37 import org.eclipse.ui.handlers.IHandlerService; 38 import org.eclipse.ui.internal.texteditor.NLSUtility; 39 import org.eclipse.ui.keys.IBindingService; 40 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 41 42 50 public class ContentAssistHandler { 51 54 private Control fControl; 55 58 private AbstractControlContentAssistSubjectAdapter fContentAssistSubjectAdapter; 59 62 private SubjectControlContentAssistant fContentAssistant; 63 67 private FocusListener fFocusListener; 68 71 private IHandlerActivation fHandlerActivation; 72 73 82 public static ContentAssistHandler createHandlerForCombo(Combo combo, SubjectControlContentAssistant contentAssistant) { 83 return new ContentAssistHandler(combo, new ComboContentAssistSubjectAdapter(combo), contentAssistant); 84 } 85 86 95 public static ContentAssistHandler createHandlerForText(Text text, SubjectControlContentAssistant contentAssistant) { 96 return new ContentAssistHandler(text, new TextContentAssistSubjectAdapter(text), contentAssistant); 97 } 98 99 106 private ContentAssistHandler( 107 Control control, 108 AbstractControlContentAssistSubjectAdapter subjectAdapter, 109 SubjectControlContentAssistant contentAssistant) { 110 fControl= control; 111 fContentAssistant= contentAssistant; 112 fContentAssistSubjectAdapter= subjectAdapter; 113 setEnabled(true); 114 fControl.addDisposeListener(new DisposeListener() { 115 public void widgetDisposed(DisposeEvent e) { 116 setEnabled(false); 117 } 118 }); 119 } 120 121 124 public boolean isEnabled() { 125 return fFocusListener != null; 126 } 127 128 135 public void setEnabled(boolean enable) { 136 if (enable == isEnabled()) 137 return; 138 139 if (enable) 140 enable(); 141 else 142 disable(); 143 } 144 145 148 private void enable() { 149 if (! fControl.isDisposed()) { 150 fContentAssistant.install(fContentAssistSubjectAdapter); 151 installCueLabelProvider(); 152 installFocusListener(); 153 if (fControl.isFocusControl()) 154 activateHandler(); 155 } 156 } 157 158 161 private void disable() { 162 if (! fControl.isDisposed()) { 163 fContentAssistant.uninstall(); 164 fContentAssistSubjectAdapter.setContentAssistCueProvider(null); 165 fControl.removeFocusListener(fFocusListener); 166 fFocusListener= null; 167 if (fHandlerActivation != null) 168 deactivateHandler(); 169 } 170 } 171 172 175 private void installCueLabelProvider() { 176 ILabelProvider labelProvider= new LabelProvider() { 177 180 public String getText(Object element) { 181 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class); 182 TriggerSequence[] activeBindings= bindingService.getActiveBindingsFor(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 183 if (activeBindings.length == 0) 184 return ContentAssistMessages.ContentAssistHandler_contentAssistAvailable; 185 return NLSUtility.format(ContentAssistMessages.ContentAssistHandler_contentAssistAvailableWithKeyBinding, activeBindings[0].format()); 186 } 187 }; 188 fContentAssistSubjectAdapter.setContentAssistCueProvider(labelProvider); 189 } 190 191 194 private void installFocusListener() { 195 fFocusListener= new FocusListener() { 196 public void focusGained(final FocusEvent e) { 197 if (fHandlerActivation == null) 198 activateHandler(); 199 } 200 public void focusLost(FocusEvent e) { 201 if (fHandlerActivation != null) 202 deactivateHandler(); 203 } 204 }; 205 fControl.addFocusListener(fFocusListener); 206 } 207 208 211 private void activateHandler() { 212 IHandlerService handlerService= (IHandlerService)PlatformUI.getWorkbench().getAdapter(IHandlerService.class); 213 if (handlerService == null) 214 return; 215 216 IHandler handler= new AbstractHandler() { 217 public Object execute(ExecutionEvent event) throws ExecutionException { 218 if (ContentAssistHandler.this.isEnabled()) fContentAssistant.showPossibleCompletions(); 220 return null; 221 } 222 }; 223 fHandlerActivation= handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler); 224 } 225 226 229 private void deactivateHandler() { 230 IHandlerService handlerService= (IHandlerService)PlatformUI.getWorkbench().getAdapter(IHandlerService.class); 231 if (handlerService != null) 232 handlerService.deactivateHandler(fHandlerActivation); 233 fHandlerActivation= null; 234 } 235 } 236 | Popular Tags |