summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/TableColumn.java
blob: 55c9ba604c8e7be9362e1fffb60c88424180575c (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package org.eclipse.swt.widgets;

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

/*
 * Licensed Materials - Property of IBM,
 * (c) Copyright IBM Corp. 1998, 2000  All Rights Reserved
 */
 
/**
 * A TableColumn stores such data as column label, label alignmentand size.
 * It routes paint requests to the corresponding table item.
 */
public /*final*/ class TableColumn extends Item {
	static final int FIRST = 0;							// index of the first column
	static final int FILL = -1;							// index that identifies the column used to 
														// fill space not used by other columns.
	private static final int DEFAULT_WIDTH = 10;

	private Table parent;
	private int index;									// 0-based column index
	private Rectangle bounds = new Rectangle(0, 0, 0, 0);
	private boolean isDefaultWidth = true;
	private boolean resize = true;
/**
 * Create a new TableColumn without adding it to the parent.
 * Currently used to create fill columns and default columns.
 * @see createFillColumn
 * @see createDefaultColumn
 * @param parent - Table widget the new instance will be a child of.
 */
TableColumn(Table parent) {
	super(parent, SWT.NULL);
	this.parent = parent;		
}
/**
 * Create a new instance of TableColumn and append it to the existing 
 * columns in 'parent'.
 * @param parent - Table widget the new instance will be a child of.
 * @param syle - style of the new TableColumn
 */
public TableColumn(Table parent, int style) {
	this(parent, style, checkNull(parent).getColumnCount());
}
/**
 * Create a new instance of TableColumn at position 'index' in the Table
 * identified by 'parent'.
 * @param parent - Table widget the new instance will be a child of.
 * @param index - position in the 'parent' at which the new instance will
 *	be located relative to the other columns.
 * @param syle - style of the new TableColumn
 */
public TableColumn(Table parent, int style, int index) {
	super(parent, checkStyle (style), index);
	
	this.parent = parent;
	if (index < 0 || index > parent.getColumnCount()) {
		error(SWT.ERROR_INVALID_RANGE);
	}
	setIndex(index);	
	parent.addColumn(this);
	setWidth(DEFAULT_WIDTH);
	setDefaultWidth(true);
	addListener(SWT.Dispose, new Listener() {
		public void handleEvent(Event event) {disposeColumn();}
	});
}
/**	 
* Adds the listener to receive events.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void addControlListener(ControlListener listener) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
	TypedListener typedListener = new TypedListener (listener);
	addListener (SWT.Resize,typedListener);
	addListener (SWT.Move,typedListener);
}
/**	 
* Adds the listener to receive events.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void addSelectionListener (SelectionListener listener) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
	TypedListener typedListener = new TypedListener (listener);
	addListener (SWT.Selection,typedListener);
	addListener (SWT.DefaultSelection,typedListener);
}
/**
 * Throw an SWT.ERROR_NULL_ARGUMENT exception if 'table' is null.
 * Otherwise return 'table'
 */
static Table checkNull(Table table) {
	if (table == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
	return table;
}
static int checkStyle (int style) {
	return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0);
}
protected void checkSubclass () {
	if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
}
/**
 * Create a new instance of TableColumn that acts as a default column
 * if the user does not create a TableColumn.
 * @param parent - Table widget the new instance will be a child of.
 */
static TableColumn createDefaultColumn(Table parent) {
	TableColumn defaultColumn = new TableColumn(parent);
	
	defaultColumn.setIndex(FIRST);
	defaultColumn.setWidth(DEFAULT_WIDTH);
	defaultColumn.setDefaultWidth(true);
	return defaultColumn;
}
/**
 * Create a new instance of TableColumn that acts as the rightmost 
 * fill column in a Table. The new object is not added to the parent
 * like a regular column is.
 * @param parent - Table widget the new instance will be a child of.
 */
static TableColumn createFillColumn(Table parent) {
	TableColumn fillColumn = new TableColumn(parent);
	
	fillColumn.setIndex(FILL);
	return fillColumn;
}
/**
 * Remove the receiver from its parent
 */
void disposeColumn() {
	getParent().removeColumn(this);
}
/**
* Gets the alignment.
* <p>
* The alignment of a widget controls the position of the
* text or image in the widget.  The alignment may be one
* of LEFT, RIGHT or CENTER.
*
* @return the alignment 
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public int getAlignment () {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	if ((style & SWT.LEFT) != 0) return SWT.LEFT;
	if ((style & SWT.CENTER) != 0) return SWT.CENTER;
	if ((style & SWT.RIGHT) != 0) return SWT.RIGHT;
	return SWT.LEFT;
}

/**
 * Answer the bounding rectangle of the receiver.
 */
Rectangle getBounds() {
	return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);	// copy the object to prevent changes
}
/**
 * Gets the Display.
 */
public Display getDisplay() {
	if (parent == null) {		// access parent field directly to prevent endless recursion
		error(SWT.ERROR_WIDGET_DISPOSED);
	}
	return parent.getDisplay();
}
/**
 * Answer the index of the receiver. Specifies the position of the
 * receiver relative to other columns in the parent.
 */
int getIndex() {
	return index;
}
/**
 * Answer the parent widget of the receiver.
 */
public Table getParent() {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	
	return parent;
}
/**
* Gets the resize attribute.
* <p>
* @return the resize attribute
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public boolean getResizable() {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	
	return resize;
}
/**
* Gets the width.
* <p>
* @return the width
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public int getWidth () {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);

	return getBounds().width;
}
/**
 * Set the colun bounds.
 */
void internalSetBounds(Rectangle newBounds) {
	bounds = newBounds;
}
/**
 * Answer whether the column has a default width or if a width has been 
 * set by the user.
 * @return 
 *  true=column width is a default width set internally
 *	false=column width has been set by the user.
 */
boolean isDefaultWidth() {
	return isDefaultWidth;
}
/**
* Packs the widget.
* <p>
* Packing a widget causes it to be resized to the
* preferred size for the widget.
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void pack() {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	Table parent = getParent();
	int index = parent.indexOf(this);

	if (getIndex() != TableColumn.FILL && index != -1) {
		setWidth(parent.getPreferredColumnWidth(index));
	}
}
/**
 * Draw the 'item' at 'yPosition' in the receiver column.
 * @param item - TableItem that should be drawn.
 * @param gc - GC to draw on
 * @param yPosition - y position to draw at in the column.
 */
void paint(TableItem item, GC gc, int yPosition) {
	Rectangle bounds = getBounds();
	Point paintPosition = new Point(bounds.x, bounds.y + yPosition);

	item.paint(gc, paintPosition, this);
}
/**	 
* Removes the listener.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void removeControlListener (ControlListener listener) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
	if (eventTable == null) return;
	eventTable.unhook (SWT.Move, listener);
	eventTable.unhook (SWT.Resize, listener);
}
/**	 
* Removes the listener.
* <p>
*
* @param listener the listener
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
* @exception SWTError(ERROR_NULL_ARGUMENT)
*	when listener is null
*/
public void removeSelectionListener(SelectionListener listener) {
	if (!isValidThread ()) error(SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error(SWT.ERROR_WIDGET_DISPOSED);
	
	if (listener == null) {
		error(SWT.ERROR_NULL_ARGUMENT);
	}
	removeListener(SWT.Selection, listener);
	removeListener(SWT.DefaultSelection, listener);
}
/**
* Sets the alignment.
* <p>
* The alignment of a widget controls the position of the
* text or image in the widget.  The alignement may be one
* of LEFT, RIGHT or CENTER.
*
* @param alignment the new alignment 
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void setAlignment(int alignment) {
	if (!isValidThread ()) error(SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error(SWT.ERROR_WIDGET_DISPOSED);
	int index = getIndex();
	
	if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) != 0 && index != 0) { // ignore calls for the first column to match Windows behavior
		style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
		style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
		getParent().getHeader().redraw(index);	
	}
}
/**
 * Set the bounding rectangle of the receiver to 'newBounds'.
 * Notify the table widget if the column width changes.
 * @param newBounds - the new bounding rectangle of the receiver,
 *	consisting of x, y, width, height
 */
void setBounds(Rectangle newBounds) {
	if (newBounds.width != bounds.width) {
		if (isDefaultWidth() == true) {
			setDefaultWidth(false);
		}
		getParent().columnChange(this, newBounds);
	}
	else {
		// columnChange causes update (via scroll) which may flush redraw 
		// based on old bounds. Setting bounds after notifying table fixes 1GABZR5
		// Table sets column bounds at appropriate time when called above with 
		// width change. Only set bounds when table was not called. Fixes 1GCGDPB
		bounds = newBounds;
	}
}
/**
 * Set whether the column has a default width or if a width has been 
 * set by the user.
 * @param isDefaultWidth
 *	true=column width is a default width set internally
 *	false=column width has been set by the user
 */
void setDefaultWidth(boolean isDefaultWidth) {
	this.isDefaultWidth = isDefaultWidth;
}
/**
 * Set the index of the receiver to 'newIndex'. The index specifies the
 * position of the receiver relative to other columns in the parent.
 */
void setIndex(int newIndex) {
	this.index = newIndex;
}
/**
* Sets the resize attribute.
* <p>
* @param resizee the resize attribute
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void setResizable(boolean resize) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);

	this.resize = resize;
}
/**
 * Set the text of the receiver to 'text'.
 */
public void setText(String newText) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	int index = getIndex();
	
	if (newText == null) {
		error(SWT.ERROR_NULL_ARGUMENT);
	}
	if (index != FILL && (text == null || text.equals(newText) == false)) {
		super.setText(newText);
		getParent().getHeader().redraw(index);
	}	
}
/**
* Sets the width.
* <p>
* @param width the width
*
* @exception SWTError(ERROR_THREAD_INVALID_ACCESS)
*	when called from the wrong thread
* @exception SWTError(ERROR_WIDGET_DISPOSED)
*	when the widget has been disposed
*/
public void setWidth(int width) {
	if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
	if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
	Table parent = getParent();
	Rectangle bounds = getBounds();
	int oldWidth = bounds.width;
	int redrawX;

	if (width != oldWidth) {
		redrawX = bounds.x;
		bounds.width = width;
		setBounds(bounds);
		// redraw at old column position if column was resized wider.
		// fixes focus rectangle. 
		redrawX += Math.min(width, oldWidth);
		parent.redraw(																
			redrawX - 2, 0, 
			2, parent.getClientArea().height, false);	// redraw 2 pixels wide to redraw item focus rectangle and grid line
	}
}
}