view org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/swt/ComboObservableValue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 6be48cf9f95c
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
 *     Ashley Cambrell - bug 198904
 *     Matthew Hall - bug 118516
 *******************************************************************************/
module org.eclipse.jface.internal.databinding.swt.ComboObservableValue;

import java.lang.all;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;

/**
 * @since 3.2
 * 
 */
public class ComboObservableValue : AbstractSWTObservableValue {

    private final Combo combo;
    private final String attribute;
    private bool updating = false;
    private String currentValue;
    private ModifyListener modifyListener;

    /**
     * @param combo
     * @param attribute
     */
    public this(Combo combo, String attribute) {
        super(combo);
        this.combo = combo;
        this.attribute = attribute;
        init();
    }
        
    /**
     * @param realm
     * @param combo
     * @param attribute
     */
    public this(Realm realm, Combo combo, String attribute) {
        super(realm, combo);
        this.combo = combo;
        this.attribute = attribute;
        init();
    }
    
    private void init() {       
        if (attribute.equals(SWTProperties.SELECTION)
                || attribute.equals(SWTProperties.TEXT)) {
            this.currentValue = combo.getText();
            modifyListener = new class() ModifyListener {

                public void modifyText(ModifyEvent e) {
                    if (!updating) {
                        String oldValue = currentValue;
                        currentValue = this.outer.combo
                                .getText();
                        
                        notifyIfChanged(oldValue, currentValue);
                    }
                }
            };
            combo.addModifyListener(modifyListener);
        } else
            throw new IllegalArgumentException();
    }

    public void doSetValue(Object value) {
        String oldValue = combo.getText();
        try {
            updating = true;
            if (attribute.equals(SWTProperties.TEXT)) {
                String stringValue = value !is null ? value.toString() : ""; //$NON-NLS-1$
                combo.setText(stringValue);
            } else if (attribute.equals(SWTProperties.SELECTION)) {
                String items[] = combo.getItems();
                int index = -1;
                if (items !is null && value !is null) {
                    for (int i = 0; i < items.length; i++) {
                        if (value.equals(items[i])) {
                            index = i;
                            break;
                        }
                    }
                    if (index is -1) {
                        combo.setText(cast(String) value);
                    } else {
                        combo.select(index); // -1 will not "unselect"
                    }
                }
            }
        } finally {
            updating = false;
            currentValue = combo.getText();
        }
        
        notifyIfChanged(oldValue, currentValue);
    }

    public Object doGetValue() {
        if (attribute.equals(SWTProperties.TEXT))
            return combo.getText();

        Assert.isTrue(attribute.equals(SWTProperties.SELECTION),
                "unexpected attribute: " + attribute); //$NON-NLS-1$
        // The problem with a ccombo, is that it changes the text and
        // fires before it update its selection index
        return combo.getText();
    }

    public Object getValueType() {
        Assert.isTrue(attribute.equals(SWTProperties.TEXT)
                || attribute.equals(SWTProperties.SELECTION),
                "unexpected attribute: " + attribute); //$NON-NLS-1$
        return String.classinfo;
    }

    /**
     * @return attribute being observed
     */
    public String getAttribute() {
        return attribute;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
     */
    public synchronized void dispose() {
        super.dispose();

        if (modifyListener !is null && !combo.isDisposed()) {
            combo.removeModifyListener(modifyListener);
        }
    }
    
    private void notifyIfChanged(String oldValue, String newValue) {
        if (!oldValue.equals(newValue)) {
            fireValueChange(Diffs.createValueDiff(oldValue, newValue));
        }
    }
}