KickJava   Java API By Example, From Geeks To Geeks.

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


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.ui.internal.browser;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.custom.ScrolledComposite;
15 import org.eclipse.swt.graphics.Color;
16 import org.eclipse.swt.graphics.Font;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.ScrollBar;
24
25 public abstract class FallbackScrolledComposite extends ScrolledComposite {
26     private static final int H_SCROLL_INCREMENT = 5;
27
28     private static final int V_SCROLL_INCREMENT = 64;
29
30     /**
31      * Creates the new instance.
32      *
33      * @param parent
34      * the parent composite
35      * @param style
36      * the style to use
37      */

38     public FallbackScrolledComposite(Composite parent, int style) {
39         super(parent, style);
40         addListener(SWT.Resize, new Listener() {
41             public void handleEvent(Event e) {
42                 reflow(true);
43             }
44         });
45         setExpandVertical(true);
46         setExpandHorizontal(true);
47         initializeScrollBars();
48     }
49
50     /**
51      * Sets the foreground of the control and its content.
52      *
53      * @param fg
54      * the new foreground color
55      */

56     public void setForeground(Color fg) {
57         super.setForeground(fg);
58         if (getContent() != null)
59             getContent().setForeground(fg);
60     }
61
62     /**
63      * Sets the background of the control and its content.
64      *
65      * @param bg
66      * the new background color
67      */

68     public void setBackground(Color bg) {
69         super.setBackground(bg);
70         if (getContent() != null)
71             getContent().setBackground(bg);
72     }
73
74     /**
75      * Sets the font of the form. This font will be used to render the title
76      * text. It will not affect the body.
77      */

78     public void setFont(Font font) {
79         super.setFont(font);
80         if (getContent() != null)
81             getContent().setFont(font);
82     }
83
84     /**
85      * Overrides 'super' to pass the proper colors and font
86      */

87     public void setContent(Control content) {
88         super.setContent(content);
89         if (content != null) {
90             content.setForeground(getForeground());
91             content.setBackground(getBackground());
92             content.setFont(getFont());
93         }
94     }
95
96     /**
97      * If content is set, transfers focus to the content.
98      */

99     public boolean setFocus() {
100         if (getContent() != null)
101             return getContent().setFocus();
102         return super.setFocus();
103     }
104
105     /**
106      * Recomputes the body layout and the scroll bars. The method should be used
107      * when changes somewhere in the form body invalidate the current layout
108      * and/or scroll bars.
109      *
110      * @param flushCache
111      * if <code>true</code>, drop the cached data
112      */

113     public void reflow(boolean flushCache) {
114         Composite c = (Composite) getContent();
115         Rectangle clientArea = getClientArea();
116         if (c == null)
117             return;
118
119         Point newSize = c
120                 .computeSize(clientArea.width, SWT.DEFAULT, flushCache);
121
122         setMinSize(newSize);
123         updatePageIncrement();
124         layout(flushCache);
125     }
126
127     private void updatePageIncrement() {
128         ScrollBar vbar = getVerticalBar();
129         if (vbar != null) {
130             Rectangle clientArea = getClientArea();
131             int increment = clientArea.height - 5;
132             vbar.setPageIncrement(increment);
133         }
134     }
135
136     private void initializeScrollBars() {
137         ScrollBar hbar = getHorizontalBar();
138         if (hbar != null) {
139             hbar.setIncrement(H_SCROLL_INCREMENT);
140         }
141         ScrollBar vbar = getVerticalBar();
142         if (vbar != null) {
143             vbar.setIncrement(V_SCROLL_INCREMENT);
144         }
145         updatePageIncrement();
146     }
147 }
148
Popular Tags