comparison jface/snippets/window/Snippet031TableStaticTooltip.d @ 146:7c4b76583cb8

Added new JFace snippets
author Frank Benoit <benoit@tionex.de>
date Mon, 11 Aug 2008 11:17:37 +0200
parents
children
comparison
equal deleted inserted replaced
145:161f7698cfb8 146:7c4b76583cb8
1 /*******************************************************************************
2 * Copyright (c) 2006 Tom Schindl 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 * Tom Schindl - initial API and implementation
10 *******************************************************************************/
11
12 module dwtx.jface.snippets.window.Snippet031TableStaticTooltip;
13
14 import dwtx.jface.viewers.IStructuredContentProvider;
15 import dwtx.jface.viewers.ITableLabelProvider;
16 import dwtx.jface.viewers.LabelProvider;
17 import dwtx.jface.viewers.TableViewer;
18 import dwtx.jface.viewers.Viewer;
19 import dwtx.jface.window.DefaultToolTip;
20 import dwtx.jface.window.ToolTip;
21 import dwt.DWT;
22 import dwt.graphics.Color;
23 import dwt.graphics.GC;
24 import dwt.graphics.Image;
25 import dwt.graphics.Point;
26 import dwt.layout.FillLayout;
27 import dwt.widgets.Display;
28 import dwt.widgets.Shell;
29 import dwt.widgets.TableColumn;
30
31 import dwt.dwthelper.utils;
32 import tango.text.convert.Format;
33
34
35 /**
36 * Example how one can create a tooltip which is not recreated for every table
37 * cell
38 *
39 * @author Tom Schindl <tom.schindl@bestsolution.at>
40 *
41 */
42 public class Snippet031TableStaticTooltip {
43 private static Image[] images;
44
45 private class MyContentProvider : IStructuredContentProvider {
46
47 /*
48 * (non-Javadoc)
49 *
50 * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
51 */
52 public Object[] getElements(Object inputElement) {
53 return arrayFromObject!(MyModel)( inputElement );
54 }
55
56 /*
57 * (non-Javadoc)
58 *
59 * @see dwtx.jface.viewers.IContentProvider#dispose()
60 */
61 public void dispose() {
62
63 }
64
65 /*
66 * (non-Javadoc)
67 *
68 * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer,
69 * java.lang.Object, java.lang.Object)
70 */
71 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
72
73 }
74
75 }
76
77 public class MyModel {
78 public int counter;
79
80 public this(int counter) {
81 this.counter = counter;
82 }
83
84 public String toString() {
85 return Format("Item {}", this.counter );
86 }
87 }
88
89 public class MyLabelProvider : LabelProvider,
90 ITableLabelProvider {
91
92 public Image getColumnImage(Object element, int columnIndex) {
93 if (columnIndex == 1) {
94 return images[(cast(MyModel) element).counter % 4];
95 }
96
97 return null;
98 }
99
100 public String getColumnText(Object element, int columnIndex) {
101 return Format("Column {} => {}", columnIndex, element.toString());
102 }
103
104 }
105
106 private static Image createImage(Display display, int red, int green,
107 int blue) {
108 Color color = new Color(display, red, green, blue);
109 Image image = new Image(display, 10, 10);
110 GC gc = new GC(image);
111 gc.setBackground(color);
112 gc.fillRectangle(0, 0, 10, 10);
113 gc.dispose();
114
115 return image;
116 }
117
118 TableViewer v;
119 public this(Shell shell) {
120 v = new TableViewer(shell, DWT.BORDER
121 | DWT.FULL_SELECTION);
122 v.setLabelProvider(new MyLabelProvider());
123 v.setContentProvider(new MyContentProvider());
124
125 TableColumn column = new TableColumn(v.getTable(), DWT.NONE);
126 column.setWidth(200);
127 column.setText("Column 1");
128
129 column = new TableColumn(v.getTable(), DWT.NONE);
130 column.setWidth(200);
131 column.setText("Column 2");
132
133 MyModel[] model = createModel();
134 v.setInput(new ArrayWrapperObject(arraycast!(Object)(model)));
135 v.getTable().setLinesVisible(true);
136 v.getTable().setHeaderVisible(true);
137
138 DefaultToolTip toolTip = new DefaultToolTip(v.getControl(),
139 ToolTip.NO_RECREATE, false);
140 toolTip.setText("Hello World\nHello World");
141 toolTip.setBackgroundColor(v.getTable().getDisplay().getSystemColor(
142 DWT.COLOR_RED));
143 toolTip.setShift(new Point(10, 5));
144 }
145
146 private MyModel[] createModel() {
147 MyModel[] elements = new MyModel[10];
148
149 for (int i = 0; i < 10; i++) {
150 elements[i] = new MyModel(i);
151 }
152
153 return elements;
154 }
155
156 /**
157 * @param args
158 */
159 public static void main(String[] args) {
160 Display display = new Display();
161
162 images = new Image[4];
163 images[0] = createImage(display, 0, 0, 255);
164 images[1] = createImage(display, 0, 255, 255);
165 images[2] = createImage(display, 0, 255, 0);
166 images[3] = createImage(display, 255, 0, 255);
167
168 Shell shell = new Shell(display);
169 shell.setLayout(new FillLayout());
170 new Snippet031TableStaticTooltip(shell);
171 shell.open();
172
173 while (!shell.isDisposed()) {
174 if (!display.readAndDispatch())
175 display.sleep();
176 }
177
178 for (int i = 0; i < images.length; i++) {
179 images[i].dispose();
180 }
181
182 display.dispose();
183
184 }
185
186 }
187
188 void main(){
189 Snippet031TableStaticTooltip.main(null);
190 }