Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet026TreeViewerTabEditing.d @ 137:96a2d0b35360
jface snippets 014, 026, 043, 047
author | yidabu <yidabu@gmail.com> |
---|---|
date | Wed, 06 Aug 2008 10:03:06 +0800 |
parents | |
children | 2b4e94cafb85 |
comparison
equal
deleted
inserted
replaced
136:7931ee9b41e6 | 137:96a2d0b35360 |
---|---|
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 * Port to the D programming language: | |
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) | |
12 *******************************************************************************/ | |
13 | |
14 module jface.snippets.Snippet026TreeViewerTabEditing; | |
15 | |
16 | |
17 import dwtx.jface.viewers.CellEditor; | |
18 import dwtx.jface.viewers.ColumnLabelProvider; | |
19 import dwtx.jface.viewers.ColumnViewerEditor; | |
20 import dwtx.jface.viewers.ColumnViewer; | |
21 import dwtx.jface.viewers.ColumnViewerEditorActivationStrategy; | |
22 import dwtx.jface.viewers.EditingSupport; | |
23 import dwtx.jface.viewers.ColumnViewerEditorActivationEvent; | |
24 import dwtx.jface.viewers.FocusCellOwnerDrawHighlighter; | |
25 import dwtx.jface.viewers.ITreeContentProvider; | |
26 import dwtx.jface.viewers.TextCellEditor; | |
27 import dwtx.jface.viewers.TreePath; | |
28 import dwtx.jface.viewers.TreeViewerFocusCellManager; | |
29 import dwtx.jface.viewers.TreeViewerEditor; | |
30 import dwtx.jface.viewers.TreeViewer; | |
31 import dwtx.jface.viewers.TreeViewerColumn; | |
32 import dwtx.jface.viewers.Viewer; | |
33 import dwt.DWT; | |
34 import dwt.events.SelectionEvent; | |
35 import dwt.events.SelectionListener; | |
36 import dwt.layout.FillLayout; | |
37 import dwt.widgets.Button; | |
38 import dwt.widgets.Display; | |
39 import dwt.widgets.Shell; | |
40 | |
41 import dwt.dwthelper.utils; | |
42 | |
43 import tango.util.Convert; | |
44 import tango.util.container.LinkedList; | |
45 | |
46 | |
47 void main(String[] args) { | |
48 Display display = new Display(); | |
49 Shell shell = new Shell(display); | |
50 shell.setLayout(new FillLayout()); | |
51 new Snippet026TreeViewerTabEditing(shell); | |
52 shell.open(); | |
53 | |
54 while (!shell.isDisposed()) { | |
55 if (!display.readAndDispatch()) | |
56 display.sleep(); | |
57 } | |
58 | |
59 display.dispose(); | |
60 } | |
61 | |
62 /** | |
63 * A simple TreeViewer to demonstrate usage | |
64 * | |
65 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
66 * | |
67 */ | |
68 public class Snippet026TreeViewerTabEditing { | |
69 alias LinkedList!(MyModel) ArrayList; | |
70 public this(Shell shell) { | |
71 Button b = new Button(shell,DWT.PUSH); | |
72 b.setText("Remove column"); | |
73 final TreeViewer v = new TreeViewer(shell, DWT.BORDER | |
74 | DWT.FULL_SELECTION); | |
75 v.getTree().setLinesVisible(true); | |
76 v.getTree().setHeaderVisible(true); | |
77 b.addSelectionListener(new class(v) SelectionListener { | |
78 TreeViewer v; | |
79 this(TreeViewer v_) | |
80 { | |
81 this.v = v_; | |
82 } | |
83 public void widgetDefaultSelected(SelectionEvent e) { | |
84 | |
85 } | |
86 | |
87 public void widgetSelected(SelectionEvent e) { | |
88 v.getTree().getColumn(1).dispose(); | |
89 } | |
90 | |
91 }); | |
92 | |
93 TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v)); | |
94 ColumnViewerEditorActivationStrategy actSupport = new class(v) ColumnViewerEditorActivationStrategy { | |
95 this(ColumnViewer v_) | |
96 { | |
97 super(v_); | |
98 } | |
99 protected bool isEditorActivationEvent( | |
100 ColumnViewerEditorActivationEvent event) { | |
101 return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL | |
102 || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION | |
103 || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == DWT.CR) | |
104 || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; | |
105 } | |
106 }; | |
107 | |
108 TreeViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL | |
109 | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | |
110 | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION); | |
111 | |
112 final TextCellEditor textCellEditor = new TextCellEditor(v.getTree()); | |
113 | |
114 TreeViewerColumn column = new TreeViewerColumn(v, DWT.NONE); | |
115 column.getColumn().setWidth(200); | |
116 column.getColumn().setMoveable(true); | |
117 column.getColumn().setText("Column 1"); | |
118 column.setLabelProvider(new class() ColumnLabelProvider { | |
119 | |
120 public String getText(Object element) { | |
121 return "Column 1 => " ~ element.toString(); | |
122 } | |
123 | |
124 }); | |
125 column.setEditingSupport(new class(v) EditingSupport { | |
126 this(ColumnViewer v_) | |
127 { | |
128 super(v_); | |
129 } | |
130 protected bool canEdit(Object element) { | |
131 return false; | |
132 } | |
133 | |
134 protected CellEditor getCellEditor(Object element) { | |
135 return textCellEditor; | |
136 } | |
137 | |
138 protected Object getValue(Object element) { | |
139 return stringcast( (to!(String)((cast(MyModel) element).counter)) ); | |
140 } | |
141 | |
142 protected void setValue(Object element, Object value) { | |
143 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
144 v.update(element, null); | |
145 } | |
146 }); | |
147 | |
148 column = new TreeViewerColumn(v, DWT.NONE); | |
149 column.getColumn().setWidth(200); | |
150 column.getColumn().setMoveable(true); | |
151 column.getColumn().setText("Column 2"); | |
152 column.setLabelProvider(new class() ColumnLabelProvider { | |
153 | |
154 public String getText(Object element) { | |
155 return "Column 2 => " ~ element.toString(); | |
156 } | |
157 | |
158 }); | |
159 column.setEditingSupport(new class(v) EditingSupport { | |
160 this(ColumnViewer v_) | |
161 { | |
162 super(v_); | |
163 } | |
164 protected bool canEdit(Object element) { | |
165 return true; | |
166 } | |
167 | |
168 protected CellEditor getCellEditor(Object element) { | |
169 return textCellEditor; | |
170 } | |
171 | |
172 protected Object getValue(Object element) { | |
173 return stringcast( to!(String)((cast(MyModel) element).counter) ); | |
174 } | |
175 | |
176 protected void setValue(Object element, Object value) { | |
177 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
178 v.update(element, null); | |
179 } | |
180 }); | |
181 | |
182 column = new TreeViewerColumn(v, DWT.NONE); | |
183 column.getColumn().setWidth(200); | |
184 column.getColumn().setMoveable(true); | |
185 column.getColumn().setText("Column 3"); | |
186 column.setLabelProvider(new class() ColumnLabelProvider { | |
187 public String getText(Object element) { | |
188 return "Column 3 => " ~ element.toString(); | |
189 } | |
190 | |
191 }); | |
192 column.setEditingSupport(new class(v) EditingSupport { | |
193 this(ColumnViewer v_) | |
194 { | |
195 super(v_); | |
196 } | |
197 protected bool canEdit(Object element) { | |
198 return true; | |
199 } | |
200 | |
201 protected CellEditor getCellEditor(Object element) { | |
202 return textCellEditor; | |
203 } | |
204 | |
205 protected Object getValue(Object element) { | |
206 return stringcast( to!(String)((cast(MyModel) element).counter) ); | |
207 } | |
208 | |
209 protected void setValue(Object element, Object value) { | |
210 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
211 v.update(element, null); | |
212 } | |
213 }); | |
214 | |
215 v.setContentProvider(new MyContentProvider()); | |
216 | |
217 v.setInput(createModel()); | |
218 } | |
219 | |
220 private MyModel createModel() { | |
221 | |
222 MyModel root = new MyModel(0, null); | |
223 root.counter = 0; | |
224 | |
225 MyModel tmp; | |
226 MyModel subItem; | |
227 for (int i = 1; i < 10; i++) { | |
228 tmp = new MyModel(i, root); | |
229 root.child.add(tmp); | |
230 for (int j = 1; j < i; j++) { | |
231 subItem = new MyModel(j, tmp); | |
232 subItem.child.add(new MyModel(j * 100, subItem)); | |
233 tmp.child.add(subItem); | |
234 } | |
235 } | |
236 | |
237 return root; | |
238 } | |
239 | |
240 private class MyContentProvider : ITreeContentProvider { | |
241 | |
242 public Object[] getElements(Object inputElement) { | |
243 return (cast(MyModel) inputElement).child.toArray(); | |
244 } | |
245 | |
246 public void dispose() { | |
247 } | |
248 | |
249 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
250 } | |
251 | |
252 public Object[] getChildren(Object parentElement) { | |
253 return getElements(parentElement); | |
254 } | |
255 | |
256 public Object getParent(Object element) { | |
257 if (element is null) { | |
258 return null; | |
259 } | |
260 return (cast(MyModel) element).parent; | |
261 } | |
262 | |
263 public bool hasChildren(Object element) { | |
264 return (cast(MyModel) element).child.size() > 0; | |
265 } | |
266 | |
267 } | |
268 | |
269 public class MyModel { | |
270 public MyModel parent; | |
271 | |
272 public ArrayList child; | |
273 | |
274 public int counter; | |
275 | |
276 public this(int counter, MyModel parent) { | |
277 this.parent = parent; | |
278 this.counter = counter; | |
279 child = new ArrayList(); | |
280 } | |
281 | |
282 public String toString() { | |
283 String rv = "Item "; | |
284 if (parent !is null) { | |
285 rv = parent.toString() ~ "."; | |
286 } | |
287 | |
288 rv ~= to!(String)(counter); | |
289 | |
290 return rv; | |
291 } | |
292 } | |
293 | |
294 } |