KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 class ScrolledCompositeLayout extends Layout {
23     
24     boolean inLayout = false;
25     static final int DEFAULT_WIDTH = 64;
26     static final int DEFAULT_HEIGHT = 64;
27     
28 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
29     ScrolledComposite sc = (ScrolledComposite)composite;
30     Point size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT);
31     if (sc.content != null) {
32         Point preferredSize = sc.content.computeSize(wHint, hHint, flushCache);
33         Point currentSize = sc.content.getSize();
34         size.x = sc.getExpandHorizontal() ? preferredSize.x : currentSize.x;
35         size.y = sc.getExpandVertical() ? preferredSize.y : currentSize.y;
36     }
37     size.x = Math.max(size.x, sc.minWidth);
38     size.y = Math.max(size.y, sc.minHeight);
39     if (wHint != SWT.DEFAULT) size.x = wHint;
40     if (hHint != SWT.DEFAULT) size.y = hHint;
41     return size;
42 }
43
44 protected boolean flushCache(Control control) {
45     return true;
46 }
47
48 protected void layout(Composite composite, boolean flushCache) {
49     if (inLayout) return;
50     ScrolledComposite sc = (ScrolledComposite)composite;
51     if (sc.content == null) return;
52     ScrollBar hBar = sc.getHorizontalBar();
53     ScrollBar vBar = sc.getVerticalBar();
54     if (hBar != null) {
55         if (hBar.getSize().y >= sc.getSize().y) {
56             return;
57         }
58     }
59     if (vBar != null) {
60         if (vBar.getSize().x >= sc.getSize().x) {
61             return;
62         }
63     }
64     inLayout = true;
65     Rectangle contentRect = sc.content.getBounds();
66     if (!sc.alwaysShowScroll) {
67         boolean hVisible = sc.needHScroll(contentRect, false);
68         boolean vVisible = sc.needVScroll(contentRect, hVisible);
69         if (!hVisible && vVisible) hVisible = sc.needHScroll(contentRect, vVisible);
70         if (hBar != null) hBar.setVisible(hVisible);
71         if (vBar != null) vBar.setVisible(vVisible);
72     }
73     Rectangle hostRect = sc.getClientArea();
74     if (sc.expandHorizontal) {
75         contentRect.width = Math.max(sc.minWidth, hostRect.width);
76     }
77     if (sc.expandVertical) {
78         contentRect.height = Math.max(sc.minHeight, hostRect.height);
79     }
80
81     if (hBar != null) {
82         hBar.setMaximum (contentRect.width);
83         hBar.setThumb (Math.min (contentRect.width, hostRect.width));
84         int hPage = contentRect.width - hostRect.width;
85         int hSelection = hBar.getSelection ();
86         if (hSelection >= hPage) {
87             if (hPage <= 0) {
88                 hSelection = 0;
89                 hBar.setSelection(0);
90             }
91             contentRect.x = -hSelection;
92         }
93     }
94
95     if (vBar != null) {
96         vBar.setMaximum (contentRect.height);
97         vBar.setThumb (Math.min (contentRect.height, hostRect.height));
98         int vPage = contentRect.height - hostRect.height;
99         int vSelection = vBar.getSelection ();
100         if (vSelection >= vPage) {
101             if (vPage <= 0) {
102                 vSelection = 0;
103                 vBar.setSelection(0);
104             }
105             contentRect.y = -vSelection;
106         }
107     }
108     
109     sc.content.setBounds (contentRect);
110     inLayout = false;
111 }
112 }
113
Popular Tags