Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet005TreeCustomMenu.d @ 89:cbba80cceb7a
Port of jface Snippets 1,2,4,5,7
author | Bill Baxter <bill@billbaxter.com> |
---|---|
date | Mon, 19 May 2008 23:42:48 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
88:9ed020f0c2a5 | 89:cbba80cceb7a |
---|---|
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 * wbaxter at gmail dot com | |
12 *******************************************************************************/ | |
13 | |
14 module jface.snippets.Snippet005TreeCustomMenu; | |
15 | |
16 import dwtx.jface.action.Action; | |
17 import dwtx.jface.action.IMenuListener; | |
18 import dwtx.jface.action.IMenuManager; | |
19 import dwtx.jface.action.MenuManager; | |
20 import dwtx.jface.viewers.IStructuredSelection; | |
21 import dwtx.jface.viewers.ITreeContentProvider; | |
22 import dwtx.jface.viewers.LabelProvider; | |
23 import dwtx.jface.viewers.TreeViewer; | |
24 import dwtx.jface.viewers.Viewer; | |
25 import dwt.layout.FillLayout; | |
26 import dwt.widgets.Display; | |
27 import dwt.widgets.Shell; | |
28 import dwt.DWT; | |
29 | |
30 import dwt.dwthelper.utils; | |
31 | |
32 import tango.util.Convert; | |
33 import tango.io.Stdout; | |
34 | |
35 /** | |
36 * Customized context menu based on TreeItem-Selection | |
37 * | |
38 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
39 * | |
40 */ | |
41 public class Snippet005TreeCustomMenu { | |
42 private class MyContentProvider : ITreeContentProvider { | |
43 | |
44 public Object[] getElements(Object inputElement) { | |
45 return (cast(MyModel) inputElement).child; | |
46 } | |
47 | |
48 /* (non-Javadoc) | |
49 * @see org.eclipse.jface.viewers.IContentProvider#dispose() | |
50 */ | |
51 public void dispose() { | |
52 | |
53 } | |
54 | |
55 /* (non-Javadoc) | |
56 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) | |
57 */ | |
58 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
59 | |
60 } | |
61 | |
62 /* (non-Javadoc) | |
63 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
64 */ | |
65 public Object[] getChildren(Object parentElement) { | |
66 return getElements(parentElement); | |
67 } | |
68 | |
69 /* (non-Javadoc) | |
70 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) | |
71 */ | |
72 public Object getParent(Object element) { | |
73 if (element is null) { | |
74 return null; | |
75 } | |
76 | |
77 return (cast(MyModel) element).parent; | |
78 } | |
79 | |
80 /* (non-Javadoc) | |
81 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) | |
82 */ | |
83 public bool hasChildren(Object element) { | |
84 return (cast(MyModel) element).child.length > 0; | |
85 } | |
86 | |
87 } | |
88 | |
89 public class MyModel { | |
90 public MyModel parent; | |
91 | |
92 public MyModel[] child; | |
93 | |
94 public int counter; | |
95 | |
96 public this(int counter, MyModel parent) { | |
97 this.parent = parent; | |
98 this.counter = counter; | |
99 } | |
100 | |
101 public String toString() { | |
102 String rv = "Item "; | |
103 if (parent !is null) { | |
104 rv = parent.toString() ~ "."; | |
105 } | |
106 | |
107 rv ~= to!(char[])(counter); | |
108 | |
109 return rv; | |
110 } | |
111 } | |
112 | |
113 public this(Shell shell) { | |
114 final TreeViewer v = new TreeViewer(shell); | |
115 v.setLabelProvider(new LabelProvider()); | |
116 v.setContentProvider(new MyContentProvider()); | |
117 v.setInput(createModel()); | |
118 | |
119 final Action a = new class Action {}; | |
120 final MenuManager mgr = new MenuManager(); | |
121 mgr.setRemoveAllWhenShown(true); | |
122 | |
123 class MyMenuListener : IMenuListener { | |
124 TreeViewer v; | |
125 Action a; | |
126 MenuManager mgr; | |
127 | |
128 this(TreeViewer v_, Action a_, MenuManager mgr_) { | |
129 this.v = v_; this.a = a_; this.mgr = mgr_; | |
130 } | |
131 | |
132 /* (non-Javadoc) | |
133 * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager) | |
134 */ | |
135 public void menuAboutToShow(IMenuManager manager) { | |
136 IStructuredSelection selection = cast(IStructuredSelection) v | |
137 .getSelection(); | |
138 if (!selection.isEmpty()) { | |
139 a.setText("Action for " | |
140 ~ (cast(MyModel) selection.getFirstElement()) | |
141 .toString()); | |
142 mgr.add(a); | |
143 } | |
144 } | |
145 } | |
146 | |
147 mgr.addMenuListener(new MyMenuListener(v,a,mgr)); | |
148 v.getControl().setMenu(mgr.createContextMenu(v.getControl())); | |
149 } | |
150 | |
151 private MyModel createModel() { | |
152 | |
153 MyModel root = new MyModel(0, null); | |
154 root.counter = 0; | |
155 | |
156 MyModel tmp; | |
157 for (int i = 1; i < 10; i++) { | |
158 tmp = new MyModel(i, root); | |
159 root.child ~= tmp; | |
160 for (int j = 1; j < i; j++) { | |
161 tmp.child ~= new MyModel(j, tmp); | |
162 } | |
163 } | |
164 | |
165 return root; | |
166 } | |
167 | |
168 } | |
169 | |
170 void main() { | |
171 Display display = new Display(); | |
172 Shell shell = new Shell(display); | |
173 shell.setLayout(new FillLayout()); | |
174 new Snippet005TreeCustomMenu(shell); | |
175 shell.open(); | |
176 | |
177 while (!shell.isDisposed()) { | |
178 if (!display.readAndDispatch()) | |
179 display.sleep(); | |
180 } | |
181 | |
182 display.dispose(); | |
183 } |