comparison 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
comparison
equal deleted inserted replaced
144:7248e4c09c4f 145:161f7698cfb8
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.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 version(JIVE) import jive.stacktrace;
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 Snippet043NoColumnTreeViewerKeyboardEditing(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 * Demonstrates how to use keyboard-editing support in a TreeViewer with no column
64 *
65 * @author Tom Schindl <tom.schindl@bestsolution.at>
66 *
67 */
68 public class Snippet043NoColumnTreeViewerKeyboardEditing {
69 alias LinkedList!(MyModel) ArrayList;
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 {
76 TreeViewer v;
77 this(TreeViewer v_)
78 {
79 this.v = v_;
80 }
81
82 public void widgetDefaultSelected(SelectionEvent e) {
83
84 }
85
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 }
92
93 });
94
95 v.setCellEditors([ new TextCellEditor(v.getTree()) ]);
96 v.setColumnProperties(["col1"]);
97 v.setCellModifier(new class(v) ICellModifier {
98 TreeViewer v;
99 this(TreeViewer v_)
100 {
101 this.v = v_;
102 }
103 public bool canModify(Object element, String property) {
104 return true;
105 }
106
107 public Object getValue(Object element, String property) {
108 return stringcast( to!(String)((cast(MyModel) element).counter) ~ "" );
109 }
110
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 }
116
117 });
118
119 TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(
120 v, new FocusCellOwnerDrawHighlighter(v));
121 ColumnViewerEditorActivationStrategy actSupport = new class(v) ColumnViewerEditorActivationStrategy {
122 this(ColumnViewer v)
123 {
124 super(v);
125 }
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 };
134
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);
140
141 v.setContentProvider(new MyContentProvider());
142
143 v.setInput(createModel());
144 }
145
146 private MyModel createModel() {
147
148 MyModel root = new MyModel(0, null);
149 root.counter = 0;
150
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 }
162
163 return root;
164 }
165
166
167
168 private class MyContentProvider : ITreeContentProvider {
169
170 public Object[] getElements(Object inputElement) {
171 return (cast(MyModel) inputElement).child.toArray();
172 }
173
174 public void dispose() {
175 }
176
177 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
178 }
179
180 public Object[] getChildren(Object parentElement) {
181 return getElements(parentElement);
182 }
183
184 public Object getParent(Object element) {
185 if (element is null) {
186 return null;
187 }
188 return (cast(MyModel) element).parent;
189 }
190
191 public bool hasChildren(Object element) {
192 return (cast(MyModel) element).child.size() > 0;
193 }
194
195 }
196
197 public class MyModel {
198 public MyModel parent;
199
200 public ArrayList child;
201
202 public int counter;
203
204 public this(int counter_, MyModel parent_) {
205 this.parent = parent_;
206 this.counter = counter_;
207 child = new ArrayList();
208 }
209
210 public String toString() {
211 String rv = "Item ";
212 if (parent !is null) {
213 rv = parent.toString() ~ ".";
214 }
215
216 rv ~= to!(String)(counter);
217
218 return rv;
219 }
220 }
221
222 }