Merge sources from S-Core's RSA git (release)
[sdk/ide/common-eplugin.git] / org.tizen.common.externals / src / org / mihalis / opal / horizontalSpinner / HorizontalSpinner.java
1 /*******************************************************************************\r
2  * Copyright (c) 2011 Laurent CARON\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     Laurent CARON (laurent.caron at gmail dot com) - Implementation\r
10  *******************************************************************************/\r
11 package org.mihalis.opal.horizontalSpinner;\r
12 \r
13 import java.text.DecimalFormatSymbols;\r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.eclipse.swt.SWT;\r
18 import org.eclipse.swt.SWTException;\r
19 import org.eclipse.swt.events.KeyAdapter;\r
20 import org.eclipse.swt.events.KeyEvent;\r
21 import org.eclipse.swt.events.ModifyEvent;\r
22 import org.eclipse.swt.events.ModifyListener;\r
23 import org.eclipse.swt.events.SelectionAdapter;\r
24 import org.eclipse.swt.events.SelectionEvent;\r
25 import org.eclipse.swt.events.SelectionListener;\r
26 import org.eclipse.swt.events.VerifyEvent;\r
27 import org.eclipse.swt.events.VerifyListener;\r
28 import org.eclipse.swt.graphics.Color;\r
29 import org.eclipse.swt.graphics.Cursor;\r
30 import org.eclipse.swt.graphics.Font;\r
31 import org.eclipse.swt.graphics.Image;\r
32 import org.eclipse.swt.layout.GridData;\r
33 import org.eclipse.swt.layout.GridLayout;\r
34 import org.eclipse.swt.widgets.Button;\r
35 import org.eclipse.swt.widgets.Composite;\r
36 import org.eclipse.swt.widgets.Menu;\r
37 import org.eclipse.swt.widgets.Text;\r
38 import org.mihalis.opal.utils.StringUtil;\r
39 \r
40 /**\r
41  * Instances of this class are selectable user interface objects that allow the user to enter and modify numeric values.\r
42  * <p>\r
43  * <dl>\r
44  * <dt><b>Styles:</b></dt>\r
45  * <dd>READ_ONLY, FLAP</dd>\r
46  * <dt><b>Events:</b></dt>\r
47  * <dd>Selection, Modify</dd>\r
48  * </dl>\r
49  * </p>\r
50  */\r
51 public class HorizontalSpinner extends Composite {\r
52     private final List<ModifyListener> modifyListeners = new ArrayList<ModifyListener>();\r
53     private final List<SelectionListener> selectionListeners = new ArrayList<SelectionListener>();\r
54 \r
55     private Button leftButton;\r
56     private Button rightButton;\r
57     private Text text;\r
58     private int digits = 0;\r
59     private int increment = 1;\r
60     private int maximum = 0;\r
61     private int minimum = 255;\r
62     private int pageIncrement = 10;\r
63     private int storedValue = 0;\r
64 \r
65     private final char decimalFormatSeparator;\r
66 \r
67     /**\r
68      * Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.\r
69      * <p>\r
70      * The style value is either one of the style constants defined in class <code>SWT</code> which is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.\r
71      * </p>\r
72      * \r
73      * @param parent a composite control which will be the parent of the new instance (cannot be null)\r
74      * @param style the style of control to construct\r
75      * \r
76      * @exception IllegalArgumentException <ul>\r
77      *                <li>ERROR_NULL_ARGUMENT - if the parent is null</li>\r
78      *                </ul>\r
79      * @exception SWTException <ul>\r
80      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>\r
81      *                <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>\r
82      *                </ul>\r
83      * \r
84      * @see SWT#READ_ONLY\r
85      * @see SWT#FLAT\r
86      */\r
87     public HorizontalSpinner(final Composite parent, final int style) {\r
88         super(parent, style);\r
89 \r
90         final GridLayout gd = new GridLayout(3, false);\r
91         gd.horizontalSpacing = gd.verticalSpacing = 0;\r
92         gd.marginWidth = gd.marginHeight = 0;\r
93         this.setLayout(gd);\r
94 \r
95         this.createContent(style);\r
96         this.addTextListeners();\r
97         this.addButtonsListener();\r
98         this.addModifyListeners();\r
99 \r
100         this.decimalFormatSeparator = new DecimalFormatSymbols().getDecimalSeparator();\r
101     }\r
102 \r
103     /**\r
104      * Create the content of the widget\r
105      * \r
106      * @param style style of the widget\r
107      */\r
108     private void createContent(final int style) {\r
109         final boolean readOnly = (style & SWT.READ_ONLY) == SWT.READ_ONLY;\r
110         final boolean flat = (style & SWT.FLAT) == SWT.FLAT;\r
111 \r
112         final int buttonStyle = SWT.ARROW | (flat ? SWT.FLAT : SWT.NONE);\r
113         this.leftButton = new Button(this, buttonStyle | SWT.LEFT);\r
114         this.leftButton.setFont(this.getFont());\r
115         this.leftButton.setBackground(this.getBackground());\r
116         this.leftButton.setCursor(this.getCursor());\r
117         this.leftButton.setEnabled(this.getEnabled());\r
118         this.leftButton.setFont(this.getFont());\r
119         this.leftButton.setForeground(this.getForeground());\r
120         this.leftButton.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));\r
121 \r
122         this.text = new Text(this, readOnly ? SWT.READ_ONLY : SWT.NONE);\r
123         final GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);\r
124         gd.minimumWidth = 40;\r
125         this.text.setLayoutData(gd);\r
126 \r
127         this.rightButton = new Button(this, buttonStyle | SWT.RIGHT);\r
128         this.rightButton.setFont(this.getFont());\r
129         this.rightButton.setBackground(this.getBackground());\r
130         this.rightButton.setCursor(this.getCursor());\r
131         this.rightButton.setEnabled(this.getEnabled());\r
132         this.rightButton.setFont(this.getFont());\r
133         this.rightButton.setForeground(this.getForeground());\r
134         this.rightButton.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false));\r
135 \r
136     }\r
137 \r
138     /**\r
139      * Add the text listeners\r
140      */\r
141     private void addTextListeners() {\r
142         this.text.addVerifyListener(new VerifyListener() {\r
143 \r
144             @Override\r
145             public void verifyText(final VerifyEvent e) {\r
146                 if (e.character != 0 && !Character.isDigit(e.character) && e.keyCode != SWT.BS && e.keyCode != SWT.DEL) {\r
147                     e.doit = false;\r
148                     return;\r
149                 }\r
150 \r
151                 e.doit = HorizontalSpinner.this.verifyEntryAndStoreValue(e.text, e.keyCode);\r
152 \r
153             }\r
154         });\r
155 \r
156         this.text.addKeyListener(new KeyAdapter() {\r
157 \r
158             /**\r
159              * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)\r
160              */\r
161             @Override\r
162             public void keyReleased(final KeyEvent e) {\r
163                 if (e.keyCode == SWT.ARROW_UP) {\r
164                     HorizontalSpinner.this.increaseValue(HorizontalSpinner.this.increment);\r
165                 }\r
166                 if (e.keyCode == SWT.ARROW_DOWN) {\r
167                     HorizontalSpinner.this.decreaseValue(HorizontalSpinner.this.increment);\r
168                 }\r
169                 if (e.keyCode == SWT.PAGE_UP) {\r
170                     HorizontalSpinner.this.increaseValue(HorizontalSpinner.this.pageIncrement);\r
171                 }\r
172                 if (e.keyCode == SWT.PAGE_DOWN) {\r
173                     HorizontalSpinner.this.decreaseValue(HorizontalSpinner.this.pageIncrement);\r
174                 }\r
175             }\r
176 \r
177         });\r
178 \r
179         this.text.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {\r
180 \r
181             /**\r
182              * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)\r
183              */\r
184             @Override\r
185             public void focusLost(final org.eclipse.swt.events.FocusEvent e) {\r
186                 if (HorizontalSpinner.this.text.getText().trim().equals("")) {\r
187                     HorizontalSpinner.this.setSelection(HorizontalSpinner.this.storedValue);\r
188                 }\r
189             }\r
190 \r
191         });\r
192 \r
193     }\r
194 \r
195     /**\r
196      * Verify the entry and store the value in the field storedValue\r
197      * \r
198      * @param entry entry to check\r
199      * @param keyCode code of the typed key\r
200      * @return <code>true</code> if the entry if correct, <code>false</code> otherwise\r
201      */\r
202     private boolean verifyEntryAndStoreValue(final String entry, final int keyCode) {\r
203         final String work;\r
204         if (keyCode == SWT.DEL) {\r
205             work = StringUtil.removeCharAt(this.text.getText(), this.text.getCaretPosition());\r
206         } else if (keyCode == SWT.BS && this.text.getCaretPosition() == 0) {\r
207             work = StringUtil.removeCharAt(this.text.getText(), this.text.getCaretPosition() - 1);\r
208         } else if (keyCode == 0) {\r
209             work = entry;\r
210         } else {\r
211             work = StringUtil.insertString(this.text.getText(), entry, this.text.getCaretPosition());\r
212         }\r
213 \r
214         try {\r
215             final double d = Double.parseDouble(work.replace(this.decimalFormatSeparator, '.'));\r
216             this.storedValue = (int) (d * Math.pow(10, this.getDigits()));\r
217         } catch (final NumberFormatException nfe) {\r
218             return false;\r
219         }\r
220 \r
221         for (final SelectionListener s : HorizontalSpinner.this.selectionListeners) {\r
222             s.widgetSelected(null);\r
223         }\r
224 \r
225         return true;\r
226     }\r
227 \r
228     /**\r
229      * Add the listener to the buttons\r
230      */\r
231     private void addButtonsListener() {\r
232         this.leftButton.addSelectionListener(new SelectionAdapter() {\r
233 \r
234             /**\r
235              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)\r
236              */\r
237             @Override\r
238             public void widgetSelected(final SelectionEvent e) {\r
239                 HorizontalSpinner.this.decreaseValue(HorizontalSpinner.this.increment);\r
240             }\r
241         });\r
242 \r
243         this.rightButton.addSelectionListener(new SelectionAdapter() {\r
244 \r
245             /**\r
246              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)\r
247              */\r
248             @Override\r
249             public void widgetSelected(final SelectionEvent e) {\r
250                 HorizontalSpinner.this.increaseValue(HorizontalSpinner.this.increment);\r
251             }\r
252         });\r
253 \r
254     }\r
255 \r
256     /**\r
257      * Increase the value stored in this snippet\r
258      * \r
259      * @param value value to increase\r
260      */\r
261     private void increaseValue(final int value) {\r
262         this.setSelection(this.getSelection() + value);\r
263 \r
264     }\r
265 \r
266     /**\r
267      * Decrease the value stored in this snippet\r
268      * \r
269      * @param value value to decrease\r
270      */\r
271     private void decreaseValue(final int value) {\r
272         this.setSelection(this.getSelection() - value);\r
273     }\r
274 \r
275     /**\r
276      * Add the modify listeners\r
277      */\r
278     private void addModifyListeners() {\r
279         this.text.addModifyListener(new ModifyListener() {\r
280 \r
281             @Override\r
282             public void modifyText(final ModifyEvent e) {\r
283                 for (final ModifyListener m : HorizontalSpinner.this.modifyListeners) {\r
284                     m.modifyText(e);\r
285                 }\r
286 \r
287             }\r
288         });\r
289 \r
290     }\r
291 \r
292     /**\r
293      * Adds the listener to the collection of listeners who will be notified when the receiver's text is modified, by sending it one of the messages defined in the <code>ModifyListener</code> interface.\r
294      * \r
295      * @param listener the listener which should be notified\r
296      * \r
297      * @exception IllegalArgumentException <ul>\r
298      *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>\r
299      *                </ul>\r
300      * @exception SWTException <ul>\r
301      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
302      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
303      *                </ul>\r
304      * \r
305      * @see ModifyListener\r
306      * @see #removeModifyListener\r
307      * @see org.eclipse.swt.widgets.Spinner#addModifyListener(org.eclipse.swt.events.ModifyListener)\r
308      */\r
309 \r
310     public void addModifyListener(final ModifyListener listener) {\r
311         this.checkWidget();\r
312         this.modifyListeners.add(listener);\r
313     }\r
314 \r
315     /**\r
316      * Adds the listener to the collection of listeners who will be notified when the control is selected by the user, by sending it one of the messages defined in the <code>SelectionListener</code> interface.\r
317      * <p>\r
318      * <code>widgetSelected</code> is not called for texts. <code>widgetDefaultSelected</code> is typically called when ENTER is pressed in a single-line text.\r
319      * </p>\r
320      * \r
321      * @param listener the listener which should be notified when the control is selected by the user\r
322      * \r
323      * @exception IllegalArgumentException <ul>\r
324      *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>\r
325      *                </ul>\r
326      * @exception SWTException <ul>\r
327      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
328      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
329      *                </ul>\r
330      * \r
331      * @see SelectionListener\r
332      * @see #removeSelectionListener\r
333      * @see SelectionEvent\r
334      */\r
335     public void addSelectionListener(final SelectionListener listener) {\r
336         this.checkWidget();\r
337         this.selectionListeners.add(listener);\r
338     }\r
339 \r
340     /**\r
341      * Copies the selected text.\r
342      * <p>\r
343      * The current selection is copied to the clipboard.\r
344      * </p>\r
345      * \r
346      * @exception SWTException <ul>\r
347      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
348      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
349      *                </ul>\r
350      */\r
351     public void copy() {\r
352         this.checkWidget();\r
353         this.text.copy();\r
354     }\r
355 \r
356     /**\r
357      * Cuts the selected text.\r
358      * <p>\r
359      * The current selection is first copied to the clipboard and then deleted from the widget.\r
360      * </p>\r
361      * \r
362      * @exception SWTException <ul>\r
363      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
364      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
365      *                </ul>\r
366      */\r
367     public void cut() {\r
368         this.checkWidget();\r
369         this.text.cut();\r
370     }\r
371 \r
372     /**\r
373      * Returns the number of decimal places used by the receiver.\r
374      * \r
375      * @return the digits\r
376      * \r
377      * @exception SWTException <ul>\r
378      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
379      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
380      *                </ul>\r
381      */\r
382     public int getDigits() {\r
383         this.checkWidget();\r
384         return this.digits;\r
385     }\r
386 \r
387     /**\r
388      * Returns the amount that the receiver's value will be modified by when the up/down arrows are pressed.\r
389      * \r
390      * @return the increment\r
391      * \r
392      * @exception SWTException <ul>\r
393      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
394      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
395      *                </ul>\r
396      */\r
397     public int getIncrement() {\r
398         this.checkWidget();\r
399         return this.increment;\r
400     }\r
401 \r
402     /**\r
403      * Returns the maximum value which the receiver will allow.\r
404      * \r
405      * @return the maximum\r
406      * \r
407      * @exception SWTException <ul>\r
408      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
409      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
410      *                </ul>\r
411      */\r
412     public int getMaximum() {\r
413         this.checkWidget();\r
414         return this.maximum;\r
415     }\r
416 \r
417     /**\r
418      * Returns the minimum value which the receiver will allow.\r
419      * \r
420      * @return the minimum\r
421      * \r
422      * @exception SWTException <ul>\r
423      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
424      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
425      *                </ul>\r
426      */\r
427     public int getMinimum() {\r
428         this.checkWidget();\r
429         return this.minimum;\r
430     }\r
431 \r
432     /**\r
433      * Returns the amount that the receiver's position will be modified by when the page up/down keys are pressed.\r
434      * \r
435      * @return the page increment\r
436      * \r
437      * @exception SWTException <ul>\r
438      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
439      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
440      *                </ul>\r
441      */\r
442     public int getPageIncrement() {\r
443         this.checkWidget();\r
444         return this.pageIncrement;\r
445     }\r
446 \r
447     /**\r
448      * Returns the <em>selection</em>, which is the receiver's position.\r
449      * \r
450      * @return the selection\r
451      * \r
452      * @exception SWTException <ul>\r
453      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
454      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
455      *                </ul>\r
456      */\r
457     public int getSelection() {\r
458         this.checkWidget();\r
459         return this.storedValue;\r
460 \r
461     }\r
462 \r
463     /**\r
464      * Returns a string containing a copy of the contents of the receiver's text field, or an empty string if there are no contents.\r
465      * \r
466      * @return the receiver's text\r
467      * \r
468      * @exception SWTException <ul>\r
469      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
470      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
471      *                </ul>\r
472      * \r
473      */\r
474     public String getText() {\r
475         this.checkWidget();\r
476         return this.text.getText();\r
477     }\r
478 \r
479     /**\r
480      * Returns the maximum number of characters that the receiver's text field is capable of holding. If this has not been changed by <code>setTextLimit()</code>, it will be the constant <code>Spinner.LIMIT</code>.\r
481      * \r
482      * @return the text limit\r
483      * \r
484      * @exception SWTException <ul>\r
485      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
486      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
487      *                </ul>\r
488      * \r
489      * @see #LIMIT\r
490      */\r
491     public int getTextLimit() {\r
492         this.checkWidget();\r
493         return this.text.getTextLimit();\r
494     }\r
495 \r
496     /**\r
497      * Pastes text from clipboard.\r
498      * <p>\r
499      * The selected text is deleted from the widget and new text inserted from the clipboard.\r
500      * </p>\r
501      * \r
502      * @exception SWTException <ul>\r
503      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
504      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
505      *                </ul>\r
506      */\r
507     public void paste() {\r
508         this.checkWidget();\r
509         this.text.paste();\r
510     }\r
511 \r
512     /**\r
513      * Removes the listener from the collection of listeners who will be notified when the receiver's text is modified.\r
514      * \r
515      * @param listener the listener which should no longer be notified\r
516      * \r
517      * @exception IllegalArgumentException <ul>\r
518      *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>\r
519      *                </ul>\r
520      * @exception SWTException <ul>\r
521      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
522      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
523      *                </ul>\r
524      * \r
525      * @see ModifyListener\r
526      * @see #addModifyListener\r
527      */\r
528     public void removeModifyListener(final ModifyListener listener) {\r
529         this.checkWidget();\r
530         this.modifyListeners.remove(listener);\r
531     }\r
532 \r
533     /**\r
534      * Removes the listener from the collection of listeners who will be notified when the control is selected by the user.\r
535      * \r
536      * @param listener the listener which should no longer be notified\r
537      * \r
538      * @exception IllegalArgumentException <ul>\r
539      *                <li>ERROR_NULL_ARGUMENT - if the listener is null</li>\r
540      *                </ul>\r
541      * @exception SWTException <ul>\r
542      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
543      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
544      *                </ul>\r
545      * \r
546      * @see SelectionListener\r
547      * @see #addSelectionListener\r
548      */\r
549     public void removeSelectionListener(final SelectionListener listener) {\r
550         this.checkWidget();\r
551         this.selectionListeners.remove(listener);\r
552     }\r
553 \r
554     /**\r
555      * Sets the number of decimal places used by the receiver.\r
556      * <p>\r
557      * The digit setting is used to allow for floating point values in the receiver. For example, to set the selection to a floating point value of 1.37 call setDigits() with a value of 2 and setSelection() with a value of 137. Similarly, if getDigits() has a value of 2 and getSelection() returns 137 this should be interpreted as 1.37. This applies to all numeric APIs.\r
558      * </p>\r
559      * \r
560      * @param value the new digits (must be greater than or equal to zero)\r
561      * \r
562      * @exception IllegalArgumentException <ul>\r
563      *                <li>ERROR_INVALID_ARGUMENT - if the value is less than zero</li>\r
564      *                </ul>\r
565      * @exception SWTException <ul>\r
566      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
567      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
568      *                </ul>\r
569      */\r
570     public void setDigits(final int value) {\r
571         this.checkWidget();\r
572         this.digits = value;\r
573         this.convertSelection();\r
574     }\r
575 \r
576     /**\r
577      * Sets the amount that the receiver's value will be modified by when the up/down arrows are pressed to the argument, which must be at least one.\r
578      * \r
579      * @param value the new increment (must be greater than zero)\r
580      * \r
581      * @exception SWTException <ul>\r
582      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
583      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
584      *                </ul>\r
585      */\r
586     public void setIncrement(final int value) {\r
587         this.checkWidget();\r
588         this.increment = value;\r
589     }\r
590 \r
591     /**\r
592      * Sets the maximum value that the receiver will allow. This new value will be ignored if it is less than the receiver's current minimum value. If the new maximum is applied then the receiver's selection value will be adjusted if necessary to fall within its new range.\r
593      * \r
594      * @param value the new maximum, which must be greater than or equal to the current minimum\r
595      * \r
596      * @exception SWTException <ul>\r
597      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
598      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
599      *                </ul>\r
600      */\r
601     public void setMaximum(final int value) {\r
602         this.checkWidget();\r
603         this.maximum = value;\r
604     }\r
605 \r
606     /**\r
607      * Sets the minimum value that the receiver will allow. This new value will be ignored if it is greater than the receiver's current maximum value. If the new minimum is applied then the receiver's selection value will be adjusted if necessary to fall within its new range.\r
608      * \r
609      * @param value the new minimum, which must be less than or equal to the current maximum\r
610      * \r
611      * @exception SWTException <ul>\r
612      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
613      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
614      *                </ul>\r
615      */\r
616     public void setMinimum(final int value) {\r
617         this.checkWidget();\r
618         this.minimum = value;\r
619     }\r
620 \r
621     /**\r
622      * Sets the amount that the receiver's position will be modified by when the page up/down keys are pressed to the argument, which must be at least one.\r
623      * \r
624      * @param value the page increment (must be greater than zero)\r
625      * \r
626      * @exception SWTException <ul>\r
627      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
628      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
629      *                </ul>\r
630      */\r
631     public void setPageIncrement(final int value) {\r
632         this.checkWidget();\r
633         this.pageIncrement = value;\r
634     }\r
635 \r
636     /**\r
637      * Sets the <em>selection</em>, which is the receiver's position, to the argument. If the argument is not within the range specified by minimum and maximum, it will be adjusted to fall within this range.\r
638      * \r
639      * @param value the new selection (must be zero or greater)\r
640      * \r
641      * @exception SWTException <ul>\r
642      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
643      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
644      *                </ul>\r
645      */\r
646     public void setSelection(int selection) {\r
647         this.checkWidget();\r
648         if (selection < this.minimum) {\r
649             selection = this.minimum;\r
650         } else if (selection > this.maximum) {\r
651             selection = this.maximum;\r
652         }\r
653 \r
654         this.storedValue = selection;\r
655         this.text.setText(this.convertSelection());\r
656         this.text.selectAll();\r
657         this.text.setFocus();\r
658 \r
659         for (final SelectionListener s : HorizontalSpinner.this.selectionListeners) {\r
660             s.widgetSelected(null);\r
661         }\r
662 \r
663     }\r
664 \r
665     /**\r
666      * Convert the selection into a string\r
667      * \r
668      * @return the string representation of the selection\r
669      */\r
670     private String convertSelection() {\r
671         if (this.getDigits() == 0) {\r
672             return String.valueOf(this.storedValue);\r
673         }\r
674         final String temp = String.valueOf(this.storedValue * Math.pow(10, -1 * this.getDigits()));\r
675         return temp.replace('.', this.decimalFormatSeparator);\r
676     }\r
677 \r
678     /**\r
679      * Sets the maximum number of characters that the receiver's text field is capable of holding to be the argument.\r
680      * <p>\r
681      * To reset this value to the default, use <code>setTextLimit(Spinner.LIMIT)</code>. Specifying a limit value larger than <code>Spinner.LIMIT</code> sets the receiver's limit to <code>Spinner.LIMIT</code>.\r
682      * </p>\r
683      * \r
684      * @param limit new text limit\r
685      * \r
686      * @exception IllegalArgumentException <ul>\r
687      *                <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>\r
688      *                </ul>\r
689      * @exception SWTException <ul>\r
690      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
691      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
692      *                </ul>\r
693      * \r
694      * @see #LIMIT\r
695      */\r
696     public void setTextLimit(final int limit) {\r
697         this.checkWidget();\r
698         this.text.setTextLimit(limit);\r
699     }\r
700 \r
701     /**\r
702      * Sets the receiver's selection, minimum value, maximum value, digits, increment and page increment all at once.\r
703      * <p>\r
704      * Note: This is similar to setting the values individually using the appropriate methods, but may be implemented in a more efficient fashion on some platforms.\r
705      * </p>\r
706      * \r
707      * @param selection the new selection value\r
708      * @param minimum the new minimum value\r
709      * @param maximum the new maximum value\r
710      * @param digits the new digits value\r
711      * @param increment the new increment value\r
712      * @param pageIncrement the new pageIncrement value\r
713      * \r
714      * @exception SWTException <ul>\r
715      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
716      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
717      *                </ul>\r
718      */\r
719     public void setValues(final int selection, final int minimum, final int maximum, final int digits, final int increment, final int pageIncrement) {\r
720         this.setMinimum(minimum);\r
721         this.setMaximum(maximum);\r
722         this.setDigits(digits);\r
723         this.setIncrement(increment);\r
724         this.setPageIncrement(pageIncrement);\r
725         this.setSelection(selection);\r
726     }\r
727 \r
728     /**\r
729      * Sets the receiver's drag detect state. If the argument is <code>true</code>, the receiver will detect drag gestures, otherwise these gestures will be ignored.\r
730      * \r
731      * @param dragDetect the new drag detect state\r
732      * \r
733      * @exception SWTException <ul>\r
734      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
735      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
736      *                </ul>\r
737      */\r
738     @Override\r
739     public boolean setFocus() {\r
740         this.checkWidget();\r
741         return this.text.setFocus();\r
742     }\r
743 \r
744     /**\r
745      * Forces the receiver to have the <em>keyboard focus</em>, causing all keyboard events to be delivered to it.\r
746      * \r
747      * @return <code>true</code> if the control got focus, and <code>false</code> if it was unable to.\r
748      * \r
749      * @exception SWTException <ul>\r
750      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
751      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
752      *                </ul>\r
753      * \r
754      * @see #setFocus\r
755      */\r
756     @Override\r
757     public boolean forceFocus() {\r
758         this.checkWidget();\r
759         return this.text.forceFocus();\r
760     }\r
761 \r
762     /**\r
763      * Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.\r
764      * <p>\r
765      * Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.\r
766      * </p>\r
767      * \r
768      * @param color the new color (or null)\r
769      * \r
770      * @exception IllegalArgumentException <ul>\r
771      *                <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>\r
772      *                </ul>\r
773      * @exception SWTException <ul>\r
774      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
775      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
776      *                </ul>\r
777      */\r
778     @Override\r
779     public void setBackground(final Color color) {\r
780         super.setBackground(color);\r
781         this.leftButton.setBackground(color);\r
782         this.rightButton.setBackground(color);\r
783         this.text.setBackground(color);\r
784     }\r
785 \r
786     /**\r
787      * Sets the receiver's background image to the image specified by the argument, or to the default system color for the control if the argument is null. The background image is tiled to fill the available space.\r
788      * <p>\r
789      * Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.\r
790      * </p>\r
791      * \r
792      * @param image the new image (or null)\r
793      * \r
794      * @exception IllegalArgumentException <ul>\r
795      *                <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>\r
796      *                <li>ERROR_INVALID_ARGUMENT - if the argument is not a bitmap</li>\r
797      *                </ul>\r
798      * @exception SWTException <ul>\r
799      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
800      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
801      *                </ul>\r
802      */\r
803     @Override\r
804     public void setBackgroundImage(final Image image) {\r
805         super.setBackgroundImage(image);\r
806         this.leftButton.setBackgroundImage(image);\r
807         this.rightButton.setBackgroundImage(image);\r
808         this.text.setBackgroundImage(image);\r
809 \r
810     }\r
811 \r
812     /**\r
813      * Sets the receiver's cursor to the cursor specified by the argument, or to the default cursor for that kind of control if the argument is null.\r
814      * <p>\r
815      * When the mouse pointer passes over a control its appearance is changed to match the control's cursor.\r
816      * </p>\r
817      * \r
818      * @param cursor the new cursor (or null)\r
819      * \r
820      * @exception IllegalArgumentException <ul>\r
821      *                <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>\r
822      *                </ul>\r
823      * @exception SWTException <ul>\r
824      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
825      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
826      *                </ul>\r
827      */\r
828     @Override\r
829     public void setCursor(final Cursor cursor) {\r
830         super.setCursor(cursor);\r
831         this.leftButton.setCursor(cursor);\r
832         this.rightButton.setCursor(cursor);\r
833         this.text.setCursor(cursor);\r
834 \r
835     }\r
836 \r
837     /**\r
838      * Enables the receiver if the argument is <code>true</code>, and disables it otherwise. A disabled control is typically not selectable from the user interface and draws with an inactive or "grayed" look.\r
839      * \r
840      * @param enabled the new enabled state\r
841      * \r
842      * @exception SWTException <ul>\r
843      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
844      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
845      *                </ul>\r
846      */\r
847     @Override\r
848     public void setEnabled(final boolean enabled) {\r
849         super.setEnabled(enabled);\r
850         this.leftButton.setEnabled(enabled);\r
851         this.rightButton.setEnabled(enabled);\r
852         this.text.setEnabled(enabled);\r
853 \r
854     }\r
855 \r
856     /**\r
857      * Sets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null.\r
858      * \r
859      * @param font the new font (or null)\r
860      * \r
861      * @exception IllegalArgumentException <ul>\r
862      *                <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>\r
863      *                </ul>\r
864      * @exception SWTException <ul>\r
865      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
866      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
867      *                </ul>\r
868      */\r
869     @Override\r
870     public void setFont(final Font font) {\r
871         super.setFont(font);\r
872         this.text.setFont(font);\r
873     }\r
874 \r
875     /**\r
876      * Sets the receiver's foreground color to the color specified by the argument, or to the default system color for the control if the argument is null.\r
877      * <p>\r
878      * Note: This operation is a hint and may be overridden by the platform.\r
879      * </p>\r
880      * \r
881      * @param color the new color (or null)\r
882      * \r
883      * @exception IllegalArgumentException <ul>\r
884      *                <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>\r
885      *                </ul>\r
886      * @exception SWTException <ul>\r
887      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
888      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
889      *                </ul>\r
890      */\r
891     @Override\r
892     public void setForeground(final Color color) {\r
893         super.setForeground(color);\r
894         this.leftButton.setForeground(color);\r
895         this.rightButton.setForeground(color);\r
896         this.text.setForeground(color);\r
897 \r
898     }\r
899 \r
900     /**\r
901      * Sets the receiver's pop up menu to the argument. All controls may optionally have a pop up menu that is displayed when the user requests one for the control. The sequence of key strokes, button presses and/or button releases that are used to request a pop up menu is platform specific.\r
902      * <p>\r
903      * Note: Disposing of a control that has a pop up menu will dispose of the menu. To avoid this behavior, set the menu to null before the control is disposed.\r
904      * </p>\r
905      * \r
906      * @param menu the new pop up menu\r
907      * \r
908      * @exception IllegalArgumentException <ul>\r
909      *                <li>ERROR_MENU_NOT_POP_UP - the menu is not a pop up menu</li>\r
910      *                <li>ERROR_INVALID_PARENT - if the menu is not in the same widget tree</li>\r
911      *                <li>ERROR_INVALID_ARGUMENT - if the menu has been disposed</li>\r
912      *                </ul>\r
913      * @exception SWTException <ul>\r
914      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
915      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
916      *                </ul>\r
917      */\r
918     @Override\r
919     public void setMenu(final Menu menu) {\r
920         super.setMenu(menu);\r
921         this.leftButton.setMenu(menu);\r
922         this.rightButton.setMenu(menu);\r
923         this.text.setMenu(menu);\r
924     }\r
925 \r
926     /**\r
927      * Sets the receiver's tool tip text to the argument, which may be null indicating that the default tool tip for the control will be shown. For a control that has a default tool tip, such as the Tree control on Windows, setting the tool tip text to an empty string replaces the default, causing no tool tip text to be shown.\r
928      * <p>\r
929      * The mnemonic indicator (character '&amp;') is not displayed in a tool tip. To display a single '&amp;' in the tool tip, the character '&amp;' can be escaped by doubling it in the string.\r
930      * </p>\r
931      * \r
932      * @param string the new tool tip text (or null)\r
933      * \r
934      * @exception SWTException <ul>\r
935      *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>\r
936      *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>\r
937      *                </ul>\r
938      */\r
939     @Override\r
940     public void setToolTipText(final String tooltipText) {\r
941         super.setToolTipText(tooltipText);\r
942         this.leftButton.setToolTipText(tooltipText);\r
943         this.rightButton.setToolTipText(tooltipText);\r
944         this.text.setToolTipText(tooltipText);\r
945     }\r
946 \r
947 }\r