KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: NumberPadDialog.java,v 1.9 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.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.graphics.Rectangle;
31 import org.eclipse.swt.layout.FormAttachment;
32 import org.eclipse.swt.layout.FormData;
33 import org.eclipse.swt.layout.FormLayout;
34 import org.eclipse.swt.widgets.Label;
35 import org.opensubsystems.core.application.ThickClient;
36 import org.opensubsystems.core.application.ThickClientDialog;
37 import org.opensubsystems.core.application.swt.ResourceManager;
38 import org.opensubsystems.core.application.swt.SWTThickClientDialogGuiImpl;
39
40 /**
41  * Dialog used to enter number using mouse or finger.
42  *
43  * @version $Id: NumberPadDialog.java,v 1.9 2007/01/07 06:14:15 bastafidli Exp $
44  * @author Martin Cerba
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.7 2006/04/05 05:33:09 bastafidli
47  */

48 public class NumberPadDialog extends SWTThickClientDialogGuiImpl
49 {
50    // Attributes ///////////////////////////////////////////////////////////////
51

52    /**
53     * Set where all listeners listening to the event notifications specific
54     * to this object are stored.
55     */

56    protected Set JavaDoc m_setListeners = new HashSet JavaDoc();
57
58    /**
59     * Variable to store number pad text
60     */

61    protected String JavaDoc m_text = null;
62
63    /**
64     * Number pad of this dialog
65     */

66    protected NumberPad m_npad = null;
67
68    // Public methods ///////////////////////////////////////////////////////////
69

70    /**
71     * Show the dialog.
72     *
73     * @param client - application displaying this dialog
74     * @param strTitle - title of the dialog
75     * @param strFirstLineText - text displayed in the first line of the dialog
76     * @param strSpecialButtonText - text on the button
77     * @param strMaxTextLength - maximal length of the text that can be entered
78     * @param strInitialText - initial text to put to the dialog
79     * @param bBigButtons - if true then the buttons will be displayed large
80     * otherwise they will be displayed middle size
81     * @param bDecimalDot - if true extra line with decimal dot will be added
82     * to dialog. If bDecimalDot id true, number of decimal
83     * places can be set
84     * @param specialButtonListener - listener executed when the button is clicked
85     */

86    public void displayDialog(
87       ThickClient client,
88       String JavaDoc strTitle,
89       String JavaDoc strFirstLineText,
90       String JavaDoc strSpecialButtonText,
91       int strMaxTextLength,
92       String JavaDoc strInitialText,
93       boolean bBigButtons,
94       boolean bDecimalDot,
95       NumberPadListener specialButtonListener
96    )
97    {
98       createDialogWindow(client, strTitle,
99                          SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
100       displayDialog(strFirstLineText, strSpecialButtonText, strMaxTextLength,
101                     strInitialText, bBigButtons, bDecimalDot,
102                     specialButtonListener);
103    }
104    
105    /**
106     * Show the dialog.
107     *
108     * @param parentDialog - dialog displaying this dialog
109     * @param strTitle - title of the dialog
110     * @param strFirstLineText - text displayed in the first line of the dialog
111     * @param strSpecialButtonText - text on the button
112     * @param strMaxTextLength - maximal length of the text that can be entered
113     * @param strInitialText - initial text to put to the dialog
114     * @param bBigButtons - if true then the buttons will be displayed large
115     * otherwise they will be displayed middle size
116     * @param bDecimalDot - if true extra line with decimal dot will be added
117     * to dialog. If bDecimalDot id true, number of decimal
118     * places can be set
119     * @param specialButtonListener - listener executed when the button is clicked
120     */

121    public void displayDialog(
122       ThickClientDialog parentDialog,
123       String JavaDoc strTitle,
124       String JavaDoc strFirstLineText,
125       String JavaDoc strSpecialButtonText,
126       int strMaxTextLength,
127       String JavaDoc strInitialText,
128       boolean bBigButtons,
129       boolean bDecimalDot,
130       NumberPadListener specialButtonListener
131    )
132    {
133       createDialogWindow(
134          parentDialog.getClient(),
135          ((SWTThickClientDialogGuiImpl)parentDialog.getGui()).getShell(),
136          strTitle, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
137       
138       displayDialog(strFirstLineText, strSpecialButtonText, strMaxTextLength,
139                     strInitialText, bBigButtons, bDecimalDot,
140                     specialButtonListener);
141    }
142    
143    /**
144     * Get the number pad text
145     *
146     * @return String
147     */

148    public String JavaDoc getText()
149    {
150       return m_text;
151    }
152
153    /**
154     * @param string - number pad text
155     */

156    public void setText(
157       String JavaDoc string
158    )
159    {
160       m_text = null;
161       if (m_npad != null)
162       {
163          m_npad.setText(string);
164          m_text = m_npad.getText();
165       }
166    }
167
168    /**
169     * This function sets maximal nuber of characters typed
170     *
171     * @param iLimit - maximal number of chracters
172     * @return boolean - sucess flag
173     */

174
175    public boolean setTextLimit(
176       int iLimit
177    )
178    {
179       if (m_npad != null)
180       {
181          return m_npad.setTextLimit(iLimit);
182       }
183       return false;
184    }
185
186    /**
187     * Add listener for this number pad.
188     *
189     * @param listener - listener to add
190     */

191    public void addNumberPadListener(
192       NumberPadListener listener
193    )
194    {
195       m_setListeners.add(listener);
196    }
197
198    /**
199     * Remove listener from this number pad.
200     *
201     * @param listener - listener to remove
202     */

203    public void removeNumberPadListener(
204       NumberPadListener listener
205    )
206    {
207       m_setListeners.remove(listener);
208    }
209
210    // Helper methods ///////////////////////////////////////////////////////////
211

212    /**
213     * Show the dialog.
214     *
215     * @param strFirstLineText - text displayed in the first line of the dialog
216     * @param strSpecialButtonText - text on the button
217     * @param strMaxTextLength - maximal length of the text that can be entered
218     * @param strInitialText - initial text to put to the dialog
219     * @param bBigButtons - if true then the buttons will be displayed large
220     * otherwise they will be displayed middle size
221     * @param bDecimalDot - if true extra line with decimal dot will be added
222     * to dialog. If bDecimalDot id true, number of decimal
223     * places can be set
224     * @param specialButtonListener - listener executed when the button is clicked
225     */

226    protected void displayDialog(
227       String JavaDoc strFirstLineText,
228       String JavaDoc strSpecialButtonText,
229       int strMaxTextLength,
230       String JavaDoc strInitialText,
231       boolean bBigButtons,
232       boolean bDecimalDot,
233       NumberPadListener specialButtonListener
234    )
235    {
236       Point bugfixSize = createClientArea(strFirstLineText, strSpecialButtonText,
237                                           strInitialText, bBigButtons,
238                                           bDecimalDot);
239       addNumberPadListener(specialButtonListener);
240       // TODO: For Miro: Move this to createClientArea
241
setTextLimit(strMaxTextLength);
242       setText(strInitialText);
243       
244       displayDialogWindow(null);
245       
246       // TODO: Bug: SWT 3.1.2: Remove this code once the return value
247
// of createClientArea is removed
248
System.out.println("bugfixSize " + bugfixSize);
249       Rectangle clientArea;
250       Point shellSize;
251       
252       clientArea = m_shell.getClientArea();
253       if (bugfixSize.x < clientArea.width)
254       {
255          bugfixSize.x = clientArea.width;
256       }
257       if (bugfixSize.y < clientArea.height)
258       {
259          bugfixSize.y = clientArea.height;
260       }
261       shellSize = m_shell.getSize();
262       System.out.println("shellSize " + shellSize);
263       bugfixSize.x += 2 * (shellSize.x - clientArea.width);
264       bugfixSize.y += (shellSize.y - clientArea.height);
265       System.out.println("new bugfixSize " + bugfixSize);
266       m_shell.setSize(bugfixSize);
267       // end of td
268

269       interactWithUser();
270    }
271    
272    /**
273     * Create the client area of the dialog.
274     *
275     * This needs to be a separate method instead of being in constructor since
276     * when this class in instantiated dynamically we do not have option to pass
277     * parameters to constructor.
278     *
279     * @param strFirstLineText - text displayed in the first line of the dialog
280     * @param strSpecialButtonText - text on the button
281     * @param strInitialText - initial text to put to the dialog
282     * @param bBigButtons - if true then the buttons will be displayed large
283     * otherwise they will be displayed middle size
284     * @param bDecimalDot - if true extra line with decimal dot will be added
285     * to dialog. If bDecimalDot id true, number of decimal
286     * places can be set
287     * TODO: Bug: SWT 3.1.2: This should be returning void, but due to the bug
288     * bellow we need to return the size so we can adjust the size of the window
289     * @return Point - size of the number pad component
290     */

291    protected Point createClientArea(
292       String JavaDoc strFirstLineText,
293       String JavaDoc strSpecialButtonText,
294       String JavaDoc strInitialText,
295       boolean bBigButtons,
296       boolean bDecimalDot
297    )
298    {
299       m_shell.setLayout(new FormLayout());
300
301       Label loginLabel = new Label(m_shell, SWT.NONE);
302       loginLabel.setText(strFirstLineText);
303       ResourceManager.getInstance().setBigLabelFont(loginLabel);
304       FormData loginLabelData = new FormData();
305       loginLabelData.top = new FormAttachment(0, 5);
306       loginLabelData.left = new FormAttachment(0, 5);
307       loginLabelData.right = new FormAttachment(100, -5);
308       loginLabel.setLayoutData(loginLabelData);
309
310       m_npad = new NumberPad(m_shell, SWT.NONE, strSpecialButtonText,
311       // TODO: Bug: SWT 3.1.2: The text here should be Cancel, but due to bug
312
// #126986 the underlying layout calculates incorrect size of the control
313
// If it is Exit, it is OK
314
"Cancel", null, bBigButtons, bDecimalDot);
315
316       FormData npadData;
317       Point preferredSize;
318       
319       preferredSize = m_npad.getPreferredSize();
320
321       npadData = new FormData(preferredSize.x, preferredSize.y);
322       npadData.top = new FormAttachment(loginLabel, 5);
323       npadData.left = new FormAttachment(loginLabel, 0, SWT.LEFT);
324       npadData.bottom = new FormAttachment(100, -5);
325       npadData.right = new FormAttachment(loginLabel, 0, SWT.RIGHT);
326       m_npad.setLayoutData(npadData);
327       m_npad.addNumberPadListener(new NumberPadListener()
328       {
329          public boolean takeAction(
330             NumberPad npad,
331             String JavaDoc strText
332          )
333          {
334             boolean bShouldClose = true;
335       
336             if ((strText == null) || (strText.length() == 0))
337             {
338                m_text = strText;
339             }
340             else
341             {
342                m_text = null;
343             }
344       
345             for (Iterator JavaDoc items = m_setListeners.iterator(); items.hasNext();)
346             {
347                bShouldClose &= ((NumberPadListener)items.next()).takeAction(
348                                                                     npad, strText);
349             }
350       
351             if (bShouldClose)
352             {
353                m_shell.close();
354             }
355             
356             return bShouldClose;
357          }
358       });
359
360       return m_npad.computeSize(SWT.DEFAULT, SWT.DEFAULT);
361    }
362 }
363
Popular Tags