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