KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ShellUtils.java,v 1.4 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.events.FocusAdapter;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.ShellAdapter;
27 import org.eclipse.swt.events.ShellEvent;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32 import org.opensubsystems.core.util.GlobalConstants;
33
34 /**
35  * Set of utility methods to work with SWT shells.
36  *
37  * @version $Id: ShellUtils.java,v 1.4 2007/01/07 06:14:22 bastafidli Exp $
38  * @author Miro Halas
39  * @code.reviewer Miro Halas
40  * @code.reviewed 1.2 2006/02/16 14:41:45 bastafidli
41  */

42 public final class ShellUtils
43 {
44    // Constants ////////////////////////////////////////////////////////////////
45

46    /**
47     * How mach space to put between this and parent dialog when positioned
48     * next to each other.
49     */

50    public static final int SPACE_BETWEEN_DIALOGS = 10;
51
52    // Constructors /////////////////////////////////////////////////////////////
53

54    /**
55     * Private constructor since this class cannot be instantiated
56     */

57    private ShellUtils(
58    )
59    {
60       // Do nothing
61
}
62    
63    // Public methods ///////////////////////////////////////////////////////////
64

65    /**
66     * The client area given by SWT method doesn't reflect position. This method
67     * returns client area with position (x, y) adjusted to real position.
68     *
69     * @param client - shell for which we want to get the area
70     * @return Rectangle - client area with real position
71     */

72    public static Rectangle getAdjustedClientArea(
73       Shell client
74    )
75    {
76       Rectangle clientRect;
77       Point clientLocation;
78       
79       clientRect = client.getClientArea();
80       clientLocation = client.getLocation();
81       
82       clientRect.x = clientLocation.x;
83       clientRect.y = clientLocation.y;
84       
85       return clientRect;
86    }
87    
88    /**
89     * Center one shell above another
90     *
91     * @param client - shell to center
92     * @param parent - shell to center above
93     */

94    public static void centerShell(
95       Shell client,
96       Shell parent
97    )
98    {
99       centerShell(client, getAdjustedClientArea(parent));
100    }
101
102    /**
103     * Center one shell above another
104     *
105     * @param client - shell to center
106     * @param parentRect - rectangle bounding parent area
107     */

108    public static void centerShell(
109       Shell client,
110       Rectangle parentRect
111    )
112    {
113       Point clientSize;
114       
115       clientSize = client.getSize();
116       client.setLocation(parentRect.x + (parentRect.width - clientSize.x) / 2,
117             parentRect.y + (parentRect.height - clientSize.y) / 2);
118    }
119    
120    /**
121     * Run some code immidiately after the shell was opened and displayed on the
122     * screen.
123     *
124     * @param window - window for which we are waiting to open
125     * @param startupCode - code which should be run
126     */

127    public static void runAfterOpened(
128       final Shell window,
129       final Runnable JavaDoc startupCode
130    )
131    {
132       // Ehen the shell is first time activated open the first popup window
133
// TODO: Bug: SWT 3.0Mx: On Linux opening of the window behaves differently
134
// than on Windows. On Linux this message is fired first so we need to open
135
// window on it
136
window.addShellListener(new ShellAdapter()
137       {
138          public void shellActivated(ShellEvent e)
139          {
140             // We have to check if the window is visible, otherwise the code
141
// would run twice
142
if (window.isVisible())
143             {
144                startupCode.run();
145             }
146          }
147
148          public void shellClosed(ShellEvent e)
149          {
150             window.removeShellListener(this);
151          }
152       });
153       // On Windows this message is fired first so we need to open window on it
154
window.getShell().addFocusListener(new FocusAdapter()
155       {
156          public void focusLost(FocusEvent e)
157          {
158             startupCode.run();
159          }
160       });
161    }
162    
163    /**
164     * Center one shell in the bounding area while the other shell is also visible.
165     *
166     * @param parent - shell which should be also visible when the dialog is
167     * centered in the bounded area
168     * @param dialog - dialog which should be visible while also parent should
169     * be made visible
170     * @param outsideBoundingArea - this is the area to which the parent dialog
171     * and this dialog should be bounded. In that
172     * case this dialog will position the parent
173     * and itself so that both are visible
174     * @return Point - original location of the parent shell
175     */

176    public static Point centerParentChilShells(
177       Shell parent,
178       Shell dialog,
179       Rectangle outsideBoundingArea
180    )
181    {
182       Rectangle client;
183       Point dialogSize;
184       Point oldClientLocation;
185       
186       client = parent.getClientArea();
187       dialogSize = dialog.getSize();
188       oldClientLocation = parent.getLocation();
189       
190       int iFreeSpace;
191       // Try to move the parent (above this dialog) so that both are visible
192
// This is how much space I have around both of the dialogs
193
iFreeSpace = outsideBoundingArea.height - dialogSize.y - client.height;
194       if (iFreeSpace < SPACE_BETWEEN_DIALOGS)
195       {
196          // Both dialogs will not fit into the area so move the parent entirely
197
// to the top and the child entirely to the bottom
198
iFreeSpace = 0;
199       }
200       else
201         {
202          // Both dialogs will fit so place one above the other with some
203
// space in between
204
iFreeSpace = (iFreeSpace - SPACE_BETWEEN_DIALOGS) / 2;
205       }
206
207       parent.setLocation(oldClientLocation.x, outsideBoundingArea.y + iFreeSpace);
208       // Center this over bounding area and not parent in case the parent
209
// was already moved in the bounding area
210
dialog.setLocation(outsideBoundingArea.x + (outsideBoundingArea.width
211                             - dialogSize.x) / 2,
212                          outsideBoundingArea.y + outsideBoundingArea.height
213                             - dialogSize.y - iFreeSpace);
214       
215       return oldClientLocation;
216    }
217    
218    /**
219     * Compute the width of the table client area taking into account scrollbar
220     * and OS deficiencies.
221     *
222     * @param source - table to calculate
223     * @return int - width of client area
224     */

225    public static int getTableClientWidth(
226       Table source
227    )
228    {
229       Rectangle area = source.getParent().getClientArea();
230       // Point preferredSize = source.computeSize(SWT.DEFAULT, SWT.DEFAULT);
231
int width = area.width - 2 * source.getBorderWidth();
232       
233       // TODO: Bug: SWT 3.0Mx for whatever reason displays horizontal
234
// scrollbar even though it should not be there so make
235
// the area intentionally smaller even though the columns will
236
// not fill out the whole area if there is no scrollbar
237
// if (preferredSize.y > area.height)
238
// {
239
// Subtract the scrollbar width from the total column width
240
// if a vertical scrollbar will be required
241
Point vBarSize = source.getVerticalBar().getSize();
242          width -= vBarSize.x;
243       // }
244

245       if (GlobalConstants.isLinux())
246       {
247          // TODO: Bug: SWT 3.0Mx/Fedora Core 1: There is a tiny blank space
248
// between scrollbar and table which is causing the table to scroll.
249
// There is no way to calculate this space so here I have my Bulgarian
250
// constant
251
width -= 3;
252       }
253       
254       return width;
255    }
256 }
257
Popular Tags