KickJava   Java API By Example, From Geeks To Geeks.

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


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.GC;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.widgets.Combo;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Listener;
26
27 import org.eclipse.core.runtime.Assert;
28
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.Document;
31 import org.eclipse.jface.text.IDocument;
32
33
34
35 /**
36  * Adapts a {@link org.eclipse.swt.widgets.Combo} to a {@link org.eclipse.jface.contentassist.IContentAssistSubjectControl}.
37  *
38  * <p>
39  * Known issues:
40  * <ul>
41  * <li>https://bugs.eclipse.org/bugs/show_bug.cgi?id=50121
42  * = &gt; Combo doesn't get Arrow_up/Down keys on GTK</li>
43  *
44  * <li>https://bugs.eclipse.org/bugs/show_bug.cgi?id=50123
45  * = &gt; Combo broken on MacOS X</li>
46  * </ul>
47  * </p>
48  *
49  * @since 3.0
50  * @deprecated As of 3.2, replaced by Platform UI's field assist support
51  */

52 public class ComboContentAssistSubjectAdapter extends AbstractControlContentAssistSubjectAdapter {
53
54     /**
55      * The document of {@link #fCombo}'s contents.
56      */

57     private class InternalDocument extends Document {
58         /**
59          * Updates this document with changes in {@link #fCombo}.
60          */

61         private ModifyListener fModifyListener;
62
63         private InternalDocument() {
64             super(fCombo.getText());
65             fModifyListener= new ModifyListener() {
66                 /*
67                  * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
68                  */

69                 public void modifyText(ModifyEvent e) {
70                     set(fCombo.getText());
71                 }
72             };
73             fCombo.addModifyListener(fModifyListener);
74         }
75
76         /*
77          * @see org.eclipse.jface.text.AbstractDocument#replace(int, int, java.lang.String)
78          */

79         public void replace(int pos, int length, String JavaDoc text) throws BadLocationException {
80             super.replace(pos, length, text);
81             fCombo.removeModifyListener(fModifyListener);
82             fCombo.setText(get());
83             fCombo.addModifyListener(fModifyListener);
84         }
85     }
86
87     /**
88      * The combo widget.
89      */

90     private Combo fCombo;
91     private HashMap JavaDoc fModifyListeners;
92
93     /**
94      * Creates a content assist subject control adapter for the given combo.
95      *
96      * @param combo the combo to adapt
97      */

98     public ComboContentAssistSubjectAdapter(Combo combo) {
99         Assert.isNotNull(combo);
100         fCombo= combo;
101         fModifyListeners= new HashMap JavaDoc();
102      }
103
104     /*
105      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getControl()
106      */

107     public Control getControl() {
108         return fCombo;
109     }
110
111     /*
112      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getLineHeight()
113      */

114     public int getLineHeight() {
115         return fCombo.getTextHeight();
116     }
117
118     /*
119      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getCaretOffset()
120      */

121     public int getCaretOffset() {
122         return fCombo.getSelection().y;
123     }
124
125     /*
126      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getLocationAtOffset(int)
127      */

128     public Point getLocationAtOffset(int offset) {
129         String JavaDoc comboString= fCombo.getText();
130         GC gc = new GC(fCombo);
131         gc.setFont(fCombo.getFont());
132         Point extent= gc.textExtent(comboString.substring(0, Math.min(offset, comboString.length())));
133         int spaceWidth= gc.textExtent(" ").x; //$NON-NLS-1$
134
gc.dispose();
135         /*
136          * XXX: the two space widths below is a workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=44072
137          */

138         int x= 2 * spaceWidth + fCombo.getClientArea().x + fCombo.getBorderWidth() + extent.x;
139         return new Point(x, fCombo.getClientArea().y);
140     }
141
142     /*
143      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getSelectionRange()
144      */

145     public Point getWidgetSelectionRange() {
146         return new Point(fCombo.getSelection().x, Math.abs(fCombo.getSelection().y - fCombo.getSelection().x));
147     }
148
149     /*
150      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getSelectedRange()
151      */

152     public Point getSelectedRange() {
153         return new Point(fCombo.getSelection().x, Math.abs(fCombo.getSelection().y - fCombo.getSelection().x));
154     }
155
156     /*
157      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#getDocument()
158      */

159     public IDocument getDocument() {
160         IDocument document= (IDocument)fCombo.getData("document"); //$NON-NLS-1$
161
if (document == null) {
162             document= new InternalDocument() ;
163             fCombo.setData("document", document); //$NON-NLS-1$
164
}
165         return document;
166     }
167
168     /*
169      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#setSelectedRange(int, int)
170      */

171     public void setSelectedRange(int i, int j) {
172         fCombo.setSelection(new Point(i, i+j));
173     }
174
175     /*
176      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#revealRange(int, int)
177      */

178     public void revealRange(int i, int j) {
179         // XXX: this should be improved
180
fCombo.setSelection(new Point(i, i+j));
181     }
182
183     /*
184      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#addSelectionListener(org.eclipse.swt.events.SelectionListener)
185      */

186     public boolean addSelectionListener(final SelectionListener selectionListener) {
187         fCombo.addSelectionListener(selectionListener);
188         Listener listener= new Listener() {
189             /*
190              * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
191              */

192             public void handleEvent(Event e) {
193                 selectionListener.widgetSelected(new SelectionEvent(e));
194
195             }
196         };
197         fCombo.addListener(SWT.Modify, listener);
198         fModifyListeners.put(selectionListener, listener);
199         return true;
200     }
201
202     /*
203      * @see org.eclipse.jface.text.contentassist.IContentAssistSubjectControl#removeSelectionListener(org.eclipse.swt.events.SelectionListener)
204      */

205     public void removeSelectionListener(SelectionListener selectionListener) {
206         fCombo.removeSelectionListener(selectionListener);
207         Object JavaDoc listener= fModifyListeners.get(selectionListener);
208         if (listener instanceof Listener)
209             fCombo.removeListener(SWT.Modify, (Listener)listener);
210     }
211 }
212
Popular Tags