Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/ViewerCell.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006, 2008 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 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation | |
11 * - fix in bug: 195908,198035,215069,215735,227421 | |
12 * Port to the D programming language: | |
13 * Frank Benoit <benoit@tionex.de> | |
14 *******************************************************************************/ | |
15 | |
16 module org.eclipse.jface.viewers.ViewerCell; | |
17 | |
18 import org.eclipse.swt.custom.StyleRange; | |
19 import org.eclipse.jface.viewers.ViewerRow; | |
20 | |
21 import org.eclipse.swt.graphics.Color; | |
22 import org.eclipse.swt.graphics.Font; | |
23 import org.eclipse.swt.graphics.Image; | |
24 import org.eclipse.swt.graphics.Rectangle; | |
25 import org.eclipse.swt.widgets.Control; | |
26 import org.eclipse.swt.widgets.Item; | |
27 import org.eclipse.swt.widgets.Widget; | |
28 | |
29 import java.lang.all; | |
30 import java.util.Set; | |
31 | |
32 /** | |
33 * The ViewerCell is the JFace representation of a cell entry in a ViewerRow. | |
34 * | |
35 * @since 3.3 | |
36 * | |
37 */ | |
38 public class ViewerCell { | |
39 private int columnIndex; | |
40 | |
41 private ViewerRow row; | |
42 | |
43 private Object element; | |
44 | |
45 /** | |
46 * Constant denoting the cell above current one (value is 1). | |
47 */ | |
48 public static const int ABOVE = 1; | |
49 | |
50 /** | |
51 * Constant denoting the cell below current one (value is 2). | |
52 */ | |
53 public static const int BELOW = 1 << 1; | |
54 | |
55 /** | |
56 * Constant denoting the cell to the left of the current one (value is 4). | |
57 */ | |
58 public static const int LEFT = 1 << 2; | |
59 | |
60 /** | |
61 * Constant denoting the cell to the right of the current one (value is 8). | |
62 */ | |
63 public static const int RIGHT = 1 << 3; | |
64 | |
65 | |
66 /** | |
67 * Create a new instance of the receiver on the row. | |
68 * | |
69 * @param row | |
70 * @param columnIndex | |
71 */ | |
72 this(ViewerRow row, int columnIndex, Object element) { | |
73 this.row = row; | |
74 this.columnIndex = columnIndex; | |
75 this.element = element; | |
76 } | |
77 | |
78 /** | |
79 * Get the index of the cell. | |
80 * | |
81 * @return the index | |
82 */ | |
83 public int getColumnIndex() { | |
84 return columnIndex; | |
85 } | |
86 | |
87 /** | |
88 * Get the bounds of the cell. | |
89 * | |
90 * @return {@link Rectangle} | |
91 */ | |
92 public Rectangle getBounds() { | |
93 return row.getBounds(columnIndex); | |
94 } | |
95 | |
96 /** | |
97 * Get the element this row represents. | |
98 * | |
99 * @return {@link Object} | |
100 */ | |
101 public Object getElement() { | |
102 if (element !is null) { | |
103 return element; | |
104 } | |
105 | |
106 if (row !is null) { | |
107 return row.getElement(); | |
108 } | |
109 | |
110 return null; | |
111 } | |
112 | |
113 /** | |
114 * Return the text for the cell. | |
115 * | |
116 * @return {@link String} | |
117 */ | |
118 public String getText() { | |
119 return row.getText(columnIndex); | |
120 } | |
121 | |
122 /** | |
123 * Return the Image for the cell. | |
124 * | |
125 * @return {@link Image} or <code>null</code> | |
126 */ | |
127 public Image getImage() { | |
128 return row.getImage(columnIndex); | |
129 } | |
130 | |
131 /** | |
132 * Set the background color of the cell. | |
133 * | |
134 * @param background | |
135 */ | |
136 public void setBackground(Color background) { | |
137 row.setBackground(columnIndex, background); | |
138 | |
139 } | |
140 | |
141 /** | |
142 * Set the foreground color of the cell. | |
143 * | |
144 * @param foreground | |
145 */ | |
146 public void setForeground(Color foreground) { | |
147 row.setForeground(columnIndex, foreground); | |
148 | |
149 } | |
150 | |
151 /** | |
152 * Set the font of the cell. | |
153 * | |
154 * @param font | |
155 */ | |
156 public void setFont(Font font) { | |
157 row.setFont(columnIndex, font); | |
158 | |
159 } | |
160 | |
161 /** | |
162 * Set the text for the cell. | |
163 * | |
164 * @param text | |
165 */ | |
166 public void setText(String text) { | |
167 row.setText(columnIndex, text); | |
168 | |
169 } | |
170 | |
171 /** | |
172 * Set the Image for the cell. | |
173 * | |
174 * @param image | |
175 */ | |
176 public void setImage(Image image) { | |
177 row.setImage(columnIndex, image); | |
178 | |
179 } | |
180 | |
181 /** | |
182 * Set the style ranges to be applied on the text label | |
183 * Note: Requires {@link StyledCellLabelProvider} with owner draw enabled. | |
184 * | |
185 * @param styleRanges the styled ranges | |
186 * | |
187 * @since 3.4 | |
188 */ | |
189 public void setStyleRanges(StyleRange[] styleRanges) { | |
190 row.setStyleRanges(columnIndex, styleRanges); | |
191 } | |
192 | |
193 | |
194 /** | |
195 * Returns the style ranges to be applied on the text label or <code>null</code> if no | |
196 * style ranges have been set. | |
197 * | |
198 * @return styleRanges the styled ranges | |
199 * | |
200 * @since 3.4 | |
201 */ | |
202 public StyleRange[] getStyleRanges() { | |
203 return row.getStyleRanges(columnIndex); | |
204 } | |
205 | |
206 /** | |
207 * Set the columnIndex. | |
208 * | |
209 * @param column | |
210 */ | |
211 void setColumn(int column) { | |
212 columnIndex = column; | |
213 | |
214 } | |
215 | |
216 /** | |
217 * Set the row to rowItem and the columnIndex to column. | |
218 * | |
219 * @param rowItem | |
220 * @param column | |
221 */ | |
222 void update(ViewerRow rowItem, int column, Object element) { | |
223 row = rowItem; | |
224 columnIndex = column; | |
225 this.element = element; | |
226 } | |
227 | |
228 /** | |
229 * Return the item for the receiver. | |
230 * | |
231 * @return {@link Item} | |
232 */ | |
233 public Widget getItem() { | |
234 return row.getItem(); | |
235 } | |
236 | |
237 /** | |
238 * Get the control for this cell. | |
239 * | |
240 * @return {@link Control} | |
241 */ | |
242 public Control getControl() { | |
243 return row.getControl(); | |
244 } | |
245 | |
246 /** | |
247 * Get the current index. This can be different from the original index when | |
248 * columns are reordered | |
249 * | |
250 * @return the current index (as shown in the UI) | |
251 * @since 3.4 | |
252 */ | |
253 public int getVisualIndex() { | |
254 return row.getVisualIndex_package(getColumnIndex()); | |
255 } | |
256 | |
257 /** | |
258 * Returns the specified neighbor of this cell, or <code>null</code> if no | |
259 * neighbor exists in the given direction. Direction constants can be | |
260 * combined by bitwise OR; for example, this method will return the cell to | |
261 * the upper-left of the current cell by passing {@link #ABOVE} | | |
262 * {@link #LEFT}. If <code>sameLevel</code> is <code>true</code>, only | |
263 * cells in sibling rows (under the same parent) will be considered. | |
264 * | |
265 * @param directionMask | |
266 * the direction mask used to identify the requested neighbor | |
267 * cell | |
268 * @param sameLevel | |
269 * if <code>true</code>, only consider cells from sibling rows | |
270 * @return the requested neighbor cell, or <code>null</code> if not found | |
271 */ | |
272 public ViewerCell getNeighbor(int directionMask, bool sameLevel) { | |
273 ViewerRow row; | |
274 | |
275 if ((directionMask & ABOVE) is ABOVE) { | |
276 row = this.row.getNeighbor(ViewerRow.ABOVE, sameLevel); | |
277 } else if ((directionMask & BELOW) is BELOW) { | |
278 row = this.row.getNeighbor(ViewerRow.BELOW, sameLevel); | |
279 } else { | |
280 row = this.row; | |
281 } | |
282 | |
283 if (row !is null) { | |
284 int columnIndex; | |
285 columnIndex = getVisualIndex(); | |
286 | |
287 int modifier = 0; | |
288 | |
289 if ((directionMask & LEFT) is LEFT) { | |
290 modifier = -1; | |
291 } else if ((directionMask & RIGHT) is RIGHT) { | |
292 modifier = 1; | |
293 } | |
294 | |
295 columnIndex += modifier; | |
296 | |
297 if (columnIndex >= 0 && columnIndex < row.getColumnCount()) { | |
298 ViewerCell cell = row.getCellAtVisualIndex(columnIndex); | |
299 if( cell !is null ) { | |
300 while( cell !is null && columnIndex < row.getColumnCount() - 1 && columnIndex > 0 ) { | |
301 if( cell.isVisible() ) { | |
302 break; | |
303 } | |
304 | |
305 columnIndex += modifier; | |
306 cell = row.getCellAtVisualIndex(columnIndex); | |
307 if( cell is null ) { | |
308 break; | |
309 } | |
310 } | |
311 } | |
312 | |
313 return cell; | |
314 } | |
315 } | |
316 | |
317 return null; | |
318 } | |
319 | |
320 /** | |
321 * @return the row | |
322 */ | |
323 public ViewerRow getViewerRow() { | |
324 return row; | |
325 } | |
326 | |
327 /** | |
328 * The location and bounds of the area where the text is drawn depends on | |
329 * various things (image displayed, control with SWT.CHECK) | |
330 * | |
331 * @return The bounds of the of the text area. May return <code>null</code> | |
332 * if the underlying widget implementation doesn't provide this | |
333 * information | |
334 * @since 3.4 | |
335 */ | |
336 public Rectangle getTextBounds() { | |
337 return row.getTextBounds(columnIndex); | |
338 } | |
339 | |
340 /** | |
341 * Returns the location and bounds of the area where the image is drawn | |
342 * | |
343 * @return The bounds of the of the image area. May return <code>null</code> | |
344 * if the underlying widget implementation doesn't provide this | |
345 * information | |
346 * @since 3.4 | |
347 */ | |
348 public Rectangle getImageBounds() { | |
349 return row.getImageBounds(columnIndex); | |
350 } | |
351 | |
352 /** | |
353 * Gets the foreground color of the cell. | |
354 * | |
355 * @return the foreground of the cell or <code>null</code> for the default foreground | |
356 * | |
357 * @since 3.4 | |
358 */ | |
359 public Color getForeground() { | |
360 return row.getForeground(columnIndex); | |
361 } | |
362 | |
363 /** | |
364 * Gets the background color of the cell. | |
365 * | |
366 * @return the background of the cell or <code>null</code> for the default background | |
367 * | |
368 * @since 3.4 | |
369 */ | |
370 public Color getBackground() { | |
371 return row.getBackground(columnIndex); | |
372 } | |
373 | |
374 /** | |
375 * Gets the font of the cell. | |
376 * | |
377 * @return the font of the cell or <code>null</code> for the default font | |
378 * | |
379 * @since 3.4 | |
380 */ | |
381 public Font getFont() { | |
382 return row.getFont(columnIndex); | |
383 } | |
384 | |
385 /* | |
386 * (non-Javadoc) | |
387 * | |
388 * @see java.lang.Object#hashCode() | |
389 */ | |
390 public override hash_t toHash() { | |
391 const int prime = 31; | |
392 int result = 1; | |
393 result = prime * result + columnIndex; | |
394 result = prime * result + ((row is null) ? 0 : row.toHash()); | |
395 return result; | |
396 } | |
397 | |
398 /* | |
399 * (non-Javadoc) | |
400 * | |
401 * @see java.lang.Object#equals(java.lang.Object) | |
402 */ | |
403 public override int opEquals(Object obj) { | |
404 if (this is obj) | |
405 return true; | |
406 if (obj is null) | |
407 return false; | |
408 if (this.classinfo !is obj.classinfo ) | |
409 return false; | |
410 ViewerCell other = cast(ViewerCell) obj; | |
411 if (columnIndex !is other.columnIndex) | |
412 return false; | |
413 if (row is null) { | |
414 if (other.row !is null) | |
415 return false; | |
416 } else if (!row.opEquals(other.row)) | |
417 return false; | |
418 return true; | |
419 } | |
420 | |
421 private int getWidth() { | |
422 return row.getWidth(columnIndex); | |
423 } | |
424 | |
425 private bool isVisible() { | |
426 return getWidth() > 0; | |
427 } | |
428 } |