KickJava   Java API By Example, From Geeks To Geeks.

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


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 SashForm
19  *
20  * @see SashForm
21  */

22 class SashFormLayout extends Layout {
23 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
24     SashForm sashForm = (SashForm)composite;
25     Control[] cArray = sashForm.getControls(true);
26     int width = 0;
27     int height = 0;
28     if (cArray.length == 0) {
29         if (wHint != SWT.DEFAULT) width = wHint;
30         if (hHint != SWT.DEFAULT) height = hHint;
31         return new Point(width, height);
32     }
33     // determine control sizes
34
boolean vertical = sashForm.getOrientation() == SWT.VERTICAL;
35     int maxIndex = 0;
36     int maxValue = 0;
37     for (int i = 0; i < cArray.length; i++) {
38         if (vertical) {
39             Point size = cArray[i].computeSize(wHint, SWT.DEFAULT, flushCache);
40             if (size.y > maxValue) {
41                 maxIndex = i;
42                 maxValue = size.y;
43             }
44             width = Math.max(width, size.x);
45         } else {
46             Point size = cArray[i].computeSize(SWT.DEFAULT, hHint, flushCache);
47             if (size.x > maxValue) {
48                 maxIndex = i;
49                 maxValue = size.x;
50             }
51             height = Math.max(height, size.y);
52         }
53     }
54     // get the ratios
55
long[] ratios = new long[cArray.length];
56     long total = 0;
57     for (int i = 0; i < cArray.length; i++) {
58         Object JavaDoc data = cArray[i].getLayoutData();
59         if (data != null && data instanceof SashFormData) {
60             ratios[i] = ((SashFormData)data).weight;
61         } else {
62             data = new SashFormData();
63             cArray[i].setLayoutData(data);
64             ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
65             
66         }
67         total += ratios[i];
68     }
69     if (ratios[maxIndex] > 0) {
70         int sashwidth = sashForm.sashes.length > 0 ? sashForm.SASH_WIDTH + sashForm.sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH;
71         if (vertical) {
72             height += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
73         } else {
74             width += (int)(total * maxValue / ratios[maxIndex]) + (cArray.length - 1) * sashwidth;
75         }
76     }
77     width += sashForm.getBorderWidth()*2;
78     height += sashForm.getBorderWidth()*2;
79     if (wHint != SWT.DEFAULT) width = wHint;
80     if (hHint != SWT.DEFAULT) height = hHint;
81     return new Point(width, height);
82 }
83
84 protected boolean flushCache(Control control) {
85     return true;
86 }
87
88 protected void layout(Composite composite, boolean flushCache) {
89     SashForm sashForm = (SashForm)composite;
90     Rectangle area = sashForm.getClientArea();
91     if (area.width <= 1 || area.height <= 1) return;
92     
93     Control[] newControls = sashForm.getControls(true);
94     if (sashForm.controls.length == 0 && newControls.length == 0) return;
95     sashForm.controls = newControls;
96     
97     Control[] controls = sashForm.controls;
98     
99     if (sashForm.maxControl != null && !sashForm.maxControl.isDisposed()) {
100         for (int i= 0; i < controls.length; i++){
101             if (controls[i] != sashForm.maxControl) {
102                 controls[i].setBounds(-200, -200, 0, 0);
103             } else {
104                 controls[i].setBounds(area);
105             }
106         }
107         return;
108     }
109     
110     // keep just the right number of sashes
111
if (sashForm.sashes.length < controls.length - 1) {
112         Sash[] newSashes = new Sash[controls.length - 1];
113         System.arraycopy(sashForm.sashes, 0, newSashes, 0, sashForm.sashes.length);
114         for (int i = sashForm.sashes.length; i < newSashes.length; i++) {
115             newSashes[i] = new Sash(sashForm, sashForm.sashStyle);
116             newSashes[i].setBackground(sashForm.background);
117             newSashes[i].setForeground(sashForm.foreground);
118             newSashes[i].addListener(SWT.Selection, sashForm.sashListener);
119         }
120         sashForm.sashes = newSashes;
121     }
122     if (sashForm.sashes.length > controls.length - 1) {
123         if (controls.length == 0) {
124             for (int i = 0; i < sashForm.sashes.length; i++) {
125                 sashForm.sashes[i].dispose();
126             }
127             sashForm.sashes = new Sash[0];
128         } else {
129             Sash[] newSashes = new Sash[controls.length - 1];
130             System.arraycopy(sashForm.sashes, 0, newSashes, 0, newSashes.length);
131             for (int i = controls.length - 1; i < sashForm.sashes.length; i++) {
132                 sashForm.sashes[i].dispose();
133             }
134             sashForm.sashes = newSashes;
135         }
136     }
137     if (controls.length == 0) return;
138     Sash[] sashes = sashForm.sashes;
139     // get the ratios
140
long[] ratios = new long[controls.length];
141     long total = 0;
142     for (int i = 0; i < controls.length; i++) {
143         Object JavaDoc data = controls[i].getLayoutData();
144         if (data != null && data instanceof SashFormData) {
145             ratios[i] = ((SashFormData)data).weight;
146         } else {
147             data = new SashFormData();
148             controls[i].setLayoutData(data);
149             ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000;
150             
151         }
152         total += ratios[i];
153     }
154     
155     int sashwidth = sashes.length > 0 ? sashForm.SASH_WIDTH + sashes [0].getBorderWidth() * 2 : sashForm.SASH_WIDTH;
156     if (sashForm.getOrientation() == SWT.HORIZONTAL) {
157         int width = (int)(ratios[0] * (area.width - sashes.length * sashwidth) / total);
158         int x = area.x;
159         controls[0].setBounds(x, area.y, width, area.height);
160         x += width;
161         for (int i = 1; i < controls.length - 1; i++) {
162             sashes[i - 1].setBounds(x, area.y, sashwidth, area.height);
163             x += sashwidth;
164             width = (int)(ratios[i] * (area.width - sashes.length * sashwidth) / total);
165             controls[i].setBounds(x, area.y, width, area.height);
166             x += width;
167         }
168         if (controls.length > 1) {
169             sashes[sashes.length - 1].setBounds(x, area.y, sashwidth, area.height);
170             x += sashwidth;
171             width = area.width - x;
172             controls[controls.length - 1].setBounds(x, area.y, width, area.height);
173         }
174     } else {
175         int height = (int)(ratios[0] * (area.height - sashes.length * sashwidth) / total);
176         int y = area.y;
177         controls[0].setBounds(area.x, y, area.width, height);
178         y += height;
179         for (int i = 1; i < controls.length - 1; i++) {
180             sashes[i - 1].setBounds(area.x, y, area.width, sashwidth);
181             y += sashwidth;
182             height = (int)(ratios[i] * (area.height - sashes.length * sashwidth) / total);
183             controls[i].setBounds(area.x, y, area.width, height);
184             y += height;
185         }
186         if (controls.length > 1) {
187             sashes[sashes.length - 1].setBounds(area.x, y, area.width, sashwidth);
188             y += sashwidth;
189             height = area.height - y;
190             controls[controls.length - 1].setBounds(area.x, y, area.width, height);
191         }
192
193     }
194 }
195 }
196
Popular Tags