KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: CharacterPadDialog.java,v 1.10 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 import org.opensubsystems.core.util.GlobalConstants;
40
41 /**
42  * Dialog used to enter text using mouse or finger.
43  *
44  * @version $Id: CharacterPadDialog.java,v 1.10 2007/01/07 06:14:15 bastafidli Exp $
45  * @author Martin Cerba
46  * @code.reviewer Miro Halas
47  * @code.reviewed 1.7 2006/04/05 05:32:11 bastafidli
48  */

49 public class CharacterPadDialog extends SWTThickClientDialogGuiImpl
50 {
51    // Attributes ///////////////////////////////////////////////////////////////
52

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

57    protected Set JavaDoc m_setListeners = new HashSet JavaDoc();
58
59    /**
60     * character pad for this dialog
61     */

62    protected CharacterPad m_chpad = null;
63
64    /**
65     * initial text for CharacterPad
66     */

67    protected String JavaDoc m_initialText = null;
68
69    /**
70     * What is the ID of the cancel button.
71     */

72    protected int m_iCancelButtonId;
73    
74    // Public methods ///////////////////////////////////////////////////////////
75

76    /**
77     * Show the dialog.
78     *
79     * @param client - application displaying this dialog
80     * @param strTitle - title of the dialog
81     * @param strFirstLineText - text displayed in the first line of the dialog
82     * @param arSpecialButtonText - texts on the buttons
83     * @param keyboardLayout - layout of keys for the keyboard pad, see the
84     * CharacterPad.KEY_LAYOUT_XXX constants
85     * @param strMaxTextLength - maximal length of the text that can be entered
86     * @param strInitialText - initial text to put to the dialog
87     * @param bBigButtons - if true then the buttons will be displayed large
88     * otherwise they will be displayed middle size
89     * @param specialButtonListener - listener executed when the button is clicked
90     */

91    public void displayDialog(
92       ThickClient client,
93       String JavaDoc strTitle,
94       String JavaDoc strFirstLineText,
95       String JavaDoc[] arSpecialButtonText,
96       CharacterPad.CharacterPadLayout keyboardLayout,
97       int strMaxTextLength,
98       String JavaDoc strInitialText,
99       boolean bBigButtons,
100       CharacterPadListener specialButtonListener
101    )
102    {
103       createDialogWindow(client, strTitle,
104                          SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
105       displayDialog(strFirstLineText, arSpecialButtonText, keyboardLayout,
106                     strMaxTextLength, strInitialText, bBigButtons,
107                     specialButtonListener);
108    }
109    
110    /**
111     * Show the dialog.
112     *
113     * @param parentDialog - parent dialog displaying this dialog
114     * @param strTitle - title of the dialog
115     * @param strFirstLineText - text displayed in the first line of the dialog
116     * @param arSpecialButtonText - texts on the buttons
117     * @param keyboardLayout - layout of keys for the keyboard pad, see the
118     * CharacterPad.KEY_LAYOUT_XXX constants
119     * @param strMaxTextLength - maximal length of the text that can be entered
120     * @param strInitialText - initial text to put to the dialog
121     * @param bBigButtons - if true then the buttons will be displayed large
122     * otherwise they will be displayed middle size
123     * @param specialButtonListener - listener executed when the button is clicked
124     */

125    public void displayDialog(
126       ThickClientDialog parentDialog,
127       String JavaDoc strTitle,
128       String JavaDoc strFirstLineText,
129       String JavaDoc[] arSpecialButtonText,
130       CharacterPad.CharacterPadLayout keyboardLayout,
131       int strMaxTextLength,
132       String JavaDoc strInitialText,
133       boolean bBigButtons,
134       CharacterPadListener specialButtonListener
135    )
136    {
137       createDialogWindow(
138          parentDialog.getClient(),
139          ((SWTThickClientDialogGuiImpl)parentDialog.getGui()).getShell(),
140          strTitle, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
141       
142       displayDialog(strFirstLineText, arSpecialButtonText, keyboardLayout,
143                     strMaxTextLength, strInitialText, bBigButtons,
144                     specialButtonListener);
145    }
146
147    /**
148     * Set the initial text for the dialog.
149     *
150     * @param initialText - initial text displayed in CharacterPad
151     */

152    public void setText(
153       String JavaDoc initialText
154    )
155    {
156       if (GlobalConstants.ERROR_CHECKING)
157       {
158          assert m_chpad != null : "CharacterPad not created";
159       }
160       
161       m_chpad.setInitialText(initialText);
162    }
163
164    /**
165     * This function sets maximal nuber of characters typed
166     *
167     * @param iLimit - maximal number of chracters
168     * @return boolean - sucess flag
169     */

170
171    public boolean setTextLimit(
172       int iLimit
173    )
174    {
175       if (m_chpad != null)
176       {
177          return m_chpad.setTextLimit(iLimit);
178       }
179       return false;
180    }
181
182    /**
183     * Add listener for this character pad.
184     *
185     * @param listener - listener to add
186     */

187    public void addCharacterPadListener(
188       CharacterPadListener listener
189    )
190    {
191       m_setListeners.add(listener);
192    }
193
194    /**
195     * Remove listener from this ncharacter pad.
196     *
197     * @param listener - listener to remove
198     */

199    public void removeCharacterPadListener(
200       CharacterPadListener listener
201    )
202    {
203       m_setListeners.remove(listener);
204    }
205
206    // Helper methods ///////////////////////////////////////////////////////////
207

208    /**
209     * Show the dialog.
210     *
211     * @param strFirstLineText - text displayed in the first line of the dialog
212     * @param arSpecialButtonText - texts on the buttons
213     * @param keyboardLayout - layout of keys for the keyboard pad, see the
214     * CharacterPad.KEY_LAYOUT_XXX constants
215     * @param strMaxTextLength - maximal length of the text that can be entered
216     * @param strInitialText - initial text to put to the dialog
217     * @param bBigButtons - if true then the buttons will be displayed large
218     * otherwise they will be displayed middle size
219     * @param specialButtonListener - listener executed when the button is clicked
220     */

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

267       interactWithUser();
268    }
269
270    /**
271     * Create the client area of the dialog.
272     *
273     * This needs to be a separate method instead of being in constructor since
274     * when this class in instantiated dynamically we do not have option to pass
275     * parameters to constructor.
276     *
277     * @param strFirstLineText - text displayed in the first line of the dialog
278     * @param specialButtons - array of strings for special action buttons
279     * displayed on the bottom of the dialog
280     * @param keyboardLayout - layout of keys for the keyboard pad, see the
281     * CharacterPad.KEY_LAYOUT_XXX constants
282     * @param specialFlags - special styles for character pad, combination of
283     * CHP_XXX values
284     * @param bBigButtons - if true then the buttons will be displayed large
285     * otherwise they will be displayed middle size
286     * TODO: Bug: SWT 3.1.2: This should be returning void, but due to the bug
287     * bellow we need to return the size so we can adjust the size of the window
288     * @return Point - size of the number pad component
289     */

290    protected Point createClientArea(
291       String JavaDoc strFirstLineText,
292       String JavaDoc[] specialButtons,
293       CharacterPad.CharacterPadLayout keyboardLayout,
294       int specialFlags,
295       boolean bBigButtons
296    )
297    {
298       
299       // Add Cancel button automatically so that everybody doesn't have to add it
300
String JavaDoc[] extendedButtons;
301       
302       if (specialButtons != null)
303       {
304          extendedButtons = new String JavaDoc[specialButtons.length + 1];
305          System.arraycopy(specialButtons, 0, extendedButtons, 0,
306                           specialButtons.length);
307       }
308       else
309       {
310          extendedButtons = new String JavaDoc[1];
311       }
312       extendedButtons[extendedButtons.length - 1] = "Cancel";
313       m_iCancelButtonId = extendedButtons.length - 1;
314       
315       m_shell.setLayout(new FormLayout());
316
317       Label textLabel = new Label(m_shell, SWT.NONE);
318       textLabel.setText(strFirstLineText);
319       ResourceManager.getInstance().setBigLabelFont(textLabel);
320       FormData textLabelData = new FormData();
321       textLabelData.top = new FormAttachment(0, 5);
322       textLabelData.left = new FormAttachment(0, 5);
323       textLabelData.right = new FormAttachment(100, -5);
324       textLabel.setLayoutData(textLabelData);
325
326       m_chpad = new CharacterPad(m_shell, SWT.NONE, extendedButtons,
327                                  keyboardLayout, specialFlags, bBigButtons);
328       
329       FormData chpadData;
330       Point preferredSize;
331       
332       preferredSize = m_chpad.getPreferredSize();
333       chpadData = new FormData(preferredSize.x, preferredSize.y);
334       chpadData.top = new FormAttachment(textLabel, 5);
335       chpadData.left = new FormAttachment(textLabel, 0, SWT.LEFT);
336       chpadData.bottom = new FormAttachment(100, -5);
337       chpadData.right = new FormAttachment(textLabel, 0, SWT.RIGHT);
338       m_chpad.setLayoutData(chpadData);
339       m_chpad.addCharacterPadListener(new CharacterPadListener()
340       {
341          public boolean takeAction(
342             CharacterPad chpad,
343             int numberValue,
344             String JavaDoc strButtonName,
345             String JavaDoc strText
346          )
347          {
348             boolean bShouldClose = true;
349             
350             if (numberValue != m_iCancelButtonId)
351             {
352                if ((strText == null) || (strText.length() == 0))
353                {
354                   strText = "";
355                }
356                for (Iterator JavaDoc items = m_setListeners.iterator(); items.hasNext();)
357                {
358                   bShouldClose &= ((CharacterPadListener) items.next()).takeAction(
359                                     chpad, numberValue, strButtonName, strText);
360                }
361             }
362             
363             if (bShouldClose)
364             {
365                m_shell.close();
366             }
367             
368             return bShouldClose;
369          }
370       });
371
372       return m_chpad.computeSize(SWT.DEFAULT, SWT.DEFAULT);
373    }
374 }
375
Popular Tags