KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > CBannerLayout


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.swt.custom;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16
17 /**
18  * This class provides the layout for CBanner
19  *
20  * @see CBanner
21  */

22 class CBannerLayout extends Layout {
23
24 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
25     CBanner banner = (CBanner)composite;
26     Control left = banner.left;
27     Control right = banner.right;
28     Control bottom = banner.bottom;
29     boolean showCurve = left != null && right != null;
30     int height = hHint;
31     int width = wHint;
32     
33     // Calculate component sizes
34
Point bottomSize = new Point(0, 0);
35     if (bottom != null) {
36         int trim = computeTrim(bottom);
37         int w = wHint == SWT.DEFAULT ? SWT.DEFAULT : Math.max(0, width - trim);
38         bottomSize = computeChildSize(bottom, w, SWT.DEFAULT, flushCache);
39     }
40     Point rightSize = new Point(0, 0);
41     if (right != null) {
42         int trim = computeTrim(right);
43         int w = SWT.DEFAULT;
44         if (banner.rightWidth != SWT.DEFAULT) {
45             w = banner.rightWidth - trim;
46             if (left != null) {
47                 w = Math.min(w, width - banner.curve_width + 2* banner.curve_indent - CBanner.MIN_LEFT - trim);
48             }
49             w = Math.max(0, w);
50         }
51         rightSize = computeChildSize(right, w, SWT.DEFAULT, flushCache);
52         if (wHint != SWT.DEFAULT) {
53             width -= rightSize.x + banner.curve_width - 2* banner.curve_indent;
54         }
55     }
56     Point leftSize = new Point(0, 0);
57     if (left != null) {
58         int trim = computeTrim(left);
59         int w = wHint == SWT.DEFAULT ? SWT.DEFAULT : Math.max(0, width - trim);
60         leftSize = computeChildSize(left, w, SWT.DEFAULT, flushCache);
61     }
62     
63     // Add up sizes
64
width = leftSize.x + rightSize.x;
65     height = bottomSize.y;
66     if (bottom != null && (left != null || right != null)) {
67         height += CBanner.BORDER_STRIPE + 2;
68     }
69     if (left != null) {
70         if (right == null) {
71             height += leftSize.y;
72         } else {
73             height += Math.max(leftSize.y, banner.rightMinHeight == SWT.DEFAULT ? rightSize.y : banner.rightMinHeight);
74         }
75     } else {
76         height += rightSize.y;
77     }
78     if (showCurve) {
79         width += banner.curve_width - 2*banner.curve_indent;
80         height += CBanner.BORDER_TOP + CBanner.BORDER_BOTTOM + 2*CBanner.BORDER_STRIPE;
81     }
82     
83     if (wHint != SWT.DEFAULT) width = wHint;
84     if (hHint != SWT.DEFAULT) height = hHint;
85     
86     return new Point(width, height);
87 }
88 Point computeChildSize(Control control, int wHint, int hHint, boolean flushCache) {
89     Object JavaDoc data = control.getLayoutData();
90     if (data == null || !(data instanceof CLayoutData)) {
91         data = new CLayoutData();
92         control.setLayoutData(data);
93     }
94     return ((CLayoutData)data).computeSize(control, wHint, hHint, flushCache);
95 }
96 int computeTrim(Control c) {
97     if (c instanceof Scrollable) {
98         Rectangle rect = ((Scrollable) c).computeTrim (0, 0, 0, 0);
99         return rect.width;
100     }
101     return c.getBorderWidth () * 2;
102 }
103 protected boolean flushCache(Control control) {
104     Object JavaDoc data = control.getLayoutData();
105     if (data != null && data instanceof CLayoutData) ((CLayoutData)data).flushCache();
106     return true;
107 }
108 protected void layout(Composite composite, boolean flushCache) {
109     CBanner banner = (CBanner)composite;
110     Control left = banner.left;
111     Control right = banner.right;
112     Control bottom = banner.bottom;
113     
114     Point size = banner.getSize();
115     boolean showCurve = left != null && right != null;
116     int width = size.x - 2*banner.getBorderWidth();
117     int height = size.y - 2*banner.getBorderWidth();
118     
119     Point bottomSize = new Point(0, 0);
120     if (bottom != null) {
121         int trim = computeTrim(bottom);
122         int w = Math.max(0, width - trim);
123         bottomSize = computeChildSize(bottom, w, SWT.DEFAULT, flushCache);
124         height -= bottomSize.y + CBanner.BORDER_STRIPE + 2;
125     }
126     if (showCurve) height -= CBanner.BORDER_TOP + CBanner.BORDER_BOTTOM + 2*CBanner.BORDER_STRIPE;
127     height = Math.max(0, height);
128     Point rightSize = new Point(0,0);
129     if (right != null) {
130         int trim = computeTrim(right);
131         int w = SWT.DEFAULT;
132         if (banner.rightWidth != SWT.DEFAULT) {
133             w = banner.rightWidth - trim;
134             if (left != null) {
135                 w = Math.min(w, width - banner.curve_width + 2* banner.curve_indent - CBanner.MIN_LEFT - trim);
136             }
137             w = Math.max(0, w);
138         }
139         rightSize = computeChildSize(right, w, SWT.DEFAULT, flushCache);
140         width = width - (rightSize.x - banner.curve_indent + banner.curve_width - banner.curve_indent);
141     }
142     Point leftSize = new Point(0, 0);
143     if (left != null) {
144         int trim = computeTrim(left);
145         int w = Math.max(0, width - trim);
146         leftSize = computeChildSize(left, w, SWT.DEFAULT, flushCache);
147     }
148
149     int x = 0;
150     int y = 0;
151     int oldStart = banner.curveStart;
152     Rectangle leftRect = null;
153     Rectangle rightRect = null;
154     Rectangle bottomRect = null;
155     if (bottom != null) {
156         bottomRect = new Rectangle(x, y+size.y-bottomSize.y, bottomSize.x, bottomSize.y);
157     }
158     if (showCurve) y += CBanner.BORDER_TOP + CBanner.BORDER_STRIPE;
159     if(left != null) {
160         leftRect = new Rectangle(x, y, leftSize.x, leftSize.y);
161         banner.curveStart = x + leftSize.x - banner.curve_indent;
162         x += leftSize.x - banner.curve_indent + banner.curve_width - banner.curve_indent;
163     }
164     if (right != null) {
165         if (left != null) {
166             rightSize.y = Math.max(leftSize.y, banner.rightMinHeight == SWT.DEFAULT ? rightSize.y : banner.rightMinHeight);
167         }
168         rightRect = new Rectangle(x, y, rightSize.x, rightSize.y);
169     }
170     if (banner.curveStart < oldStart) {
171         banner.redraw(banner.curveStart - CBanner.CURVE_TAIL, 0, oldStart + banner.curve_width - banner.curveStart + CBanner.CURVE_TAIL + 5, size.y, false);
172     }
173     if (banner.curveStart > oldStart) {
174         banner.redraw(oldStart - CBanner.CURVE_TAIL, 0, banner.curveStart + banner.curve_width - oldStart + CBanner.CURVE_TAIL + 5, size.y, false);
175     }
176     /*
177      * The paint events must be flushed in order to make the curve draw smoothly
178      * while the user drags the divider.
179      * On Windows, it is necessary to flush the paints before the children are
180      * resized because otherwise the children (particularly toolbars) will flash.
181      */

182     banner.update();
183     banner.curveRect = new Rectangle(banner.curveStart, 0, banner.curve_width, size.y);
184     if (bottomRect != null) bottom.setBounds(bottomRect);
185     if (rightRect != null) right.setBounds(rightRect);
186     if (leftRect != null) left.setBounds(leftRect);
187 }
188 }
189
Popular Tags