comparison jface/snippets/Snippet031TableViewerCustomTooltipsMultiSelection.d @ 92:f5c5b4bf7971

added jface.snippets.Snippet031TableViewerCustomTooltipsMultiSelection
author yidabu <yidabu@gmail.com>
date Fri, 23 May 2008 07:53:22 +0800
parents
children bf8adb3de664
comparison
equal deleted inserted replaced
91:961ca8a76cad 92:f5c5b4bf7971
1 module jface.snippets.Snippet031TableViewerCustomTooltipsMultiSelection;
2
3 /*******************************************************************************
4 * Copyright (c) 2007 Adam Neal and others.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * Adam Neal - initial API and implementation
12 * Port to the D programming language:
13 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ )
14 *******************************************************************************/
15
16
17 // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet031TableViewerCustomTooltipsMultiSelection.java?view=markup
18
19 import tango.util.collection.ArrayBag;
20 alias ArrayBag!(MyModel) ArrayList;
21 import tango.util.Convert;
22
23 import dwtx.jface.viewers.ArrayContentProvider;
24 import dwtx.jface.viewers.ILabelProviderListener;
25 import dwtx.jface.viewers.ITableLabelProvider;
26 import dwtx.jface.viewers.TableViewer;
27 import dwt.DWT;
28 import dwt.graphics.Image;
29 import dwt.graphics.Point;
30 import dwt.graphics.Rectangle;
31 import dwt.layout.FillLayout;
32 import dwt.widgets.Display;
33 import dwt.widgets.Event;
34 import dwt.widgets.Label;
35 import dwt.widgets.Listener;
36 import dwt.widgets.Shell;
37 import dwt.widgets.Table;
38 import dwt.widgets.TableColumn;
39 import dwt.widgets.TableItem;
40 import dwt.dwthelper.System;
41 import dwt.widgets.Listener;
42
43 alias char[] String;
44 void main(String[] args)
45 {
46 Snippet031TableViewerCustomTooltipsMultiSelection.main(args);
47 }
48
49 /**
50 * A simple TableViewer to demonstrate how custom tooltips could be created easily while preserving
51 * the multiple selection.
52 *
53 * This is a modified example taken from Tom Schindl's Snippet023TreeViewerCustomTooltips.java
54 *
55 * This code is for users pre 3.3 others could use newly added tooltip support in {@link CellLabelProvider}
56
57 * @author Adam Neal <Adam_Neal@ca.ibm.com>
58 *
59 */
60 public class Snippet031TableViewerCustomTooltipsMultiSelection {
61 public class MyLableProvider : ITableLabelProvider {
62
63 public Image getColumnImage(Object element, int columnIndex) {
64 return null;
65 }
66
67 public String getColumnText(Object element, int columnIndex) {
68 //if (element instanceof MyModel) {
69 switch (columnIndex) {
70 case 0: return (cast(MyModel)element).col1;
71 case 1: return (cast(MyModel)element).col2;
72 }
73 //}
74
75 return "";
76 }
77
78 public void addListener(ILabelProviderListener listener) {
79 /* Ignore */
80 }
81
82 public void dispose() {
83 /* Ignore */
84 }
85
86 public bool isLabelProperty(Object element, String property) {
87 return false;
88 }
89
90 public void removeListener(ILabelProviderListener listener) {
91 /* Ignore */
92 }
93
94 }
95
96
97
98 public this(Shell shell) {
99
100
101 final Table table = new Table(shell, DWT.H_SCROLL | DWT.V_SCROLL | DWT.MULTI | DWT.FULL_SELECTION);
102 table.setHeaderVisible(true);
103 table.setLinesVisible(true);
104
105 final TableViewer v = new TableViewer(table);
106 TableColumn tableColumn1 = new TableColumn(table, DWT.NONE);
107 TableColumn tableColumn2 = new TableColumn(table, DWT.NONE);
108
109 String column1 = "Column 1", column2 = "Column 2";
110 /* Setup the table columns */
111 tableColumn1.setText(column1);
112 tableColumn2.setText(column2);
113 tableColumn1.pack();
114 tableColumn2.pack();
115
116 v.setColumnProperties([ column1, column2 ]);
117 v.setLabelProvider(new MyLableProvider());
118 v.setContentProvider(new ArrayContentProvider!(MyModel));
119 v.setInput(createModel());
120
121 tooltipLabelListener = new TooltipLabelListener();
122
123 /**
124 * The listener that gets added to the table. This listener is responsible for creating the tooltips
125 * when hovering over a cell item. This listener will listen for the following events:
126 * <li>DWT.KeyDown - to remove the tooltip</li>
127 * <li>DWT.Dispose - to remove the tooltip</li>
128 * <li>DWT.MouseMove - to remove the tooltip</li>
129 * <li>DWT.MouseHover - to set the tooltip</li>
130 */
131
132 Listener tableListener = dgListener(&handleTableListener, table);
133
134 table.addListener (DWT.Dispose, tableListener);
135 table.addListener (DWT.KeyDown, tableListener);
136 table.addListener (DWT.MouseMove, tableListener);
137 table.addListener (DWT.MouseHover, tableListener);
138 }
139
140 void handleTableListener(Event event, Table table)
141 {
142 Shell tooltip = null;
143 Label label = null;
144
145 /*
146 * (non-Javadoc)
147 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
148 */
149 switch (event.type) {
150 case DWT.KeyDown:
151 case DWT.Dispose:
152 case DWT.MouseMove: {
153 if (tooltip is null) break;
154 tooltip.dispose ();
155 tooltip = null;
156 label = null;
157 break;
158 }
159 case DWT.MouseHover: {
160 Point coords = new Point(event.x, event.y);
161 TableItem item = table.getItem(coords);
162 if (item !is null) {
163 int columnCount = table.getColumnCount();
164 for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
165 if (item.getBounds(columnIndex).contains(coords)) {
166 /* Dispose of the old tooltip (if one exists */
167 if (tooltip !is null && !tooltip.isDisposed ()) tooltip.dispose ();
168
169 /* Create a new Tooltip */
170 tooltip = new Shell (table.getShell(), DWT.ON_TOP | DWT.NO_FOCUS | DWT.TOOL);
171 tooltip.setBackground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND));
172 FillLayout layout = new FillLayout ();
173 layout.marginWidth = 2;
174 tooltip.setLayout (layout);
175 label = new Label (tooltip, DWT.NONE);
176 label.setForeground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_FOREGROUND));
177 label.setBackground (table.getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND));
178
179 /* Store the TableItem with the label so we can pass the mouse event later */
180 label.setData ("_TableItem_", item);
181
182 /* Set the tooltip text */
183 label.setText("Tooltip: " ~ to!(char[])(item.getData()) ~ " : " ~ to!(char[])(columnIndex));
184
185 /* Setup Listeners to remove the tooltip and transfer the received mouse events */
186 label.addListener (DWT.MouseExit, tooltipLabelListener);
187 label.addListener (DWT.MouseDown, tooltipLabelListener);
188
189 /* Set the size and position of the tooltip */
190 Point size = tooltip.computeSize (DWT.DEFAULT, DWT.DEFAULT);
191 Rectangle rect = item.getBounds (columnIndex);
192 Point pt = table.toDisplay (rect.x, rect.y);
193 tooltip.setBounds (pt.x, pt.y, size.x, size.y);
194
195 /* Show it */
196 tooltip.setVisible (true);
197 break;
198 }
199 }
200 }
201 }
202 }
203
204 }
205
206 /**
207 * This listener is added to the tooltip so that it can either dispose itself if the mouse
208 * exits the tooltip or so it can pass the selection event through to the table.
209 */
210 final TooltipLabelListener tooltipLabelListener;
211 final class TooltipLabelListener : Listener {
212 private bool isCTRLDown(Event e) {
213 return (e.stateMask & DWT.CTRL) != 0;
214 }
215 /*
216 * (non-Javadoc)
217 * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
218 */
219 public void handleEvent (Event event) {
220 Label label = cast(Label)event.widget;
221 Shell shell = label.getShell ();
222 switch (event.type) {
223 case DWT.MouseDown: /* Handle a user Click */
224 /* Extract our Data */
225 Event e = new Event ();
226 e.item = cast(TableItem) label.getData ("_TableItem_");
227 Table table = (cast(TableItem) e.item).getParent();
228
229 /* Construct the new Selection[] to show */
230 TableItem [] newSelection = null;
231 if (isCTRLDown(event)) {
232 /* We have 2 scenario's.
233 * 1) We are selecting an already selected element - so remove it from the selected indices
234 * 2) We are selecting a non-selected element - so add it to the selected indices
235 */
236 TableItem[] sel = table.getSelection();
237 for (int i = 0; i < sel.length; ++i) {
238 //if (e.item.equals(sel[i])) {
239 if (e.item is sel[i]) {
240 // We are de-selecting this element
241 newSelection = new TableItem[sel.length - 1];
242 System.arraycopy(sel, 0, newSelection, 0, i);
243 System.arraycopy(sel, i+1, newSelection, i, sel.length - i - 1);
244 break;
245 }
246 }
247
248 /*
249 * If we haven't created the newSelection[] yet, than we are adding the newly selected element
250 * into the list of selected indicies
251 */
252 if (newSelection is null) {
253 newSelection = new TableItem[sel.length + 1];
254 System.arraycopy(sel, 0, newSelection, 0, sel.length);
255 newSelection[sel.length] = cast(TableItem) e.item;
256 }
257
258 } else {
259 /* CTRL is not down, so we simply select the single element */
260 newSelection = [ cast(TableItem) e.item ];
261 }
262 /* Set the new selection of the table and notify the listeners */
263 table.setSelection (newSelection);
264 table.notifyListeners (DWT.Selection, e);
265
266 /* Remove the Tooltip */
267 shell.dispose ();
268 table.setFocus();
269 break;
270 case DWT.MouseExit:
271 shell.dispose ();
272 break;
273 }
274 }}
275
276
277 private ArrayList createModel() {
278 ArrayList list = new ArrayList;
279 list.add(new MyModel("A", "B"));
280 list.add(new MyModel("C", "D"));
281 list.add(new MyModel("E", "F"));
282 return list;
283 }
284
285 public static void main(String[] args) {
286 Display display = new Display();
287 Shell shell = new Shell(display);
288 shell.setLayout(new FillLayout());
289 new Snippet031TableViewerCustomTooltipsMultiSelection(shell);
290 shell.open();
291
292 while (!shell.isDisposed()) {
293 if (!display.readAndDispatch())
294 display.sleep();
295 }
296
297 display.dispose();
298 }
299 }
300
301 public class MyModel {
302 public String col1, col2;
303
304 public this(String c1, String c2) {
305 col1 = c1;
306 col2 = c2;
307 }
308
309 public String toString() {
310 return col1 ~ col2;
311 }
312
313 }