Mercurial > projects > dwt-samples
annotate jface/snippets/viewers/Snippet026TreeViewerTabEditing.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/Snippet026TreeViewerTabEditing.d@42c3056512ba |
children |
rev | line source |
---|---|
137 | 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: | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
11 * yidabu at gmail dot com ( D China http://www.d-programming-language-china.org/ ) |
137 | 12 *******************************************************************************/ |
13 | |
145
161f7698cfb8
Moved jface snippets to viewers subpackage, there are more with conflicting numbers for other packages
Frank Benoit <benoit@tionex.de>
parents:
143
diff
changeset
|
14 module snippets.viewers.Snippet026TreeViewerTabEditing; |
137 | 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; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
42 import dwtx.dwtxhelper.Collection; |
137 | 43 |
44 import tango.util.Convert; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
45 |
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
46 version(JIVE) import jive.stacktrace; |
137 | 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 | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
66 * |
137 | 67 * @author Tom Schindl <tom.schindl@bestsolution.at> |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
68 * |
137 | 69 */ |
70 public class Snippet026TreeViewerTabEditing { | |
140 | 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 { | |
137 | 79 TreeViewer v; |
80 this(TreeViewer v_) | |
81 { | |
82 this.v = v_; | |
83 } | |
140 | 84 public void widgetDefaultSelected(SelectionEvent e) { |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
85 |
140 | 86 } |
137 | 87 |
140 | 88 public void widgetSelected(SelectionEvent e) { |
89 v.getTree().getColumn(1).dispose(); | |
90 } | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
91 |
140 | 92 }); |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
93 |
140 | 94 TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v)); |
95 ColumnViewerEditorActivationStrategy actSupport = new class(v) ColumnViewerEditorActivationStrategy { | |
137 | 96 this(ColumnViewer v_) |
97 { | |
98 super(v_); | |
99 } | |
140 | 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 }; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
108 |
140 | 109 TreeViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL |
110 | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | |
111 | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION); | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
112 |
140 | 113 final TextCellEditor textCellEditor = new TextCellEditor(v.getTree()); |
137 | 114 |
140 | 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 { | |
137 | 120 |
140 | 121 public String getText(Object element) { |
122 return "Column 1 => " ~ element.toString(); | |
123 } | |
137 | 124 |
140 | 125 }); |
126 column.setEditingSupport(new class(v) EditingSupport { | |
137 | 127 this(ColumnViewer v_) |
128 { | |
129 super(v_); | |
130 } | |
140 | 131 protected bool canEdit(Object element) { |
132 return false; | |
133 } | |
137 | 134 |
140 | 135 protected CellEditor getCellEditor(Object element) { |
136 return textCellEditor; | |
137 } | |
137 | 138 |
140 | 139 protected Object getValue(Object element) { |
140 return stringcast( (to!(String)((cast(MyModel) element).counter)) ); | |
141 } | |
137 | 142 |
140 | 143 protected void setValue(Object element, Object value) { |
144 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
145 v.update(element, null); | |
146 } | |
147 }); | |
137 | 148 |
140 | 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 { | |
137 | 154 |
140 | 155 public String getText(Object element) { |
156 return "Column 2 => " ~ element.toString(); | |
157 } | |
137 | 158 |
140 | 159 }); |
160 column.setEditingSupport(new class(v) EditingSupport { | |
137 | 161 this(ColumnViewer v_) |
162 { | |
163 super(v_); | |
164 } | |
140 | 165 protected bool canEdit(Object element) { |
166 return true; | |
167 } | |
137 | 168 |
140 | 169 protected CellEditor getCellEditor(Object element) { |
170 return textCellEditor; | |
171 } | |
137 | 172 |
140 | 173 protected Object getValue(Object element) { |
174 return stringcast( to!(String)((cast(MyModel) element).counter) ); | |
175 } | |
137 | 176 |
140 | 177 protected void setValue(Object element, Object value) { |
178 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
179 v.update(element, null); | |
180 } | |
181 }); | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
182 |
140 | 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 } | |
137 | 191 |
140 | 192 }); |
193 column.setEditingSupport(new class(v) EditingSupport { | |
137 | 194 this(ColumnViewer v_) |
195 { | |
196 super(v_); | |
197 } | |
140 | 198 protected bool canEdit(Object element) { |
199 return true; | |
200 } | |
137 | 201 |
140 | 202 protected CellEditor getCellEditor(Object element) { |
203 return textCellEditor; | |
204 } | |
137 | 205 |
140 | 206 protected Object getValue(Object element) { |
207 return stringcast( to!(String)((cast(MyModel) element).counter) ); | |
208 } | |
137 | 209 |
140 | 210 protected void setValue(Object element, Object value) { |
211 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
212 v.update(element, null); | |
213 } | |
214 }); | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
215 |
140 | 216 v.setContentProvider(new MyContentProvider()); |
137 | 217 |
140 | 218 v.setInput(createModel()); |
219 } | |
137 | 220 |
140 | 221 private MyModel createModel() { |
137 | 222 |
140 | 223 MyModel root = new MyModel(0, null); |
224 root.counter = 0; | |
137 | 225 |
140 | 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 } | |
137 | 237 |
140 | 238 return root; |
239 } | |
137 | 240 |
140 | 241 private class MyContentProvider : ITreeContentProvider { |
137 | 242 |
140 | 243 public Object[] getElements(Object inputElement) { |
244 return (cast(MyModel) inputElement).child.toArray(); | |
245 } | |
137 | 246 |
140 | 247 public void dispose() { |
248 } | |
137 | 249 |
140 | 250 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
251 } | |
137 | 252 |
140 | 253 public Object[] getChildren(Object parentElement) { |
254 return getElements(parentElement); | |
255 } | |
137 | 256 |
140 | 257 public Object getParent(Object element) { |
258 if (element is null) { | |
259 return null; | |
260 } | |
261 return (cast(MyModel) element).parent; | |
262 } | |
137 | 263 |
140 | 264 public bool hasChildren(Object element) { |
265 return (cast(MyModel) element).child.size() > 0; | |
266 } | |
137 | 267 |
140 | 268 } |
137 | 269 |
140 | 270 public class MyModel { |
271 public MyModel parent; | |
137 | 272 |
140 | 273 public ArrayList child; |
137 | 274 |
140 | 275 public int counter; |
137 | 276 |
140 | 277 public this(int counter, MyModel parent) { |
278 this.parent = parent; | |
279 this.counter = counter; | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
280 child = new ArrayList(); |
140 | 281 } |
137 | 282 |
140 | 283 public String toString() { |
284 String rv = "Item "; | |
285 if (parent !is null) { | |
286 rv = parent.toString() ~ "."; | |
287 } | |
137 | 288 |
140 | 289 rv ~= to!(String)(counter); |
137 | 290 |
140 | 291 return rv; |
292 } | |
293 } | |
137 | 294 |
295 } |