Donate Share

GFace - custom reusable SWT controls

The forum address has changed, you have been automatically redirected. Please update any bookmarks to use the new URL.

Subscribe

datePicker and CellEditor

You are viewing a single message from this topic. View all messages.

  1. 2008-10-09 20:27:42 UTC
    I do that.
    This is a revisited version because in the original version there was custom code.
    If you improve it post it or if you find it interesting please vote to include it in the next release of gface.

    Thank you.
    Andrea

    //-----------------------------------------------------------------------
    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    import org.eclipse.core.runtime.Assert;
    import org.eclipse.jface.viewers.CellEditor;
    import org.eclipse.swt.SWT;
    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;

    import com.gface.date.DatePickerCombo;
    import com.gface.date.DatePickerStyle;

    /**
    * @author acastelli
    */
    public class DatePickerCellEditorGeneric extends CellEditor
    {

    private String dateFormatPattern;

    private int swtStyle = SWT.BORDER;

    private int datePickerStyle = SWT.FLAT
    | DatePickerStyle.HIDE_WHEN_NOT_IN_FOCUS
    | DatePickerStyle.WEEKS_STARTS_ON_MONDAY
    | DatePickerStyle.YEAR_BUTTONS;

    /**
    * The list of items to present in the combo box.
    */
    private Calendar cal;

    private DatePickerCombo datePickerCombo;

    private 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 DatePickerCellEditor#setItems
    * @see CellEditor#dispose
    */
    public DatePickerCellEditorGeneric(Composite parent)
    {
    super(parent);
    setStyle(defaultStyle);
    }

    public Calendar getCal()
    {
    return this.cal;
    }

    public void setCal(Calendar cal)
    {
    Assert.isNotNull(cal);
    this.cal = cal;
    }

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

    datePickerCombo = new DatePickerCombo(parent, swtStyle, datePickerStyle);
    datePickerCombo.setDateFormat(new SimpleDateFormat(dateFormatPattern));

    datePickerCombo.setFont(parent.getFont());

    datePickerCombo.addKeyListener(new KeyAdapter()
    {

    // hook key pressed - see PR 14201
    @Override
    public void keyPressed(KeyEvent e)
    {
    keyReleaseOccured(e);
    }
    });

    datePickerCombo.addSelectionListener(new SelectionAdapter()
    {

    @Override
    public void widgetDefaultSelected(SelectionEvent event)
    {
    applyEditorValueAndDeactivate();
    }

    @Override
    public void widgetSelected(SelectionEvent event)
    {
    // applyEditorValueAndDeactivate();
    }
    });

    datePickerCombo.addTraverseListener(new TraverseListener()
    {

    public void keyTraversed(TraverseEvent e)
    {
    if (e.detail == SWT.TRAVERSE_ESCAPE
    || e.detail == SWT.TRAVERSE_RETURN)
    {
    e.doit = false;
    }
    }
    });

    datePickerCombo.addFocusListener(new FocusAdapter()
    {

    @Override
    public void focusLost(FocusEvent e)
    {
    DatePickerCellEditorGeneric.this.focusLost();
    }
    });
    return datePickerCombo;
    }

    /**
    * The <code>DatePickerCellEditor</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>
    */
    @Override
    protected Object doGetValue()
    {
    return datePickerCombo.getCal();
    }

    /*
    * (non-Javadoc) Method declared on CellEditor.
    */
    @Override
    protected void doSetFocus()
    {
    datePickerCombo.setFocus();
    }

    /**
    * The <code>DatePickerCellEditor</code> implementation of this <code>CellEditor</code> framework method sets the
    * minimum width of the cell. The minimum width is 10 characters if <code>datePickerCombo</code> is not
    * <code>null</code> or <code>disposed</code> else 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.
    */
    @Override
    public LayoutData getLayoutData()
    {
    LayoutData layoutData = super.getLayoutData();
    if ((datePickerCombo == null) || datePickerCombo.isDisposed())
    {
    layoutData.minimumWidth = 60;
    } else
    {
    // make the datePickerCombo 10 characters wide
    GC gc = new GC(datePickerCombo);
    layoutData.minimumWidth = (gc.getFontMetrics()
    .getAverageCharWidth() * 10) + 10;
    gc.dispose();
    }
    return layoutData;
    }

    /**
    * The <code>DatePickerCellEditor</code> implementation of this <code>CellEditor</code> framework method accepts a
    * zero-based index of a selection.
    *
    * @param value
    * the zero-based index of the selection wrapped as an <code>Integer</code>
    */
    @Override
    protected void doSetValue(Object value)
    {
    Assert.isTrue(datePickerCombo != null && (value instanceof Calendar));
    datePickerCombo.setCal((Calendar) value);
    }

    /**
    * Applies the currently selected value and deactivates the cell editor
    */
    void applyEditorValueAndDeactivate()
    {
    // must set the selection before getting value

    Calendar currentDate = datePickerCombo.getCal();

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

    if (!isValid)
    {
    // Only format if the 'index' is valid
    if (currentDate == null)
    {
    // try to insert the current value into the error message.
    setErrorMessage("Not valid date");
    } else
    {
    setErrorMessage("Error");
    }
    }

    try
    {
    fireApplyEditorValue();
    } catch (RuntimeException xdme)
    {
    errorManage();

    } finally
    {
    deactivate();
    }
    }

    /**
    *Override it to manage the error
    */
    protected void errorManage()
    {

    }

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

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

    // set the date format to use
    public void setDateFormatPattern(String dateFormatPattern)
    {
    this.dateFormatPattern = dateFormatPattern;
    }

    }
< Previous | 1 | Next >

Add a Reply

This forum does not allow anonymous participation.

Log in to add a reply. Not registered? Create an account to participate and receive email updates when replies are posted to this topic.