1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
/**
* This class provides the implementation of a Composite which is automatically
* scrolled when its size is smaller than a specified size.
* <P>
* <b>Styles :</b> (All styles of Composite).
* <p>
* <b>Events :</b> (none).
* <p>
* <b><i>Date :</i></b> 1 mai 2008.
* @version 1.0
* @author Achille Roussel
* @see Composite
* @see ScrollBar
*/
public class ScrollableComposite extends Composite {
/** The vertical scroll bar. */
ScrollBar vScroll;
/** The horizontal scroll bar. */
ScrollBar hScroll;
/** The horizontal offset of the content. */
int offsetX;
/** The vertical offset of the content. */
int offsetY;
/** The minimal width of the control. */
int minWidth;
/** The minimal height of the control. */
int minHeight;
/** The flag to auto-hide or not the scroll bars. */
boolean alwaysShowScroll;
/**
* Creates a new instance of ScrollableComposite.
* @param parent the parent Composite of the control.
* @param style the style which describes the look of the control.
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the parent has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the parent</li>
* </ul>
*/
public ScrollableComposite(Composite parent, int style) {
super(parent, style);
offsetX = 0;
offsetY = 0;
minWidth = SWT.DEFAULT;
minHeight = SWT.DEFAULT;
alwaysShowScroll = true;
boolean showScrollV = (style & SWT.V_SCROLL) != 0;
boolean showScrollH = (style & SWT.H_SCROLL) != 0;
if (showScrollV || showScrollH) {
Listener listener = new Listener() {
@Override
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Resize : onResize(); break;
case SWT.Selection : onSelection(event); break;
}
}
};
if (showScrollV) {
vScroll = getVerticalBar();
vScroll.setMinimum(0);
vScroll.addListener(SWT.Selection, listener);
}
if (showScrollH) {
hScroll = getHorizontalBar();
hScroll.setMinimum(0);
hScroll.addListener(SWT.Selection, listener);
}
addListener(SWT.Resize, listener);
}
}
/** Called when the control needs to be resized. */
void onResize() {
setLayoutDeferred(true);
Point p = getMinSize();
Point q = getSize();
Rectangle r = super.getClientArea();
boolean showScrollV = q.y < p.y;
boolean showScrollH = q.x < p.x;
if (q.y >= p.y) {
offsetY = 0;
vScroll.setSelection(0);
}
if (q.x >= p.x) {
offsetX = 0;
hScroll.setSelection(0);
}
if (showScrollV && (r.y + r.height) > (p.y - offsetY)) {
offsetY = p.y - (r.y + r.height);
}
if (showScrollH && (r.x + r.width) > (p.x - offsetX)) {
offsetX = p.x - (r.x + r.width);
}
if (vScroll != null) {
if (!alwaysShowScroll) vScroll.setVisible(showScrollV);
else if (!vScroll.getVisible()) vScroll.setVisible(true);
vScroll.setMaximum(p.y);
vScroll.setThumb(r.height); //adjusts the size of the vertical bar
}
if (hScroll != null) {
if (!alwaysShowScroll) hScroll.setVisible(showScrollH);
else if (!hScroll.getVisible()) hScroll.setVisible(true);
hScroll.setMaximum(p.x);
hScroll.setThumb(r.width); //adjusts the size of the horizontal bar
}
setLayoutDeferred(false);
}
/** Called when one of the scroll bars is selected. */
void onSelection(Event e) {
if (vScroll != null && e.widget == vScroll) {
offsetY = vScroll.getSelection();
layout(false);
}
else if (hScroll != null && e.widget == hScroll) {
offsetX = hScroll.getSelection();
layout(false);
}
}
/**
* Sets the minimum size of the receiver to the given argument which can
* be a positive value of <code>SWT.DEFAULT</code> to use the values given
* by the <code>computeSize()</code> method. Every negatives values will be
* set to zero. If there is no horizontal scroll bar the width value will
* be ignored to keep the default behavior of a Composite and vice versa for
* the vertical scroll bar.
* @param width the minimum width of the receiver.
* @param height the minimum height of the receiver.
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the receiver</li>
* </ul>
*/
public void setMinSize(int width, int height) {
checkWidget();
minWidth = width == SWT.DEFAULT ? width : Math.max(width, 0);
minHeight = height == SWT.DEFAULT ? height : Math.max(height, 0);
}
/**
* Sets the minimum size of the receiver to the given argument which can
* be a positive value of <code>SWT.DEFAULT</code> to use the values given
* by the <code>computeSize()</code> method. Every negatives values will be
* set to zero. If there is no horizontal scroll bar the width value will
* be ignored to keep the default behavior of a Composite and vice versa for
* the vertical scroll bar.
* @param width the minimum width of the receiver.
* @param height the minimum height of the receiver.
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the receiver</li>
* </ul>
*/
public void setMinSize(Point size) {
checkWidget();
if (size == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
setMinSize(size.x, size.y);
}
/**
* Returns the minimum size of the receiver before its content will be scrolled.
* @return the receiver's minimum size.
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the receiver</li>
* </ul>
*/
public Point getMinSize() {
checkWidget();
return computeSize(minWidth, minHeight, false);
}
/**
* Setting this flag at false will cause the receiver to hide its scroll bars
* when they are not needed anymore. If no scroll bar has been created the
* method does nothing.
* @param show the receiver's scroll bars visible state.
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the receiver</li>
* </ul>
*/
public void setAlwayShowScrollBars(boolean show) {
checkWidget();
alwaysShowScroll = show;
if (show) {
if (vScroll != null) vScroll.setVisible(true);
if (hScroll != null) hScroll.setVisible(true);
}
else {
Rectangle clientArea = super.getClientArea();
Point minSize = getMinSize();
if (clientArea.isEmpty() || alwaysShowScroll) return;
if (clientArea.height >= minSize.y && vScroll.getVisible()) {
vScroll.setVisible(false);
}
else if (!vScroll.getVisible()) {
vScroll.setVisible(true);
}
if (clientArea.width >= minSize.x && hScroll.getVisible()) {
hScroll.setVisible(false);
}
else if (!hScroll.getVisible()) {
hScroll.setVisible(true);
}
}
}
/**
* Returns true if the receiver's scroll bars are always visible, false otherwise.
* @return the receiver's scroll bars visible state.
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
* that created the receiver</li>
* </ul>
*/
public boolean getAlwaysShowScrollBars() {
checkWidget();
return alwaysShowScroll;
}
@Override
public Rectangle getClientArea() {
Rectangle r = super.getClientArea();
Point p = null;
if ((minWidth == SWT.DEFAULT || minHeight == SWT.DEFAULT) &&
(vScroll != null || hScroll != null)) {
p = computeSize(minWidth, minHeight, false);
}
else if (minWidth != SWT.DEFAULT && minHeight != SWT.DEFAULT) {
p = new Point(minWidth, minHeight);
}
if (p != null) {
if (vScroll != null) {
r.y -= offsetY;
if (r.height < p.y) {
r.height = p.y;
}
}
if (hScroll != null) {
r.x -= offsetX;
if (r.width < p.x) {
r.width = p.x;
}
}
}
return r;
}
/**
* Main method to test the control.
* @param args nothing
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
//The scrollable Composite
ScrollableComposite container = new ScrollableComposite(shell,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
GridLayout layout = new GridLayout(2, false);
container.setLayout(layout);
container.setAlwayShowScrollBar(false);
//The content of the scrollable Composite
for (int i=0; i<6; i++) {
Button b = new Button(container, SWT.PUSH);
b.setText("This is the test of\na ScrollableComposite");
b.setLayoutData(new GridData(GridData.FILL_BOTH));
b.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
}
shell.setLayout(new FillLayout());
shell.pack();
Point p = shell.getSize();
Rectangle r = display.getBounds();
shell.setLocation(r.width / 2 - p.x / 2, r.height / 2 - p.y / 2);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
} |
Partager