[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[news.eclipse.platform] Use of customised cellEditor
|
- From: aruna_23@xxxxxxxxx (Aruna)
- Date: Thu, 31 May 2007 13:13:53 +0000 (UTC)
- Newsgroups: eclipse.platform
- Organization: Eclipse
- User-agent: NewsPortal/0.36 (http://florian-amrhein.de/newsportal)
Hi
I have created my own celleditor that has a Spin control ,by extending the
CellEditor class.
Code is as below
public class SpinCellEditor extends CellEditor {
/**
* Image registry key for three dot image (value
"cell_editor_dots_button_image").
*/
public static final String CELL_EDITOR_IMG_DOTS_BUTTON =
"cell_editor_dots_button_image";//$NON-NLS-1$
/**
* The editor control.
*/
private Composite editor;
/**
* The button.
*/
private Spinner spinner;
/**
* Listens for 'focusLost' events and fires the 'apply' event as long as
the focus wasn't lost because the dialog was opened.
*/
private FocusListener buttonFocusListener;
/**
* The value of this cell editor; initially null.
*/
private Object value = null;
static {
ImageRegistry reg = JFaceResources.getImageRegistry();
reg.put(CELL_EDITOR_IMG_DOTS_BUTTON,
ImageDescriptor.createFromFile(
DialogCellEditor.class,
"images/dots_button.gif"));//$NON-NLS-1$
}
/**
* Internal class for laying out the dialog.
*/
private class SliderSampleCellLayout extends Layout
{
public void layout(Composite editor, boolean force)
{
Rectangle bounds = editor.getClientArea();
spinner.setBounds(0, 0, bounds.width, bounds.height);
}
public Point computeSize(Composite editor, int wHint, int hHint,
boolean force)
{
if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
{
return new Point(wHint, hHint);
}
Point buttonSize = spinner.computeSize(SWT.DEFAULT,
SWT.DEFAULT,
force);
// Just return the button width to ensure the button is not
clipped
// if the label is long.
// The label will just use whatever extra width there is
Point result = new Point(buttonSize.x, buttonSize.y);
return result;
}
}
/**
* Default DialogCellEditor style
*/
private static final int defaultStyle = SWT.NONE;
/**
* Creates a new dialog cell editor with no control
* @since 2.1
*/
public SpinCellEditor()
{
setStyle(defaultStyle);
}
/**
* Creates a new dialog cell editor parented under the given control.
* The cell editor value is null initially, and has no
* validator.
*
* @param parent the parent control
*/
public SpinCellEditor(Composite parent)
{
this(parent, defaultStyle);
}
/**
* Creates a new dialog cell editor parented under the given control.
* The cell editor value is null initially, and has no
* validator.
*
* @param parent the parent control
* @param style the style bits
* @since 2.1
*/
public SpinCellEditor(Composite parent, int style)
{
super(parent, style);
}
/**
* Creates the button for this cell editor under the given parent
control.
*
* The default implementation of this framework method creates the
button
* display on the right hand side of the dialog cell editor. Subclasses
* may extend or reimplement.
*
*
* @param parent the parent control
* @return the new button control
*/
protected Spinner createSpinner(Composite parent)
{
Spinner result = new Spinner(parent, SWT.HORIZONTAL);
//$NON-NLS-1$
return result;
}
/* (non-Javadoc)
* Method declared on CellEditor.
*/
protected Control createControl(Composite parent)
{
Font font = parent.getFont();
Color bg = parent.getBackground();
editor = new Composite(parent, getStyle());
editor.setFont(font);
editor.setBackground(bg);
editor.setLayout(new SliderSampleCellLayout());
spinner = createSpinner(editor);
spinner.setFont(font);
spinner.setBackground(editor.getBackground());
updateContents(value);
spinner.addFocusListener(getButtonFocusListener());
spinner.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
// Remove the button's focus listener since it's guaranteed
// to lose focus when the dialog opens
spinner.removeFocusListener(getButtonFocusListener());
Object newValue = spinner.getSelection();
// Re-add the listener once the dialog closes
spinner.addFocusListener(getButtonFocusListener());
if (newValue != null)
{
boolean newValidState = isCorrect(newValue);
if (newValidState)
{
markDirty();
doSetValue(newValue);
} else {
// try to insert the current value into the error
message.
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[] { newValue.toString() }));
}
fireApplyEditorValue();
}
}
});
setValueValid(true);
return editor;
}
/* (non-Javadoc)
*
* Override in order to remove the button's focus listener if the
celleditor
* is deactivating.
*/
public void deactivate()
{
if (spinner != null && !spinner.isDisposed())
{
spinner.removeFocusListener(getButtonFocusListener());
}
super.deactivate();
}
/* (non-Javadoc)
* Method declared on CellEditor.
*/
protected Object doGetValue()
{
return value;
}
/* (non-Javadoc)
* Method declared on CellEditor.
* The focus is set to the cell editor's button.
*/
protected void doSetFocus()
{
spinner.setFocus();
// add a FocusListener to the button
spinner.addFocusListener(getButtonFocusListener());
}
/**
* Return a listener for button focus.
* @return FocusListener
*/
private FocusListener getButtonFocusListener()
{
if (buttonFocusListener == null)
{
buttonFocusListener = new FocusListener()
{
public void focusGained(FocusEvent e)
{
// Do nothing
}
public void focusLost(FocusEvent e)
{
SpinCellEditor.this.focusLost();
}
};
}
return buttonFocusListener;
}
/* (non-Javadoc)
* Method declared on CellEditor.
*/
protected void doSetValue(Object value)
{
this.value = value;
updateContents(value);
}
/**
* Updates the controls showing the value of this cell editor.
*
* The default implementation of this framework method just converts
* the passed object to a string using toString and
* sets this as the text of the label widget.
*/
protected void updateContents(Object value)
{
String text = "";//$NON-NLS-1$
if (value != null)
{
text = value.toString();
if(spinner!=null)
{
spinner.setSelection(Integer.parseInt(text));
}
}
}
}
The problem is when I am trying to use this CellEditor using
EditingSupport class,the behaviour is as described..
If the value of the spin is changed by clicking once on the Arrow,the
value is incremented and focus is lost.
Also if I enter a digit in the celleditor ,focus is lost after entering
every digit....which should not be the case...the focus must be in the
celleditor until I type in the value for the control as in the case of
TextCellEditor..
How can I acheive this???
Thanks in advance
Aruna