summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT/emulated/treetable/org/eclipse/swt/widgets/SelectableItem.java
blob: b47e21db05edf539545ad8ed6709892898ecc3b4 (plain)
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
package org.eclipse.swt.widgets;

/*
 * Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

/**
 * This class implements common behavior of TreeItem and TableItem.
 */
abstract class SelectableItem extends Item {
	protected static final int CHECKBOX_PADDING = 1;	// Space behind check box, before item image or text
		
	private SelectableItemWidget parent;				// parent widget of the receiver
	private boolean isSelected = false;					// item selection state
	private boolean isChecked = false;					// item checked state. Can be one of checked and unchecked
	private boolean isGrayed = false;					// item grayed state. The gray state is combined with the 
														// checked state to create gray checked and gray unchecked.
/**
 * Create a new instance of the receiver.
 * @param parent - widget the receiver is created in 
 * @param style - widget style. see Widget class for details
 */
SelectableItem(SelectableItemWidget parent, int style) {
	super(parent, style);
	setParent(parent);	
}
public void dispose() {
	if (isDisposed()) return;
	super.dispose();
	doDispose();
}
void doDispose() {
	setParent(null);
}
/**
 * Draw the check box of the receiver at 'position' using 'gc'.
 * @param gc - GC to draw on. 
 * @param destinationPosition - position on the GC to draw at.
 * @return Answer the position where drawing stopped.
 */
Point drawCheckbox(GC gc, Point position) {
	SelectableItemWidget parent = getSelectableParent();
	Image image;
	Point imageExtent;
	Rectangle imageBounds;
	int imageOffset;
	int xInset;
	int yInset;

	if (getGrayed() == true) {
		image = parent.getGrayUncheckedImage();
	}
	else {
		image = parent.getUncheckedImage();
	}
	if (image != null) {
		imageExtent = parent.getCheckBoxExtent();
		imageOffset = (parent.getItemHeight() - imageExtent.y) / 2;
		gc.drawImage(image, position.x, position.y + imageOffset);
		if (getChecked() == true) {
			image = parent.getCheckMarkImage();
			imageBounds = image.getBounds();
			xInset = (imageExtent.x - imageBounds.width) / 2;
			yInset = (imageExtent.y - imageBounds.height) / 2;
			gc.drawImage(image, position.x + xInset, position.y + imageOffset + yInset);
		}
		position.x += imageExtent.x;
	}
	position.x += CHECKBOX_PADDING;									// leave extra space behind check box	
	return position;
}
void drawInsertMark(GC gc, Point position) {
	SelectableItemWidget parent = getSelectableParent();
	Point selectionExtent = getSelectionExtent();
	final int markerWidth = getInsertMarkWidth();
	int insertMarkYOffset = 0;

	if (selectionExtent == null) {
		return;
	}
	if (parent.isInsertAfter()) {
		insertMarkYOffset = selectionExtent.y - markerWidth;
	}
	gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
	gc.fillRectangle(position.x, position.y + insertMarkYOffset, selectionExtent.x, markerWidth);
	gc.setBackground(parent.getBackground());
}
/**
 * Answer the bounding rectangle of the item check box.
 * All points within this rectangle hit the check box.
 */
Rectangle getCheckboxBounds() {
	SelectableItemWidget parent = getSelectableParent();
	Point checkBoxExtent;
	int redrawPosition;
	Rectangle checkboxBounds = new Rectangle(0, 0, 0, 0);

	if (isCheckable() == true) {
		checkboxBounds.x = getCheckboxXPosition();
		redrawPosition = parent.getRedrawY(this);
		if (redrawPosition != -1) {
			checkboxBounds.y = redrawPosition;
		}
		checkBoxExtent = parent.getCheckBoxExtent();
		checkboxBounds.width = checkBoxExtent.x;
		checkboxBounds.height = checkBoxExtent.y;
		checkboxBounds.y += (parent.getItemHeight() - checkBoxExtent.y) / 2;
	}
	return checkboxBounds;
}
/**
 * Answer the x position of the item check box
 */
abstract int getCheckboxXPosition();
/**
 * Return whether or not the receiver is checked.
 * Always return false if the parent of the receiver does not
 * have the CHECK style.
 */
public boolean getChecked() {
	checkWidget();
	boolean checked = false;
	
	if (isCheckable() == true) {
		checked = isChecked;
	}
	return checked;
}
/**
 * Answer the display of the receiver's parent widget.
 */
public Display getDisplay() {
	SelectableItemWidget parent = getSelectableParent();

	if (parent == null) {
		error(SWT.ERROR_WIDGET_DISPOSED);
	}
	return parent.getDisplay();
}

/**
 * Gets the grayed state.
 * <p>
 * @return the item grayed state.
 *
 * @exception SWTError <ul>
 *		<li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
 *		<li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
 *	</ul>
 */
public boolean getGrayed () {
	checkWidget();
	boolean grayed = false;
	
	if (isCheckable() == true) {
		grayed = isGrayed;
	}
	return grayed;
}
/**
 * Return the width in pixels of the line drawn to indicate the 
 * drop insert position during a drag and drop operation.
 */
int getInsertMarkWidth() {
	return 2;
}
/**
 * Answer the parent widget of the receiver.
 */
SelectableItemWidget getSelectableParent() {
	return parent;
}
/**
 * Answer the background color to use for drawing the 
 * selection rectangle.
 */
Color getSelectionBackgroundColor() {
	Display display = getSelectableParent().getDisplay();	

	return display.getSystemColor(SWT.COLOR_LIST_SELECTION);
}
/**
 * Return the size of the rectangle drawn to indicate the
 * selected state of the receiver.
 */
abstract Point getSelectionExtent();
/** 
 * Answer the foreground color to use for drawing the 
 * selection rectangle.
 */
Color getSelectionForegroundColor() {
	Display display = getSelectableParent().getDisplay();	

	return display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
}
/**
 * Return the x position of the selection rectangle
 */
abstract int getSelectionX();
/**
 * Answer whether 'posiiton' is inside the item check box.
 * @return
 *	true - item check box hit
 *	false - item check box not hit
 */
boolean isCheckHit(Point position) {
	boolean isCheckHit = false;
	
	if (isCheckable() == true) {
		isCheckHit = getCheckboxBounds().contains(position);
	}
	return isCheckHit;
}
/**
 * Return whether or not the receiver has a check box and can 
 * be checked.
 */
boolean isCheckable() {
	return (getSelectableParent().getStyle() & SWT.CHECK) != 0;
}
/**
 * Answer whether the receiver is selected.
 * @return 
 *	true - the receiver is selected
 * 	false - the receiver is not selected
 */
boolean isSelected() {
	return isSelected;
}
/**
 * Redraw the insert mark
 * @param yPosition - y position in the receiver's client area 
 *	where the item should be drawn.
 */
void redrawInsertMark(int yPosition) {
	SelectableItemWidget parent = getSelectableParent();
	Point selectionExtent = getSelectionExtent();
	int redrawHeight = getInsertMarkWidth();

	if (selectionExtent != null) {
		parent.redraw(getSelectionX(), yPosition, selectionExtent.x, redrawHeight, false);
		parent.redraw(getSelectionX(), yPosition + selectionExtent.y - redrawHeight, selectionExtent.x, redrawHeight, false);
	}	
}
/**
 * Redraw the selection
 * @param yPosition - y position in the receiver's client area 
 *	where the item should be drawn.
 */
void redrawSelection(int yPosition) {
	SelectableItemWidget parent = getSelectableParent();
	Point selectionExtent = getSelectionExtent();

	if (selectionExtent != null) {
		parent.redraw(getSelectionX(), yPosition, selectionExtent.x, selectionExtent.y, false);
	}
}
/**
 * Set the checked state to 'checked' if the parent of the 
 * receiver has the CHECK style.
 */
public void setChecked(boolean checked) {
	checkWidget();
	SelectableItemWidget parent = getSelectableParent();
	Rectangle redrawRectangle = getCheckboxBounds();

	if (isCheckable() == true && isChecked != checked) {
		isChecked = checked;
		parent.redraw(
			redrawRectangle.x, redrawRectangle.y, 
			redrawRectangle.width, redrawRectangle.height, false);
	}	
}

/**
 * Sets the grayed state.
 * <p>
 * @param grayed the new grayed state.
 *
 * @exception SWTError <ul>
 *		<li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
 *		<li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
 *	</ul>
 */
public void setGrayed (boolean grayed) {
	checkWidget();
	SelectableItemWidget parent = getSelectableParent();
	Rectangle redrawRectangle = getCheckboxBounds();

	if (isCheckable() == true && isGrayed != grayed) {
		isGrayed = grayed;
		parent.redraw(
			redrawRectangle.x, redrawRectangle.y, 
			redrawRectangle.width, redrawRectangle.height, false);
	}	
}

/**
 * Set the receiver's parent widget to 'parent'.
 */
void setParent(SelectableItemWidget parent) {
	this.parent = parent;
}
/**
 * Set whether the receiver is selected.
 * @param selected - true=the receiver is selected
 * 	false=the receiver is not selected
 */
void setSelected(boolean selected) {
	isSelected = selected;
}
}