31
|
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 *******************************************************************************/
|
32
|
13 module org.eclipse.jface.snippets.viewers.Snippet006TableMultiLineCells;
|
31
|
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
|
32
|
18 import org.eclipse.jface.resource.JFaceResources;
|
|
19 import org.eclipse.jface.viewers.ColumnPixelData;
|
|
20 import org.eclipse.jface.viewers.IStructuredContentProvider;
|
|
21 import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
|
|
22 import org.eclipse.jface.viewers.StructuredSelection;
|
|
23 import org.eclipse.jface.viewers.TableLayout;
|
|
24 import org.eclipse.jface.viewers.Viewer;
|
|
25 import org.eclipse.jface.viewers.TableViewer;
|
|
26 import org.eclipse.swt.SWT;
|
|
27 import org.eclipse.swt.graphics.Font;
|
|
28 import org.eclipse.swt.graphics.Point;
|
|
29 import org.eclipse.swt.layout.GridData;
|
|
30 import org.eclipse.swt.layout.GridLayout;
|
|
31 import org.eclipse.swt.widgets.Composite;
|
|
32 import org.eclipse.swt.widgets.Display;
|
|
33 import org.eclipse.swt.widgets.Event;
|
|
34 import org.eclipse.swt.widgets.Shell;
|
|
35 import org.eclipse.swt.widgets.TableColumn;
|
31
|
36
|
32
|
37 import java.lang.all;
|
31
|
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();
|
32
|
51 Shell shell = new Shell(display, SWT.CLOSE|SWT.RESIZE);
|
31
|
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 *
|
32
|
168 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
|
31
|
169 */
|
|
170 public void createPartControl(Composite parent) {
|
32
|
171 viewer = new TableViewer(parent, SWT.FULL_SELECTION);
|
31
|
172
|
|
173 viewer.setContentProvider(new class() IStructuredContentProvider {
|
|
174 /*
|
|
175 * (non-Javadoc)
|
|
176 *
|
32
|
177 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
|
31
|
178 */
|
|
179 public void dispose() {
|
|
180 }
|
|
181
|
|
182 /*
|
|
183 * (non-Javadoc)
|
|
184 *
|
32
|
185 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
|
31
|
186 */
|
|
187 public Object[] getElements(Object inputElement) {
|
|
188 return entries;
|
|
189 }
|
|
190
|
|
191 /*
|
|
192 * (non-Javadoc)
|
|
193 *
|
32
|
194 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
|
31
|
195 * java.lang.Object, java.lang.Object)
|
|
196 */
|
32
|
197 public void inputChanged(org.eclipse.jface.viewers.Viewer.Viewer viewer, Object oldInput, Object newInput) {
|
31
|
198 }
|
|
199 });
|
|
200 createColumns();
|
|
201
|
|
202 viewer.setLabelProvider(new class OwnerDrawLabelProvider {
|
|
203 /* (non-Javadoc)
|
32
|
204 * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#measure(org.eclipse.swt.widgets.Event, java.lang.Object)
|
31
|
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 *
|
32
|
217 * @see org.eclipse.jface.viewers.OwnerDrawLabelProvider#paint(org.eclipse.swt.widgets.Event,
|
31
|
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
|
32
|
246 TableColumn tc = new TableColumn(viewer.getTable(), SWT.NONE, 0);
|
31
|
247 layout.addColumnData(new ColumnPixelData(350));
|
|
248 tc.setText("Lines");
|
|
249
|
|
250 }
|
|
251
|
|
252 public void setFocus() {
|
|
253 }
|
|
254 }
|
|
255
|