1 11 12 package org.eclipse.ui.fieldassist; 13 14 import org.eclipse.core.commands.AbstractHandler; 15 import org.eclipse.core.commands.ExecutionEvent; 16 import org.eclipse.core.commands.IHandler; 17 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 18 import org.eclipse.jface.fieldassist.ControlDecoration; 19 import org.eclipse.jface.fieldassist.FieldDecoration; 20 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 21 import org.eclipse.jface.fieldassist.IContentProposalProvider; 22 import org.eclipse.jface.fieldassist.IControlContentAdapter; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.DisposeEvent; 26 import org.eclipse.swt.events.DisposeListener; 27 import org.eclipse.swt.events.FocusEvent; 28 import org.eclipse.swt.events.FocusListener; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.handlers.IHandlerActivation; 32 import org.eclipse.ui.handlers.IHandlerService; 33 import org.eclipse.ui.internal.WorkbenchMessages; 34 import org.eclipse.ui.keys.IBindingService; 35 36 51 public class ContentAssistCommandAdapter extends ContentProposalAdapter { 52 53 private static final String CONTENT_ASSIST_DECORATION_ID = "org.eclipse.ui.fieldAssist.ContentAssistField"; private String commandId; 55 56 60 public static final String CONTENT_PROPOSAL_COMMAND = "org.eclipse.ui.edit.text.contentAssist.proposals"; 62 private static final int DEFAULT_AUTO_ACTIVATION_DELAY = 500; 66 67 private IHandlerService handlerService; 68 69 private IHandlerActivation activeHandler; 70 71 private IHandler proposalHandler = new AbstractHandler() { 72 public Object execute(ExecutionEvent event) { 73 openProposalPopup(); 74 return null; 75 } 76 77 }; 78 private ControlDecoration decoration; 79 80 106 public ContentAssistCommandAdapter(Control control, 107 IControlContentAdapter controlContentAdapter, 108 IContentProposalProvider proposalProvider, String commandId, 109 char[] autoActivationCharacters) { 110 this(control, controlContentAdapter, proposalProvider, commandId, 111 autoActivationCharacters, false); 112 } 113 114 148 public ContentAssistCommandAdapter(Control control, 149 IControlContentAdapter controlContentAdapter, 150 IContentProposalProvider proposalProvider, String commandId, 151 char[] autoActivationCharacters, boolean installDecoration) { 152 super(control, controlContentAdapter, proposalProvider, null, 153 autoActivationCharacters); 154 this.commandId = commandId; 155 if (commandId == null) { 156 this.commandId = CONTENT_PROPOSAL_COMMAND; 157 } 158 159 if (autoActivationCharacters == null) { 163 this.setAutoActivationCharacters(new char[] {}); 164 } 165 setAutoActivationDelay(DEFAULT_AUTO_ACTIVATION_DELAY); 167 168 addListeners(control); 170 171 this.handlerService = (IHandlerService) PlatformUI.getWorkbench() 173 .getService(IHandlerService.class); 174 if (installDecoration) { 175 decoration = new ControlDecoration(control, SWT.TOP | SWT.LEFT); 178 decoration.setShowOnlyOnFocus(true); 179 FieldDecoration dec = getContentAssistFieldDecoration(); 180 decoration.setImage(dec.getImage()); 181 decoration.setDescriptionText(dec.getDescription()); 182 } 183 184 } 185 186 190 private void addListeners(Control control) { 191 control.addFocusListener(new FocusListener() { 192 public void focusLost(FocusEvent e) { 193 if (activeHandler != null) { 194 handlerService.deactivateHandler(activeHandler); 195 activeHandler = null; 196 } 197 } 198 199 public void focusGained(FocusEvent e) { 200 if (isEnabled()) { 201 if (activeHandler == null) { 202 activeHandler = handlerService.activateHandler( 203 commandId, proposalHandler); 204 } 205 } else { 206 if (activeHandler != null) { 207 handlerService.deactivateHandler(activeHandler); 208 } 209 activeHandler = null; 210 } 211 } 212 }); 213 control.addDisposeListener(new DisposeListener() { 214 public void widgetDisposed(DisposeEvent e) { 215 if (activeHandler != null) { 216 handlerService.deactivateHandler(activeHandler); 217 activeHandler = null; 218 } 219 220 } 221 }); 222 } 223 224 230 public String getCommandId() { 231 return commandId; 232 } 233 234 244 private FieldDecoration getContentAssistFieldDecoration() { 245 FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault(); 246 String decId = CONTENT_ASSIST_DECORATION_ID + getCommandId(); 248 FieldDecoration dec = registry.getFieldDecoration(decId); 249 250 if (dec == null) { 252 FieldDecoration originalDec = registry 253 .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL); 254 255 registry.registerFieldDecoration(decId, null, originalDec 256 .getImage()); 257 dec = registry.getFieldDecoration(decId); 258 } 259 IBindingService bindingService = (IBindingService) PlatformUI 262 .getWorkbench().getService(IBindingService.class); 263 dec 264 .setDescription(NLS 265 .bind( 266 WorkbenchMessages.ContentAssist_Cue_Description_Key, 267 bindingService 268 .getBestActiveBindingFormattedFor(getCommandId()))); 269 270 return dec; 272 } 273 274 282 public void setEnabled(boolean enabled) { 283 super.setEnabled(enabled); 284 if (decoration == null) { 285 return; 286 } 287 if (enabled) { 288 decoration.show(); 289 } else { 290 decoration.hide(); 291 } 292 } 293 } 294 | Popular Tags |