Search in sources :

Example 1 with ValueListener

use of org.springsource.ide.eclipse.commons.livexp.core.ValueListener in project eclipse-integration-commons by spring-projects.

the class ChooseOneSectionCombo method createContents.

@Override
public void createContents(Composite page) {
    Composite field = new Composite(page, SWT.NONE);
    GridLayout layout = GridLayoutFactory.fillDefaults().numColumns(2).create();
    field.setLayout(layout);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(field);
    Label fieldNameLabel = new Label(field, SWT.NONE);
    fieldNameLabel.setText(label);
    GridDataFactory labelGridData = GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.CENTER);
    if (useFieldLabelWidthHint) {
        labelGridData.hint(UIConstants.fieldLabelWidthHint(fieldNameLabel), SWT.DEFAULT);
    }
    labelGridData.applyTo(fieldNameLabel);
    final Combo combo = new Combo(field, inputParser == null ? SWT.READ_ONLY : SWT.NONE);
    options.addListener(new ValueListener<T[]>() {

        public void gotValue(org.springsource.ide.eclipse.commons.livexp.core.LiveExpression<T[]> exp, T[] value) {
            if (combo != null) {
                String oldText = combo.getText();
                // This will clear the selection sometimes
                combo.setItems(getLabels());
                combo_setText(combo, oldText);
            }
        }
    });
    GridDataFactory gridData = GridDataFactory.fillDefaults();
    if (inputParser != null) {
        gridData = gridData.hint(FIELD_TEXT_AREA_WIDTH, SWT.DEFAULT).minSize(FIELD_TEXT_AREA_WIDTH, SWT.DEFAULT);
    }
    if (grabHorizontal) {
        gridData = gridData.grab(grabHorizontal, false);
    }
    gridData.applyTo(combo);
    combo.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            handleModifyText(combo);
        }
    });
    selection.selection.addListener(UIValueListener.from((exp, newSelection) -> {
        if (newSelection != null && !combo.isDisposed()) {
            // Technically, not entirely correct. This might
            // select the wrong element if more than one option
            // has the same label text.
            String newText = labelProvider.getText(newSelection);
            combo_setText(combo, newText);
            if (!combo.getText().equals(newText)) {
                // widget rejected the selection. To avoid widget state
                // and model state getting out-of-sync, refelct current
                // widget state back to the model:
                handleModifyText(combo);
            }
        }
    }));
}
Also used : FIELD_TEXT_AREA_WIDTH(org.springsource.ide.eclipse.commons.livexp.ui.UIConstants.FIELD_TEXT_AREA_WIDTH) Combo(org.eclipse.swt.widgets.Combo) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Collection(java.util.Collection) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) Assert(org.eclipse.core.runtime.Assert) SelectionModel(org.springsource.ide.eclipse.commons.livexp.core.SelectionModel) FieldModel(org.springsource.ide.eclipse.commons.livexp.core.FieldModel) UIValueListener(org.springsource.ide.eclipse.commons.livexp.core.UIValueListener) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) Objects(java.util.Objects) LiveExpression(org.springsource.ide.eclipse.commons.livexp.core.LiveExpression) ModifyListener(org.eclipse.swt.events.ModifyListener) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) ValidationResult(org.springsource.ide.eclipse.commons.livexp.core.ValidationResult) ValueListener(org.springsource.ide.eclipse.commons.livexp.core.ValueListener) Label(org.eclipse.swt.widgets.Label) LiveVariable(org.springsource.ide.eclipse.commons.livexp.core.LiveVariable) Parser(org.springsource.ide.eclipse.commons.livexp.util.Parser) GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) ModifyListener(org.eclipse.swt.events.ModifyListener) Label(org.eclipse.swt.widgets.Label) Combo(org.eclipse.swt.widgets.Combo) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) GridLayout(org.eclipse.swt.layout.GridLayout) ModifyEvent(org.eclipse.swt.events.ModifyEvent) SWT(org.eclipse.swt.SWT)

Aggregations

Collection (java.util.Collection)1 Objects (java.util.Objects)1 Assert (org.eclipse.core.runtime.Assert)1 GridDataFactory (org.eclipse.jface.layout.GridDataFactory)1 GridLayoutFactory (org.eclipse.jface.layout.GridLayoutFactory)1 SWT (org.eclipse.swt.SWT)1 ModifyEvent (org.eclipse.swt.events.ModifyEvent)1 ModifyListener (org.eclipse.swt.events.ModifyListener)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Combo (org.eclipse.swt.widgets.Combo)1 Composite (org.eclipse.swt.widgets.Composite)1 Label (org.eclipse.swt.widgets.Label)1 FieldModel (org.springsource.ide.eclipse.commons.livexp.core.FieldModel)1 LiveExpression (org.springsource.ide.eclipse.commons.livexp.core.LiveExpression)1 LiveVariable (org.springsource.ide.eclipse.commons.livexp.core.LiveVariable)1 SelectionModel (org.springsource.ide.eclipse.commons.livexp.core.SelectionModel)1 UIValueListener (org.springsource.ide.eclipse.commons.livexp.core.UIValueListener)1 ValidationResult (org.springsource.ide.eclipse.commons.livexp.core.ValidationResult)1 ValueListener (org.springsource.ide.eclipse.commons.livexp.core.ValueListener)1 FIELD_TEXT_AREA_WIDTH (org.springsource.ide.eclipse.commons.livexp.ui.UIConstants.FIELD_TEXT_AREA_WIDTH)1