KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > thickclient > application > swt > NumberPad


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: NumberPad.java,v 1.5 2007/01/07 06:14:15 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.thickclient.application.swt;
23
24 import java.util.Date JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.MessageBox;
38 import org.opensubsystems.core.application.swt.ResourceManager;
39 import org.opensubsystems.patterns.thickclient.application.ModificationListener;
40
41 /**
42  * Control, which allows user to type numbers using mouse or finger on a screen.
43  *
44  * @version $Id: NumberPad.java,v 1.5 2007/01/07 06:14:15 bastafidli Exp $
45  * @author Miro Halas
46  * @code.reviewer Miro Halas
47  * @code.reviewed 1.3 2006/04/05 05:32:51 bastafidli
48  */

49 public class NumberPad extends Composite
50                        implements ModificationListener
51 {
52    // Constants ////////////////////////////////////////////////////////////////
53

54    /**
55     * Base (smallest) control height constant.
56     */

57    protected static final int BASE_HEIGHT = 100;
58    
59    // Attributes ///////////////////////////////////////////////////////////////
60

61    /**
62     * Preferred size of this control based on the specified parameters.
63     */

64    protected Point m_preferredSize;
65    
66    /**
67     * Set where all listeners listening to the event notifications specific
68     * to this object are stored.
69     */

70    protected Set JavaDoc m_setListeners = new HashSet JavaDoc();
71    
72    /**
73     * Field which collects typed numbers.
74     */

75    protected NumericText m_numberText;
76    
77    /**
78     * Date when the content was last modified.
79     * TODO: Improve: Shouldn't this be timestamp?
80     */

81    protected Date JavaDoc m_lastModified;
82
83    /**
84     * Text to display to confirm canceling of the operation using cancel button.
85     * If null, no confirmation is displayed.
86     */

87    protected String JavaDoc m_strCancelConfirmation;
88    
89    /**
90     * If true then numbers with decimal point are allowed (there can be at most
91     * 1 decimal point character in the number).
92     */

93    protected boolean m_bDecimalDot;
94    
95    // Constructor //////////////////////////////////////////////////////////////
96

97    /**
98     * Construct new instance of the number pad control.
99     *
100     * @param parent - who is parent of this control.
101     * @param style - what style to use
102     * @param strSetButtonText - text to display in button when the user wants
103     * to finish typing number, if null, the button
104     * won't be displayed
105     * @param bBigButtons - if true then the buttons will be displayed using large
106     * fonts otherwise they will be displayed middle size
107     * @param bDecimalDot - if true extra line of buttons with decimal dot will
108     * be added to dialog. If bDecimalDot id true, number
109     * of decimal places can be set
110     */

111    public NumberPad(
112       final Composite parent,
113       int style,
114       String JavaDoc strSetButtonText,
115       boolean bBigButtons,
116       boolean bDecimalDot
117    )
118    {
119       this(parent, style, strSetButtonText, null, null, bBigButtons, bDecimalDot);
120    }
121
122    /**
123     * Construct new instance
124     *
125     * @param parent - who is parent of this screen
126     * @param style - what style to use
127     * @param strSetButtonText - text to display in button which user uses to
128     * finish typing number, if null, the button won't
129     * be displayed
130     * @param strCancelButtonText - text to display in button to cancel the
131     * operation, if null, the button won't be
132     * displayed
133     * @param strCancelConfirmation - text to display to confirm canceling of the
134     * operation using cancel button. If null, no
135     * confirmation is displayed.
136     * @param bBigButtons - if true then the buttons will be displayed using large
137     * fonts otherwise they will be displayed middle size
138     * @param bDecimalDot - if true extra line of buttons with decimal dot will
139     * be added to dialog. If bDecimalDot id true, number
140     * of decimal places can be set
141     */

142    public NumberPad(
143       final Composite parent,
144       int style,
145       String JavaDoc strSetButtonText,
146       String JavaDoc strCancelButtonText,
147       String JavaDoc strCancelConfirmation,
148       boolean bBigButtons,
149       boolean bDecimalDot
150    )
151    {
152       super(parent, style);
153
154       m_strCancelConfirmation = strCancelConfirmation;
155       m_bDecimalDot = bDecimalDot;
156       
157       int iDialogLineHeight;
158       
159       // Use the following constants 6 & 9.5 which will make the line height
160
// about the same height regardless of what is the font size. This
161
// height should be enough to allow finger to click the button
162
// without touching surrounding buttons
163
// TODO: Improve: These constants should be part of ResourceManager
164
if (bBigButtons)
165       {
166          iDialogLineHeight = (int)(ResourceManager.BIG_BUTTON_FONT_HEIGHT * 6);
167       }
168       else
169       {
170          iDialogLineHeight = (int)(ResourceManager.MIDDLE_BUTTON_FONT_HEIGHT * 9.5);
171       }
172
173       if (bDecimalDot)
174       {
175          m_preferredSize = new Point(iDialogLineHeight * 3,
176                                  iDialogLineHeight * 3 + BASE_HEIGHT);
177       }
178       else
179       {
180          m_preferredSize = new Point(iDialogLineHeight * 3,
181                                  // The height can be about 80% of the width
182
(int)Math.round((((float)iDialogLineHeight)
183                                                  * 2.5f)) + BASE_HEIGHT);
184       }
185       
186       GridLayout layout = new GridLayout();
187       
188       layout.numColumns = 6;
189       layout.marginHeight = 0;
190       layout.marginWidth = 0;
191       layout.makeColumnsEqualWidth = true;
192       setLayout(layout);
193       
194       // User can type only numbers
195
m_numberText = new NumericText(this, SWT.BORDER, bDecimalDot);
196       ResourceManager.getInstance().setBigTextFont(m_numberText.getTextField());
197
198       GridData numberData;
199       
200       numberData = new GridData();
201       numberData.horizontalSpan = 6;
202       numberData.horizontalAlignment = GridData.FILL;
203       numberData.verticalAlignment = GridData.FILL;
204       // Expand horizontally if more space becomes available
205
numberData.grabExcessHorizontalSpace = true;
206       m_numberText.setLayoutData(numberData);
207       
208       createButton("1", bBigButtons,
209                    new TextModificationSelectionAdapter(null, this, "1",
210                           m_numberText.getTextField()));
211       createButton("2", bBigButtons,
212                    new TextModificationSelectionAdapter(null, this, "2",
213                           m_numberText.getTextField()));
214       createButton("3", bBigButtons,
215                    new TextModificationSelectionAdapter(null, this, "3",
216                           m_numberText.getTextField()));
217       createButton("4", bBigButtons,
218                    new TextModificationSelectionAdapter(null, this, "4",
219                           m_numberText.getTextField()));
220       createButton("5", bBigButtons,
221                    new TextModificationSelectionAdapter(null, this, "5",
222                           m_numberText.getTextField()));
223       createButton("6", bBigButtons,
224                    new TextModificationSelectionAdapter(null, this, "6",
225                           m_numberText.getTextField()));
226       createButton("7", bBigButtons,
227                    new TextModificationSelectionAdapter(null, this, "7",
228                           m_numberText.getTextField()));
229       createButton("8", bBigButtons,
230                    new TextModificationSelectionAdapter(null, this, "8",
231                           m_numberText.getTextField()));
232       createButton("9", bBigButtons,
233                    new TextModificationSelectionAdapter(null, this, "9",
234                           m_numberText.getTextField()));
235       // C for clear content
236
createButton("C", bBigButtons,
237                    new TextModificationSelectionAdapter(null, this,
238                           TextModificationSelectionAdapter.FUNCTION_CLEAR,
239                           m_numberText.getTextField()));
240       createButton("0", bBigButtons,
241                    new TextModificationSelectionAdapter(null, this, "0",
242                            m_numberText.getTextField()));
243       // CA for clear all content
244
createButton("CA", bBigButtons,
245                    new TextModificationSelectionAdapter(null, this,
246                           TextModificationSelectionAdapter.FUNCTION_CLEAR_ALL,
247                           m_numberText.getTextField()));
248
249       if (m_bDecimalDot)
250       {
251          GridData decimalData;
252          Button decimalDotButton = new Button(this, SWT.BORDER);
253          
254          decimalDotButton.setText(m_numberText.getDecimalPointText());
255          if (bBigButtons)
256          {
257             ResourceManager.getInstance().setBigButtonFont(decimalDotButton);
258          }
259          else
260          {
261             ResourceManager.getInstance().setMiddleButtonFont(decimalDotButton);
262          }
263          decimalData = new GridData();
264          decimalData.horizontalSpan = 6;
265          decimalData.horizontalAlignment = GridData.FILL;
266          decimalData.verticalAlignment = GridData.FILL;
267          decimalDotButton.setLayoutData(decimalData);
268          decimalDotButton.addSelectionListener(
269                              new TextModificationSelectionAdapter(
270                                     null, this, m_numberText.getDecimalPointText(),
271                                     m_numberText.getTextField()));
272       }
273       
274       if ((strSetButtonText != null) && (strSetButtonText.length() > 0))
275       {
276          Button setButton;
277          GridData setData;
278          
279          setButton = new Button(this, SWT.BORDER);
280          setButton.setText(strSetButtonText);
281    
282          if (bBigButtons)
283          {
284             ResourceManager.getInstance().setBigButtonFont(setButton);
285          }
286          else
287          {
288             ResourceManager.getInstance().setMiddleButtonFont(setButton);
289          }
290          setData = new GridData();
291          if ((strCancelButtonText == null) || (strCancelButtonText.length() == 0))
292          {
293             setData.horizontalSpan = 6;
294          }
295          else
296          {
297             setData.horizontalSpan = 3;
298          }
299          setData.horizontalAlignment = GridData.FILL;
300          setData.verticalAlignment = GridData.FILL;
301          setButton.setLayoutData(setData);
302          setButton.addSelectionListener(new DelegatingSelectionAdapter()
303          {
304             public void widgetSelected(
305                SelectionEvent event
306             )
307             {
308                notifyNumberPadListeners();
309             }
310          });
311       }
312       
313       if ((strCancelButtonText != null) && (strCancelButtonText.length() > 0))
314       {
315          Button cancelButton;
316          GridData cancelData;
317          
318          cancelButton = new Button(this, SWT.BORDER);
319          cancelButton.setText(strCancelButtonText);
320    
321          if (bBigButtons)
322          {
323             ResourceManager.getInstance().setBigButtonFont(cancelButton);
324          }
325          else
326          {
327             ResourceManager.getInstance().setMiddleButtonFont(cancelButton);
328          }
329          cancelData = new GridData();
330          if ((strSetButtonText == null) || (strSetButtonText.length() == 0))
331          {
332             cancelData.horizontalSpan = 6;
333          }
334          else
335          {
336             cancelData.horizontalSpan = 3;
337          }
338          cancelData.horizontalAlignment = GridData.FILL;
339          cancelData.verticalAlignment = GridData.FILL;
340          cancelButton.setLayoutData(cancelData);
341          cancelButton.addSelectionListener(new DelegatingSelectionAdapter()
342          {
343             public void widgetSelected(
344                SelectionEvent event
345             )
346             {
347                if ((m_strCancelConfirmation != null)
348                   && (m_strCancelConfirmation.length() > 0))
349                {
350                   int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO
351                               | SWT.ICON_QUESTION;
352                   MessageBox messageBox = new MessageBox(getShell(), style);
353                   messageBox.setText("Question");
354                   messageBox.setMessage(m_strCancelConfirmation);
355                   if (messageBox.open() == SWT.YES)
356                   {
357                      parent.getShell().close();
358                   }
359                }
360                else
361                {
362                   parent.getShell().close();
363                }
364             }
365          });
366       }
367    }
368
369    // Public methods ///////////////////////////////////////////////////////////
370

371    /**
372     * Get preferred size of this control based on the specified parameters.
373     *
374     * @return Point
375     */

376    public Point getPreferredSize()
377    {
378       return m_preferredSize;
379    }
380    
381    /**
382     * Add listener for this number pad.
383     *
384     * @param listener - listener to add
385     */

386    public void addNumberPadListener(
387       NumberPadListener listener
388    )
389    {
390       m_setListeners.add(listener);
391    }
392    
393    /**
394     * Remove listener from this number pad.
395     *
396     * @param listener - listener to remove
397     */

398    public void removeNumberPadListener(
399       NumberPadListener listener
400    )
401    {
402       m_setListeners.remove(listener);
403    }
404    
405    /**
406     * {@inheritDoc}
407     */

408    public void modified(
409    )
410    {
411       // Remember date of last modification
412
m_lastModified = new Date JavaDoc();
413    }
414
415    /**
416     * @return Date - time of the last selection of the item.
417     */

418    public Date JavaDoc getLastModified()
419    {
420       return m_lastModified;
421    }
422    
423    /**
424     * Get text typed in the numeric text field.
425     *
426     * @return String - text typed in the numeric text field
427     */

428    public String JavaDoc getText(
429    )
430    {
431       return m_numberText.getTextField().getText();
432    }
433
434    /**
435     * Set text in the numeric text field.
436     *
437     * @param text - text typed in the numeric text field
438     */

439    public void setText(
440       String JavaDoc text
441    )
442    {
443       m_numberText.getTextField().setText(text);
444       // set cursor position to end
445
m_numberText.getTextField().setSelection(text.length(), text.length());
446    }
447
448    /**
449     * This function sets maximal nuber of characters typed
450     *
451     * @param iLimit - maximal number of chracters
452     * @return boolean - sucess flag
453     */

454    public boolean setTextLimit(
455       int iLimit
456    )
457    {
458       if (m_numberText != null)
459       {
460          return m_numberText.setTextLimit(iLimit);
461       }
462       
463       return false;
464    }
465
466    /**
467     * Format text to represent a number, e.g. remove leading 0 etc.
468     *
469     * @param inputText - text to format
470     * @return String formatted string
471     */

472    public String JavaDoc formatText(
473       String JavaDoc inputText
474    )
475    {
476       if (m_bDecimalDot)
477       {
478          return Double.toString(Double.parseDouble(inputText));
479       }
480       else
481       {
482          return Integer.toString(Integer.parseInt(inputText));
483       }
484    }
485
486    // Helper methods ///////////////////////////////////////////////////////////
487

488    /**
489     * Notify all listeners about event.
490     */

491    protected void notifyNumberPadListeners(
492    )
493    {
494       String JavaDoc strText;
495       boolean bShouldClear = false;
496       
497       strText = m_numberText.getTextField().getText();
498       if ((strText == null) || (strText.length() == 0))
499       {
500          strText = "0";
501       }
502       for (Iterator JavaDoc items = m_setListeners.iterator(); items.hasNext();)
503       {
504          bShouldClear = ((NumberPadListener)items.next()).takeAction(this,
505                                                                         strText)
506                         || bShouldClear;
507       }
508       if (bShouldClear)
509       {
510          if (!m_numberText.getTextField().isDisposed())
511          {
512             m_numberText.getTextField().setText("");
513          }
514       }
515    }
516
517    /**
518     * Create new button for this number pad.
519     *
520     * @param strText - text to display on the button
521     * @param bBigButtons - if true then the buttons will be displayed large
522     * otherwise they will be displayed middle size
523     * @param listener - listener processing events from the button
524     */

525    protected void createButton(
526       String JavaDoc strText,
527       boolean bBigButtons,
528       SelectionListener listener
529    )
530    {
531       Button digit;
532       GridData digitData;
533       
534       digit = new Button(this, SWT.BORDER);
535       digit.setText(strText);
536       if (bBigButtons)
537       {
538          ResourceManager.getInstance().setBigButtonFont(digit);
539       }
540       else
541       {
542          ResourceManager.getInstance().setMiddleButtonFont(digit);
543       }
544       digitData = new GridData();
545       digitData.horizontalSpan = 2;
546       digitData.horizontalAlignment = GridData.FILL;
547       digitData.verticalAlignment = GridData.FILL;
548       // Expand vertically if more space becomes available
549
digitData.grabExcessVerticalSpace = true;
550       digit.setLayoutData(digitData);
551       digit.addSelectionListener(listener);
552    }
553 }
554
Popular Tags