KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > fieldassist > TextContentAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.fieldassist;
12
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.graphics.Rectangle;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Text;
17
18 /**
19  * An {@link IControlContentAdapter} for SWT Text controls. This is a
20  * convenience class for easily creating a {@link ContentProposalAdapter} for
21  * text fields.
22  *
23  * @since 3.2
24  */

25 public class TextContentAdapter implements IControlContentAdapter {
26
27     /*
28      * (non-Javadoc)
29      *
30      * @see org.eclipse.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(org.eclipse.swt.widgets.Control)
31      */

32     public String JavaDoc getControlContents(Control control) {
33         return ((Text) control).getText();
34     }
35
36     /*
37      * (non-Javadoc)
38      *
39      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control,
40      * java.lang.String, int)
41      */

42     public void setControlContents(Control control, String JavaDoc text,
43             int cursorPosition) {
44         ((Text) control).setText(text);
45         ((Text) control).setSelection(cursorPosition, cursorPosition);
46     }
47
48     /*
49      * (non-Javadoc)
50      *
51      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control,
52      * java.lang.String, int)
53      */

54     public void insertControlContents(Control control, String JavaDoc text,
55             int cursorPosition) {
56         Point selection = ((Text) control).getSelection();
57         ((Text) control).insert(text);
58         // Insert will leave the cursor at the end of the inserted text. If this
59
// is not what we wanted, reset the selection.
60
if (cursorPosition < text.length()) {
61             ((Text) control).setSelection(selection.x + cursorPosition,
62                     selection.x + cursorPosition);
63         }
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse.swt.widgets.Control)
70      */

71     public int getCursorPosition(Control control) {
72         return ((Text) control).getCaretPosition();
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse.swt.widgets.Control)
79      */

80     public Rectangle getInsertionBounds(Control control) {
81         Text text = (Text) control;
82         Point caretOrigin = text.getCaretLocation();
83         return new Rectangle(caretOrigin.x, caretOrigin.y, 1, text
84                 .getLineHeight());
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse.swt.widgets.Control,
91      * int)
92      */

93     public void setCursorPosition(Control control, int position) {
94         ((Text) control).setSelection(new Point(position, position));
95     }
96 }
97
Popular Tags