blob: 5e83745e739403000212e25b0695facf878fe5b4 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2005 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
******************************************************************************/
package org.eclipse.jface.fieldassist;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;
/**
* An {@link IControlContentAdapter} for SWT Combo controls. This is a
* convenience class for easily creating a {@link ContentProposalAdapter} for
* combo fields.
*
* <p>
* This API is considered experimental. It is still evolving during 3.2 and is
* subject to change. It is being released to obtain feedback from early
* adopters.
*
* @since 3.2
*/
public class ComboContentAdapter implements IControlContentAdapter {
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.taskassistance.IControlContentAdapter#getControlContents(org.eclipse.swt.widgets.Control)
*/
public String getControlContents(Control control) {
return ((Combo) control).getText();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.fieldassist.IControlContentAdapter#setControlContents(org.eclipse.swt.widgets.Control,
* java.lang.String, int)
*/
public void setControlContents(Control control, String text,
int cursorPosition) {
((Combo) control).setText(text);
((Combo) control).setSelection(new Point(cursorPosition, cursorPosition));
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.fieldassist.IControlContentAdapter#insertControlContents(org.eclipse.swt.widgets.Control,
* java.lang.String, int)
*/
public void insertControlContents(Control control, String text,
int cursorPosition) {
Combo combo = (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);
}
public int getCursorPosition(Control control) {
return ((Combo) control).getSelection().x;
}
}