comparison dwt/widgets/TreeColumn.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 module dwt.widgets.TreeColumn;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.events.ControlListener;
19 import dwt.events.SelectionEvent;
20 import dwt.events.SelectionListener;
21 import dwt.graphics.Image;
22 import dwt.internal.cocoa.NSString;
23 import dwt.internal.cocoa.NSTableColumn;
24 import dwt.internal.cocoa.OS;
25
26 /**
27 * Instances of this class represent a column in a tree widget.
28 * <p><dl>
29 * <dt><b>Styles:</b></dt>
30 * <dd>LEFT, RIGHT, CENTER</dd>
31 * <dt><b>Events:</b></dt>
32 * <dd> Move, Resize, Selection</dd>
33 * </dl>
34 * </p><p>
35 * Note: Only one of the styles LEFT, RIGHT and CENTER may be specified.
36 * </p><p>
37 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
38 * </p>
39 *
40 * @since 3.1
41 */
42 public class TreeColumn extends Item {
43 NSTableColumn nsColumn;
44 Tree parent;
45 String toolTipText;
46
47 /**
48 * Constructs a new instance of this class given its parent
49 * (which must be a <code>Tree</code>) and a style value
50 * describing its behavior and appearance. The item is added
51 * to the end of the items maintained by its parent.
52 * <p>
53 * The style value is either one of the style constants defined in
54 * class <code>DWT</code> which is applicable to instances of this
55 * class, or must be built by <em>bitwise OR</em>'ing together
56 * (that is, using the <code>int</code> "|" operator) two or more
57 * of those <code>DWT</code> style constants. The class description
58 * lists the style constants that are applicable to the class.
59 * Style bits are also inherited from superclasses.
60 * </p>
61 *
62 * @param parent a composite control which will be the parent of the new instance (cannot be null)
63 * @param style the style of control to construct
64 *
65 * @exception IllegalArgumentException <ul>
66 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
67 * </ul>
68 * @exception DWTException <ul>
69 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
70 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
71 * </ul>
72 *
73 * @see DWT#LEFT
74 * @see DWT#RIGHT
75 * @see DWT#CENTER
76 * @see Widget#checkSubclass
77 * @see Widget#getStyle
78 */
79 public TreeColumn (Tree parent, int style) {
80 super (parent, checkStyle (style));
81 this.parent = parent;
82 parent.createItem (this, parent.getColumnCount ());
83 }
84
85 /**
86 * Constructs a new instance of this class given its parent
87 * (which must be a <code>Tree</code>), a style value
88 * describing its behavior and appearance, and the index
89 * at which to place it in the items maintained by its parent.
90 * <p>
91 * The style value is either one of the style constants defined in
92 * class <code>DWT</code> which is applicable to instances of this
93 * class, or must be built by <em>bitwise OR</em>'ing together
94 * (that is, using the <code>int</code> "|" operator) two or more
95 * of those <code>DWT</code> style constants. The class description
96 * lists the style constants that are applicable to the class.
97 * Style bits are also inherited from superclasses.
98 * </p>
99 *
100 * @param parent a composite control which will be the parent of the new instance (cannot be null)
101 * @param style the style of control to construct
102 * @param index the zero-relative index to store the receiver in its parent
103 *
104 * @exception IllegalArgumentException <ul>
105 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
106 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
107 * </ul>
108 * @exception DWTException <ul>
109 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
110 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
111 * </ul>
112 *
113 * @see DWT#LEFT
114 * @see DWT#RIGHT
115 * @see DWT#CENTER
116 * @see Widget#checkSubclass
117 * @see Widget#getStyle
118 */
119 public TreeColumn (Tree parent, int style, int index) {
120 super (parent, checkStyle (style));
121 this.parent = parent;
122 parent.createItem (this, index);
123 }
124
125 /**
126 * Adds the listener to the collection of listeners who will
127 * be notified when the control is moved or resized, by sending
128 * it one of the messages defined in the <code>ControlListener</code>
129 * interface.
130 *
131 * @param listener the listener which should be notified
132 *
133 * @exception IllegalArgumentException <ul>
134 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
135 * </ul>
136 * @exception DWTException <ul>
137 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
138 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
139 * </ul>
140 *
141 * @see ControlListener
142 * @see #removeControlListener
143 */
144 public void addControlListener(ControlListener listener) {
145 checkWidget ();
146 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
147 TypedListener typedListener = new TypedListener (listener);
148 addListener (DWT.Resize,typedListener);
149 addListener (DWT.Move,typedListener);
150 }
151
152 /**
153 * Adds the listener to the collection of listeners who will
154 * be notified when the control is selected by the user, by sending
155 * it one of the messages defined in the <code>SelectionListener</code>
156 * interface.
157 * <p>
158 * <code>widgetSelected</code> is called when the column header is selected.
159 * <code>widgetDefaultSelected</code> is not called.
160 * </p>
161 *
162 * @param listener the listener which should be notified when the control is selected by the user
163 *
164 * @exception IllegalArgumentException <ul>
165 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
166 * </ul>
167 * @exception DWTException <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 * @see SelectionListener
173 * @see #removeSelectionListener
174 * @see SelectionEvent
175 */
176 public void addSelectionListener (SelectionListener listener) {
177 checkWidget ();
178 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
179 TypedListener typedListener = new TypedListener (listener);
180 addListener (DWT.Selection,typedListener);
181 addListener (DWT.DefaultSelection,typedListener);
182 }
183
184 static int checkStyle (int style) {
185 return checkBits (style, DWT.LEFT, DWT.CENTER, DWT.RIGHT, 0, 0, 0);
186 }
187
188 protected void checkSubclass () {
189 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS);
190 }
191
192 void destroyWidget () {
193 parent.destroyItem (this);
194 releaseHandle ();
195 }
196
197 /**
198 * Returns a value which describes the position of the
199 * text or image in the receiver. The value will be one of
200 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>.
201 *
202 * @return the alignment
203 *
204 * @exception DWTException <ul>
205 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
206 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
207 * </ul>
208 */
209 public int getAlignment () {
210 checkWidget ();
211 if ((style & DWT.LEFT) !is 0) return DWT.LEFT;
212 if ((style & DWT.CENTER) !is 0) return DWT.CENTER;
213 if ((style & DWT.RIGHT) !is 0) return DWT.RIGHT;
214 return DWT.LEFT;
215 }
216
217 String getNameText () {
218 return getText ();
219 }
220
221 /**
222 * Returns the receiver's parent, which must be a <code>Tree</code>.
223 *
224 * @return the receiver's parent
225 *
226 * @exception DWTException <ul>
227 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
228 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
229 * </ul>
230 */
231 public Tree getParent () {
232 checkWidget ();
233 return parent;
234 }
235
236 /**
237 * Gets the moveable attribute. A column that is
238 * not moveable cannot be reordered by the user
239 * by dragging the header but may be reordered
240 * by the programmer.
241 *
242 * @return the moveable attribute
243 *
244 * @exception DWTException <ul>
245 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
246 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
247 * </ul>
248 *
249 * @see Tree#getColumnOrder()
250 * @see Tree#setColumnOrder(int[])
251 * @see TreeColumn#setMoveable(bool)
252 * @see DWT#Move
253 *
254 * @since 3.2
255 */
256 public bool getMoveable () {
257 checkWidget ();
258 // int [] flags = new int [1];
259 // OS.GetDataBrowserPropertyFlags (parent.handle, id, flags);
260 // return (flags [0] & OS.kDataBrowserListViewMovableColumn) !is 0;
261 return false;
262 }
263
264 /**
265 * Gets the resizable attribute. A column that is
266 * not resizable cannot be dragged by the user but
267 * may be resized by the programmer.
268 *
269 * @return the resizable attribute
270 *
271 * @exception DWTException <ul>
272 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
273 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
274 * </ul>
275 */
276 public bool getResizable () {
277 checkWidget ();
278 return nsColumn.resizingMask() !is OS.NSTableColumnNoResizing;
279 }
280
281 /**
282 * Returns the receiver's tool tip text, or null if it has
283 * not been set.
284 *
285 * @return the receiver's tool tip text
286 *
287 * @exception DWTException <ul>
288 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
289 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
290 * </ul>
291 *
292 * @since 3.2
293 */
294 public String getToolTipText () {
295 checkWidget ();
296 return toolTipText;
297 }
298
299 /**
300 * Gets the width of the receiver.
301 *
302 * @return the width
303 *
304 * @exception DWTException <ul>
305 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
306 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
307 * </ul>
308 */
309 public int getWidth () {
310 checkWidget ();
311 return (int)nsColumn.width();
312 }
313
314 /**
315 * Causes the receiver to be resized to its preferred size.
316 * For a composite, this involves computing the preferred size
317 * from its layout, if there is one.
318 *
319 * @exception DWTException <ul>
320 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
321 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
322 * </ul>
323 *
324 */
325 public void pack () {
326 checkWidget ();
327 // GC gc = new GC (parent);
328 // int width = gc.stringExtent (text).x;
329 //TODO extra header
330 // int index = parent.indexOf (this);
331 // width = Math.max (width, calculateWidth (parent.childIds, index, gc, width));
332 //
333 // gc.dispose ();
334 // setWidth (width + parent.getInsetWidth (id, true));
335 //TODO this only takes care of the header
336 nsColumn.sizeToFit();
337 }
338
339 void releaseHandle () {
340 super.releaseHandle ();
341 if (nsColumn !is null) nsColumn.release();
342 nsColumn = null;
343 parent = null;
344 }
345
346 void releaseWidget () {
347 super.releaseWidget ();
348 if (parent.sortColumn is this) {
349 parent.sortColumn = null;
350 }
351 }
352
353 /**
354 * Removes the listener from the collection of listeners who will
355 * be notified when the control is moved or resized.
356 *
357 * @param listener the listener which should no longer be notified
358 *
359 * @exception IllegalArgumentException <ul>
360 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
361 * </ul>
362 * @exception DWTException <ul>
363 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
364 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
365 * </ul>
366 *
367 * @see ControlListener
368 * @see #addControlListener
369 */
370 public void removeControlListener (ControlListener listener) {
371 checkWidget ();
372 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
373 if (eventTable is null) return;
374 eventTable.unhook (DWT.Move, listener);
375 eventTable.unhook (DWT.Resize, listener);
376 }
377
378 /**
379 * Removes the listener from the collection of listeners who will
380 * be notified when the control is selected by the user.
381 *
382 * @param listener the listener which should no longer be notified
383 *
384 * @exception IllegalArgumentException <ul>
385 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
386 * </ul>
387 * @exception DWTException <ul>
388 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
389 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
390 * </ul>
391 *
392 * @see SelectionListener
393 * @see #addSelectionListener
394 */
395 public void removeSelectionListener(SelectionListener listener) {
396 checkWidget ();
397 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
398 if (eventTable is null) return;
399 eventTable.unhook (DWT.Selection, listener);
400 eventTable.unhook (DWT.DefaultSelection,listener);
401 }
402
403 /**
404 * Controls how text and images will be displayed in the receiver.
405 * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
406 * or <code>CENTER</code>.
407 *
408 * @param alignment the new alignment
409 *
410 * @exception DWTException <ul>
411 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
412 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
413 * </ul>
414 */
415 public void setAlignment (int alignment) {
416 checkWidget ();
417 if ((alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER)) is 0) return;
418 int index = parent.indexOf (this);
419 if (index is -1 || index is 0) return;
420 style &= ~(DWT.LEFT | DWT.RIGHT | DWT.CENTER);
421 style |= alignment & (DWT.LEFT | DWT.RIGHT | DWT.CENTER);
422 //TODO
423 }
424
425 public void setImage (Image image) {
426 checkWidget();
427 if (image !is null && image.isDisposed ()) {
428 error (DWT.ERROR_INVALID_ARGUMENT);
429 }
430 // int index = parent.indexOf (this);
431 // if (index is -1) return;
432 // if (iconRef !is 0) {
433 // OS.ReleaseIconRef (iconRef);
434 // iconRef = 0;
435 // }
436 // super.setImage (image);
437 // if (image !is null) {
438 // if (OS.VERSION < 0x1040) {
439 // iconRef = createIconRef (image);
440 // }
441 // }
442 // updateHeader ();
443 }
444
445 /**
446 * Sets the moveable attribute. A column that is
447 * moveable can be reordered by the user by dragging
448 * the header. A column that is not moveable cannot be
449 * dragged by the user but may be reordered
450 * by the programmer.
451 *
452 * @param moveable the moveable attribute
453 *
454 * @exception DWTException <ul>
455 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
456 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
457 * </ul>
458 *
459 * @see Tree#setColumnOrder(int[])
460 * @see Tree#getColumnOrder()
461 * @see TreeColumn#getMoveable()
462 * @see DWT#Move
463 *
464 * @since 3.2
465 */
466 public void setMoveable (bool moveable) {
467 checkWidget ();
468 // int [] flags = new int [1];
469 // OS.GetDataBrowserPropertyFlags (parent.handle, id, flags);
470 // if (moveable) {
471 // flags [0] |= OS.kDataBrowserListViewMovableColumn;
472 // } else {
473 // flags [0] &= ~OS.kDataBrowserListViewMovableColumn;
474 // }
475 // OS.SetDataBrowserPropertyFlags (parent.handle, id, flags [0]);
476 }
477
478 /**
479 * Sets the resizable attribute. A column that is
480 * not resizable cannot be dragged by the user but
481 * may be resized by the programmer.
482 *
483 * @param resizable the resize attribute
484 *
485 * @exception DWTException <ul>
486 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
487 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
488 * </ul>
489 */
490 public void setResizable (bool resizable) {
491 checkWidget ();
492 nsColumn.setResizingMask(resizable ? OS.NSTableColumnUserResizingMask : OS.NSTableColumnNoResizing);
493 }
494
495 public void setText (String string) {
496 checkWidget ();
497 if (string is null) error (DWT.ERROR_NULL_ARGUMENT);
498 super.setText (string);
499 char [] buffer = new char [text.length ()];
500 text.getChars (0, buffer.length, buffer, 0);
501 int length = fixMnemonic (buffer);
502 nsColumn.headerCell().setTitle(NSString.stringWithCharacters(buffer, length));
503 }
504
505 /**
506 * Sets the receiver's tool tip text to the argument, which
507 * may be null indicating that no tool tip text should be shown.
508 *
509 * @param string the new tool tip text (or null)
510 *
511 * @exception DWTException <ul>
512 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
513 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
514 * </ul>
515 *
516 * @since 3.2
517 */
518 public void setToolTipText (String string) {
519 checkWidget();
520 toolTipText = string;
521 }
522
523 /**
524 * Sets the width of the receiver.
525 *
526 * @param width the new width
527 *
528 * @exception DWTException <ul>
529 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
530 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
531 * </ul>
532 */
533 public void setWidth (int width) {
534 checkWidget ();
535 if (width < 0) return;
536 nsColumn.setWidth (width);
537 }
538 }