001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.fieldassist;
011:
012: import org.eclipse.swt.graphics.GC;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.graphics.Rectangle;
015: import org.eclipse.swt.widgets.Combo;
016: import org.eclipse.swt.widgets.Control;
017:
018: /**
019: * An {@link IControlContentAdapter} for SWT Combo controls. This is a
020: * convenience class for easily creating a {@link ContentProposalAdapter} for
021: * combo fields.
022: *
023: * @since 3.2
024: */
025: public class ComboContentAdapter implements IControlContentAdapter {
026:
027: /*
028: * (non-Javadoc)
029: *
030: * @see org.eclipse.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(org.eclipse.swt.widgets.Control)
031: */
032: public String getControlContents(Control control) {
033: return ((Combo) control).getText();
034: }
035:
036: /*
037: * (non-Javadoc)
038: *
039: * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control,
040: * java.lang.String, int)
041: */
042: public void setControlContents(Control control, String text,
043: int cursorPosition) {
044: ((Combo) control).setText(text);
045: ((Combo) control).setSelection(new Point(cursorPosition,
046: cursorPosition));
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control,
053: * java.lang.String, int)
054: */
055: public void insertControlContents(Control control, String text,
056: int cursorPosition) {
057: Combo combo = (Combo) control;
058: String contents = combo.getText();
059: Point selection = combo.getSelection();
060: StringBuffer sb = new StringBuffer();
061: sb.append(contents.substring(0, selection.x));
062: sb.append(text);
063: if (selection.y < contents.length()) {
064: sb.append(contents
065: .substring(selection.y, contents.length()));
066: }
067: combo.setText(sb.toString());
068: selection.x = selection.x + cursorPosition;
069: selection.y = selection.x;
070: combo.setSelection(selection);
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getCursorPosition(org.eclipse.swt.widgets.Control)
077: */
078: public int getCursorPosition(Control control) {
079: return ((Combo) control).getSelection().x;
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse.swt.widgets.Control)
086: */
087: public Rectangle getInsertionBounds(Control control) {
088: Combo combo = (Combo) control;
089: int position = combo.getSelection().y;
090: String contents = combo.getText();
091: GC gc = new GC(combo);
092: gc.setFont(combo.getFont());
093: Point extent = gc.textExtent(contents.substring(0, Math.min(
094: position, contents.length())));
095: gc.dispose();
096: return new Rectangle(combo.getClientArea().x + extent.x, combo
097: .getClientArea().y, 1, combo.getClientArea().height);
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.eclipse.jface.fieldassist.IControlContentAdapter#setCursorPosition(org.eclipse.swt.widgets.Control,
104: * int)
105: */
106: public void setCursorPosition(Control control, int index) {
107: ((Combo) control).setSelection(new Point(index, index));
108: }
109:
110: }
|