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.ComboContentAdapter;
|
|
14
|
|
15 import dwtx.jface.fieldassist.IControlContentAdapter;
|
|
16
|
|
17 import dwt.graphics.GC;
|
|
18 import dwt.graphics.Point;
|
|
19 import dwt.graphics.Rectangle;
|
|
20 import dwt.widgets.Combo;
|
|
21 import dwt.widgets.Control;
|
|
22
|
|
23 import dwt.dwthelper.utils;
|
|
24 import tango.text.Text;
|
|
25 alias tango.text.Text.Text!(char) StringBuffer;
|
|
26 /**
|
|
27 * An {@link IControlContentAdapter} for DWT Combo controls. This is a
|
|
28 * convenience class for easily creating a {@link ContentProposalAdapter} for
|
|
29 * combo fields.
|
|
30 *
|
|
31 * @since 3.2
|
|
32 */
|
|
33 public class ComboContentAdapter : IControlContentAdapter {
|
|
34
|
|
35 /*
|
|
36 * (non-Javadoc)
|
|
37 *
|
|
38 * @see dwtx.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(dwt.widgets.Control)
|
|
39 */
|
|
40 public String getControlContents(Control control) {
|
|
41 return (cast(Combo) control).getText();
|
|
42 }
|
|
43
|
|
44 /*
|
|
45 * (non-Javadoc)
|
|
46 *
|
|
47 * @see dwtx.jface.fieldassist.IControlContentAdapter#setControlContents(dwt.widgets.Control,
|
|
48 * java.lang.String, int)
|
|
49 */
|
|
50 public void setControlContents(Control control, String text,
|
|
51 int cursorPosition) {
|
|
52 (cast(Combo) control).setText(text);
|
|
53 (cast(Combo) control)
|
|
54 .setSelection(new Point(cursorPosition, cursorPosition));
|
|
55 }
|
|
56
|
|
57 /*
|
|
58 * (non-Javadoc)
|
|
59 *
|
|
60 * @see dwtx.jface.fieldassist.IControlContentAdapter#insertControlContents(dwt.widgets.Control,
|
|
61 * java.lang.String, int)
|
|
62 */
|
|
63 public void insertControlContents(Control control, String text,
|
|
64 int cursorPosition) {
|
|
65 Combo combo = cast(Combo) control;
|
|
66 String contents = combo.getText();
|
|
67 Point selection = combo.getSelection();
|
|
68 StringBuffer sb = new StringBuffer();
|
|
69 sb.append(contents.substring(0, selection.x));
|
|
70 sb.append(text);
|
|
71 if (selection.y < contents.length) {
|
|
72 sb.append(contents.substring(selection.y, contents.length));
|
|
73 }
|
|
74 combo.setText(sb.toString());
|
|
75 selection.x = selection.x + cursorPosition;
|
|
76 selection.y = selection.x;
|
|
77 combo.setSelection(selection);
|
|
78 }
|
|
79
|
|
80 /*
|
|
81 * (non-Javadoc)
|
|
82 *
|
|
83 * @see dwtx.jface.fieldassist.IControlContentAdapter#getCursorPosition(dwt.widgets.Control)
|
|
84 */
|
|
85 public int getCursorPosition(Control control) {
|
|
86 return (cast(Combo) control).getSelection().x;
|
|
87 }
|
|
88
|
|
89 /*
|
|
90 * (non-Javadoc)
|
|
91 *
|
|
92 * @see dwtx.jface.fieldassist.IControlContentAdapter#getInsertionBounds(dwt.widgets.Control)
|
|
93 */
|
|
94 public Rectangle getInsertionBounds(Control control) {
|
|
95 Combo combo = cast(Combo) control;
|
|
96 int position = combo.getSelection().y;
|
|
97 String contents = combo.getText();
|
|
98 GC gc = new GC(combo);
|
|
99 gc.setFont(combo.getFont());
|
|
100 Point extent = gc.textExtent(contents.substring(0, Math.min(position,
|
|
101 contents.length)));
|
|
102 gc.dispose();
|
|
103 return new Rectangle(combo.getClientArea().x + extent.x, combo
|
|
104 .getClientArea().y, 1, combo.getClientArea().height);
|
|
105 }
|
|
106
|
|
107 /*
|
|
108 * (non-Javadoc)
|
|
109 *
|
|
110 * @see dwtx.jface.fieldassist.IControlContentAdapter#setCursorPosition(dwt.widgets.Control,
|
|
111 * int)
|
|
112 */
|
|
113 public void setCursorPosition(Control control, int index) {
|
|
114 (cast(Combo) control).setSelection(new Point(index, index));
|
|
115 }
|
|
116
|
|
117 }
|