diff dwtx/jface/fieldassist/TextContentAdapter.d @ 29:f12d40e7da8f

fieldassist
author Frank Benoit <benoit@tionex.de>
date Thu, 03 Apr 2008 18:56:20 +0200
parents
children 46a6e0e6ccd4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/fieldassist/TextContentAdapter.d	Thu Apr 03 18:56:20 2008 +0200
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.fieldassist.TextContentAdapter;
+
+import dwtx.jface.fieldassist.IControlContentAdapter;
+
+import dwt.graphics.Point;
+import dwt.graphics.Rectangle;
+import dwt.widgets.Control;
+import dwt.widgets.Text;
+
+import dwt.dwthelper.utils;
+
+/**
+ * An {@link IControlContentAdapter} for DWT Text controls. This is a
+ * convenience class for easily creating a {@link ContentProposalAdapter} for
+ * text fields.
+ *
+ * @since 3.2
+ */
+public class TextContentAdapter : IControlContentAdapter {
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(dwt.widgets.Control)
+     */
+    public String getControlContents(Control control) {
+        return (cast(Text) control).getText();
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#setControlContents(dwt.widgets.Control,
+     *      java.lang.String, int)
+     */
+    public void setControlContents(Control control, String text,
+            int cursorPosition) {
+        (cast(Text) control).setText(text);
+        (cast(Text) control).setSelection(cursorPosition, cursorPosition);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#insertControlContents(dwt.widgets.Control,
+     *      java.lang.String, int)
+     */
+    public void insertControlContents(Control control, String text,
+            int cursorPosition) {
+        Point selection = (cast(Text) control).getSelection();
+        (cast(Text) control).insert(text);
+        // Insert will leave the cursor at the end of the inserted text. If this
+        // is not what we wanted, reset the selection.
+        if (cursorPosition < text.length) {
+            (cast(Text) control).setSelection(selection.x + cursorPosition,
+                    selection.x + cursorPosition);
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#getCursorPosition(dwt.widgets.Control)
+     */
+    public int getCursorPosition(Control control) {
+        return (cast(Text) control).getCaretPosition();
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#getInsertionBounds(dwt.widgets.Control)
+     */
+    public Rectangle getInsertionBounds(Control control) {
+        Text text = cast(Text) control;
+        Point caretOrigin = text.getCaretLocation();
+        return new Rectangle(caretOrigin.x, caretOrigin.y, 1, text
+                .getLineHeight());
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#setCursorPosition(dwt.widgets.Control,
+     *      int)
+     */
+    public void setCursorPosition(Control control, int position) {
+        (cast(Text) control).setSelection(new Point(position, position));
+    }
+}