Mercurial > projects > dwt-samples
annotate jface/snippets/viewers/Snippet043NoColumnTreeViewerKeyboardEditing.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/Snippet043NoColumnTreeViewerKeyboardEditing.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.Snippet043NoColumnTreeViewerKeyboardEditing; |
137 | 15 |
16 import dwtx.jface.viewers.CellEditor; | |
17 import dwtx.jface.viewers.ColumnViewerEditor; | |
18 import dwtx.jface.viewers.ColumnViewer; | |
19 import dwtx.jface.viewers.ColumnViewerEditorActivationStrategy; | |
20 import dwtx.jface.viewers.ColumnViewerEditorActivationEvent; | |
21 import dwtx.jface.viewers.FocusCellOwnerDrawHighlighter; | |
22 import dwtx.jface.viewers.ICellModifier; | |
23 import dwtx.jface.viewers.ITreeContentProvider; | |
24 import dwtx.jface.viewers.TextCellEditor; | |
25 import dwtx.jface.viewers.TreePath; | |
26 import dwtx.jface.viewers.TreeViewerFocusCellManager; | |
27 import dwtx.jface.viewers.TreeViewerEditor; | |
28 import dwtx.jface.viewers.TreeViewer; | |
29 import dwtx.jface.viewers.Viewer; | |
30 import dwt.DWT; | |
31 import dwt.events.SelectionEvent; | |
32 import dwt.events.SelectionListener; | |
33 import dwt.layout.FillLayout; | |
34 import dwt.widgets.Button; | |
35 import dwt.widgets.Display; | |
36 import dwt.widgets.Item; | |
37 import dwt.widgets.Shell; | |
38 | |
39 import dwt.dwthelper.utils; | |
40 | |
41 import tango.util.Convert; | |
42 import tango.util.container.LinkedList; | |
43 | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
44 version(JIVE) import jive.stacktrace; |
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
45 |
137 | 46 |
47 void main(String[] args) { | |
48 Display display = new Display(); | |
49 Shell shell = new Shell(display); | |
50 shell.setLayout(new FillLayout()); | |
51 new Snippet043NoColumnTreeViewerKeyboardEditing(shell); | |
52 shell.open(); | |
53 | |
54 while (!shell.isDisposed()) { | |
55 if (!display.readAndDispatch()) | |
56 display.sleep(); | |
57 } | |
58 | |
59 display.dispose(); | |
60 } | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
61 |
137 | 62 /** |
63 * Demonstrates how to use keyboard-editing support in a TreeViewer with no column | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
64 * |
137 | 65 * @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
|
66 * |
137 | 67 */ |
68 public class Snippet043NoColumnTreeViewerKeyboardEditing { | |
69 alias LinkedList!(MyModel) ArrayList; | |
140 | 70 public this(Shell shell) { |
71 Button b = new Button(shell, DWT.PUSH); | |
72 b.setText("BBB"); | |
73 final TreeViewer v = new TreeViewer(shell, DWT.BORDER | |
74 | DWT.FULL_SELECTION); | |
75 b.addSelectionListener(new class(v) SelectionListener { | |
137 | 76 TreeViewer v; |
77 this(TreeViewer v_) | |
78 { | |
79 this.v = v_; | |
80 } | |
143
42c3056512ba
redirect the jface examples to the new collection wrappers
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
81 |
140 | 82 public void widgetDefaultSelected(SelectionEvent e) { |
137 | 83 |
140 | 84 } |
137 | 85 |
140 | 86 public void widgetSelected(SelectionEvent e) { |
87 MyModel root = cast(MyModel) v.getInput(); | |
88 TreePath path = new TreePath([ root, root.child.get(1), | |
89 (cast(MyModel) root.child.get(1)).child.get(0)] ); | |
90 v.editElement(path, 0); | |
91 } | |
137 | 92 |
140 | 93 }); |
137 | 94 |
140 | 95 v.setCellEditors([ new TextCellEditor(v.getTree()) ]); |
96 v.setColumnProperties(["col1"]); | |
97 v.setCellModifier(new class(v) ICellModifier { | |
137 | 98 TreeViewer v; |
99 this(TreeViewer v_) | |
100 { | |
101 this.v = v_; | |
102 } | |
140 | 103 public bool canModify(Object element, String property) { |
104 return true; | |
105 } | |
137 | 106 |
140 | 107 public Object getValue(Object element, String property) { |
108 return stringcast( to!(String)((cast(MyModel) element).counter) ~ "" ); | |
109 } | |
137 | 110 |
140 | 111 public void modify(Object element, String property, Object value) { |
112 element = (cast(Item) element).getData(); | |
113 (cast(MyModel) element).counter = Integer.parseInt(value.toString()); | |
114 v.update(element, null); | |
115 } | |
137 | 116 |
140 | 117 }); |
137 | 118 |
140 | 119 TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager( |
120 v, new FocusCellOwnerDrawHighlighter(v)); | |
121 ColumnViewerEditorActivationStrategy actSupport = new class(v) ColumnViewerEditorActivationStrategy { | |
137 | 122 this(ColumnViewer v) |
123 { | |
124 super(v); | |
125 } | |
140 | 126 protected bool isEditorActivationEvent( |
127 ColumnViewerEditorActivationEvent event) { | |
128 return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL | |
129 || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION | |
130 || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == DWT.CR) | |
131 || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; | |
132 } | |
133 }; | |
137 | 134 |
140 | 135 TreeViewerEditor.create(v, focusCellManager, actSupport, |
136 ColumnViewerEditor.TABBING_HORIZONTAL | |
137 | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | |
138 | ColumnViewerEditor.TABBING_VERTICAL | |
139 | ColumnViewerEditor.KEYBOARD_ACTIVATION); | |
137 | 140 |
140 | 141 v.setContentProvider(new MyContentProvider()); |
137 | 142 |
140 | 143 v.setInput(createModel()); |
144 } | |
137 | 145 |
140 | 146 private MyModel createModel() { |
137 | 147 |
140 | 148 MyModel root = new MyModel(0, null); |
149 root.counter = 0; | |
137 | 150 |
140 | 151 MyModel tmp; |
152 MyModel subItem; | |
153 for (int i = 1; i < 10; i++) { | |
154 tmp = new MyModel(i, root); | |
155 root.child.add(tmp); | |
156 for (int j = 1; j < i; j++) { | |
157 subItem = new MyModel(j, tmp); | |
158 subItem.child.add(new MyModel(j * 100, subItem)); | |
159 tmp.child.add(subItem); | |
160 } | |
161 } | |
137 | 162 |
140 | 163 return root; |
164 } | |
137 | 165 |
166 | |
167 | |
140 | 168 private class MyContentProvider : ITreeContentProvider { |
137 | 169 |
140 | 170 public Object[] getElements(Object inputElement) { |
171 return (cast(MyModel) inputElement).child.toArray(); | |
172 } | |
137 | 173 |
140 | 174 public void dispose() { |
175 } | |
137 | 176 |
140 | 177 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
178 } | |
137 | 179 |
140 | 180 public Object[] getChildren(Object parentElement) { |
181 return getElements(parentElement); | |
182 } | |
137 | 183 |
140 | 184 public Object getParent(Object element) { |
185 if (element is null) { | |
186 return null; | |
187 } | |
188 return (cast(MyModel) element).parent; | |
189 } | |
137 | 190 |
140 | 191 public bool hasChildren(Object element) { |
192 return (cast(MyModel) element).child.size() > 0; | |
193 } | |
137 | 194 |
140 | 195 } |
137 | 196 |
140 | 197 public class MyModel { |
198 public MyModel parent; | |
137 | 199 |
140 | 200 public ArrayList child; |
137 | 201 |
140 | 202 public int counter; |
137 | 203 |
140 | 204 public this(int counter_, MyModel parent_) { |
205 this.parent = parent_; | |
206 this.counter = counter_; | |
137 | 207 child = new ArrayList(); |
140 | 208 } |
137 | 209 |
140 | 210 public String toString() { |
211 String rv = "Item "; | |
212 if (parent !is null) { | |
213 rv = parent.toString() ~ "."; | |
214 } | |
137 | 215 |
140 | 216 rv ~= to!(String)(counter); |
137 | 217 |
140 | 218 return rv; |
219 } | |
220 } | |
137 | 221 |
222 } |