1 20 package org.apache.directory.ldapstudio.aciitemeditor.sourceeditor; 21 22 23 import org.eclipse.core.commands.ExecutionEvent; 24 import org.eclipse.core.commands.IHandler; 25 import org.eclipse.jface.contentassist.ComboContentAssistSubjectAdapter; 26 import org.eclipse.jface.contentassist.SubjectControlContentAssistant; 27 import org.eclipse.jface.contentassist.TextContentAssistSubjectAdapter; 28 import org.eclipse.jface.text.ITextViewer; 29 import org.eclipse.swt.events.FocusEvent; 30 import org.eclipse.swt.events.FocusListener; 31 import org.eclipse.swt.events.TraverseEvent; 32 import org.eclipse.swt.events.TraverseListener; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.swt.widgets.Combo; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Text; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.handlers.IHandlerActivation; 39 import org.eclipse.ui.handlers.IHandlerService; 40 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 41 42 43 50 public class DialogContentAssistant extends SubjectControlContentAssistant implements FocusListener 51 { 52 private Control control; 53 54 private IHandlerActivation handlerActivation; 55 56 private boolean possibleCompletionsVisible; 57 58 59 62 public DialogContentAssistant() 63 { 64 super(); 65 this.possibleCompletionsVisible = false; 66 } 67 68 69 75 public void install( Text text ) 76 { 77 this.control = text; 78 this.control.addFocusListener( this ); 79 super.install( new TextContentAssistSubjectAdapter( text ) ); 80 } 81 82 83 89 public void install( Combo combo ) 90 { 91 this.control = combo; 92 this.control.addFocusListener( this ); 93 super.install( new ComboContentAssistSubjectAdapter( combo ) ); 94 } 95 96 97 100 public void install( ITextViewer viewer ) 101 { 102 this.control = viewer.getTextWidget(); 103 this.control.addFocusListener( this ); 104 105 this.control.addTraverseListener( new TraverseListener() 107 { 108 public void keyTraversed( TraverseEvent e ) 109 { 110 if ( possibleCompletionsVisible ) 111 { 112 e.doit = false; 113 } 114 } 115 } ); 116 117 super.install( viewer ); 118 } 119 120 121 124 public void uninstall() 125 { 126 if ( this.handlerActivation != null ) 127 { 128 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 129 IHandlerService.class ); 130 handlerService.deactivateHandler( this.handlerActivation ); 131 this.handlerActivation = null; 132 } 133 134 if ( this.control != null ) 135 { 136 this.control.removeFocusListener( this ); 137 } 138 139 super.uninstall(); 140 } 141 142 143 146 protected Point restoreCompletionProposalPopupSize() 147 { 148 possibleCompletionsVisible = true; 149 return super.restoreCompletionProposalPopupSize(); 150 } 151 152 153 156 public String showPossibleCompletions() 157 { 158 possibleCompletionsVisible = true; 159 return super.showPossibleCompletions(); 160 } 161 162 163 166 protected void possibleCompletionsClosed() 167 { 168 this.possibleCompletionsVisible = false; 169 super.possibleCompletionsClosed(); 170 } 171 172 173 176 public void focusGained( FocusEvent e ) 177 { 178 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 179 IHandlerService.class ); 180 if ( handlerService != null ) 181 { 182 IHandler handler = new org.eclipse.core.commands.AbstractHandler() 183 { 184 public Object execute( ExecutionEvent event ) throws org.eclipse.core.commands.ExecutionException 185 { 186 showPossibleCompletions(); 187 return null; 188 } 189 }; 190 this.handlerActivation = handlerService.activateHandler( 191 ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler ); 192 } 193 } 194 195 196 199 public void focusLost( FocusEvent e ) 200 { 201 if ( this.handlerActivation != null ) 202 { 203 IHandlerService handlerService = ( IHandlerService ) PlatformUI.getWorkbench().getAdapter( 204 IHandlerService.class ); 205 handlerService.deactivateHandler( this.handlerActivation ); 206 this.handlerActivation = null; 207 } 208 } 209 } 210 | Popular Tags |