Mercurial > projects > dwt-samples
comparison jface/snippets/Snippet002TreeViewer.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.Snippet002TreeViewer; | |
15 | |
16 import dwt.DWT; | |
17 import dwtx.jface.viewers.ITreeContentProvider; | |
18 import dwtx.jface.viewers.LabelProvider; | |
19 import dwtx.jface.viewers.TreeViewer; | |
20 import dwtx.jface.viewers.Viewer; | |
21 import dwt.layout.FillLayout; | |
22 import dwt.widgets.Display; | |
23 import dwt.widgets.Shell; | |
24 | |
25 import dwt.dwthelper.utils; | |
26 | |
27 import tango.util.Convert; | |
28 | |
29 /** | |
30 * A simple TreeViewer to demonstrate usage | |
31 * | |
32 * @author Tom Schindl <tom.schindl@bestsolution.at> | |
33 * | |
34 */ | |
35 class Snippet002TreeViewer { | |
36 private class MyContentProvider : ITreeContentProvider { | |
37 | |
38 /* (non-Javadoc) | |
39 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) | |
40 */ | |
41 public Object[] getElements(Object inputElement) { | |
42 return (cast(MyModel)inputElement).child/*.dup*/; | |
43 } | |
44 | |
45 /* (non-Javadoc) | |
46 * @see org.eclipse.jface.viewers.IContentProvider#dispose() | |
47 */ | |
48 public void dispose() { | |
49 | |
50 } | |
51 | |
52 /* (non-Javadoc) | |
53 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) | |
54 */ | |
55 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
56 | |
57 } | |
58 | |
59 /* (non-Javadoc) | |
60 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) | |
61 */ | |
62 public Object[] getChildren(Object parentElement) { | |
63 return getElements(parentElement); | |
64 } | |
65 | |
66 /* (non-Javadoc) | |
67 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) | |
68 */ | |
69 public Object getParent(Object element) { | |
70 if( element is null) { | |
71 return null; | |
72 } | |
73 | |
74 return (cast(MyModel)element).parent; | |
75 } | |
76 | |
77 /* (non-Javadoc) | |
78 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) | |
79 */ | |
80 public bool hasChildren(Object element) { | |
81 return (cast(MyModel)element).child.length > 0; | |
82 } | |
83 | |
84 } | |
85 | |
86 public class MyModel { | |
87 public MyModel parent; | |
88 public MyModel[] child; | |
89 public int counter; | |
90 | |
91 public this(int counter, MyModel parent) { | |
92 this.parent = parent; | |
93 this.counter = counter; | |
94 } | |
95 | |
96 public String toString() { | |
97 String rv = "Item "; | |
98 if( parent !is null ) { | |
99 rv = parent.toString() ~ "."; | |
100 } | |
101 | |
102 rv ~= to!(char[])(counter); | |
103 | |
104 return rv; | |
105 } | |
106 } | |
107 | |
108 public this(Shell shell) { | |
109 TreeViewer v = new TreeViewer(shell); | |
110 v.setLabelProvider(new LabelProvider()); | |
111 v.setContentProvider(new MyContentProvider()); | |
112 v.setInput(createModel()); | |
113 } | |
114 | |
115 private MyModel createModel() { | |
116 MyModel root = new MyModel(0,null); | |
117 root.counter = 0; | |
118 | |
119 MyModel tmp; | |
120 for( int i = 1; i < 10; i++ ) { | |
121 tmp = new MyModel(i, root); | |
122 root.child ~= tmp; | |
123 for( int j = 1; j < i; j++ ) { | |
124 tmp.child ~= new MyModel(j,tmp); | |
125 } | |
126 } | |
127 | |
128 return root; | |
129 } | |
130 | |
131 } | |
132 | |
133 | |
134 void main() { | |
135 Display display = new Display (); | |
136 Shell shell = new Shell(display); | |
137 shell.setLayout(new FillLayout()); | |
138 new Snippet002TreeViewer(shell); | |
139 shell.open (); | |
140 | |
141 while (!shell.isDisposed ()) { | |
142 if (!display.readAndDispatch ()) display.sleep (); | |
143 } | |
144 | |
145 display.dispose (); | |
146 } |