KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > DialogContentAssistant


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

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 /**
45  * The DialogContentAssistant is used to provide content assist and
46  * a proposal popup within a SWT {@link Text}, {@link Combo} or
47  * {@link ITextViewer}.
48  *
49  * It provides a special handling of ESC keystrokes:
50  * When the proposal popup is shown ESC is catched and the popup is closed.
51  * This ensures that a dialog isn't closed on a ESC keystroke
52  * while the proposal popup is opened.
53  *
54  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
55  * @version $Rev$, $Date$
56  */

57 public class DialogContentAssistant extends SubjectControlContentAssistant implements FocusListener
58 {
59
60     /** The control */
61     private Control control;
62
63     /** The handler activation. */
64     private IHandlerActivation handlerActivation;
65
66     /** The possible completions visible. */
67     private boolean possibleCompletionsVisible;
68
69
70     /**
71      * Creates a new instance of DialogContentAssistant.
72      */

73     public DialogContentAssistant()
74     {
75         this.possibleCompletionsVisible = false;
76     }
77
78
79     /**
80      * Installs content assist on the given text.
81      *
82      * @param text the text
83      */

84     public void install( Text text )
85     {
86         control = text;
87         control.addFocusListener( this );
88         super.install( new TextContentAssistSubjectAdapter( text ) );
89     }
90
91
92     /**
93      * Installs content assist on the given combo.
94      *
95      * @param combo the combo
96      */

97     public void install( Combo combo )
98     {
99         control = combo;
100         control.addFocusListener( this );
101         super.install( new ComboContentAssistSubjectAdapter( combo ) );
102     }
103
104
105     /**
106      * Installs content assist on the given text viewer.
107      *
108      * @param viewer the text viewer
109      */

110     public void install( ITextViewer viewer )
111     {
112         control = viewer.getTextWidget();
113         control.addFocusListener( this );
114
115         // stop traversal (ESC) if popup is shown
116
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     /**
132      * Uninstalls content assist on the control.
133      */

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     /**
154      * {@inheritDoc}
155      */

156     protected Point restoreCompletionProposalPopupSize()
157     {
158         possibleCompletionsVisible = true;
159         return super.restoreCompletionProposalPopupSize();
160     }
161
162
163     /**
164      * {@inheritDoc}
165      */

166     public String JavaDoc showPossibleCompletions()
167     {
168         possibleCompletionsVisible = true;
169         return super.showPossibleCompletions();
170     }
171
172
173     /**
174      * {@inheritDoc}
175      */

176     protected void possibleCompletionsClosed()
177     {
178         possibleCompletionsVisible = false;
179         super.possibleCompletionsClosed();
180     }
181
182
183     /**
184      * {@inheritDoc}
185      */

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 JavaDoc 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     /**
207      * {@inheritDoc}
208      */

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