|
What this is
Other links
The source code/*******************************************************************************
* Copyright (c) 2000, 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
* Tom Schindl <tom.schindl@bestsolution.at> - bugfix in 174739
*******************************************************************************/
package org.eclipse.jface.viewers;
import java.text.MessageFormat; // Not using ICU to support standalone JFace
// scenario
import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
/**
* A cell editor that presents a list of items in a combo box. The cell editor's
* value is the zero-based index of the selected item.
* <p>
* This class may be instantiated; it is not intended to be subclassed.
* </p>
*/
public class ComboBoxCellEditor extends AbstractComboBoxCellEditor {
/**
* The list of items to present in the combo box.
*/
private String[] items;
/**
* The zero-based index of the selected item.
*/
int selection;
/**
* The custom combo box control.
*/
CCombo comboBox;
/**
* Default ComboBoxCellEditor style
*/
private static final int defaultStyle = SWT.NONE;
/**
* Creates a new cell editor with no control and no st of choices.
* Initially, the cell editor has no cell validator.
*
* @since 2.1
* @see CellEditor#setStyle
* @see CellEditor#create
* @see ComboBoxCellEditor#setItems
* @see CellEditor#dispose
*/
public ComboBoxCellEditor() {
setStyle(defaultStyle);
}
/**
* Creates a new cell editor with a combo containing the given list of
* choices and parented under the given control. The cell editor value is
* the zero-based index of the selected item. Initially, the cell editor has
* no cell validator and the first item in the list is selected.
*
* @param parent
* the parent control
* @param items
* the list of strings for the combo box
*/
public ComboBoxCellEditor(Composite parent, String[] items) {
this(parent, items, defaultStyle);
}
/**
* Creates a new cell editor with a combo containing the given list of
* choices and parented under the given control. The cell editor value is
* the zero-based index of the selected item. Initially, the cell editor has
* no cell validator and the first item in the list is selected.
*
* @param parent
* the parent control
* @param items
* the list of strings for the combo box
* @param style
* the style bits
* @since 2.1
*/
public ComboBoxCellEditor(Composite parent, String[] items, int style) {
super(parent, style);
setItems(items);
}
/**
* Returns the list of choices for the combo box
*
* @return the list of choices for the combo box
*/
public String[] getItems() {
return this.items;
}
/**
* Sets the list of choices for the combo box
*
* @param items
* the list of choices for the combo box
*/
public void setItems(String[] items) {
Assert.isNotNull(items);
this.items = items;
populateComboBoxItems();
}
/*
* (non-Javadoc) Method declared on CellEditor.
*/
protected Control createControl(Composite parent) {
comboBox = new CCombo(parent, getStyle());
comboBox.setFont(parent.getFont());
populateComboBoxItems();
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) {
selection = comboBox.getSelectionIndex();
}
});
comboBox.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_ESCAPE
|| e.detail == SWT.TRAVERSE_RETURN) {
e.doit = false;
}
}
});
comboBox.addFocusListener(new FocusAdapter() {
public void focusLost(FocusEvent e) {
ComboBoxCellEditor.this.focusLost();
}
});
return comboBox;
}
/**
* The <code>ComboBoxCellEditor implementation of this
* <code>CellEditor 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
*/
protected Object doGetValue() {
return new Integer(selection);
}
/*
* (non-Javadoc) Method declared on CellEditor.
*/
protected void doSetFocus() {
comboBox.setFocus();
}
/**
* The <code>ComboBoxCellEditor implementation of this
* <code>CellEditor framework method sets the minimum width of the
* cell. The minimum width is 10 characters if <code>comboBox is
* not <code>null or
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.