KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > contentassist > ContentAssistSubjectControlAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text.contentassist;
12
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.custom.VerifyKeyListener;
15 import org.eclipse.swt.events.KeyListener;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.widgets.Control;
19
20 import org.eclipse.core.runtime.Assert;
21
22 import org.eclipse.jface.contentassist.*;
23
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IEventConsumer;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.ITextViewerExtension;
28 import org.eclipse.jface.text.contentassist.ContextInformationPopup.ContextFrame;
29
30
31 /**
32  * This content assist adapter delegates the calls either to
33  * a text viewer or to a content assist subject control.
34  *
35  * @since 3.0
36  */

37 final class ContentAssistSubjectControlAdapter implements IContentAssistSubjectControl {
38
39     /**
40      * The text viewer which is used as content assist subject control.
41      */

42     private ITextViewer fViewer;
43
44     /**
45      * The content assist subject control.
46      */

47     private IContentAssistSubjectControl fContentAssistSubjectControl;
48
49
50     /**
51      * Creates an adapter for the given content assist subject control.
52      *
53      * @param contentAssistSubjectControl the content assist subject control
54      */

55     ContentAssistSubjectControlAdapter(IContentAssistSubjectControl contentAssistSubjectControl) {
56         Assert.isNotNull(contentAssistSubjectControl);
57         fContentAssistSubjectControl= contentAssistSubjectControl;
58     }
59
60     /**
61      * Creates an adapter for the given text viewer.
62      *
63      * @param viewer the text viewer
64      */

65     public ContentAssistSubjectControlAdapter(ITextViewer viewer) {
66         Assert.isNotNull(viewer);
67         fViewer= viewer;
68     }
69
70     /*
71      * @see IContentAssistSubjectControl#getLineHeight()
72      */

73     public int getLineHeight() {
74         if (fContentAssistSubjectControl != null)
75             return fContentAssistSubjectControl.getLineHeight();
76         
77         return fViewer.getTextWidget().getLineHeight(getCaretOffset());
78     }
79
80     /*
81      * @see IContentAssistSubjectControl#getControl()
82      */

83     public Control getControl() {
84         if (fContentAssistSubjectControl != null)
85             return fContentAssistSubjectControl.getControl();
86         return fViewer.getTextWidget();
87     }
88
89     /*
90      * @see IContentAssistSubjectControl#getLocationAtOffset(int)
91      */

92     public Point getLocationAtOffset(int offset) {
93         if (fContentAssistSubjectControl != null)
94             return fContentAssistSubjectControl.getLocationAtOffset(offset);
95         return fViewer.getTextWidget().getLocationAtOffset(offset);
96     }
97
98     /*
99      * @see IContentAssistSubjectControl#getWidgetSelectionRange()
100      */

101     public Point getWidgetSelectionRange() {
102         if (fContentAssistSubjectControl != null)
103             return fContentAssistSubjectControl.getWidgetSelectionRange();
104         return fViewer.getTextWidget().getSelectionRange();
105     }
106
107     /*
108      * @see IContentAssistSubjectControl#getSelectedRange()
109      */

110     public Point getSelectedRange() {
111         if (fContentAssistSubjectControl != null)
112             return fContentAssistSubjectControl.getSelectedRange();
113         return fViewer.getSelectedRange();
114     }
115
116     /*
117      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getCaretOffset()
118      */

119     public int getCaretOffset() {
120         if (fContentAssistSubjectControl != null)
121             return fContentAssistSubjectControl.getCaretOffset();
122         return fViewer.getTextWidget().getCaretOffset();
123     }
124
125     /*
126      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getLineDelimiter()
127      */

128     public String JavaDoc getLineDelimiter() {
129         if (fContentAssistSubjectControl != null)
130             return fContentAssistSubjectControl.getLineDelimiter();
131         return fViewer.getTextWidget().getLineDelimiter();
132     }
133
134     /*
135      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#addKeyListener(org.eclipse.swt.events.KeyListener)
136      */

137     public void addKeyListener(KeyListener keyListener) {
138         if (fContentAssistSubjectControl != null)
139             fContentAssistSubjectControl.addKeyListener(keyListener);
140         else
141             fViewer.getTextWidget().addKeyListener(keyListener);
142     }
143
144     /*
145      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#removeKeyListener(org.eclipse.swt.events.KeyListener)
146      */

147     public void removeKeyListener(KeyListener keyListener) {
148         if (fContentAssistSubjectControl != null)
149             fContentAssistSubjectControl.removeKeyListener(keyListener);
150         else
151             fViewer.getTextWidget().removeKeyListener(keyListener);
152     }
153
154     /*
155      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getDocument()
156      */

157     public IDocument getDocument() {
158         if (fContentAssistSubjectControl != null)
159             return fContentAssistSubjectControl.getDocument();
160         return fViewer.getDocument();
161     }
162
163     /*
164      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#prependVerifyKeyListener(VerifyKeyListener)
165      */

166     public boolean prependVerifyKeyListener(VerifyKeyListener verifyKeyListener) {
167         if (fContentAssistSubjectControl != null) {
168             return fContentAssistSubjectControl.prependVerifyKeyListener(verifyKeyListener);
169         } else if (fViewer instanceof ITextViewerExtension) {
170             ITextViewerExtension e= (ITextViewerExtension) fViewer;
171             e.prependVerifyKeyListener(verifyKeyListener);
172             return true;
173         } else {
174
175             StyledText textWidget= fViewer.getTextWidget();
176             if (Helper.okToUse(textWidget)) {
177                 textWidget.addVerifyKeyListener(verifyKeyListener);
178                 return true;
179             }
180         }
181         return false;
182     }
183
184     /*
185      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#appendVerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener)
186      */

187     public boolean appendVerifyKeyListener(VerifyKeyListener verifyKeyListener) {
188         if (fContentAssistSubjectControl != null)
189             return fContentAssistSubjectControl.appendVerifyKeyListener(verifyKeyListener);
190         else if (fViewer instanceof ITextViewerExtension) {
191             ITextViewerExtension extension= (ITextViewerExtension)fViewer;
192             extension.appendVerifyKeyListener(verifyKeyListener);
193             return true;
194         } else {
195             StyledText textWidget= fViewer.getTextWidget();
196             if (Helper.okToUse(textWidget)) {
197                 textWidget.addVerifyKeyListener(verifyKeyListener);
198                 return true;
199             }
200         }
201         return false;
202     }
203
204     /*
205      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#removeVerifyKeyListener(org.eclipse.swt.custom.VerifyKeyListener)
206      */

207     public void removeVerifyKeyListener(VerifyKeyListener verifyKeyListener) {
208         if (fContentAssistSubjectControl != null) {
209             fContentAssistSubjectControl.removeVerifyKeyListener(verifyKeyListener);
210         } else if (fViewer instanceof ITextViewerExtension) {
211             ITextViewerExtension extension= (ITextViewerExtension) fViewer;
212             extension.removeVerifyKeyListener(verifyKeyListener);
213         } else {
214             StyledText textWidget= fViewer.getTextWidget();
215             if (Helper.okToUse(textWidget))
216                 textWidget.removeVerifyKeyListener(verifyKeyListener);
217         }
218     }
219
220     /*
221      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#setEventConsumer(org.eclipse.jface.text.contentassist.ContentAssistant.InternalListener)
222      */

223     public void setEventConsumer(IEventConsumer eventConsumer) {
224         if (fContentAssistSubjectControl != null)
225             fContentAssistSubjectControl.setEventConsumer(eventConsumer);
226         else
227             fViewer.setEventConsumer(eventConsumer);
228     }
229
230     /*
231      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#setSelectedRange(int, int)
232      */

233     public void setSelectedRange(int i, int j) {
234         if (fContentAssistSubjectControl != null)
235             fContentAssistSubjectControl.setSelectedRange(i, j);
236         else
237             fViewer.setSelectedRange(i, j);
238     }
239
240     /*
241      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#revealRange(int, int)
242      */

243     public void revealRange(int i, int j) {
244         if (fContentAssistSubjectControl != null)
245             fContentAssistSubjectControl.revealRange(i, j);
246         else
247             fViewer.revealRange(i, j);
248     }
249
250     /*
251      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#canAddVerifyKeyListener()
252      */

253     public boolean supportsVerifyKeyListener() {
254         if (fContentAssistSubjectControl != null)
255             return fContentAssistSubjectControl.supportsVerifyKeyListener();
256         return true;
257     }
258     /**
259      * Returns the characters which when typed by the user should automatically
260      * initiate proposing completions. The position is used to determine the
261      * appropriate content assist processor to invoke.
262      *
263      * @param contentAssistant the content assistant
264      * @param offset a document offset
265      * @return the auto activation characters
266      * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
267      */

268     public char[] getCompletionProposalAutoActivationCharacters(ContentAssistant contentAssistant, int offset) {
269         if (fContentAssistSubjectControl != null)
270             return contentAssistant.getCompletionProposalAutoActivationCharacters(fContentAssistSubjectControl, offset);
271         return contentAssistant.getCompletionProposalAutoActivationCharacters(fViewer, offset);
272     }
273
274     /**
275      * Returns the characters which when typed by the user should automatically
276      * initiate the presentation of context information. The position is used
277      * to determine the appropriate content assist processor to invoke.
278      *
279      * @param contentAssistant the content assistant
280      * @param offset a document offset
281      * @return the auto activation characters
282      *
283      * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
284      */

285     char[] getContextInformationAutoActivationCharacters(ContentAssistant contentAssistant, int offset) {
286         if (fContentAssistSubjectControl != null)
287             return contentAssistant.getContextInformationAutoActivationCharacters(fContentAssistSubjectControl, offset);
288         return contentAssistant.getContextInformationAutoActivationCharacters(fViewer, offset);
289     }
290
291     /**
292     * Creates and returns a completion proposal popup for the given content assistant.
293     *
294     * @param contentAssistant the content assistant
295     * @param controller the additional info controller
296     * @return the completion proposal popup
297     */

298     CompletionProposalPopup createCompletionProposalPopup(ContentAssistant contentAssistant, AdditionalInfoController controller) {
299         if (fContentAssistSubjectControl != null)
300             return new CompletionProposalPopup(contentAssistant, fContentAssistSubjectControl, controller);
301         return new CompletionProposalPopup(contentAssistant, fViewer, controller);
302
303     }
304
305     /**
306      * Creates and returns a context info popup for the given content assistant.
307      *
308      * @param contentAssistant the content assistant
309      * @return the context info popup or <code>null</code>
310      */

311     ContextInformationPopup createContextInfoPopup(ContentAssistant contentAssistant) {
312         if (fContentAssistSubjectControl != null)
313             return new ContextInformationPopup(contentAssistant, fContentAssistSubjectControl);
314         return new ContextInformationPopup(contentAssistant, fViewer);
315
316     }
317
318     /**
319      * Returns the context information validator that should be used to
320      * determine when the currently displayed context information should
321      * be dismissed. The position is used to determine the appropriate
322      * content assist processor to invoke.
323      *
324      * @param contentAssistant the content assistant
325      * @param offset a document offset
326      * @return an validator
327      */

328     public IContextInformationValidator getContextInformationValidator(ContentAssistant contentAssistant, int offset) {
329         if (fContentAssistSubjectControl != null)
330             return contentAssistant.getContextInformationValidator(fContentAssistSubjectControl, offset);
331         return contentAssistant.getContextInformationValidator(fViewer, offset);
332     }
333
334     /**
335      * Returns the context information presenter that should be used to
336      * display context information. The position is used to determine the
337      * appropriate content assist processor to invoke.
338      *
339      * @param contentAssistant the content assistant
340      * @param offset a document offset
341      * @return a presenter
342      */

343     public IContextInformationPresenter getContextInformationPresenter(ContentAssistant contentAssistant, int offset) {
344         if (fContentAssistSubjectControl != null)
345             return contentAssistant.getContextInformationPresenter(fContentAssistSubjectControl, offset);
346         return contentAssistant.getContextInformationPresenter(fViewer, offset);
347     }
348
349     /**
350      * Installs this adapter's information validator on the given context frame.
351      *
352      * @param frame the context frame
353      */

354     public void installValidator(ContextFrame frame) {
355         if (fContentAssistSubjectControl != null) {
356             if (frame.fValidator instanceof ISubjectControlContextInformationValidator)
357                 ((ISubjectControlContextInformationValidator)frame.fValidator).install(frame.fInformation, fContentAssistSubjectControl, frame.fOffset);
358         } else
359             frame.fValidator.install(frame.fInformation, fViewer, frame.fOffset);
360     }
361
362     /**
363      * Installs this adapter's information presenter on the given context frame.
364      *
365      * @param frame the context frame
366      */

367     public void installContextInformationPresenter(ContextFrame frame) {
368         if (fContentAssistSubjectControl != null) {
369             if (frame.fPresenter instanceof ISubjectControlContextInformationPresenter)
370                 ((ISubjectControlContextInformationPresenter)frame.fValidator).install(frame.fInformation, fContentAssistSubjectControl, frame.fBeginOffset);
371         } else
372             frame.fPresenter.install(frame.fInformation, fViewer, frame.fBeginOffset);
373     }
374
375     /**
376      * Returns an array of context information objects computed based
377      * on the specified document position. The position is used to determine
378      * the appropriate content assist processor to invoke.
379      *
380      * @param contentAssistant the content assistant
381      * @param offset a document offset
382      * @return an array of context information objects
383      * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
384      */

385     public IContextInformation[] computeContextInformation(ContentAssistant contentAssistant, int offset) {
386         if (fContentAssistSubjectControl != null)
387             return contentAssistant.computeContextInformation(fContentAssistSubjectControl, offset);
388         return contentAssistant.computeContextInformation(fViewer, offset);
389     }
390
391     /*
392      * @see IContentAssistSubjectControl#addSelectionListener(SelectionListener)
393      */

394     public boolean addSelectionListener(SelectionListener selectionListener) {
395         if (fContentAssistSubjectControl != null)
396             return fContentAssistSubjectControl.addSelectionListener(selectionListener);
397         fViewer.getTextWidget().addSelectionListener(selectionListener);
398         return true;
399     }
400
401     /*
402      * @see IContentAssistSubjectControl#removeSelectionListener(SelectionListener)
403      */

404     public void removeSelectionListener(SelectionListener selectionListener) {
405         if (fContentAssistSubjectControl != null)
406             fContentAssistSubjectControl.removeSelectionListener(selectionListener);
407         else
408             fViewer.getTextWidget().removeSelectionListener(selectionListener);
409     }
410 }
411
Popular Tags