Mercurial > projects > dwt-samples
comparison jface/snippets/viewers/Snippet006TableMultiLineCells.d @ 145:161f7698cfb8
Moved jface snippets to viewers subpackage, there are more with conflicting numbers for other packages
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 08 Aug 2008 14:40:21 +0200 |
parents | jface/snippets/Snippet006TableMultiLineCells.d@9ed020f0c2a5 |
children |
comparison
equal
deleted
inserted
replaced
144:7248e4c09c4f | 145:161f7698cfb8 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2005, 2007 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 * Port to the D programming language: | |
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) | |
12 *******************************************************************************/ | |
13 module snippets.viewers.Snippet006TableMultiLineCells; | |
14 | |
15 | |
16 // http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet006TableMultiLineCells.java?view=markup | |
17 | |
18 import dwtx.jface.resource.JFaceResources; | |
19 import dwtx.jface.viewers.ColumnPixelData; | |
20 import dwtx.jface.viewers.IStructuredContentProvider; | |
21 import dwtx.jface.viewers.OwnerDrawLabelProvider; | |
22 import dwtx.jface.viewers.StructuredSelection; | |
23 import dwtx.jface.viewers.TableLayout; | |
24 import dwtx.jface.viewers.Viewer; | |
25 import dwtx.jface.viewers.TableViewer; | |
26 import dwt.DWT; | |
27 import dwt.graphics.Font; | |
28 import dwt.graphics.Point; | |
29 import dwt.layout.GridData; | |
30 import dwt.layout.GridLayout; | |
31 import dwt.widgets.Composite; | |
32 import dwt.widgets.Display; | |
33 import dwt.widgets.Event; | |
34 import dwt.widgets.Shell; | |
35 import dwt.widgets.TableColumn; | |
36 | |
37 import dwt.dwthelper.utils; | |
38 version(JIVE){ | |
39 import jive.stacktrace; | |
40 } | |
41 | |
42 void main(String[] args) { | |
43 (new Snippet006TableMultiLineCells()).main(args); | |
44 } | |
45 | |
46 public class Snippet006TableMultiLineCells { | |
47 | |
48 public static void main(String[] args) { | |
49 | |
50 Display display = new Display(); | |
51 Shell shell = new Shell(display, DWT.CLOSE|DWT.RESIZE); | |
52 shell.setSize(400, 400); | |
53 shell.setLayout(new GridLayout()); | |
54 | |
55 Snippet006TableMultiLineCells example = new Snippet006TableMultiLineCells(); | |
56 example.createPartControl(shell); | |
57 | |
58 shell.open(); | |
59 | |
60 while (!shell.isDisposed()) { | |
61 if (!display.readAndDispatch()) | |
62 display.sleep(); | |
63 } | |
64 display.dispose(); | |
65 } | |
66 | |
67 class LineEntry { | |
68 | |
69 String line; | |
70 int columnWidth; | |
71 | |
72 /** | |
73 * Create a new instance of the receiver with name text constrained to a | |
74 * column of width. | |
75 * | |
76 * @param text | |
77 * @param width | |
78 */ | |
79 this(String text, int width) { | |
80 line = text; | |
81 columnWidth = width; | |
82 } | |
83 | |
84 /** | |
85 * Get the height of the event. | |
86 * | |
87 * @param index | |
88 * @return int | |
89 */ | |
90 public int getHeight(Event event) { | |
91 event.gc.setLineWidth(columnWidth); | |
92 return event.gc.textExtent(line).y; | |
93 } | |
94 | |
95 /** | |
96 * Get the width of the event. | |
97 * | |
98 * @param index | |
99 * @return | |
100 */ | |
101 public int getWidth(Event event) { | |
102 | |
103 return columnWidth; | |
104 } | |
105 | |
106 /** | |
107 * Get the font we are using. | |
108 * | |
109 * @return Font | |
110 */ | |
111 protected Font getFont() { | |
112 return JFaceResources.getFont(JFaceResources.HEADER_FONT); | |
113 } | |
114 | |
115 /** | |
116 * @param event | |
117 */ | |
118 public void draw(Event event) { | |
119 event.gc.drawText(line, event.x, event.y); | |
120 | |
121 } | |
122 } | |
123 | |
124 private TableViewer viewer; | |
125 | |
126 private LineEntry[] entries; | |
127 | |
128 public this() { | |
129 String[] lines = [ | |
130 "This day is called the feast of Crispian:", | |
131 "He that outlives this day, \n and comes safe home,", | |
132 "Will stand a tip-toe when the day is named,", | |
133 "And rouse him at the name of Crispian.", | |
134 "He that shall live this day,\n and see old age,", | |
135 "Will yearly on the vigil feast his neighbours,", | |
136 "And say 'To-morrow is Saint Crispian:'", | |
137 "Then will he strip his sleeve and show his scars.", | |
138 "And say 'These wounds I had on Crispin's day.'", | |
139 "Old men forget:\n yet all shall be forgot,", | |
140 "But he'll remember with advantages", | |
141 "What feats he did that day:\n then shall our names.", | |
142 "Familiar in his mouth as household words", | |
143 "Harry the king, Bedford and Exeter,", | |
144 "Warwick and Talbot,\n Salisbury and Gloucester,", | |
145 "Be in their flowing cups freshly remember'd.", | |
146 "This story shall the good man teach his son;", | |
147 "And Crispin Crispian shall ne'er go by,", | |
148 "From this day to the ending of the world,", | |
149 "But we in it shall be remember'd;", | |
150 "We few,\n we happy few,\n we band of brothers;", | |
151 "For he to-day that sheds his blood with me", | |
152 "Shall be my brother;\n be he ne'er so vile,", | |
153 "This day shall gentle his condition:", | |
154 "And gentlemen in England now a-bed", | |
155 "Shall think themselves accursed they were not here,", | |
156 "And hold their manhoods cheap whiles any speaks", | |
157 "That fought with us upon Saint Crispin's day." ]; | |
158 | |
159 entries = new LineEntry[lines.length]; | |
160 for (int i = 0; i < lines.length; i++) { | |
161 entries[i] = new LineEntry(lines[i], 35); | |
162 } | |
163 } | |
164 | |
165 /* | |
166 * (non-Javadoc) | |
167 * | |
168 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(dwt.widgets.Composite) | |
169 */ | |
170 public void createPartControl(Composite parent) { | |
171 viewer = new TableViewer(parent, DWT.FULL_SELECTION); | |
172 | |
173 viewer.setContentProvider(new class() IStructuredContentProvider { | |
174 /* | |
175 * (non-Javadoc) | |
176 * | |
177 * @see dwtx.jface.viewers.IContentProvider#dispose() | |
178 */ | |
179 public void dispose() { | |
180 } | |
181 | |
182 /* | |
183 * (non-Javadoc) | |
184 * | |
185 * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) | |
186 */ | |
187 public Object[] getElements(Object inputElement) { | |
188 return entries; | |
189 } | |
190 | |
191 /* | |
192 * (non-Javadoc) | |
193 * | |
194 * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer, | |
195 * java.lang.Object, java.lang.Object) | |
196 */ | |
197 public void inputChanged(dwtx.jface.viewers.Viewer.Viewer viewer, Object oldInput, Object newInput) { | |
198 } | |
199 }); | |
200 createColumns(); | |
201 | |
202 viewer.setLabelProvider(new class OwnerDrawLabelProvider { | |
203 /* (non-Javadoc) | |
204 * @see dwtx.jface.viewers.OwnerDrawLabelProvider#measure(dwt.widgets.Event, java.lang.Object) | |
205 */ | |
206 protected void measure(Event event, Object element) { | |
207 LineEntry line = cast(LineEntry) element; | |
208 Point size = event.gc.textExtent(line.line); | |
209 event.width = viewer.getTable().getColumn(event.index).getWidth(); | |
210 int lines = (event.width > 0 ? (size.x / event.width + 1) : 1); | |
211 event.height = size.y * lines; | |
212 } | |
213 | |
214 /* | |
215 * (non-Javadoc) | |
216 * | |
217 * @see dwtx.jface.viewers.OwnerDrawLabelProvider#paint(dwt.widgets.Event, | |
218 * java.lang.Object) | |
219 */ | |
220 protected void paint(Event event, Object element) { | |
221 | |
222 LineEntry entry = cast(LineEntry) element; | |
223 event.gc.drawText(entry.line, event.x, event.y, true); | |
224 } | |
225 }); | |
226 viewer.setInput(this); | |
227 | |
228 GridData data = new GridData(GridData.GRAB_HORIZONTAL | |
229 | GridData.GRAB_VERTICAL | GridData.FILL_BOTH); | |
230 | |
231 viewer.getControl().setLayoutData(data); | |
232 OwnerDrawLabelProvider.setUpOwnerDraw(viewer); | |
233 | |
234 viewer.setSelection(new StructuredSelection(entries[1])); | |
235 } | |
236 | |
237 /** | |
238 * Create the columns to be used in the tree. | |
239 */ | |
240 private void createColumns() { | |
241 TableLayout layout = new TableLayout(); | |
242 viewer.getTable().setLayout(layout); | |
243 viewer.getTable().setHeaderVisible(true); | |
244 viewer.getTable().setLinesVisible(true); | |
245 | |
246 TableColumn tc = new TableColumn(viewer.getTable(), DWT.NONE, 0); | |
247 layout.addColumnData(new ColumnPixelData(350)); | |
248 tc.setText("Lines"); | |
249 | |
250 } | |
251 | |
252 public void setFocus() { | |
253 } | |
254 } | |
255 |