diff dwtx/jface/fieldassist/ComboContentAdapter.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/ComboContentAdapter.d	Thu Apr 03 18:56:20 2008 +0200
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * 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.ComboContentAdapter;
+
+import dwtx.jface.fieldassist.IControlContentAdapter;
+
+import dwt.graphics.GC;
+import dwt.graphics.Point;
+import dwt.graphics.Rectangle;
+import dwt.widgets.Combo;
+import dwt.widgets.Control;
+
+import dwt.dwthelper.utils;
+import tango.text.Text;
+alias tango.text.Text.Text!(char) StringBuffer;
+/**
+ * An {@link IControlContentAdapter} for DWT Combo controls. This is a
+ * convenience class for easily creating a {@link ContentProposalAdapter} for
+ * combo fields.
+ *
+ * @since 3.2
+ */
+public class ComboContentAdapter : IControlContentAdapter {
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(dwt.widgets.Control)
+     */
+    public String getControlContents(Control control) {
+        return (cast(Combo) 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(Combo) control).setText(text);
+        (cast(Combo) control)
+                .setSelection(new Point(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) {
+        Combo combo = cast(Combo) control;
+        String contents = combo.getText();
+        Point selection = combo.getSelection();
+        StringBuffer sb = new StringBuffer();
+        sb.append(contents.substring(0, selection.x));
+        sb.append(text);
+        if (selection.y < contents.length) {
+            sb.append(contents.substring(selection.y, contents.length));
+        }
+        combo.setText(sb.toString());
+        selection.x = selection.x + cursorPosition;
+        selection.y = selection.x;
+        combo.setSelection(selection);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#getCursorPosition(dwt.widgets.Control)
+     */
+    public int getCursorPosition(Control control) {
+        return (cast(Combo) control).getSelection().x;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#getInsertionBounds(dwt.widgets.Control)
+     */
+    public Rectangle getInsertionBounds(Control control) {
+        Combo combo = cast(Combo) control;
+        int position = combo.getSelection().y;
+        String contents = combo.getText();
+        GC gc = new GC(combo);
+        gc.setFont(combo.getFont());
+        Point extent = gc.textExtent(contents.substring(0, Math.min(position,
+                contents.length)));
+        gc.dispose();
+        return new Rectangle(combo.getClientArea().x + extent.x, combo
+                .getClientArea().y, 1, combo.getClientArea().height);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.fieldassist.IControlContentAdapter#setCursorPosition(dwt.widgets.Control,
+     *      int)
+     */
+    public void setCursorPosition(Control control, int index) {
+        (cast(Combo) control).setSelection(new Point(index, index));
+    }
+
+}