KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > layout > CellLayoutUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.layout;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Shell;
20
21 /**
22  * Contains helper methods for CellLayout. Many of these methods are workarounds for
23  * known SWT bugs. They should be updated once the original bugs are fixed in SWT.
24  *
25  * @since 3.0
26  */

27 class CellLayoutUtil {
28
29     private static Point zero = new Point(0, 0);
30
31     private static Point minimumShellSize;
32
33     private static CellData defaultData = new CellData();
34
35     /**
36      * Returns the minimum size for the given composite. That is,
37      * this returns the smallest values that will have any effect
38      * when passed into the composite's setSize method. Passing any
39      * smaller value is equivalent to passing the minimum size.
40      * <p>
41      * This method is intended for use by layouts. The layout can
42      * use this information when determining its preferred size.
43      * Returning a preferred size smaller than the composite's
44      * minimum size is pointless since the composite could never
45      * be set to that size. The layout may choose a different preferred
46      * size in this situation.
47      * </p><p>
48      * Note that this method is only concerned with restrictions imposed
49      * by the composite; not it's layout. If the only restriction on the
50      * composite's size is imposed by the layout, then this method returns (0,0).
51      * </p><p>
52      * Currently SWT does not expose this information through
53      * API, so this method is developed using trial-and-error. Whenever
54      * a composite is discovered that will not accept sizes below
55      * a certain threshold on some platform, this method should be
56      * updated to reflect that fact.
57      * </p><p>
58      * At this time, the only known composite that has a minimum size
59      * are Shells.
60      * </p>
61      *
62      * @param toCompute the composite whose minimum size is being computed
63      * @return a size, in pixels, which is the smallest value that can be
64      * passed into the composite's setSize(...) method.
65      */

66     static Point computeMinimumSize(Composite toCompute) {
67         if (toCompute instanceof Shell) {
68             if (minimumShellSize == null) {
69                 Shell testShell = new Shell((Shell) toCompute, SWT.DIALOG_TRIM
70                         | SWT.RESIZE);
71                 testShell.setSize(0, 0);
72                 minimumShellSize = testShell.getSize();
73                 testShell.dispose();
74             }
75
76             return minimumShellSize;
77         }
78
79         // If any other composites are discovered to have minimum sizes,
80
// add heuristics for them here.
81

82         // Otherwise, the composite can be reduced to size (0,0)
83

84         return zero;
85     }
86
87     /**
88      * Returns the CellData associated with the given control. If the control
89      * does not have any layout data associated with it, a default object is returned.
90      * If the control has a GridData object associated with it, an equivalent
91      * CellData object will be returned.
92      *
93      * @param control
94      * @return
95      */

96     static CellData getData(Control control) {
97         Object JavaDoc layoutData = control.getLayoutData();
98         CellData data = null;
99
100         if (layoutData instanceof CellData) {
101             data = (CellData) layoutData;
102         } else if (layoutData instanceof GridData) {
103             data = new CellData((GridData) layoutData);
104         }
105
106         if (data == null) {
107             data = defaultData;
108         }
109
110         return data;
111     }
112 }
113
Popular Tags