KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > contentassist > TextContentAssistSubjectAdapter


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.contentassist;
12
13 import java.util.HashMap JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.swt.widgets.Listener;
24 import org.eclipse.swt.widgets.Text;
25
26 import org.eclipse.core.runtime.Assert;
27
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.Document;
30 import org.eclipse.jface.text.IDocument;
31
32
33 /**
34  * Adapts a {@link org.eclipse.swt.widgets.Text} to an {@link org.eclipse.jface.contentassist.IContentAssistSubjectControl}.
35  *
36  * @see org.eclipse.swt.widgets.Text
37  * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl
38  * @since 3.0
39  * @deprecated As of 3.2, replaced by Platform UI's field assist support
40  */

41 public class TextContentAssistSubjectAdapter extends AbstractControlContentAssistSubjectAdapter {
42
43     /**
44      * The document backing this adapter's text widget.
45      */

46     private class InternalDocument extends Document {
47
48         /**
49          * Updates this document with changes in this adapter's text widget.
50          */

51         private ModifyListener fModifyListener;
52
53         private InternalDocument() {
54             super(fText.getText());
55             fModifyListener= new ModifyListener() {
56                 /*
57                  * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
58                  */

59                 public void modifyText(ModifyEvent e) {
60                     set(fText.getText());
61                 }
62             };
63             fText.addModifyListener(fModifyListener);
64         }
65
66         /*
67          * @see org.eclipse.jface.text.AbstractDocument#replace(int, int, java.lang.String)
68          */

69         public void replace(int pos, int length, String JavaDoc text) throws BadLocationException {
70             super.replace(pos, length, text);
71             fText.removeModifyListener(fModifyListener);
72             fText.setText(get());
73             fText.addModifyListener(fModifyListener);
74         }
75     }
76
77     /** The text. */
78     private Text fText;
79     /** The modify listeners. */
80     private HashMap JavaDoc fModifyListeners= new HashMap JavaDoc();
81
82     /**
83      * Creates a content assist subject control adapter for the given text widget.
84      *
85      * @param text the text widget to adapt
86      */

87     public TextContentAssistSubjectAdapter(Text text) {
88         Assert.isNotNull(text);
89         fText= text;
90     }
91
92     /*
93      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getControl()
94      */

95     public Control getControl() {
96         return fText;
97     }
98
99     /*
100      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getLineHeight()
101      */

102     public int getLineHeight() {
103         return fText.getLineHeight();
104     }
105
106     /*
107      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getCaretOffset()
108      */

109     public int getCaretOffset() {
110         return fText.getCaretPosition();
111     }
112
113     /*
114      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getLocationAtOffset(int)
115      */

116     public Point getLocationAtOffset(int offset) {
117         Point caretLocation= fText.getCaretLocation();
118         /*
119          * XXX workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=52520
120          */

121         caretLocation.y += 2;
122         return caretLocation;
123     }
124
125     /*
126      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getWidgetSelectionRange()
127      */

128     public Point getWidgetSelectionRange() {
129         return new Point(fText.getSelection().x, Math.abs(fText.getSelection().y - fText.getSelection().x));
130     }
131
132     /*
133      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getSelectedRange()
134      */

135     public Point getSelectedRange() {
136         return new Point(fText.getSelection().x, Math.abs(fText.getSelection().y - fText.getSelection().x));
137     }
138
139     /*
140      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#getDocument()
141      */

142     public IDocument getDocument() {
143         IDocument document= (IDocument)fText.getData("document"); //$NON-NLS-1$
144
if (document == null) {
145             document= new InternalDocument() ;
146             fText.setData("document", document); //$NON-NLS-1$
147
}
148         return document;
149     }
150
151     /*
152      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#setSelectedRange(int, int)
153      */

154     public void setSelectedRange(int i, int j) {
155         fText.setSelection(new Point(i, i+j));
156     }
157
158     /*
159      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#revealRange(int, int)
160      */

161     public void revealRange(int i, int j) {
162         // XXX: this should be improved
163
fText.setSelection(new Point(i, i+j));
164     }
165
166     /*
167      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#addSelectionListener(org.eclipse.swt.events.SelectionListener)
168      */

169     public boolean addSelectionListener(final SelectionListener selectionListener) {
170         fText.addSelectionListener(selectionListener);
171         Listener listener= new Listener() {
172             /*
173              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
174              */

175             public void handleEvent(Event e) {
176                 selectionListener.widgetSelected(new SelectionEvent(e));
177
178             }
179         };
180         fText.addListener(SWT.Modify, listener);
181         fModifyListeners.put(selectionListener, listener);
182         return true;
183     }
184
185     /*
186      * @see org.eclipse.jface.contentassist.IContentAssistSubjectControl#removeSelectionListener(org.eclipse.swt.events.SelectionListener)
187      */

188     public void removeSelectionListener(SelectionListener selectionListener) {
189         fText.removeSelectionListener(selectionListener);
190         Object JavaDoc listener= fModifyListeners.get(selectionListener);
191         if (listener instanceof Listener)
192             fText.removeListener(SWT.Modify, (Listener)listener);
193     }
194 }
195
Popular Tags