Bug 331742 - Select all text inside a Spinner on key down
Summary: Select all text inside a Spinner on key down
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.1   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-03 02:40 EST by Albert CLA
Modified: 2019-09-06 16:14 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 Albert CLA 2010-12-03 02:40:32 EST
Build Identifier: M20100211-1343

Spinner class is missing an important feature:

It would be great if Spinner had a similar behavior as DateTime widget (created with SWT.DATE | SWT.LONG) where only digits are allowed to be entered and selected element (day, month or year) is reselected once it was modified using the keyboard. This way, selected text is always overwritten when a digit is input and there is no need to edit it using backspace or delete keys, nor to manually select digits in order to overwrite them.

I tried a few approaches to achieve this behavior, but with no luck (please see http://www.eclipse.org/forums/index.php?t=msg&th=201239&start=0&S=7b8adb42081edef76ee4526860ec80ef).

I hope it can be done something about it.


Reproducible: Always
Comment 1 Albert CLA 2010-12-03 04:06:07 EST
Perhaps my description wasn't clear enough: with every new character (digit) entered, all text should be selected, but when typing in subsequent characters, existing text inside a Spinner should not be overwritten, except if some limit is exceeded; suppose I want to set a limit for a number that is entered, e.g. number cannot be less than 1 or greater than 20. In this case behavior should be like this, for example:

* "1" is entered and selected,
* after that "2" is entered, so we have "12" selected inside a Spinner (the first "1" is NOT overwritten),
* but once I add, say "3", this would result in "123", which exceeds the 20 limit - so "12" is overwritten with "3" and selected.

Basically, this is the same behavior as DateTime widget features.
Comment 2 Albert CLA 2010-12-03 06:31:15 EST
I played with this some more and I found out that this can be achieved. Here is the snippet that demonstrates desired behavior:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class SpinnerTest
{  
    SpinnerTest()
    {
        Display display = new Display();
        Shell shell = new Shell(display);    
        
        shell.setLayout(new FormLayout());
        
        final Spinner spinner = new Spinner (shell, SWT.BORDER);
        
        FormData formData = new FormData();
        formData.top = new FormAttachment(0, 10);            
        formData.left = new FormAttachment(10, 0);
        formData.right = new FormAttachment(10, 70);            
        spinner.setLayoutData(formData);
        
        shell.setSize(200, 100);
        
        spinner.setMinimum(1);
        spinner.setMaximum(20);
        spinner.setTextLimit(3);
        
        spinner.addKeyListener(new KeyListener()
        {   
            String oldSpinnerText;
            String newSpinnerText;
            
            @Override
            public void keyPressed(KeyEvent event)
            {   
                try
                {
                    oldSpinnerText = String.valueOf(spinner.getSelection());                    
                    String newText = String.valueOf(event.character);                    
                    newSpinnerText = oldSpinnerText + newText;
                    
                    if (Integer.valueOf(newSpinnerText) >= spinner.getMaximum())
                    {
                        oldSpinnerText = String.valueOf(spinner.getSelection());
                        spinner.setSelection(Integer.valueOf(newText));                        
                        newSpinnerText = newText;
                    }
                }
                catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
                
            }
            @Override
            public void keyReleased(KeyEvent event)
            {   
                spinner.setSelection(Integer.valueOf(newSpinnerText));
            }
        });
        
        // Never deselect digits in spinner when clicking inside digits field
        spinner.addMouseListener(new MouseListener()
        {   
            @Override
            public void mouseUp(MouseEvent arg0)
            {   
                 spinner.setSelection(spinner.getSelection());
            }
            
            @Override
            public void mouseDown(MouseEvent arg0)
            {
                spinner.setSelection(spinner.getSelection());                
            }
            
            @Override
            public void mouseDoubleClick(MouseEvent arg0)
            {   
                
            }
        });
        
        shell.open();
       
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
            {
                display.sleep();
            }   
       }
       display.dispose();
    }
    
    public static void main(String[] argv)
    {
      new SpinnerTest();
    }
    
}

Maybe I didn't think of all cases that can occur, but generally this seems like an acceptable solution.
Comment 3 Albert CLA 2010-12-03 09:35:49 EST
The above snippet works on Windows and Mac but not on GTK - digits are not selected.
Comment 4 Eclipse Webmaster CLA 2019-09-06 16:14:24 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.