Layout Test

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
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class LayoutTest {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		
		ScrolledComposite container = new ScrolledComposite(shell, SWT.V_SCROLL);
		container.setExpandHorizontal(true);
		container.setAlwaysShowScrollBars(true);
		
		final Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("Test case");
		label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		label.setLayoutData(gridData);
		
		final Widget widget = new Widget(composite, SWT.NONE);
		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		widget.setLayoutData(gridData);
		widget.addPaintListener(new PaintListener() {
			
			@Override
			public void paintControl(PaintEvent event) {
				System.out.println(widget.getSize());
			}
		});
		widget.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent event) {
				composite.pack();
			}
			
			@Override
			public void controlMoved(ControlEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		container.setContent(composite);
		
		composite.pack();
		
		shell.pack();
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	private static class Widget extends Canvas {
		
		public Widget(Composite parent, int style) {
			super(parent, style);
			setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
		}
		
		@Override
		public Point computeSize(int wHint, int hHint, boolean changed) {
			int width = wHint;
			if (wHint == SWT.DEFAULT)
				width = getSize().x;
			return new Point(width, width);
		}
	}

}