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