Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/fieldassist/ComboContentAdapter.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | dccb717aa902 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2008 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 org.eclipse.jface.fieldassist.ComboContentAdapter; | |
14 | |
15 import org.eclipse.swt.SWT; | |
16 import org.eclipse.jface.fieldassist.IControlContentAdapter; | |
17 import org.eclipse.jface.fieldassist.IControlContentAdapter2; | |
18 | |
19 import org.eclipse.swt.graphics.GC; | |
20 import org.eclipse.swt.graphics.Point; | |
21 import org.eclipse.swt.graphics.Rectangle; | |
22 import org.eclipse.swt.widgets.Combo; | |
23 import org.eclipse.swt.widgets.Control; | |
24 | |
25 import java.lang.all; | |
26 import java.util.Set; | |
27 import tango.text.Text; | |
28 alias tango.text.Text.Text!(char) StringBuffer; | |
29 /** | |
30 * An {@link IControlContentAdapter} for SWT Combo controls. This is a | |
31 * convenience class for easily creating a {@link ContentProposalAdapter} for | |
32 * combo fields. | |
33 * | |
34 * @since 3.2 | |
35 */ | |
36 public class ComboContentAdapter : IControlContentAdapter, | |
37 IControlContentAdapter2 { | |
38 | |
39 /* | |
40 * Set to <code>true</code> if we should compute the text | |
41 * vertical bounds rather than just use the field size. | |
42 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=164748 | |
43 * The corresponding SWT bug is | |
44 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=44072 | |
45 */ | |
46 private static final bool COMPUTE_TEXT_USING_CLIENTAREA = !"carbon".equals(SWT.getPlatform()); //$NON-NLS-1$ | |
47 | |
48 | |
49 /* | |
50 * (non-Javadoc) | |
51 * | |
52 * @see org.eclipse.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(org.eclipse.swt.widgets.Control) | |
53 */ | |
54 public String getControlContents(Control control) { | |
55 return (cast(Combo) control).getText(); | |
56 } | |
57 | |
58 /* | |
59 * (non-Javadoc) | |
60 * | |
61 * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control, | |
62 * java.lang.String, int) | |
63 */ | |
64 public void setControlContents(Control control, String text, | |
65 int cursorPosition) { | |
66 (cast(Combo) control).setText(text); | |
67 (cast(Combo) control) | |
68 .setSelection(new Point(cursorPosition, cursorPosition)); | |
69 } | |
70 | |
71 /* | |
72 * (non-Javadoc) | |
73 * | |
74 * @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control, | |
75 * java.lang.String, int) | |
76 */ | |
77 public void insertControlContents(Control control, String text, | |
78 int cursorPosition) { | |
79 Combo combo = cast(Combo) control; | |
80 String contents = combo.getText(); | |
81 Point selection = combo.getSelection(); | |
82 StringBuffer sb = new StringBuffer(); | |
83 sb.append(contents.substring(0, selection.x)); | |
84 sb.append(text); | |
85 if (selection.y < contents.length) { | |
86 sb.append(contents.substring(selection.y, contents.length)); | |
87 } | |
88 combo.setText(sb.toString()); | |
89 selection.x = selection.x + cursorPosition; | |
90 selection.y = selection.x; | |
91 combo.setSelection(selection); | |
92 } | |
93 | |
94 /* | |
95 * (non-Javadoc) | |
96 * | |
97 * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse.swt.widgets.Control) | |
98 */ | |
99 public int getCursorPosition(Control control) { | |
100 return (cast(Combo) control).getSelection().x; | |
101 } | |
102 | |
103 /* | |
104 * (non-Javadoc) | |
105 * | |
106 * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse.swt.widgets.Control) | |
107 */ | |
108 public Rectangle getInsertionBounds(Control control) { | |
109 // This doesn't take horizontal scrolling into affect. | |
110 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=204599 | |
111 Combo combo = cast(Combo) control; | |
112 int position = combo.getSelection().y; | |
113 String contents = combo.getText(); | |
114 GC gc = new GC(combo); | |
115 gc.setFont(combo.getFont()); | |
116 Point extent = gc.textExtent(contents.substring(0, Math.min(position, | |
117 contents.length))); | |
118 gc.dispose(); | |
119 if (COMPUTE_TEXT_USING_CLIENTAREA) { | |
120 return new Rectangle(combo.getClientArea().x + extent.x, combo | |
121 .getClientArea().y, 1, combo.getClientArea().height); | |
122 } | |
123 return new Rectangle(extent.x, 0, 1, combo.getSize().y); | |
124 } | |
125 | |
126 /* | |
127 * (non-Javadoc) | |
128 * | |
129 * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse.swt.widgets.Control, | |
130 * int) | |
131 */ | |
132 public void setCursorPosition(Control control, int index) { | |
133 (cast(Combo) control).setSelection(new Point(index, index)); | |
134 } | |
135 | |
136 /** | |
137 * @see org.eclipse.jface.fieldassist.IControlContentAdapter2#getSelection(org.eclipse.swt.widgets.Control) | |
138 * | |
139 * @since 3.4 | |
140 */ | |
141 public Point getSelection(Control control) { | |
142 return (cast(Combo) control).getSelection(); | |
143 } | |
144 | |
145 /** | |
146 * @see org.eclipse.jface.fieldassist.IControlContentAdapter2#setSelection(org.eclipse.swt.widgets.Control, | |
147 * org.eclipse.swt.graphics.Point) | |
148 * | |
149 * @since 3.4 | |
150 */ | |
151 public void setSelection(Control control, Point range) { | |
152 (cast(Combo) control).setSelection(range); | |
153 } | |
154 | |
155 } |