comparison org.eclipse.jface.snippets/EclipseJfaceSnippets/org/eclipse/jface/snippets/viewers/Snippet031TableViewerCustomTooltipsMultiSelection.d @ 31:5d87d4191adf

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