KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > application > swt > SWTThickClientDialogGuiImpl


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: SWTThickClientDialogGuiImpl.java,v 1.10 2007/01/07 06:14:22 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.core.application.swt;
23
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import org.opensubsystems.core.application.ThickClient;
30 import org.opensubsystems.core.application.ThickClientDialog;
31 import org.opensubsystems.core.application.ThickClientDialogGui;
32 import org.opensubsystems.core.util.GlobalConstants;
33
34 /**
35  * Base class for SWT Thick Client dialogs.
36  *
37  * @version $Id: SWTThickClientDialogGuiImpl.java,v 1.10 2007/01/07 06:14:22 bastafidli Exp $
38  * @author Miro Halas
39  * @code.reviewer Miro Halas
40  * @code.reviewed 1.7 2006/04/05 04:57:36 bastafidli
41  */

42 public abstract class SWTThickClientDialogGuiImpl implements ThickClientDialogGui
43 {
44    // Attributes ///////////////////////////////////////////////////////////////
45

46    /**
47     * Client application displaying this dialog.
48     */

49    protected ThickClient m_client;
50    
51    /**
52     * Shell representing this dialog.
53     */

54    protected Shell m_shell;
55    
56    /**
57     * Parent shell of this dialog which can be null if the dialog is a top level
58     * window.
59     */

60    protected Shell m_parent;
61
62    /**
63     * If this parameter is set it represents the area to which the parent dialog
64     * and this dialog should be bound. In that case this dialog will position
65     * the parent and itself so that both are visible, otherwise if this
66     * parameter is not set ans is null, then this dialog will be centered on top
67     * of the parent shell.
68     */

69    protected Rectangle m_outsideBoundingArea;
70    
71    /**
72     * If not null then original location of the dialog parent.
73     */

74    protected Point m_oldClientLocation;
75
76    // Public methods ///////////////////////////////////////////////////////////
77

78    /**
79     * {@inheritDoc}
80     */

81    public Object JavaDoc displayMessage(
82       String JavaDoc strTitle,
83       String JavaDoc strMessage,
84       Object JavaDoc additionalInfo
85    )
86    {
87       return ((SWTThickClientGui)m_client.getGui()).displayMessage(m_shell,
88                                                        strTitle, strMessage,
89                                                        additionalInfo);
90    }
91    
92    /**
93     * Get client displaying this dialog.
94     *
95     * @return ThickClient - client displaying this dialog
96     */

97    public ThickClient getClient()
98    {
99       return m_client;
100    }
101    
102    /**
103     * Get shell of this dialog.
104     *
105     * @return Shell - shell of this dialog
106     */

107    public Shell getShell(
108    )
109    {
110       return m_shell;
111    }
112
113    // Helper methods ///////////////////////////////////////////////////////////
114

115    /**
116     * Create the shell for this top level dialog.
117     *
118     * @param client - client application displaying this dialog
119     * @param strTitle - title of the shell
120     * @param iStyle - style of the shell
121     */

122    protected void createDialogWindow(
123       ThickClient client,
124       String JavaDoc strTitle,
125       int iStyle
126    )
127    {
128       SWTThickClientGui gui = (SWTThickClientGui)client.getGui();
129       Shell parentShell = gui.getShell();
130       
131       createDialogWindow(client, parentShell, strTitle, iStyle);
132       // m_outsideBoundingArea automatically initialized to null
133
}
134
135    /**
136     * Create the shell for this child dialog.
137     *
138     * @param dialog - dialog for which the window is being created
139     * @param strTitle - title of the shell
140     * @param iStyle - style of the shell
141     */

142    protected void createDialogWindow(
143       ThickClientDialog dialog,
144       String JavaDoc strTitle,
145       int iStyle
146    )
147    {
148       ThickClient client;
149       ThickClientDialog parentDialog;
150       Shell parentShell = null;
151       
152       client = dialog.getClient();
153       parentDialog = dialog.getParentDialog();
154       if (parentDialog != null)
155       {
156          SWTThickClientDialogGuiImpl gui;
157          
158          // This dialog has a parent dialog so get the shell which will be
159
// parent of the newly created window
160
gui = (SWTThickClientDialogGuiImpl)parentDialog.getGui();
161          parentShell = gui.getShell();
162       }
163       else
164       {
165          // This is a top level dialog (either it is a dialog displayed directly
166
// by client or even dialog displayed before the client is visible)
167
SWTThickClientGui gui;
168          
169          gui = (SWTThickClientGui)client.getGui();
170          if (gui != null)
171          {
172             parentShell = gui.getShell();
173          }
174       }
175       createDialogWindow(client, parentShell, strTitle, iStyle);
176    }
177
178    /**
179     * Create the shell for this dialog.
180     *
181     * @param client - client application displaying this dialog
182     * @param parentShell - parent shell of this dialog
183     * @param strTitle - title of the shell
184     * @param iStyle - style of the shell
185     */

186    protected void createDialogWindow(
187       ThickClient client,
188       Shell parentShell,
189       String JavaDoc strTitle,
190       int iStyle
191    )
192    {
193       m_client = client;
194       m_parent = parentShell;
195       
196       SWTThickClientGui gui = (SWTThickClientGui)m_client.getGui();
197       Image iconImage = gui.getIconImage();
198       Display display = gui.getDisplay();
199          
200       if (m_parent == null)
201       {
202          m_shell = new Shell(display, iStyle);
203       }
204       else
205       {
206          m_shell = new Shell(parentShell, iStyle);
207          
208          // This is at least a second level dialog therefore make sure that this
209
// dialog and the parent dialog are both visible since that will make
210
// work with both more pleasant since while working on the top level
211
// user can see the one above
212
Shell clientAreaShell;
213
214          clientAreaShell = gui.getShell();
215          // This might be first level dialog
216
// client->dialog1 and in that case we do not want to do anything
217
if (clientAreaShell != parentShell)
218          {
219             // This might be second level dialog with no client
220
// dialog1->dialog2
221
// and in that case we want to position them where the client would
222
// be if it exists
223
if (clientAreaShell == null)
224             {
225                m_outsideBoundingArea = gui.determineClientArea(display,
226                                               m_client.getScreenPosition(),
227                                               m_client.isFixedSize());
228             }
229             else
230             {
231                // This might be second level dialog
232
// client->dialog1->dialog2
233
// or this might be third or above level dialog
234
// (client->dialog1->dialog2->dialog3)
235
// so position them on top of client area instead of the dialog1
236
// (in case of third level) since it wouldn't make sense
237
// to restrict dialog 2 and 3 just to area of dialog 1
238
m_outsideBoundingArea = ShellUtils.getAdjustedClientArea(
239                                                      clientAreaShell);
240             }
241          }
242       }
243
244       // Set the default cursor for this shell which will hide it on the
245
// touchscreen if such option is specified
246
ResourceManager.getInstance().setDefaultCursor(m_shell);
247       if (iconImage != null)
248       {
249          m_shell.setImage(iconImage);
250       }
251       m_shell.setText(strTitle);
252    }
253
254    /**
255     * Display the dialog window. This method should be called when the dialog
256     * is ready to be displayed on the screen and interact with the user.
257     *
258     * @param ptSize - size of the dialog or null if the dialog should have it's
259     * natural size
260     */

261    protected void displayDialogWindow(
262       Point ptSize
263    )
264    {
265       displayDialogWindow(ptSize, m_outsideBoundingArea);
266    }
267    
268    /**
269     * Display the dialog window. This method should be called when the dialog
270     * is ready to be displayed on the screen and interact with the user.
271     *
272     * Note: This method is private so that derived classes are forced to use
273     * one of the protected methods to make it simpler for the derived
274     * classes to decide what method to use.
275     *
276     * @param ptSize - size of the dialog or null if the dialog should have it's
277     * natural size
278     * @param outsideBoundingArea - if this parameter is set it represents the
279     * area to which the parent dialog and this
280     * dialog should be bound. In that case this
281     * dialog will position the parent and itself so
282     * that both are visible, otherwise if this
283     * parameter is not set, then this dialog will
284     * be centered on top of the parent dialog.
285     */

286    private void displayDialogWindow(
287       Point ptSize,
288       Rectangle outsideBoundingArea
289    )
290    {
291       SWTThickClientGui gui = (SWTThickClientGui)m_client.getGui();
292       Display display = gui.getDisplay();
293       
294       m_shell.pack(true);
295       if (ptSize != null)
296       {
297          m_shell.setSize(ptSize);
298       }
299
300       if (m_parent == null)
301       {
302          Rectangle area = null;
303          
304          area = gui.determineClientArea(display,
305                                         m_client.getScreenPosition(),
306                                         m_client.isFixedSize());
307          ShellUtils.centerShell(m_shell, area);
308       }
309       else
310       {
311          if (outsideBoundingArea == null)
312          {
313             ShellUtils.centerShell(m_shell, m_parent);
314          }
315          else
316          {
317             m_oldClientLocation = ShellUtils.centerParentChilShells(m_parent,
318                                      m_shell, outsideBoundingArea);
319          }
320       }
321      
322       m_shell.open();
323    }
324    
325    /**
326     * Main message loop which processes messages for the dialog caused by
327     * user actions such as typing or mouse clicks.
328     */

329    protected void interactWithUser(
330    )
331    {
332       try
333       {
334          SWTThickClientGui gui = (SWTThickClientGui)m_client.getGui();
335          Display display = gui.getDisplay();
336          
337          while (!m_shell.isDisposed())
338          {
339             if (!display.readAndDispatch())
340             {
341                display.sleep();
342             }
343          }
344       }
345       finally
346       {
347          // If we move the parent then move it back
348
if (m_oldClientLocation != null)
349          {
350             if (GlobalConstants.ERROR_CHECKING)
351             {
352                assert m_parent != null
353                       : "If old location is initialized parent has to be as well.";
354             }
355             
356             m_parent.setLocation(m_oldClientLocation);
357          }
358       }
359    }
360 }
361
Popular Tags