Bug 124106 - Here goes the code for SpinnerCellEditor
Summary: Here goes the code for SpinnerCellEditor
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1.1   Edit
Hardware: PC Windows 2000
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-17 08:13 EST by Kaniska Mandal CLA
Modified: 2019-09-06 15:31 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kaniska Mandal CLA 2006-01-17 08:13:05 EST
I have a working piece of code for SpinnerCellEditor.

Can u plz. tell me how to add this to eclipse cvs ?
Comment 1 Kim Horne CLA 2006-01-17 12:47:26 EST
Attach it here as a patch and someone on our team will review it on your behalf.
Comment 2 Kaniska Mandal CLA 2006-01-19 08:56:12 EST
// SpinnerCellEditor is based on the code of ComboboxCellEditor ...

package com.tibco.cep.eclipse.editor.resource.presentation.uifields;

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 org.eclipse.swt.widgets.Spinner;

/**
 * This is a cell editor to display a spinner.   
 * 
 * @author kaniska
 * 
 */

public class SpinnerCellEditor extends CellEditor {


    /**
     * The zero-based index of the selected item.
     */
    int selection;

    /**
     * The custom spinner control.
     */
    Spinner spinner;

    /**
     * Default SpinnerCellEditor style
     */
    private static final int defaultStyle = SWT.NONE;	
	
    public SpinnerCellEditor() {
        setStyle(defaultStyle);
    }
    
    public SpinnerCellEditor(Composite parent) {
        super(parent, defaultStyle);
    }    
    
   
    protected Control createControl(Composite parent) {    	
    	spinner = new Spinner(parent, SWT.NONE);
    	spinner.setMinimum(0);
        spinner.setMaximum(1024);
        spinner.setSelection(0);
        spinner.setIncrement(1);
        spinner.setSize(120, 80);
               
        spinner.addKeyListener(new KeyAdapter() {
            // hook key pressed - see PR 14201  
            public void keyPressed(KeyEvent e) {
                keyReleaseOccured(e);
            }
        });

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

            public void widgetSelected(SelectionEvent event) {
                selection = spinner.getSelection();
            }
        });

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

        spinner.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                SpinnerCellEditor.this.focusLost();
            }
        });    
        
    	return spinner;
    }    
    
	  /**
     * The <code>SpinnerCellEditor</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 new Integer(selection);
    }

    /* (non-Javadoc)
     * Method declared on CellEditor.
     */
    protected void doSetFocus() {
    	spinner.setFocus();
    }
    
    /**
     * 
     */
    protected void doSetValue(Object value) {
  
    	if(null != value && ((String)value).length()>0)
        	selection = new Integer((String)value).intValue();
        else
        	selection = 0;
        spinner.setSelection(selection);
        
    }
    
    public LayoutData getLayoutData() {
        LayoutData layoutData = super.getLayoutData();
        if ((spinner == null) || spinner.isDisposed())
            layoutData.minimumWidth = 30;
        else {
            // make the spinner 10 characters wide
            GC gc = new GC(spinner);
            layoutData.minimumWidth = (gc.getFontMetrics()
                    .getAverageCharWidth() * 10) + 10;
            gc.dispose();
        }
                
        return layoutData;
    }
    
    void applyEditorValueAndDeactivate() {
        //	must set the selection before getting value
        selection = spinner.getSelection();
        Object newValue = doGetValue();
        markDirty();
        boolean isValid = isCorrect(newValue);
        setValueValid(isValid);
        fireApplyEditorValue();
        deactivate();
    }

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

    /*
     *  (non-Javadoc)
     * @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
     */
    protected void keyReleaseOccured(KeyEvent keyEvent) {
        if (keyEvent.character == '\u001b') { // Escape character
            fireCancelEditor();
        } else if (keyEvent.character == '\t') { // tab key
            applyEditorValueAndDeactivate();
        }
    }
    
}
Comment 3 Eric Moffatt CLA 2009-04-24 11:29:29 EDT
Changing to enhancement.
Comment 4 Eclipse Webmaster CLA 2019-09-06 15:31:49 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.