KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 ViewForm
19  *
20  * @see ViewForm
21  */

22 class ViewFormLayout extends Layout {
23
24 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
25     ViewForm form = (ViewForm)composite;
26     Control left = form.topLeft;
27     Control center = form.topCenter;
28     Control right = form.topRight;
29     Control content = form.content;
30     
31     Point leftSize = new Point(0, 0);
32     if (left != null) {
33         leftSize = computeChildSize(left, SWT.DEFAULT, SWT.DEFAULT, flushCache);
34     }
35     Point centerSize = new Point(0, 0);
36     if (center != null) {
37          centerSize = computeChildSize(center, SWT.DEFAULT, SWT.DEFAULT, flushCache);
38     }
39     Point rightSize = new Point(0, 0);
40     if (right != null) {
41          rightSize = computeChildSize(right, SWT.DEFAULT, SWT.DEFAULT, flushCache);
42     }
43     Point size = new Point(0, 0);
44     // calculate width of title bar
45
if (form.separateTopCenter ||
46         (wHint != SWT.DEFAULT && leftSize.x + centerSize.x + rightSize.x > wHint)) {
47         size.x = leftSize.x + rightSize.x;
48         if (leftSize.x > 0 && rightSize.x > 0) size.x += form.horizontalSpacing;
49         size.x = Math.max(centerSize.x, size.x);
50         size.y = Math.max(leftSize.y, rightSize.y);
51         if (center != null){
52             size.y += centerSize.y;
53             if (left != null ||right != null)size.y += form.verticalSpacing;
54         }
55     } else {
56         size.x = leftSize.x + centerSize.x + rightSize.x;
57         int count = -1;
58         if (leftSize.x > 0) count++;
59         if (centerSize.x > 0) count++;
60         if (rightSize.x > 0) count++;
61         if (count > 0) size.x += count * form.horizontalSpacing;
62         size.y = Math.max(leftSize.y, Math.max(centerSize.y, rightSize.y));
63     }
64     
65     if (content != null) {
66         if (left != null || right != null || center != null) size.y += 1; // allow space for a vertical separator
67
Point contentSize = new Point(0, 0);
68         contentSize = computeChildSize(content, SWT.DEFAULT, SWT.DEFAULT, flushCache);
69         size.x = Math.max (size.x, contentSize.x);
70         size.y += contentSize.y;
71         if (size.y > contentSize.y) size.y += form.verticalSpacing;
72     }
73     
74     size.x += 2*form.marginWidth;
75     size.y += 2*form.marginHeight;
76     
77     if (wHint != SWT.DEFAULT) size.x = wHint;
78     if (hHint != SWT.DEFAULT) size.y = hHint;
79     
80     return size;
81 }
82
83 Point computeChildSize(Control control, int wHint, int hHint, boolean flushCache) {
84     Object JavaDoc data = control.getLayoutData();
85     if (data == null || !(data instanceof CLayoutData)) {
86         data = new CLayoutData();
87         control.setLayoutData(data);
88     }
89     return ((CLayoutData)data).computeSize(control, wHint, hHint, flushCache);
90 }
91
92 int computeTrim(Control c) {
93     if (c instanceof Scrollable) {
94         Rectangle rect = ((Scrollable) c).computeTrim (0, 0, 0, 0);
95         return rect.width;
96     }
97     return c.getBorderWidth () * 2;
98 }
99
100 protected boolean flushCache(Control control) {
101     Object JavaDoc data = control.getLayoutData();
102     if (data != null && data instanceof CLayoutData) ((CLayoutData)data).flushCache();
103     return true;
104 }
105
106 protected void layout(Composite composite, boolean flushCache) {
107     ViewForm form = (ViewForm)composite;
108     Control left = form.topLeft;
109     Control center = form.topCenter;
110     Control right = form.topRight;
111     Control content = form.content;
112     
113     Rectangle rect = composite.getClientArea();
114     
115     Point leftSize = new Point(0, 0);
116     if (left != null && !left.isDisposed()) {
117         leftSize = computeChildSize(left, SWT.DEFAULT, SWT.DEFAULT, flushCache);
118     }
119     Point centerSize = new Point(0, 0);
120     if (center != null && !center.isDisposed()) {
121          centerSize = computeChildSize(center, SWT.DEFAULT, SWT.DEFAULT, flushCache);
122     }
123     Point rightSize = new Point(0, 0);
124     if (right != null && !right.isDisposed()) {
125          rightSize = computeChildSize(right, SWT.DEFAULT, SWT.DEFAULT, flushCache);
126     }
127     
128     int minTopWidth = leftSize.x + centerSize.x + rightSize.x + 2*form.marginWidth + 2*form.highlight;
129     int count = -1;
130     if (leftSize.x > 0) count++;
131     if (centerSize.x > 0) count++;
132     if (rightSize.x > 0) count++;
133     if (count > 0) minTopWidth += count * form.horizontalSpacing;
134         
135     int x = rect.x + rect.width - form.marginWidth - form.highlight;
136     int y = rect.y + form.marginHeight + form.highlight;
137     
138     boolean top = false;
139     if (form.separateTopCenter || minTopWidth > rect.width) {
140         int topHeight = Math.max(rightSize.y, leftSize.y);
141         if (right != null && !right.isDisposed()) {
142             top = true;
143             x -= rightSize.x;
144             right.setBounds(x, y, rightSize.x, topHeight);
145             x -= form.horizontalSpacing;
146         }
147         if (left != null && !left.isDisposed()) {
148             top = true;
149             int trim = computeTrim(left);
150             int leftW = x - rect.x - form.marginWidth - form.highlight - trim;
151             leftSize = computeChildSize(left, leftW, SWT.DEFAULT, false);
152             left.setBounds(rect.x + form.marginWidth + form.highlight, y, leftSize.x, topHeight);
153         }
154         if (top) y += topHeight + form.verticalSpacing;
155         if (center != null && !center.isDisposed()) {
156             top = true;
157             int trim = computeTrim(center);
158             int w = rect.width - 2*form.marginWidth - 2*form.highlight - trim;
159             centerSize = computeChildSize(center, w, SWT.DEFAULT, false);
160             center.setBounds(rect.x + rect.width - form.marginWidth - form.highlight - centerSize.x, y, centerSize.x, centerSize.y);
161             y += centerSize.y + form.verticalSpacing;
162         }
163     } else {
164         int topHeight = Math.max(rightSize.y, Math.max(centerSize.y, leftSize.y));
165         if (right != null && !right.isDisposed()) {
166             top = true;
167             x -= rightSize.x;
168             right.setBounds(x, y, rightSize.x, topHeight);
169             x -= form.horizontalSpacing;
170         }
171         if (center != null && !center.isDisposed()) {
172             top = true;
173             x -= centerSize.x;
174             center.setBounds(x, y, centerSize.x, topHeight);
175             x -= form.horizontalSpacing;
176         }
177         if (left != null && !left.isDisposed()) {
178             top = true;
179             Rectangle trim = left instanceof Composite ? ((Composite)left).computeTrim(0, 0, 0, 0) : new Rectangle(0, 0, 0, 0);
180             int w = x - rect.x - form.marginWidth - form.highlight - trim.width;
181             int h = topHeight - trim.height;
182             leftSize = computeChildSize(left, w, h, false);
183             left.setBounds(rect.x + form.marginWidth + form.highlight, y, leftSize.x, topHeight);
184         }
185         if (top)y += topHeight + form.verticalSpacing;
186     }
187     int oldSeperator = form.separator;
188     form.separator = -1;
189     if (content != null && !content.isDisposed()) {
190         if (left != null || right!= null || center != null){
191             form.separator = y;
192             y++;
193         }
194          content.setBounds(rect.x + form.marginWidth + form.highlight, y, rect.width - 2 * form.marginWidth - 2*form.highlight, rect.y + rect.height - y - form.marginHeight - form.highlight);
195     }
196     if (oldSeperator != -1 && form.separator != -1) {
197         int t = Math.min(form.separator, oldSeperator);
198         int b = Math.max(form.separator, oldSeperator);
199         form.redraw(form.borderLeft, t, form.getSize().x - form.borderLeft - form.borderRight, b - t, false);
200     }
201 }
202 }
203
Popular Tags