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