Summary: | Select all text inside a Spinner on key down | ||
---|---|---|---|
Product: | [Eclipse Project] Platform | Reporter: | Albert <albert.pikus> |
Component: | SWT | Assignee: | Platform-SWT-Inbox <platform-swt-inbox> |
Status: | NEW --- | QA Contact: | |
Severity: | enhancement | ||
Priority: | P3 | CC: | eclipse.felipe |
Version: | 4.1 | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | Windows 7 | ||
Whiteboard: |
Description
Albert
2010-12-03 02:40:32 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. 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. The above snippet works on Windows and Mac but not on GTK - digits are not selected. 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. |