Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 234417
Collapse All | Expand All

(-)src/org/eclipse/draw2d/examples/swt/InheritBackgroundExample.java (+80 lines)
Added Link Here
1
package org.eclipse.draw2d.examples.swt;
2
3
import org.eclipse.draw2d.ColorConstants;
4
import org.eclipse.draw2d.FigureCanvas;
5
import org.eclipse.draw2d.Label;
6
import org.eclipse.swt.SWT;
7
import org.eclipse.swt.graphics.GC;
8
import org.eclipse.swt.graphics.Image;
9
import org.eclipse.swt.layout.FillLayout;
10
import org.eclipse.swt.layout.GridData;
11
import org.eclipse.swt.layout.GridLayout;
12
import org.eclipse.swt.widgets.Composite;
13
import org.eclipse.swt.widgets.Display;
14
import org.eclipse.swt.widgets.Group;
15
import org.eclipse.swt.widgets.Shell;
16
17
public class InheritBackgroundExample {
18
19
	static Shell shell;
20
	static Image bg;
21
	
22
	static {
23
		bg = new Image(null, 10, 800);
24
		GC gc = new GC(bg);
25
		gc.setForeground(ColorConstants.white);
26
		gc.setBackground(ColorConstants.lightGreen);
27
		gc.fillGradientRectangle(0, 0, 300, 400, true);
28
		gc.setForeground(ColorConstants.lightGreen);
29
		gc.setBackground(ColorConstants.white);
30
		gc.fillGradientRectangle(0, 400, 300, 400, true);
31
		gc.dispose();
32
	}
33
34
	public static void main(String[] args) {
35
		Display d = Display.getDefault();
36
		shell = new Shell(d, SWT.SHELL_TRIM);
37
		shell.setText("Figure Canvas inheriting background from parent Composite");
38
		shell.setLayout(new GridLayout(2, false));
39
		shell.setBackgroundImage(bg);
40
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
41
42
		FigureCanvas canvas;
43
44
		canvas = createCanvas(shell,
45
				SWT.DOUBLE_BUFFERED | SWT.BORDER | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL,
46
				"Paints Background");
47
		canvas.setBackground(ColorConstants.orange);
48
		
49
		canvas = createCanvas(shell,
50
				SWT.DOUBLE_BUFFERED | SWT.BORDER | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL,
51
				"Inherits Background");
52
		canvas.getLightweightSystem().getRootFigure().setOpaque(false);
53
54
		shell.pack();
55
		shell.open();
56
		while (!shell.isDisposed())
57
			while (!d.readAndDispatch())
58
				d.sleep();
59
	}
60
61
	private static FigureCanvas createCanvas(Composite parent, int style, String text) {
62
		Group group = new Group(parent, SWT.NONE);
63
		group.setLayoutData(new GridData(GridData.FILL_BOTH));
64
		group.setText(text);
65
		parent = group;
66
		group.setLayout(new FillLayout());
67
		
68
		FigureCanvas canvas = new FigureCanvas(style, parent);
69
		canvas.setContents(new Label(
70
				"This is a figure canvas\n" +
71
				"with a label.  The background\n" +
72
				"is either painted by the root\n" +
73
				"figure, or by the operating system.\n" +
74
				"Note that vertical scrolling may not be\n" +
75
				"as fast when the background from the\n" +
76
				"parent is inherited."));
77
		return canvas;
78
	}
79
80
}
(-)src/org/eclipse/draw2d/FigureCanvas.java (-14 / +73 lines)
Lines 28-33 Link Here
28
/**
28
/**
29
 * A Canvas that contains {@link Figure Figures}.
29
 * A Canvas that contains {@link Figure Figures}.
30
 * 
30
 * 
31
 * <dl>
32
 * <dt><b>Required Styles (when using certain constructors):</b></dt>
33
 * <dd>V_SCROLL, H_SCROLL, NO_REDRAW_RESIZE</dd>
34
 * <dt><b>Optional Styles:</b></dt>
35
 * <dd>DOUBLE_BUFFERED, RIGHT_TO_LEFT, LEFT_TO_RIGHT, NO_BACKGROUND, BORDER</dd>
36
 * </dl>
31
 * <p>
37
 * <p>
32
 * Note: Only one of the styles RIGHT_TO_LEFT, LEFT_TO_RIGHT may be specified.
38
 * Note: Only one of the styles RIGHT_TO_LEFT, LEFT_TO_RIGHT may be specified.
33
 * </p>
39
 * </p>
Lines 36-41 Link Here
36
	extends Canvas
42
	extends Canvas
37
{
43
{
38
44
45
private static final int ACCEPTED_STYLES =
46
		SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT
47
		| SWT.V_SCROLL | SWT.H_SCROLL
48
		| SWT.NO_BACKGROUND
49
		| SWT.NO_REDRAW_RESIZE
50
		| SWT.DOUBLE_BUFFERED
51
		| SWT.BORDER;
52
53
/**
54
 * The default styles are mixed in when certain constructors are used. This
55
 * constant is a bitwise OR of the following SWT style constants:
56
 * <UL>
57
 *   <LI>{@link SWT#NO_REDRAW_RESIZE}</LI>
58
 *   <LI>{@link SWT#NO_BACKGROUND}</LI>
59
 *   <LI>{@link SWT#V_SCROLL}</LI>
60
 *   <LI>{@link SWT#H_SCROLL}</LI>
61
 * </UL>
62
 */
63
static final int DEFAULT_STYLES =
64
		SWT.NO_REDRAW_RESIZE
65
		| SWT.NO_BACKGROUND
66
		| SWT.V_SCROLL
67
		| SWT.H_SCROLL;
68
69
private static final int REQUIRED_STYLES = 
70
		SWT.NO_REDRAW_RESIZE
71
		| SWT.V_SCROLL
72
		| SWT.H_SCROLL;
73
39
/** Never show scrollbar */
74
/** Never show scrollbar */
40
public static int NEVER = 0;
75
public static int NEVER = 0;
41
/** Automatically show scrollbar when needed */
76
/** Automatically show scrollbar when needed */
Lines 81-87 Link Here
81
private final LightweightSystem lws;
116
private final LightweightSystem lws;
82
117
83
/**
118
/**
84
 * Creates a new FigureCanvas with the given parent.
119
 * Creates a new FigureCanvas with the given parent and the {@link #DEFAULT_STYLES}.
85
 * 
120
 * 
86
 * @param parent the parent
121
 * @param parent the parent
87
 */
122
 */
Lines 90-98 Link Here
90
}
125
}
91
126
92
/**
127
/**
93
 * Constructor
128
 * Constructor which applies the default styles plus any optional styles indicated.
94
 * @param parent the parent composite
129
 * @param parent the parent composite
95
 * @param style look at class javadoc for valid styles
130
 * @param style see the class javadoc for optional styles
96
 * @since 3.1
131
 * @since 3.1
97
 */
132
 */
98
public FigureCanvas(Composite parent, int style) {
133
public FigureCanvas(Composite parent, int style) {
Lines 100-106 Link Here
100
}
135
}
101
136
102
/**
137
/**
103
 * Constructs a new FigureCanvas with the given parent and LightweightSystem.
138
 * Constructor which uses the given styles verbatim. Certain styles must be used
139
 * with this class. Refer to the class javadoc for more details.
140
 * @param style see the class javadoc for <b>required</b> and optional styles
141
 * @param parent the parent composite
142
 * @since 3.4
143
 */
144
public FigureCanvas(int style, Composite parent) {
145
	this(style, parent, new LightweightSystem());
146
}
147
148
/**
149
 * Constructs a new FigureCanvas with the given parent and LightweightSystem, using
150
 * the {@link #DEFAULT_STYLES}.
104
 * @param parent the parent
151
 * @param parent the parent
105
 * @param lws the LightweightSystem
152
 * @param lws the LightweightSystem
106
 */
153
 */
Lines 109-123 Link Here
109
}
156
}
110
157
111
/**
158
/**
112
 * Constructor
159
 * Constructor taking a lightweight system and SWT style, which is used verbatim.
160
 * Certain styles must be used with this class. Refer to the class javadoc for
161
 * more details.
162
 * @param style see the class javadoc for <b>required</b> and optional styles
113
 * @param parent the parent composite
163
 * @param parent the parent composite
114
 * @param style look at class javadoc for valid styles
164
 * @param lws the LightweightSystem
115
 * @param lws the lightweight system
165
 * @since 3.4
116
 * @since 3.1
117
 */
166
 */
118
public FigureCanvas(Composite parent, int style, LightweightSystem lws) {
167
public FigureCanvas(int style, Composite parent, LightweightSystem lws) {
119
	super(parent, checkStyle(style | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND
168
	super(parent, checkStyle(style));
120
			| SWT.V_SCROLL | SWT.H_SCROLL));
121
	getHorizontalBar().setVisible(false);
169
	getHorizontalBar().setVisible(false);
122
	getVerticalBar().setVisible(false);
170
	getVerticalBar().setVisible(false);
123
	this.lws = lws;
171
	this.lws = lws;
Lines 125-134 Link Here
125
	hook();
173
	hook();
126
}
174
}
127
175
176
/**
177
 * Constructor
178
 * @param parent the parent composite
179
 * @param style look at class javadoc for valid styles
180
 * @param lws the lightweight system
181
 * @since 3.1
182
 */
183
public FigureCanvas(Composite parent, int style, LightweightSystem lws) {
184
	this(style | DEFAULT_STYLES, parent, lws);
185
}
186
128
private static int checkStyle(int style) {
187
private static int checkStyle(int style) {
129
	int validStyles = SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT | SWT.V_SCROLL | SWT.H_SCROLL
188
	if ((style & REQUIRED_STYLES) != REQUIRED_STYLES)
130
			| SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED | SWT.BORDER;
189
		throw new IllegalArgumentException("Required style missing on FigureCanvas"); //$NON-NLS-1$
131
	if ((style & ~validStyles) != 0)
190
	if ((style & ~ACCEPTED_STYLES) != 0)
132
		throw new IllegalArgumentException("Invalid style being set on FigureCanvas"); //$NON-NLS-1$
191
		throw new IllegalArgumentException("Invalid style being set on FigureCanvas"); //$NON-NLS-1$
133
	return style;
192
	return style;
134
}
193
}

Return to bug 234417