1 11 package org.eclipse.jface.contentassist; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.Platform; 21 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.custom.VerifyKeyListener; 24 import org.eclipse.swt.events.DisposeEvent; 25 import org.eclipse.swt.events.DisposeListener; 26 import org.eclipse.swt.events.KeyEvent; 27 import org.eclipse.swt.events.KeyListener; 28 import org.eclipse.swt.events.VerifyEvent; 29 import org.eclipse.swt.graphics.Image; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Event; 32 import org.eclipse.swt.widgets.Listener; 33 34 import org.eclipse.jface.fieldassist.ControlDecoration; 35 import org.eclipse.jface.resource.ImageDescriptor; 36 import org.eclipse.jface.text.IEventConsumer; 37 import org.eclipse.jface.viewers.ILabelProvider; 38 import org.eclipse.jface.viewers.ILabelProviderListener; 39 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 40 41 42 52 public abstract class AbstractControlContentAssistSubjectAdapter implements IContentAssistSubjectControl { 53 54 protected static final boolean DEBUG= "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.jface.text/debug/ContentAssistSubjectAdapters")); 56 59 private List fVerifyKeyListeners; 60 63 private Set fKeyListeners; 64 68 private Listener fControlListener; 69 73 private ILabelProvider fCueLabelProvider; 74 78 private ControlDecoration fControlDecoration; 79 83 private Image fCachedDefaultCueImage; 84 85 88 public AbstractControlContentAssistSubjectAdapter() { 89 fVerifyKeyListeners= new ArrayList (1); 90 fKeyListeners= new HashSet (1); 91 } 92 93 96 public abstract Control getControl(); 97 98 101 public void addKeyListener(KeyListener keyListener) { 102 fKeyListeners.add(keyListener); 103 104 if (DEBUG) 105 System.out.println("AbstractControlContentAssistSubjectAdapter#addKeyListener()"); 107 installControlListener(); 108 } 109 110 113 public void removeKeyListener(KeyListener keyListener) { 114 boolean deleted= fKeyListeners.remove(keyListener); 115 116 if (DEBUG) { 117 if (!deleted) 118 System.out.println("removeKeyListener -> wasn't here"); System.out.println("AbstractControlContentAssistSubjectAdapter#removeKeyListener() -> " + fKeyListeners.size()); } 121 122 uninstallControlListener(); 123 } 124 125 128 public boolean supportsVerifyKeyListener() { 129 return true; 130 } 131 132 135 public boolean appendVerifyKeyListener(final VerifyKeyListener verifyKeyListener) { 136 fVerifyKeyListeners.add(verifyKeyListener); 137 138 if (DEBUG) 139 System.out.println("AbstractControlContentAssistSubjectAdapter#appendVerifyKeyListener() -> " + fVerifyKeyListeners.size()); 141 installControlListener(); 142 return true; 143 } 144 145 148 public boolean prependVerifyKeyListener(final VerifyKeyListener verifyKeyListener) { 149 fVerifyKeyListeners.add(0, verifyKeyListener); 150 151 if (DEBUG) 152 System.out.println("AbstractControlContentAssistSubjectAdapter#prependVerifyKeyListener() -> " + fVerifyKeyListeners.size()); 154 installControlListener(); 155 return true; 156 } 157 158 161 public void removeVerifyKeyListener(VerifyKeyListener verifyKeyListener) { 162 fVerifyKeyListeners.remove(verifyKeyListener); 163 164 if (DEBUG) 165 System.out.println("AbstractControlContentAssistSubjectAdapter#removeVerifyKeyListener() -> " + fVerifyKeyListeners.size()); 167 uninstallControlListener(); 168 } 169 170 173 public void setEventConsumer(IEventConsumer eventConsumer) { 174 if (DEBUG) 176 System.out.println("AbstractControlContentAssistSubjectAdapter#setEventConsumer()"); } 178 179 182 public String getLineDelimiter() { 183 return System.getProperty("line.separator"); } 185 186 190 private void installControlListener() { 191 if (DEBUG) 192 System.out.println("AbstractControlContentAssistSubjectAdapter#installControlListener() -> k: " + fKeyListeners.size() + ", v: " + fVerifyKeyListeners.size()); 194 if (fControlListener != null) 195 return; 196 197 fControlListener= new Listener() { 198 public void handleEvent(Event e) { 199 if (! getControl().isFocusControl()) 200 return; VerifyEvent verifyEvent= new VerifyEvent(e); 202 KeyEvent keyEvent= new KeyEvent(e); 203 switch (e.type) { 204 case SWT.Traverse : 205 206 if (DEBUG) 207 dump("before traverse", e, verifyEvent); 209 verifyEvent.doit= true; 210 for (Iterator iter= fVerifyKeyListeners.iterator(); iter.hasNext(); ) { 211 ((VerifyKeyListener) iter.next()).verifyKey(verifyEvent); 212 if (! verifyEvent.doit) { 213 e.detail= SWT.TRAVERSE_NONE; 214 e.doit= true; 215 if (DEBUG) 216 dump("traverse eaten by verify", e, verifyEvent); return; 218 } 219 220 if (DEBUG) 221 dump("traverse OK", e, verifyEvent); } 223 break; 224 225 case SWT.KeyDown: 226 for (Iterator iter= fVerifyKeyListeners.iterator(); iter.hasNext(); ) { 227 ((VerifyKeyListener) iter.next()).verifyKey(verifyEvent); 228 if (! verifyEvent.doit) { 229 e.doit= verifyEvent.doit; 230 if (DEBUG) 231 dump("keyDown eaten by verify", e, verifyEvent); return; 233 } 234 } 235 236 if (DEBUG) 237 dump("keyDown OK", e, verifyEvent); 239 for (Iterator iter= fKeyListeners.iterator(); iter.hasNext();) { 240 ((KeyListener) iter.next()).keyPressed(keyEvent); 241 } 242 break; 243 244 default : 245 Assert.isTrue(false); 246 } 247 } 248 249 256 private void dump(String who, Event e, VerifyEvent ve) { 257 StringBuffer sb= new StringBuffer ("--- [AbstractControlContentAssistSubjectAdapter]\n"); sb.append(who); 259 sb.append(" - e: keyCode="+e.keyCode+hex(e.keyCode)); sb.append("; character="+e.character+hex(e.character)); sb.append("; stateMask="+e.stateMask+hex(e.stateMask)); sb.append("; doit="+e.doit); sb.append("; detail="+e.detail+hex(e.detail)); sb.append("; widget="+e.widget); sb.append("\n"); sb.append(" verifyEvent keyCode="+e.keyCode+hex(e.keyCode)); sb.append("; character="+e.character+hex(e.character)); sb.append("; stateMask="+e.stateMask+hex(e.stateMask)); sb.append("; doit="+ve.doit); sb.append("; widget="+e.widget); System.out.println(sb); 272 } 273 274 private String hex(int i) { 275 return "[0x" + Integer.toHexString(i) + ']'; } 277 }; 278 getControl().addListener(SWT.Traverse, fControlListener); 279 getControl().addListener(SWT.KeyDown, fControlListener); 280 281 if (DEBUG) 282 System.out.println("AbstractControlContentAssistSubjectAdapter#installControlListener() - installed"); } 284 285 290 private void uninstallControlListener() { 291 if (fControlListener == null || fKeyListeners.size() + fVerifyKeyListeners.size() != 0) { 292 293 if (DEBUG) 294 System.out.println("AbstractControlContentAssistSubjectAdapter#uninstallControlListener() -> k: " + fKeyListeners.size() + ", v: " + fVerifyKeyListeners.size()); 296 return; 297 } 298 getControl().removeListener(SWT.Traverse, fControlListener); 299 getControl().removeListener(SWT.KeyDown, fControlListener); 300 fControlListener= null; 301 302 if (DEBUG) 303 System.out.println("AbstractControlContentAssistSubjectAdapter#uninstallControlListener() - done"); } 305 306 330 public void setContentAssistCueProvider(final ILabelProvider labelProvider) { 331 if (fCueLabelProvider != null) { 332 fCueLabelProvider.dispose(); 333 } 334 335 fCueLabelProvider= labelProvider; 336 337 if (labelProvider == null) { 338 if (fControlDecoration != null) { 339 fControlDecoration.dispose(); 340 fControlDecoration= null; 341 } 342 343 } else { 344 if (fControlDecoration == null) { 345 fControlDecoration= new ControlDecoration(getControl(), (SWT.TOP | SWT.LEFT)); 346 getControl().addDisposeListener(new DisposeListener() { 347 public void widgetDisposed(DisposeEvent e) { 348 if (fCueLabelProvider != null) { 349 fCueLabelProvider.dispose(); 350 fCueLabelProvider= null; 351 } 352 if (fControlDecoration != null) { 353 fControlDecoration.dispose(); 354 fControlDecoration= null; 355 } 356 if (fCachedDefaultCueImage != null) { 357 fCachedDefaultCueImage.dispose(); 358 fCachedDefaultCueImage= null; 359 } 360 } 361 }); 362 fControlDecoration.setShowHover(true); 363 fControlDecoration.setShowOnlyOnFocus(true); 364 } 365 366 ILabelProviderListener listener= new ILabelProviderListener() { 367 public void labelProviderChanged(LabelProviderChangedEvent event) { 368 fControlDecoration.setDescriptionText(labelProvider.getText(getControl())); 369 Image image= labelProvider.getImage(getControl()); 370 if (image == null) 371 image= getDefaultCueImage(); 372 fControlDecoration.setImage(image); 373 } 374 }; 375 labelProvider.addListener(listener); 376 listener.labelProviderChanged(new LabelProviderChangedEvent(labelProvider)); 378 } 379 } 380 381 387 private Image getDefaultCueImage() { 388 if (fCachedDefaultCueImage == null) { 389 ImageDescriptor cueID= ImageDescriptor.createFromFile(AbstractControlContentAssistSubjectAdapter.class, "images/content_assist_cue.gif"); fCachedDefaultCueImage= cueID.createImage(getControl().getDisplay()); 391 } 392 return fCachedDefaultCueImage; 393 } 394 } 395 | Popular Tags |