KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > TableItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13  
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class represent a selectable user interface object
20  * that represents an item in a table.
21  * <dl>
22  * <dt><b>Styles:</b></dt>
23  * <dd>(none)</dd>
24  * <dt><b>Events:</b></dt>
25  * <dd>(none)</dd>
26  * </dl>
27  * <p>
28  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
29  * </p>
30  */

31
32 public class TableItem extends Item {
33     Table parent;
34     String JavaDoc [] strings;
35     Image [] images;
36     boolean checked, grayed, cached;
37     int imageIndent, background = -1, foreground = -1, font = -1;
38     int [] cellBackground, cellForeground, cellFont;
39
40 /**
41  * Constructs a new instance of this class given its parent
42  * (which must be a <code>Table</code>) and a style value
43  * describing its behavior and appearance. The item is added
44  * to the end of the items maintained by its parent.
45  * <p>
46  * The style value is either one of the style constants defined in
47  * class <code>SWT</code> which is applicable to instances of this
48  * class, or must be built by <em>bitwise OR</em>'ing together
49  * (that is, using the <code>int</code> "|" operator) two or more
50  * of those <code>SWT</code> style constants. The class description
51  * lists the style constants that are applicable to the class.
52  * Style bits are also inherited from superclasses.
53  * </p>
54  *
55  * @param parent a composite control which will be the parent of the new instance (cannot be null)
56  * @param style the style of control to construct
57  *
58  * @exception IllegalArgumentException <ul>
59  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
60  * </ul>
61  * @exception SWTException <ul>
62  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
63  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
64  * </ul>
65  *
66  * @see SWT
67  * @see Widget#checkSubclass
68  * @see Widget#getStyle
69  */

70 public TableItem (Table parent, int style) {
71     this (parent, style, checkNull (parent).getItemCount (), true);
72 }
73
74 /**
75  * Constructs a new instance of this class given its parent
76  * (which must be a <code>Table</code>), a style value
77  * describing its behavior and appearance, and the index
78  * at which to place it in the items maintained by its parent.
79  * <p>
80  * The style value is either one of the style constants defined in
81  * class <code>SWT</code> which is applicable to instances of this
82  * class, or must be built by <em>bitwise OR</em>'ing together
83  * (that is, using the <code>int</code> "|" operator) two or more
84  * of those <code>SWT</code> style constants. The class description
85  * lists the style constants that are applicable to the class.
86  * Style bits are also inherited from superclasses.
87  * </p>
88  *
89  * @param parent a composite control which will be the parent of the new instance (cannot be null)
90  * @param style the style of control to construct
91  * @param index the zero-relative index to store the receiver in its parent
92  *
93  * @exception IllegalArgumentException <ul>
94  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
95  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
96  * </ul>
97  * @exception SWTException <ul>
98  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
99  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
100  * </ul>
101  *
102  * @see SWT
103  * @see Widget#checkSubclass
104  * @see Widget#getStyle
105  */

106 public TableItem (Table parent, int style, int index) {
107     this (parent, style, index, true);
108 }
109
110 TableItem (Table parent, int style, int index, boolean create) {
111     super (parent, style);
112     this.parent = parent;
113     if (create) parent.createItem (this, index);
114 }
115
116 static Table checkNull (Table control) {
117     if (control == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
118     return control;
119 }
120
121 protected void checkSubclass () {
122     if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
123 }
124
125 void clear () {
126     text = "";
127     image = null;
128     strings = null;
129     images = null;
130     imageIndent = 0;
131     checked = grayed = false;
132     background = foreground = font = -1;
133     cellBackground = cellForeground = cellFont = null;
134     if ((parent.style & SWT.VIRTUAL) != 0) cached = false;
135 }
136
137 void destroyWidget () {
138     parent.destroyItem (this);
139     releaseHandle ();
140 }
141
142 /**
143  * Returns the receiver's background color.
144  *
145  * @return the background color
146  *
147  * @exception SWTException <ul>
148  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
149  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
150  * </ul>
151  *
152  * @since 2.0
153  */

154 public Color getBackground () {
155     checkWidget ();
156     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
157     if (background == -1) return parent.getBackground ();
158     return Color.win32_new (display, background);
159 }
160
161 /**
162  * Returns the background color at the given column index in the receiver.
163  *
164  * @param index the column index
165  * @return the background color
166  *
167  * @exception SWTException <ul>
168  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
169  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
170  * </ul>
171  *
172  * @since 3.0
173  */

174 public Color getBackground (int index) {
175     checkWidget ();
176     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
177     int count = Math.max (1, parent.getColumnCount ());
178     if (0 > index || index > count - 1) return getBackground ();
179     int pixel = cellBackground != null ? cellBackground [index] : -1;
180     return pixel == -1 ? getBackground () : Color.win32_new (display, pixel);
181 }
182
183 /**
184  * Returns a rectangle describing the receiver's size and location
185  * relative to its parent.
186  *
187  * @return the receiver's bounding rectangle
188  *
189  * @exception SWTException <ul>
190  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
191  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
192  * </ul>
193  *
194  * @since 3.2
195  */

196 public Rectangle getBounds () {
197     checkWidget();
198     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
199     int itemIndex = parent.indexOf (this);
200     if (itemIndex == -1) return new Rectangle (0, 0, 0, 0);
201     RECT rect = getBounds (itemIndex, 0, true, false, false);
202     int width = rect.right - rect.left, height = rect.bottom - rect.top;
203     return new Rectangle (rect.left, rect.top, width, height);
204 }
205
206 /**
207  * Returns a rectangle describing the receiver's size and location
208  * relative to its parent at a column in the table.
209  *
210  * @param index the index that specifies the column
211  * @return the receiver's bounding column rectangle
212  *
213  * @exception SWTException <ul>
214  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
215  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
216  * </ul>
217  */

218 public Rectangle getBounds (int index) {
219     checkWidget();
220     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
221     int itemIndex = parent.indexOf (this);
222     if (itemIndex == -1) return new Rectangle (0, 0, 0, 0);
223     RECT rect = getBounds (itemIndex, index, true, true, true);
224     int width = rect.right - rect.left, height = rect.bottom - rect.top;
225     return new Rectangle (rect.left, rect.top, width, height);
226 }
227
228 RECT getBounds (int row, int column, boolean getText, boolean getImage, boolean fullText) {
229     return getBounds (row, column, getText, getImage, fullText, false, 0);
230 }
231
232 RECT getBounds (int row, int column, boolean getText, boolean getImage, boolean fullText, boolean fullImage, int hDC) {
233     if (!getText && !getImage) return new RECT ();
234     int columnCount = parent.getColumnCount ();
235     if (!(0 <= column && column < Math.max (1, columnCount))) {
236         return new RECT ();
237     }
238     if (parent.fixScrollWidth) parent.setScrollWidth (null, true);
239     RECT rect = new RECT ();
240     int hwnd = parent.handle;
241     int bits = OS.SendMessage (hwnd, OS.LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0);
242     if (column == 0 && (bits & OS.LVS_EX_FULLROWSELECT) == 0) {
243         if (parent.explorerTheme) {
244             rect.left = OS.LVIR_ICON;
245             parent.ignoreCustomDraw = true;
246             int code = OS.SendMessage (hwnd, OS. LVM_GETITEMRECT, row, rect);
247             parent.ignoreCustomDraw = false;
248             if (code == 0) return new RECT ();
249             if (getText) {
250                 int width = 0;
251                 int hFont = cellFont != null ? cellFont [column] : -1;
252                 if (hFont == -1) hFont = font;
253                 if (hFont == -1 && hDC == 0) {
254                     TCHAR buffer = new TCHAR (parent.getCodePage (), text, true);
255                     width = OS.SendMessage (hwnd, OS.LVM_GETSTRINGWIDTH, 0, buffer);
256                 } else {
257                     TCHAR buffer = new TCHAR (parent.getCodePage (), text, false);
258                     int textDC = hDC != 0 ? hDC : OS.GetDC (hwnd), oldFont = -1;
259                     if (hDC == 0) {
260                         if (hFont == -1) hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0);
261                         oldFont = OS.SelectObject (textDC, hFont);
262                     }
263                     RECT textRect = new RECT ();
264                     int flags = OS.DT_NOPREFIX | OS.DT_SINGLELINE | OS.DT_CALCRECT;
265                     OS.DrawText (textDC, buffer, buffer.length (), textRect, flags);
266                     width = textRect.right - textRect.left;
267                     if (hDC == 0) {
268                         if (oldFont != -1) OS.SelectObject (textDC, oldFont);
269                         OS.ReleaseDC (hwnd, textDC);
270                     }
271                 }
272                 if (!getImage) rect.left = rect.right;
273                 rect.right += width + Table.INSET * 2;
274             }
275         } else {
276             if (getText) {
277                 rect.left = OS.LVIR_SELECTBOUNDS;
278                 parent.ignoreCustomDraw = true;
279                 int code = OS.SendMessage (hwnd, OS.LVM_GETITEMRECT, row, rect);
280                 parent.ignoreCustomDraw = false;
281                 if (code == 0) return new RECT ();
282                 if (!getImage) {
283                     RECT iconRect = new RECT ();
284                     iconRect.left = OS.LVIR_ICON;
285                     parent.ignoreCustomDraw = true;
286                     code = OS.SendMessage (hwnd, OS. LVM_GETITEMRECT, row, iconRect);
287                     parent.ignoreCustomDraw = false;
288                     if (code != 0) rect.left = iconRect.right;
289                 }
290             } else {
291                 rect.left = OS.LVIR_ICON;
292                 parent.ignoreCustomDraw = true;
293                 int code = OS.SendMessage (hwnd, OS.LVM_GETITEMRECT, row, rect);
294                 parent.ignoreCustomDraw = false;
295                 if (code == 0) return new RECT ();
296             }
297         }
298         if (fullText || fullImage) {
299             RECT headerRect = new RECT ();
300             int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0);
301             OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, 0, headerRect);
302             OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2);
303             if (getText && fullText) rect.right = headerRect.right;
304             if (getImage && fullImage) rect.left = headerRect.left;
305         }
306     } else {
307         /*
308         * Feature in Windows. LVM_GETSUBITEMRECT returns an image width
309         * even when the subitem does not contain an image. The fix is to
310         * test for this case and adjust the rectangle to represent the area
311         * the table is actually drawing.
312         */

313         boolean hasImage = (column == 0 && image != null) || (images != null && images [column] != null);
314         rect.top = column;
315         if (fullText || fullImage || hDC == 0) {
316             /*
317             * Bug in Windows. Despite the fact that the documentation states
318             * that LVIR_BOUNDS and LVIR_LABEL are identical when used with
319             * LVM_GETSUBITEMRECT, LVIR_BOUNDS can return a zero height. The
320             * fix is to use LVIR_LABEL.
321             */

322             rect.left = getText ? OS.LVIR_LABEL : OS.LVIR_ICON;
323             parent.ignoreCustomDraw = true;
324             int code = OS.SendMessage (hwnd, OS. LVM_GETSUBITEMRECT, row, rect);
325             parent.ignoreCustomDraw = false;
326             if (code == 0) return new RECT ();
327             /*
328             * Feature in Windows. Calling LVM_GETSUBITEMRECT with LVIR_LABEL
329             * and zero for the column number gives the bounds of the first item
330             * without including the bounds of the icon. This is undocumented.
331             * When called with values greater than zero, the icon bounds are
332             * included and this behavior is documented. If the icon is needed
333             * in the bounds of the first item, the fix is to adjust the item
334             * bounds using the icon bounds.
335             */

336             if (column == 0 && getText && getImage) {
337                 RECT iconRect = new RECT ();
338                 iconRect.left = OS.LVIR_ICON;
339                 parent.ignoreCustomDraw = true;
340                 code = OS.SendMessage (hwnd, OS. LVM_GETSUBITEMRECT, row, iconRect);
341                 parent.ignoreCustomDraw = false;
342                 if (code != 0) rect.left = iconRect.left;
343             }
344             if (hasImage) {
345                 if (column != 0 && getText && !getImage) {
346                     RECT iconRect = new RECT ();
347                     iconRect.top = column;
348                     iconRect.left = OS.LVIR_ICON;
349                     if (OS.SendMessage (hwnd, OS. LVM_GETSUBITEMRECT, row, iconRect) != 0) {
350                         rect.left = iconRect.right + Table.INSET / 2;
351                     }
352                 }
353             } else {
354                 if (getImage && !getText) rect.right = rect.left;
355             }
356             if (column == 0 && fullImage) {
357                 RECT headerRect = new RECT ();
358                 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0);
359                 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, 0, headerRect);
360                 OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2);
361                 rect.left = headerRect.left;
362             }
363         } else {
364             rect.left = OS.LVIR_ICON;
365             parent.ignoreCustomDraw = true;
366             int code = OS.SendMessage (hwnd, OS. LVM_GETSUBITEMRECT, row, rect);
367             parent.ignoreCustomDraw = false;
368             if (code == 0) return new RECT ();
369             if (!hasImage) rect.right = rect.left;
370             if (getText) {
371                 String JavaDoc string = column == 0 ? text : strings != null ? strings [column] : null;
372                 if (string != null) {
373                     RECT textRect = new RECT ();
374                     TCHAR buffer = new TCHAR (parent.getCodePage (), string, false);
375                     int flags = OS.DT_NOPREFIX | OS.DT_SINGLELINE | OS.DT_CALCRECT;
376                     OS.DrawText (hDC, buffer, buffer.length (), textRect, flags);
377                     rect.right += textRect.right - textRect.left + Table.INSET * 3 + 1;
378                 }
379             }
380         }
381     }
382     /*
383     * Bug in Windows. In version 5.80 of COMCTL32.DLL, the top
384     * of the rectangle returned by LVM_GETSUBITEMRECT is off by
385     * the grid width when the grid is visible. The fix is to
386     * move the top of the rectangle up by the grid width.
387     */

388     int gridWidth = parent.getLinesVisible () ? Table.GRID_WIDTH : 0;
389     if (OS.COMCTL32_VERSION >= OS.VERSION (5, 80)) rect.top -= gridWidth;
390     if (column != 0) rect.left += gridWidth;
391     rect.right = Math.max (rect.right, rect.left);
392     rect.top += gridWidth;
393     rect.bottom = Math.max (rect.bottom - gridWidth, rect.top);
394     return rect;
395 }
396
397 /**
398  * Returns <code>true</code> if the receiver is checked,
399  * and false otherwise. When the parent does not have
400  * the <code>CHECK</code> style, return false.
401  *
402  * @return the checked state of the checkbox
403  *
404  * @exception SWTException <ul>
405  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
406  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
407  * </ul>
408  */

409 public boolean getChecked () {
410     checkWidget();
411     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
412     if ((parent.style & SWT.CHECK) == 0) return false;
413     return checked;
414 }
415
416 /**
417  * Returns the font that the receiver will use to paint textual information for this item.
418  *
419  * @return the receiver's font
420  *
421  * @exception SWTException <ul>
422  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
423  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
424  * </ul>
425  *
426  * @since 3.0
427  */

428 public Font getFont () {
429     checkWidget ();
430     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
431     return font == -1 ? parent.getFont () : Font.win32_new (display, font);
432 }
433
434 /**
435  * Returns the font that the receiver will use to paint textual information
436  * for the specified cell in this item.
437  *
438  * @param index the column index
439  * @return the receiver's font
440  *
441  * @exception SWTException <ul>
442  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
443  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
444  * </ul>
445  *
446  * @since 3.0
447  */

448 public Font getFont (int index) {
449     checkWidget ();
450     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
451     int count = Math.max (1, parent.getColumnCount ());
452     if (0 > index || index > count -1) return getFont ();
453     int hFont = (cellFont != null) ? cellFont [index] : font;
454     return hFont == -1 ? getFont () : Font.win32_new (display, hFont);
455 }
456
457 /**
458  * Returns the foreground color that the receiver will use to draw.
459  *
460  * @return the receiver's foreground color
461  *
462  * @exception SWTException <ul>
463  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
464  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
465  * </ul>
466  *
467  * @since 2.0
468  */

469 public Color getForeground () {
470     checkWidget ();
471     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
472     if (foreground == -1) return parent.getForeground ();
473     return Color.win32_new (display, foreground);
474 }
475
476 /**
477  *
478  * Returns the foreground color at the given column index in the receiver.
479  *
480  * @param index the column index
481  * @return the foreground color
482  *
483  * @exception SWTException <ul>
484  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
485  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
486  * </ul>
487  *
488  * @since 3.0
489  */

490 public Color getForeground (int index) {
491     checkWidget ();
492     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
493     int count = Math.max (1, parent.getColumnCount ());
494     if (0 > index || index > count -1) return getForeground ();
495     int pixel = cellForeground != null ? cellForeground [index] : -1;
496     return pixel == -1 ? getForeground () : Color.win32_new (display, pixel);
497 }
498
499 /**
500  * Returns <code>true</code> if the receiver is grayed,
501  * and false otherwise. When the parent does not have
502  * the <code>CHECK</code> style, return false.
503  *
504  * @return the grayed state of the checkbox
505  *
506  * @exception SWTException <ul>
507  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
508  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
509  * </ul>
510  */

511 public boolean getGrayed () {
512     checkWidget();
513     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
514     if ((parent.style & SWT.CHECK) == 0) return false;
515     return grayed;
516 }
517
518 public Image getImage () {
519     checkWidget();
520     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
521     return super.getImage ();
522 }
523
524 /**
525  * Returns the image stored at the given column index in the receiver,
526  * or null if the image has not been set or if the column does not exist.
527  *
528  * @param index the column index
529  * @return the image stored at the given column index in the receiver
530  *
531  * @exception SWTException <ul>
532  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
533  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
534  * </ul>
535  */

536 public Image getImage (int index) {
537     checkWidget();
538     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
539     if (index == 0) return getImage ();
540     if (images != null) {
541         if (0 <= index && index < images.length) return images [index];
542     }
543     return null;
544 }
545
546 /**
547  * Returns a rectangle describing the size and location
548  * relative to its parent of an image at a column in the
549  * table. An empty rectangle is returned if index exceeds
550  * the index of the table's last column.
551  *
552  * @param index the index that specifies the column
553  * @return the receiver's bounding image rectangle
554  *
555  * @exception SWTException <ul>
556  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
557  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
558  * </ul>
559  */

560 public Rectangle getImageBounds (int index) {
561     checkWidget();
562     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
563     int itemIndex = parent.indexOf (this);
564     if (itemIndex == -1) return new Rectangle (0, 0, 0, 0);
565     RECT rect = getBounds (itemIndex, index, false, true, false);
566     int width = rect.right - rect.left, height = rect.bottom - rect.top;
567     return new Rectangle (rect.left, rect.top, width, height);
568 }
569
570 /**
571  * Gets the image indent.
572  *
573  * @return the indent
574  *
575  * @exception SWTException <ul>
576  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
577  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
578  * </ul>
579  */

580 public int getImageIndent () {
581     checkWidget();
582     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
583     return imageIndent;
584 }
585
586 String JavaDoc getNameText () {
587     if ((parent.style & SWT.VIRTUAL) != 0) {
588         if (!cached) return "*virtual*"; //$NON-NLS-1$
589
}
590     return super.getNameText ();
591 }
592
593 /**
594  * Returns the receiver's parent, which must be a <code>Table</code>.
595  *
596  * @return the receiver's parent
597  *
598  * @exception SWTException <ul>
599  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
600  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
601  * </ul>
602  */

603 public Table getParent () {
604     checkWidget();
605     return parent;
606 }
607
608 public String JavaDoc getText () {
609     checkWidget();
610     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
611     return super.getText ();
612 }
613
614 /**
615  * Returns the text stored at the given column index in the receiver,
616  * or empty string if the text has not been set.
617  *
618  * @param index the column index
619  * @return the text stored at the given column index in the receiver
620  *
621  * @exception SWTException <ul>
622  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
623  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
624  * </ul>
625  */

626 public String JavaDoc getText (int index) {
627     checkWidget();
628     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
629     if (index == 0) return getText ();
630     if (strings != null) {
631         if (0 <= index && index < strings.length) {
632             String JavaDoc string = strings [index];
633             return string != null ? string : "";
634         }
635     }
636     return "";
637 }
638
639 /**
640  * Returns a rectangle describing the size and location
641  * relative to its parent of the text at a column in the
642  * table. An empty rectangle is returned if index exceeds
643  * the index of the table's last column.
644  *
645  * @param index the index that specifies the column
646  * @return the receiver's bounding text rectangle
647  *
648  * @exception SWTException <ul>
649  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
650  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
651  * </ul>
652  *
653  * @since 3.3
654  */

655 public Rectangle getTextBounds (int index) {
656     checkWidget();
657     if (!parent.checkData (this, true)) error (SWT.ERROR_WIDGET_DISPOSED);
658     int itemIndex = parent.indexOf (this);
659     if (itemIndex == -1) return new Rectangle (0, 0, 0, 0);
660     RECT rect = getBounds (itemIndex, index, true, false, true);
661     rect.left += 2;
662     if (index != 0) rect.left += Table.INSET;
663     rect.left = Math.min (rect.left, rect.right);
664     rect.right = rect.right - Table.INSET;
665     int width = Math.max (0, rect.right - rect.left);
666     int height = Math.max (0, rect.bottom - rect.top);
667     return new Rectangle (rect.left, rect.top, width, height);
668 }
669
670 void redraw () {
671     if (parent.currentItem == this || parent.drawCount != 0) return;
672     int hwnd = parent.handle;
673     if (!OS.IsWindowVisible (hwnd)) return;
674     int index = parent.indexOf (this);
675     if (index == -1) return;
676     OS.SendMessage (hwnd, OS.LVM_REDRAWITEMS, index, index);
677 }
678
679 void redraw (int column, boolean drawText, boolean drawImage) {
680     if (parent.currentItem == this || parent.drawCount != 0) return;
681     int hwnd = parent.handle;
682     if (!OS.IsWindowVisible (hwnd)) return;
683     int index = parent.indexOf (this);
684     if (index == -1) return;
685     RECT rect = getBounds (index, column, drawText, drawImage, true);
686     OS.InvalidateRect (hwnd, rect, true);
687 }
688
689 void releaseHandle () {
690     super.releaseHandle ();
691     parent = null;
692 }
693
694 void releaseWidget () {
695     super.releaseWidget ();
696     strings = null;
697     images = null;
698     cellBackground = cellForeground = cellFont = null;
699 }
700
701 /**
702  * Sets the receiver's background color to the color specified
703  * by the argument, or to the default system color for the item
704  * if the argument is null.
705  *
706  * @param color the new color (or null)
707  *
708  * @exception IllegalArgumentException <ul>
709  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
710  * </ul>
711  * @exception SWTException <ul>
712  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
713  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
714  * </ul>
715  *
716  * @since 2.0
717  */

718 public void setBackground (Color color) {
719     checkWidget ();
720     if (color != null && color.isDisposed ()) {
721         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
722     }
723     int pixel = -1;
724     if (color != null) {
725         parent.setCustomDraw (true);
726         pixel = color.handle;
727     }
728     if (background == pixel) return;
729     background = pixel;
730     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
731     redraw ();
732 }
733
734 /**
735  * Sets the background color at the given column index in the receiver
736  * to the color specified by the argument, or to the default system color for the item
737  * if the argument is null.
738  *
739  * @param index the column index
740  * @param color the new color (or null)
741  *
742  * @exception IllegalArgumentException <ul>
743  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
744  * </ul>
745  * @exception SWTException <ul>
746  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
747  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
748  * </ul>
749  *
750  * @since 3.0
751  */

752 public void setBackground (int index, Color color) {
753     checkWidget ();
754     if (color != null && color.isDisposed ()) {
755         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
756     }
757     int count = Math.max (1, parent.getColumnCount ());
758     if (0 > index || index > count - 1) return;
759     int pixel = -1;
760     if (color != null) {
761         parent.setCustomDraw (true);
762         pixel = color.handle;
763     }
764     if (cellBackground == null) {
765         cellBackground = new int [count];
766         for (int i = 0; i < count; i++) {
767             cellBackground [i] = -1;
768         }
769     }
770     if (cellBackground [index] == pixel) return;
771     cellBackground [index] = pixel;
772     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
773     redraw (index, true, true);
774 }
775
776 /**
777  * Sets the checked state of the checkbox for this item. This state change
778  * only applies if the Table was created with the SWT.CHECK style.
779  *
780  * @param checked the new checked state of the checkbox
781  *
782  * @exception SWTException <ul>
783  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
784  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
785  * </ul>
786  */

787 public void setChecked (boolean checked) {
788     checkWidget();
789     if ((parent.style & SWT.CHECK) == 0) return;
790     if (this.checked == checked) return;
791     setChecked (checked, false);
792 }
793
794 void setChecked (boolean checked, boolean notify) {
795     this.checked = checked;
796     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
797     if (notify) {
798         Event event = new Event();
799         event.item = this;
800         event.detail = SWT.CHECK;
801         parent.postEvent (SWT.Selection, event);
802     }
803     redraw ();
804 }
805
806 /**
807  * Sets the font that the receiver will use to paint textual information
808  * for this item to the font specified by the argument, or to the default font
809  * for that kind of control if the argument is null.
810  *
811  * @param font the new font (or null)
812  *
813  * @exception IllegalArgumentException <ul>
814  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
815  * </ul>
816  * @exception SWTException <ul>
817  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
818  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
819  * </ul>
820  *
821  * @since 3.0
822  */

823 public void setFont (Font font){
824     checkWidget ();
825     if (font != null && font.isDisposed ()) {
826         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
827     }
828     int hFont = -1;
829     if (font != null) {
830         parent.setCustomDraw (true);
831         hFont = font.handle;
832     }
833     if (this.font == hFont) return;
834     this.font = hFont;
835     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
836     /*
837     * Bug in Windows. Despite the fact that every item in the
838     * table always has LPSTR_TEXTCALLBACK, Windows caches the
839     * bounds for the selected items. This means that
840     * when you change the string to be something else, Windows
841     * correctly asks you for the new string but when the item
842     * is selected, the selection draws using the bounds of the
843     * previous item. The fix is to reset LPSTR_TEXTCALLBACK
844     * even though it has not changed, causing Windows to flush
845     * cached bounds.
846     */

847     if ((parent.style & SWT.VIRTUAL) == 0 && cached) {
848         int itemIndex = parent.indexOf (this);
849         if (itemIndex != -1) {
850             int hwnd = parent.handle;
851             LVITEM lvItem = new LVITEM ();
852             lvItem.mask = OS.LVIF_TEXT;
853             lvItem.iItem = itemIndex;
854             lvItem.pszText = OS.LPSTR_TEXTCALLBACK;
855             OS.SendMessage (hwnd, OS.LVM_SETITEM, 0, lvItem);
856             cached = false;
857         }
858     }
859     parent.setScrollWidth (this, false);
860     redraw ();
861 }
862
863 /**
864  * Sets the font that the receiver will use to paint textual information
865  * for the specified cell in this item to the font specified by the
866  * argument, or to the default font for that kind of control if the
867  * argument is null.
868  *
869  * @param index the column index
870  * @param font the new font (or null)
871  *
872  * @exception IllegalArgumentException <ul>
873  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
874  * </ul>
875  * @exception SWTException <ul>
876  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
877  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
878  * </ul>
879  *
880  * @since 3.0
881  */

882 public void setFont (int index, Font font) {
883     checkWidget ();
884     if (font != null && font.isDisposed ()) {
885         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
886     }
887     int count = Math.max (1, parent.getColumnCount ());
888     if (0 > index || index > count - 1) return;
889     int hFont = -1;
890     if (font != null) {
891         parent.setCustomDraw (true);
892         hFont = font.handle;
893     }
894     if (cellFont == null) {
895         cellFont = new int [count];
896         for (int i = 0; i < count; i++) {
897             cellFont [i] = -1;
898         }
899     }
900     if (cellFont [index] == hFont) return;
901     cellFont [index] = hFont;
902     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
903     if (index == 0) {
904         /*
905         * Bug in Windows. Despite the fact that every item in the
906         * table always has LPSTR_TEXTCALLBACK, Windows caches the
907         * bounds for the selected items. This means that
908         * when you change the string to be something else, Windows
909         * correctly asks you for the new string but when the item
910         * is selected, the selection draws using the bounds of the
911         * previous item. The fix is to reset LPSTR_TEXTCALLBACK
912         * even though it has not changed, causing Windows to flush
913         * cached bounds.
914         */

915         if ((parent.style & SWT.VIRTUAL) == 0 && cached) {
916             int itemIndex = parent.indexOf (this);
917             if (itemIndex != -1) {
918                 int hwnd = parent.handle;
919                 LVITEM lvItem = new LVITEM ();
920                 lvItem.mask = OS.LVIF_TEXT;
921                 lvItem.iItem = itemIndex;
922                 lvItem.pszText = OS.LPSTR_TEXTCALLBACK;
923                 OS.SendMessage (hwnd, OS.LVM_SETITEM, 0, lvItem);
924                 cached = false;
925             }
926         }
927         parent.setScrollWidth (this, false);
928     }
929     redraw (index, true, false);
930 }
931
932 /**
933  * Sets the receiver's foreground color to the color specified
934  * by the argument, or to the default system color for the item
935  * if the argument is null.
936  *
937  * @param color the new color (or null)
938  *
939  * @exception IllegalArgumentException <ul>
940  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
941  * </ul>
942  * @exception SWTException <ul>
943  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
944  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
945  * </ul>
946  *
947  * @since 2.0
948  */

949 public void setForeground (Color color){
950     checkWidget ();
951     if (color != null && color.isDisposed ()) {
952         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
953     }
954     int pixel = -1;
955     if (color != null) {
956         parent.setCustomDraw (true);
957         pixel = color.handle;
958     }
959     if (foreground == pixel) return;
960     foreground = pixel;
961     if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
962     redraw ();
963 }
964
965 /**
966  * Sets the foreground color at the given column index in the receiver
967  * to the color specified by the argument, or to the default system color for the item
968  * if the argument is null.
969  *
970  * @param index the column index
971  * @param color the new color (or null)
972  *
973  * @exception IllegalArgumentException <ul>
974  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
975  * </ul>
976  * @exception SWTException <ul>
977  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
978  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
979  * </ul>
980  *
981  * @since 3.0
982  */

983 public void setForeground (int index, Color color){
984     checkWidget ();
985     if (color != null && color.isDisposed ()) {
986         SWT.error (SWT.ERROR_INVALID_ARGUMENT);
987     }
988     int count = Math.max (1, parent.getColumnCount ());
989     if (0 > index || index > count - 1) return;
990     int pixel = -1;
991     if (color != null) {
992         parent.setCustomDraw (true);
993         pixel = color.handle;
994     }
995     if (cellForeground == null) {
996         cellForeground = new int [count];
997         for (int i = 0; i < count; i++) {
998             cellForeground [i] = -1;
999         }
1000    }
1001    if (cellForeground [index] == pixel) return;
1002    cellForeground [index] = pixel;
1003    if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
1004    redraw (index, true, false);
1005}
1006
1007/**
1008 * Sets the grayed state of the checkbox for this item. This state change
1009 * only applies if the Table was created with the SWT.CHECK style.
1010 *
1011 * @param grayed the new grayed state of the checkbox;
1012 *
1013 * @exception SWTException <ul>
1014 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1015 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1016 * </ul>
1017 */

1018public void setGrayed (boolean grayed) {
1019    checkWidget();
1020    if ((parent.style & SWT.CHECK) == 0) return;
1021    if (this.grayed == grayed) return;
1022    this.grayed = grayed;
1023    if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
1024    redraw ();
1025}
1026
1027/**
1028 * Sets the image for multiple columns in the table.
1029 *
1030 * @param images the array of new images
1031 *
1032 * @exception IllegalArgumentException <ul>
1033 * <li>ERROR_NULL_ARGUMENT - if the array of images is null</li>
1034 * <li>ERROR_INVALID_ARGUMENT - if one of the images has been disposed</li>
1035 * </ul>
1036 * @exception SWTException <ul>
1037 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1038 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1039 * </ul>
1040 */

1041public void setImage (Image [] images) {
1042    checkWidget();
1043    if (images == null) error (SWT.ERROR_NULL_ARGUMENT);
1044    for (int i=0; i<images.length; i++) {
1045        setImage (i, images [i]);
1046    }
1047}
1048
1049/**
1050 * Sets the receiver's image at a column.
1051 *
1052 * @param index the column index
1053 * @param image the new image
1054 *
1055 * @exception IllegalArgumentException <ul>
1056 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
1057 * </ul>
1058 * @exception SWTException <ul>
1059 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1060 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1061 * </ul>
1062 */

1063public void setImage (int index, Image image) {
1064    checkWidget();
1065    if (image != null && image.isDisposed ()) {
1066        error(SWT.ERROR_INVALID_ARGUMENT);
1067    }
1068    Image oldImage = null;
1069    if (index == 0) {
1070        if (image != null && image.type == SWT.ICON) {
1071            if (image.equals (this.image)) return;
1072        }
1073        oldImage = this.image;
1074        super.setImage (image);
1075    }
1076    int count = Math.max (1, parent.getColumnCount ());
1077    if (0 > index || index > count - 1) return;
1078    if (images == null && index != 0) {
1079        images = new Image [count];
1080        images [0] = image;
1081    }
1082    if (images != null) {
1083        if (image != null && image.type == SWT.ICON) {
1084            if (image.equals (images [index])) return;
1085        }
1086        oldImage = images [index];
1087        images [index] = image;
1088    }
1089    if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
1090    
1091    /* Ensure that the image list is created */
1092    parent.imageIndex (image, index);
1093    
1094    if (index == 0) parent.setScrollWidth (this, false);
1095    boolean drawText = (image == null && oldImage != null) || (image != null && oldImage == null);
1096    redraw (index, drawText, true);
1097}
1098
1099public void setImage (Image image) {
1100    checkWidget ();
1101    setImage (0, image);
1102}
1103
1104/**
1105 * Sets the indent of the first column's image, expressed in terms of the image's width.
1106 *
1107 * @param indent the new indent
1108 *
1109 * </ul>
1110 * @exception SWTException <ul>
1111 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1112 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1113 * </ul>
1114 *
1115 * @deprecated this functionality is not supported on most platforms
1116 */

1117public void setImageIndent (int indent) {
1118    checkWidget();
1119    if (indent < 0) return;
1120    if (imageIndent == indent) return;
1121    imageIndent = indent;
1122    if ((parent.style & SWT.VIRTUAL) != 0) {
1123        cached = true;
1124    } else {
1125        int index = parent.indexOf (this);
1126        if (index != -1) {
1127            int hwnd = parent.handle;
1128            LVITEM lvItem = new LVITEM ();
1129            lvItem.mask = OS.LVIF_INDENT;
1130            lvItem.iItem = index;
1131            lvItem.iIndent = indent;
1132            OS.SendMessage (hwnd, OS.LVM_SETITEM, 0, lvItem);
1133        }
1134    }
1135    parent.setScrollWidth (this, false);
1136    redraw ();
1137}
1138
1139/**
1140 * Sets the text for multiple columns in the table.
1141 *
1142 * @param strings the array of new strings
1143 *
1144 * @exception IllegalArgumentException <ul>
1145 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
1146 * </ul>
1147 * @exception SWTException <ul>
1148 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1149 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1150 * </ul>
1151 */

1152public void setText (String JavaDoc [] strings) {
1153    checkWidget();
1154    if (strings == null) error (SWT.ERROR_NULL_ARGUMENT);
1155    for (int i=0; i<strings.length; i++) {
1156        String JavaDoc string = strings [i];
1157        if (string != null) setText (i, string);
1158    }
1159}
1160
1161/**
1162 * Sets the receiver's text at a column
1163 *
1164 * @param index the column index
1165 * @param string the new text
1166 *
1167 * @exception IllegalArgumentException <ul>
1168 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
1169 * </ul>
1170 * @exception SWTException <ul>
1171 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1172 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1173 * </ul>
1174 */

1175public void setText (int index, String JavaDoc string) {
1176    checkWidget();
1177    if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
1178    if (index == 0) {
1179        if (string.equals (text)) return;
1180        super.setText (string);
1181    }
1182    int count = Math.max (1, parent.getColumnCount ());
1183    if (0 > index || index > count - 1) return;
1184    if (strings == null && index != 0) {
1185        strings = new String JavaDoc [count];
1186        strings [0] = text;
1187    }
1188    if (strings != null) {
1189        if (string.equals (strings [index])) return;
1190        strings [index] = string;
1191    }
1192    if ((parent.style & SWT.VIRTUAL) != 0) cached = true;
1193    if (index == 0) {
1194        /*
1195        * Bug in Windows. Despite the fact that every item in the
1196        * table always has LPSTR_TEXTCALLBACK, Windows caches the
1197        * bounds for the selected items. This means that
1198        * when you change the string to be something else, Windows
1199        * correctly asks you for the new string but when the item
1200        * is selected, the selection draws using the bounds of the
1201        * previous item. The fix is to reset LPSTR_TEXTCALLBACK
1202        * even though it has not changed, causing Windows to flush
1203        * cached bounds.
1204        */

1205        if ((parent.style & SWT.VIRTUAL) == 0 && cached) {
1206            int itemIndex = parent.indexOf (this);
1207            if (itemIndex != -1) {
1208                int hwnd = parent.handle;
1209                LVITEM lvItem = new LVITEM ();
1210                lvItem.mask = OS.LVIF_TEXT;
1211                lvItem.iItem = itemIndex;
1212                lvItem.pszText = OS.LPSTR_TEXTCALLBACK;
1213                OS.SendMessage (hwnd, OS.LVM_SETITEM, 0, lvItem);
1214                cached = false;
1215            }
1216        }
1217        parent.setScrollWidth (this, false);
1218    }
1219    redraw (index, true, false);
1220}
1221
1222public void setText (String JavaDoc string) {
1223    checkWidget();
1224    setText (0, string);
1225}
1226
1227}
1228
Popular Tags