1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets; 22 23 24 import org.eclipse.core.commands.ExecutionEvent; 25 import org.eclipse.core.commands.IHandler; 26 import org.eclipse.jface.contentassist.ComboContentAssistSubjectAdapter; 27 import org.eclipse.jface.contentassist.SubjectControlContentAssistant; 28 import org.eclipse.jface.contentassist.TextContentAssistSubjectAdapter; 29 import org.eclipse.jface.text.ITextViewer; 30 import org.eclipse.swt.events.FocusEvent; 31 import org.eclipse.swt.events.FocusListener; 32 import org.eclipse.swt.events.TraverseEvent; 33 import org.eclipse.swt.events.TraverseListener; 34 import org.eclipse.swt.graphics.Point; 35 import org.eclipse.swt.widgets.Combo; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Text; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.handlers.IHandlerActivation; 40 import org.eclipse.ui.handlers.IHandlerService; 41 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 42 43 44 57 public class DialogContentAssistant extends SubjectControlContentAssistant implements FocusListener 58 { 59 60 61 private Control control; 62 63 64 private IHandlerActivation handlerActivation; 65 66 67 private boolean possibleCompletionsVisible; 68 69 70 73 public DialogContentAssistant() 74 { 75 this.possibleCompletionsVisible = false; 76 } 77 78 79 84 public void install( Text text ) 85 { 86 control = text; 87 control.addFocusListener( this ); 88 super.install( new TextContentAssistSubjectAdapter( text ) ); 89 } 90 91 92 97 public void install( Combo combo ) 98 { 99 control = combo; 100 control.addFocusListener( this ); 101 super.install( new ComboContentAssistSubjectAdapter( combo ) ); 102 } 103 104 105 110 public void install( ITextViewer viewer ) 111 { 112 control = viewer.getTextWidget(); 113 control.addFocusListener( this ); 114 115 control.addTraverseListener( new TraverseListener() 117 { 118 public void keyTraversed( TraverseEvent e ) 119 { 120 if ( possibleCompletionsVisible ) 121 { 122 e.doit = false; 123 } 124 } 125 } ); 126 127 super.install( viewer ); 128 } 129 130 131 134 public void uninstall() 135 { 136 if ( handlerActivation != null ) 137 { 138 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 139 IHandlerService.class ); 140 handlerService.deactivateHandler( handlerActivation ); 141 handlerActivation = null; 142 } 143 144 if ( control != null ) 145 { 146 control.removeFocusListener( this ); 147 } 148 149 super.uninstall(); 150 } 151 152 153 156 protected Point restoreCompletionProposalPopupSize() 157 { 158 possibleCompletionsVisible = true; 159 return super.restoreCompletionProposalPopupSize(); 160 } 161 162 163 166 public String showPossibleCompletions() 167 { 168 possibleCompletionsVisible = true; 169 return super.showPossibleCompletions(); 170 } 171 172 173 176 protected void possibleCompletionsClosed() 177 { 178 possibleCompletionsVisible = false; 179 super.possibleCompletionsClosed(); 180 } 181 182 183 186 public void focusGained( FocusEvent e ) 187 { 188 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 189 IHandlerService.class ); 190 if ( handlerService != null ) 191 { 192 IHandler handler = new org.eclipse.core.commands.AbstractHandler() 193 { 194 public Object execute( ExecutionEvent event ) throws org.eclipse.core.commands.ExecutionException 195 { 196 showPossibleCompletions(); 197 return null; 198 } 199 }; 200 handlerActivation = handlerService.activateHandler( 201 ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler ); 202 } 203 } 204 205 206 209 public void focusLost( FocusEvent e ) 210 { 211 if ( handlerActivation != null ) 212 { 213 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 214 IHandlerService.class ); 215 handlerService.deactivateHandler( handlerActivation ); 216 handlerActivation = null; 217 } 218 } 219 220 } 221 | Popular Tags |