Mercurial > projects > dwt-samples
comparison jface/snippets/viewers/Snippet054NativeControlsInViewers.d @ 179:89de7ff0752c default tip
Add JFace snippet Snippet054NativeControlsInViewers, thanks to WasserDragoon
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 29 Apr 2009 11:01:41 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
178:61dd51f8d45f | 179:89de7ff0752c |
---|---|
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 jface.snippets.viewers.Snippet054NativeControlsInViewers; | |
13 | |
14 import dwtx.jface.viewers.CellLabelProvider; | |
15 import dwtx.jface.viewers.ColumnLabelProvider; | |
16 import dwtx.jface.viewers.IStructuredContentProvider; | |
17 import dwtx.jface.viewers.TableViewer; | |
18 import dwtx.jface.viewers.TableViewerColumn; | |
19 import dwtx.jface.viewers.Viewer; | |
20 import dwtx.jface.viewers.ViewerCell; | |
21 import dwt.DWT; | |
22 import dwt.custom.TableEditor; | |
23 import dwt.dwthelper.utils; | |
24 import dwt.events.DisposeEvent; | |
25 import dwt.events.DisposeListener; | |
26 import dwt.events.SelectionAdapter; | |
27 import dwt.events.SelectionEvent; | |
28 import dwt.events.SelectionListener; | |
29 import dwt.layout.FillLayout; | |
30 import dwt.layout.RowLayout; | |
31 import dwt.widgets.Button; | |
32 import dwt.widgets.Composite; | |
33 import dwt.widgets.Display; | |
34 import dwt.widgets.Shell; | |
35 import dwt.widgets.TableItem; | |
36 | |
37 import tango.util.Convert; | |
38 import tango.util.collection.ArraySeq; | |
39 | |
40 /** | |
41 * Example how to place native controls into a viewer with the new JFace-API | |
42 * because has the potential to eat up all your handles you should think about | |
43 * alternate approaches e.g. takeing a screenshot of the control | |
44 * | |
45 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
46 * | |
47 */ | |
48 public class Snippet054NativeControlsInViewers { | |
49 | |
50 private class MyContentProvider: IStructuredContentProvider { | |
51 | |
52 /* | |
53 * (non-Javadoc) | |
54 * | |
55 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) | |
56 */ | |
57 public Object[] getElements(Object inputElement) { | |
58 return (cast(ArraySeq!(MyModel))inputElement).toArray; | |
59 } | |
60 | |
61 /* | |
62 * (non-Javadoc) | |
63 * | |
64 * @see org.eclipse.jface.viewers.IContentProvider#dispose() | |
65 */ | |
66 public void dispose() { | |
67 | |
68 } | |
69 | |
70 /* | |
71 * (non-Javadoc) | |
72 * | |
73 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, | |
74 * java.lang.Object, java.lang.Object) | |
75 */ | |
76 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
77 | |
78 } | |
79 | |
80 } | |
81 | |
82 public class MyModel { | |
83 public int counter; | |
84 | |
85 public this(int counter) { | |
86 this.counter = counter; | |
87 } | |
88 | |
89 public String toString() { | |
90 return "Item " ~ to!(char[])(this.counter); | |
91 } | |
92 } | |
93 | |
94 public this(Shell shell) { | |
95 final TableViewer v = new TableViewer(shell, DWT.BORDER | |
96 | DWT.FULL_SELECTION); | |
97 v.setContentProvider(new MyContentProvider()); | |
98 | |
99 TableViewerColumn column = new TableViewerColumn(v, DWT.NONE); | |
100 column.getColumn().setWidth(200); | |
101 column.getColumn().setText("Column 1"); | |
102 column.setLabelProvider(new class() ColumnLabelProvider { | |
103 | |
104 public String getText(Object element) { | |
105 return element.toString(); | |
106 } | |
107 | |
108 }); | |
109 | |
110 column = new TableViewerColumn(v, DWT.NONE); | |
111 column.getColumn().setWidth(200); | |
112 column.getColumn().setText("Column 2"); | |
113 column.setLabelProvider(new class() CellLabelProvider { | |
114 | |
115 public void update(ViewerCell cell) { | |
116 final TableItem item = cast(TableItem) cell.getItem(); | |
117 DisposeListener listener = new class(item) DisposeListener { | |
118 | |
119 private TableItem item; | |
120 | |
121 public this(TableItem item) { | |
122 this.item = item; | |
123 } | |
124 | |
125 public void widgetDisposed(DisposeEvent e) { | |
126 if( item.getData("EDITOR") !is null ) { | |
127 TableEditor editor = cast(TableEditor) item.getData("EDITOR"); | |
128 editor.getEditor().dispose(); | |
129 editor.dispose(); | |
130 } | |
131 } | |
132 | |
133 }; | |
134 | |
135 if (item.getData("EDITOR") !is null) { | |
136 TableEditor editor = cast(TableEditor) item.getData("EDITOR"); | |
137 editor.getEditor().dispose(); | |
138 editor.dispose(); | |
139 } | |
140 | |
141 if( item.getData("DISPOSELISTNER") !is null ) { | |
142 item.removeDisposeListener(cast(DisposeListener) item.getData("DISPOSELISTNER")); | |
143 } | |
144 | |
145 TableEditor editor = new TableEditor(item.getParent()); | |
146 item.setData("EDITOR", editor); | |
147 Composite comp = new Composite(item.getParent(), DWT.NONE); | |
148 comp.setBackground(item.getParent().getBackground()); | |
149 comp.setBackgroundMode(DWT.INHERIT_DEFAULT); | |
150 RowLayout l = new RowLayout(); | |
151 l.marginHeight = 0; | |
152 l.marginWidth = 0; | |
153 l.marginTop = 0; | |
154 l.marginBottom = 0; | |
155 comp.setLayout(l); | |
156 Button rad = new Button(comp, DWT.RADIO); | |
157 Button rad1 = new Button(comp, DWT.RADIO); | |
158 Button rad2 = new Button(comp, DWT.RADIO); | |
159 | |
160 editor.grabHorizontal = true; | |
161 editor.setEditor(comp, item, 1); | |
162 | |
163 item.addDisposeListener(listener); | |
164 item.setData("DISPOSELISTNER",cast(Object)listener); | |
165 } | |
166 | |
167 }); | |
168 | |
169 ArraySeq!(MyModel) model = createModel(10); | |
170 v.setInput(model); | |
171 v.getTable().setLinesVisible(true); | |
172 v.getTable().setHeaderVisible(true); | |
173 } | |
174 | |
175 private ArraySeq!(MyModel) createModel(int amount) { | |
176 ArraySeq!(MyModel) elements = new ArraySeq!(MyModel); | |
177 elements.capacity = amount; | |
178 | |
179 for (int i = 0; i < amount; i++) { | |
180 elements ~= new MyModel(i); | |
181 } | |
182 | |
183 return elements; | |
184 } | |
185 } | |
186 | |
187 void main() { | |
188 Display display = new Display(); | |
189 Shell shell = new Shell(display); | |
190 shell.setLayout(new FillLayout()); | |
191 new Snippet054NativeControlsInViewers(shell); | |
192 shell.open(); | |
193 | |
194 while (!shell.isDisposed()) { | |
195 if (!display.readAndDispatch()) | |
196 display.sleep(); | |
197 } | |
198 | |
199 display.dispose(); | |
200 | |
201 } |