KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > ToolbarLayout


1 /**********************************************************************
2  * Copyright (c) 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.ui.internal.browser;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.graphics.Rectangle;
16 import org.eclipse.swt.widgets.Combo;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Layout;
20 /**
21  * Custom layout for the browser's toolbar.
22  */

23 public class ToolbarLayout extends Layout {
24     private static final int SPACING = 5;
25     private static final int EXTRA_BUSY_SPACING = 2;
26     private static final int MARGIN = 2;
27
28     /* (non-Javadoc)
29      * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite, int, int, boolean)
30      */

31     protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
32         if (hHint != SWT.DEFAULT)
33             return new Point(wHint, hHint);
34         
35         Control[] children = composite.getChildren();
36         int h = 0;
37         int size = children.length;
38         for (int i = 0; i < size; i++) {
39             h = Math.max(h, children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
40         }
41         
42         return new Point(wHint, h + MARGIN * 2);
43     }
44
45     /* (non-Javadoc)
46      * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite, boolean)
47      */

48     protected void layout(Composite composite, boolean flushCache) {
49         Control[] children = composite.getChildren();
50         Rectangle r = composite.getClientArea();
51         
52         int size = children.length;
53         Point[] sizes = new Point[size];
54         for (int i = 0; i < size; i++) {
55             sizes[i] = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT);
56         }
57
58         int h = r.height - MARGIN * 2;
59         
60         // put busy icon at right with a little extra spacing
61
int busy = size - 1;
62         children[busy].setBounds(r.x + r.width - MARGIN - sizes[busy].x,
63                 r.y + MARGIN + (h-sizes[busy].y) / 2,
64                 sizes[busy].x, sizes[busy].y);
65         
66         // find combo
67
int combo = -1;
68         int tw = r.width - MARGIN * 2 - (size - 1) * SPACING
69                 - sizes[size-1].x - EXTRA_BUSY_SPACING;
70         for (int i = 0; i < size - 1; i++) {
71             if (children[i] instanceof Combo)
72                 combo = i;
73             else
74                 tw -= sizes[i].x;
75         }
76         if (combo >= 0)
77             sizes[combo].x = tw;
78         
79         // space out other children with their standard size, give combo all
80
// remaining space (if it exists)
81
int x = MARGIN;
82         for (int i = 0; i < size - 1; i++) {
83             children[i].setBounds(r.x + x, r.y + MARGIN + (h-sizes[i].y) / 2,
84                     sizes[i].x, sizes[i].y);
85             x += SPACING + sizes[i].x;
86         }
87     }
88 }
Popular Tags