KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > sourceeditor > 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 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 /**
44  * This class implements the Content Assistant Dialog used in the ACI Item
45  * Source Editor for displaying proposals
46  *
47  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
48  * @version $Rev$, $Date$
49  */

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     /**
60      * Creates a new instance of DialogContentAssistant.
61      */

62     public DialogContentAssistant()
63     {
64         super();
65         this.possibleCompletionsVisible = false;
66     }
67
68
69     /**
70      * Installs content assist support on the given subject.
71      *
72      * @param text
73      * the one who requests content assist
74      */

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     /**
84      * Installs content assist support on the given subject.
85      *
86      * @param combo
87      * the one who requests content assist
88      */

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     /* (non-Javadoc)
98      * @see org.eclipse.jface.text.contentassist.ContentAssistant#install(org.eclipse.jface.text.ITextViewer)
99      */

100     public void install( ITextViewer viewer )
101     {
102         this.control = viewer.getTextWidget();
103         this.control.addFocusListener( this );
104
105         // stop traversal (ESC) if popup is shown
106
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     /* (non-Javadoc)
122      * @see org.eclipse.jface.text.contentassist.ContentAssistant#uninstall()
123      */

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     /* (non-Javadoc)
144      * @see org.eclipse.jface.text.contentassist.ContentAssistant#restoreCompletionProposalPopupSize()
145      */

146     protected Point restoreCompletionProposalPopupSize()
147     {
148         possibleCompletionsVisible = true;
149         return super.restoreCompletionProposalPopupSize();
150     }
151
152
153     /* (non-Javadoc)
154      * @see org.eclipse.jface.text.contentassist.ContentAssistant#showPossibleCompletions()
155      */

156     public String JavaDoc showPossibleCompletions()
157     {
158         possibleCompletionsVisible = true;
159         return super.showPossibleCompletions();
160     }
161
162
163     /* (non-Javadoc)
164      * @see org.eclipse.jface.text.contentassist.ContentAssistant#possibleCompletionsClosed()
165      */

166     protected void possibleCompletionsClosed()
167     {
168         this.possibleCompletionsVisible = false;
169         super.possibleCompletionsClosed();
170     }
171
172
173     /* (non-Javadoc)
174      * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
175      */

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 JavaDoc 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     /* (non-Javadoc)
197      * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
198      */

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