view dwtx/jface/viewers/ComboBoxViewerCellEditor.d @ 70:46a6e0e6ccd4

Merge with d-fied sources of 3.4M7
author Frank Benoit <benoit@tionex.de>
date Thu, 22 May 2008 01:36:46 +0200
parents
children 4878bef4a38e
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2006 Tom Schindl 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 *                                                 bugfix in 174739
 *     Eric Rizzo - bug 213315
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/

module dwtx.jface.viewers.ComboBoxViewerCellEditor;

import java.text.MessageFormat;

import dwt.DWT;
import dwt.custom.CCombo;
import dwt.events.FocusAdapter;
import dwt.events.FocusEvent;
import dwt.events.KeyAdapter;
import dwt.events.KeyEvent;
import dwt.events.SelectionAdapter;
import dwt.events.SelectionEvent;
import dwt.events.TraverseEvent;
import dwt.events.TraverseListener;
import dwt.graphics.GC;
import dwt.widgets.Composite;
import dwt.widgets.Control;
import dwtx.core.runtime.Assert;

/**
 * A cell editor that presents a list of items in a combo box. In contrast to
 * {@link ComboBoxCellEditor} it wraps the underlying {@link CCombo} using a
 * {@link ComboViewer}
 */
public class ComboBoxViewerCellEditor extends AbstractComboBoxCellEditor {

    /**
     * The custom combo box control.
     */
    ComboViewer viewer;

    Object selectedValue;

    /**
     * Default ComboBoxCellEditor style
     */
    private static final int defaultStyle = DWT.NONE;

    /**
     * Creates a new cell editor with a combo viewer and a default style
     *
     * @param parent
     *            the parent control
     */
    public ComboBoxViewerCellEditor(Composite parent) {
        this(parent, defaultStyle);
    }

    /**
     * Creates a new cell editor with a combo viewer and the given style
     *
     * @param parent
     *            the parent control
     * @param style
     *            the style bits
     */
    public ComboBoxViewerCellEditor(Composite parent, int style) {
        super(parent, style);
        setValueValid(true);
    }

    /*
     * (non-Javadoc) Method declared on CellEditor.
     */
    protected Control createControl(Composite parent) {

        CCombo comboBox = new CCombo(parent, getStyle());
        comboBox.setFont(parent.getFont());
        viewer = new ComboViewer(comboBox);

        comboBox.addKeyListener(new KeyAdapter() {
            // hook key pressed - see PR 14201
            public void keyPressed(KeyEvent e) {
                keyReleaseOccured(e);
            }
        });

        comboBox.addSelectionListener(new SelectionAdapter() {
            public void widgetDefaultSelected(SelectionEvent event) {
                applyEditorValueAndDeactivate();
            }

            public void widgetSelected(SelectionEvent event) {
                ISelection selection = viewer.getSelection();
                if (selection.isEmpty()) {
                    selectedValue = null;
                } else {
                    selectedValue = ((IStructuredSelection) selection)
                            .getFirstElement();
                }
            }
        });

        comboBox.addTraverseListener(new TraverseListener() {
            public void keyTraversed(TraverseEvent e) {
                if (e.detail is DWT.TRAVERSE_ESCAPE
                        || e.detail is DWT.TRAVERSE_RETURN) {
                    e.doit = false;
                }
            }
        });

        comboBox.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                ComboBoxViewerCellEditor.this.focusLost();
            }
        });
        return comboBox;
    }

    /**
     * The <code>ComboBoxCellEditor</code> implementation of this
     * <code>CellEditor</code> framework method returns the zero-based index
     * of the current selection.
     *
     * @return the zero-based index of the current selection wrapped as an
     *         <code>Integer</code>
     */
    protected Object doGetValue() {
        return selectedValue;
    }

    /*
     * (non-Javadoc) Method declared on CellEditor.
     */
    protected void doSetFocus() {
        viewer.getControl().setFocus();
    }

    /**
     * The <code>ComboBoxCellEditor</code> implementation of this
     * <code>CellEditor</code> framework method sets the minimum width of the
     * cell. The minimum width is 10 characters if <code>comboBox</code> is
     * not <code>null</code> or <code>disposed</code> eles it is 60 pixels
     * to make sure the arrow button and some text is visible. The list of
     * CCombo will be wide enough to show its longest item.
     */
    public LayoutData getLayoutData() {
        LayoutData layoutData = super.getLayoutData();
        if ((viewer.getControl() is null) || viewer.getControl().isDisposed()) {
            layoutData.minimumWidth = 60;
        } else {
            // make the comboBox 10 characters wide
            GC gc = new GC(viewer.getControl());
            layoutData.minimumWidth = (gc.getFontMetrics()
                    .getAverageCharWidth() * 10) + 10;
            gc.dispose();
        }
        return layoutData;
    }

    /**
     * Set a new value
     *
     * @param value
     *            the new value
     */
    protected void doSetValue(Object value) {
        Assert.isTrue(viewer !is null);
        selectedValue = value;
        if (value is null) {
            viewer.setSelection(StructuredSelection.EMPTY);
        } else {
            viewer.setSelection(new StructuredSelection(value));
        }
    }

    /**
     * @param labelProvider
     *            the label provider used
     * @see StructuredViewer#setLabelProvider(IBaseLabelProvider)
     */
    public void setLabelProvider(IBaseLabelProvider labelProvider) {
        viewer.setLabelProvider(labelProvider);
    }

    /**
     * @param provider
     *            the content provider used
     * @see StructuredViewer#setContentProvider(IContentProvider)
     */
    public void setContenProvider(IStructuredContentProvider provider) {
        viewer.setContentProvider(provider);
    }

    /**
     * @param input
     *            the input used
     * @see StructuredViewer#setInput(Object)
     */
    public void setInput(Object input) {
        viewer.setInput(input);
    }

    /**
     * @return get the viewer
     */
    public ComboViewer getViewer() {
        return viewer;
    }

    /**
     * Applies the currently selected value and deactiavates the cell editor
     */
    void applyEditorValueAndDeactivate() {
        // must set the selection before getting value
        ISelection selection = viewer.getSelection();
        if (selection.isEmpty()) {
            selectedValue = null;
        } else {
            selectedValue = ((IStructuredSelection) selection)
                    .getFirstElement();
        }

        Object newValue = doGetValue();
        markDirty();
        bool isValid = isCorrect(newValue);
        setValueValid(isValid);

        if (!isValid) {
            MessageFormat.format(getErrorMessage(),
                    new Object[] { selectedValue });
        }

        fireApplyEditorValue();
        deactivate();
    }

    /*
     * (non-Javadoc)
     *
     * @see dwtx.jface.viewers.CellEditor#focusLost()
     */
    protected void focusLost() {
        if (isActivated()) {
            applyEditorValueAndDeactivate();
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see dwtx.jface.viewers.CellEditor#keyReleaseOccured(dwt.events.KeyEvent)
     */
    protected void keyReleaseOccured(KeyEvent keyEvent) {
        if (keyEvent.character is '\u001b') { // Escape character
            fireCancelEditor();
        } else if (keyEvent.character is '\t') { // tab key
            applyEditorValueAndDeactivate();
        }
    }
}