29
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.fieldassist.TextContentAdapter;
|
|
14
|
|
15 import dwtx.jface.fieldassist.IControlContentAdapter;
|
|
16
|
|
17 import dwt.graphics.Point;
|
|
18 import dwt.graphics.Rectangle;
|
|
19 import dwt.widgets.Control;
|
|
20 import dwt.widgets.Text;
|
|
21
|
|
22 import dwt.dwthelper.utils;
|
|
23
|
|
24 /**
|
|
25 * An {@link IControlContentAdapter} for DWT Text controls. This is a
|
|
26 * convenience class for easily creating a {@link ContentProposalAdapter} for
|
|
27 * text fields.
|
|
28 *
|
|
29 * @since 3.2
|
|
30 */
|
|
31 public class TextContentAdapter : IControlContentAdapter {
|
|
32
|
|
33 /*
|
|
34 * (non-Javadoc)
|
|
35 *
|
|
36 * @see dwtx.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(dwt.widgets.Control)
|
|
37 */
|
|
38 public String getControlContents(Control control) {
|
|
39 return (cast(Text) control).getText();
|
|
40 }
|
|
41
|
|
42 /*
|
|
43 * (non-Javadoc)
|
|
44 *
|
|
45 * @see dwtx.jface.fieldassist.IControlContentAdapter#setControlContents(dwt.widgets.Control,
|
|
46 * java.lang.String, int)
|
|
47 */
|
|
48 public void setControlContents(Control control, String text,
|
|
49 int cursorPosition) {
|
|
50 (cast(Text) control).setText(text);
|
|
51 (cast(Text) control).setSelection(cursorPosition, cursorPosition);
|
|
52 }
|
|
53
|
|
54 /*
|
|
55 * (non-Javadoc)
|
|
56 *
|
|
57 * @see dwtx.jface.fieldassist.IControlContentAdapter#insertControlContents(dwt.widgets.Control,
|
|
58 * java.lang.String, int)
|
|
59 */
|
|
60 public void insertControlContents(Control control, String text,
|
|
61 int cursorPosition) {
|
|
62 Point selection = (cast(Text) control).getSelection();
|
|
63 (cast(Text) control).insert(text);
|
|
64 // Insert will leave the cursor at the end of the inserted text. If this
|
|
65 // is not what we wanted, reset the selection.
|
|
66 if (cursorPosition < text.length) {
|
|
67 (cast(Text) control).setSelection(selection.x + cursorPosition,
|
|
68 selection.x + cursorPosition);
|
|
69 }
|
|
70 }
|
|
71
|
|
72 /*
|
|
73 * (non-Javadoc)
|
|
74 *
|
|
75 * @see dwtx.jface.fieldassist.IControlContentAdapter#getCursorPosition(dwt.widgets.Control)
|
|
76 */
|
|
77 public int getCursorPosition(Control control) {
|
|
78 return (cast(Text) control).getCaretPosition();
|
|
79 }
|
|
80
|
|
81 /*
|
|
82 * (non-Javadoc)
|
|
83 *
|
|
84 * @see dwtx.jface.fieldassist.IControlContentAdapter#getInsertionBounds(dwt.widgets.Control)
|
|
85 */
|
|
86 public Rectangle getInsertionBounds(Control control) {
|
|
87 Text text = cast(Text) control;
|
|
88 Point caretOrigin = text.getCaretLocation();
|
|
89 return new Rectangle(caretOrigin.x, caretOrigin.y, 1, text
|
|
90 .getLineHeight());
|
|
91 }
|
|
92
|
|
93 /*
|
|
94 * (non-Javadoc)
|
|
95 *
|
|
96 * @see dwtx.jface.fieldassist.IControlContentAdapter#setCursorPosition(dwt.widgets.Control,
|
|
97 * int)
|
|
98 */
|
|
99 public void setCursorPosition(Control control, int position) {
|
|
100 (cast(Text) control).setSelection(new Point(position, position));
|
|
101 }
|
|
102 }
|