Mercurial > projects > dwt-samples
comparison jface/FileTreeViewer.d @ 62:caaf053c44d6
more examples
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Apr 2008 17:08:20 +0200 |
parents | |
children | 59ae5360d98f |
comparison
equal
deleted
inserted
replaced
61:ffb8196501b3 | 62:caaf053c44d6 |
---|---|
1 module jface.FileTreeViewer; | |
2 | |
3 import dwt.DWT; | |
4 import dwt.widgets.Label; | |
5 import dwt.widgets.Control; | |
6 import dwt.widgets.Composite; | |
7 import dwt.widgets.Display; | |
8 import dwt.widgets.Shell; | |
9 import dwt.widgets.Button; | |
10 | |
11 import dwt.events.SelectionAdapter; | |
12 import dwt.events.SelectionEvent; | |
13 | |
14 import dwt.graphics.Image; | |
15 import dwt.graphics.ImageData; | |
16 | |
17 import dwt.layout.GridLayout; | |
18 import dwt.layout.GridData; | |
19 | |
20 import dwtx.jface.window.ApplicationWindow; | |
21 | |
22 import dwtx.jface.viewers.Viewer; | |
23 import dwtx.jface.viewers.TreeViewer; | |
24 import dwtx.jface.viewers.ITreeContentProvider; | |
25 import dwtx.jface.viewers.ILabelProvider; | |
26 import dwtx.jface.viewers.ILabelProviderListener; | |
27 import dwtx.jface.viewers.LabelProviderChangedEvent; | |
28 | |
29 import jive.stacktrace; | |
30 | |
31 import dwt.dwthelper.utils; | |
32 import dwt.dwthelper.ByteArrayInputStream; | |
33 | |
34 //------------------------------------ | |
35 //import dwt.dwthelper.utils; | |
36 //------------------------------------ | |
37 | |
38 import tango.io.FileRoots; | |
39 import tango.io.FilePath; | |
40 | |
41 import tango.util.collection.model.Seq; | |
42 import tango.util.collection.ArraySeq; | |
43 | |
44 | |
45 void main(){ | |
46 auto hw = new FileTree; | |
47 hw.run(); | |
48 } | |
49 | |
50 class FileTree : ApplicationWindow { | |
51 | |
52 TreeViewer tv; | |
53 | |
54 this(){ | |
55 super(null); | |
56 } | |
57 | |
58 public void run(){ | |
59 setBlockOnOpen(true); | |
60 open(); | |
61 Display.getCurrent().dispose(); | |
62 } | |
63 | |
64 protected void configureShell( Shell shell ){ | |
65 super.configureShell(shell); | |
66 shell.setText( "File Tree" ); | |
67 shell.setSize( 400, 400 ); | |
68 } | |
69 | |
70 protected Control createContents(Composite parent){ | |
71 /+ Label label = new Label( parent, DWT.CENTER ); | |
72 label.setText( "Hello, World" ); | |
73 return label;+/ | |
74 | |
75 auto composite = new Composite( parent, DWT.NONE ); | |
76 composite.setLayout( new GridLayout(1,false)); | |
77 | |
78 // Add a checkbox to toggle whether the labels preserve case | |
79 auto preserveCase = new Button( composite, DWT.CHECK ); | |
80 preserveCase.setText( "&Preserve case" ); | |
81 | |
82 // Create the tree viewer to display the file tree | |
83 tv = new TreeViewer( composite ); | |
84 tv.getTree().setLayoutData( new GridData( GridData.FILL_BOTH )); | |
85 tv.setContentProvider( new FileTreeContentProvider()); | |
86 tv.setLabelProvider( new FileTreeLabelProvider() ); | |
87 tv.setInput( stringcast("root") ); | |
88 | |
89 // When user checks the checkbox, toggle the preserve case attribute | |
90 // of the label provider | |
91 preserveCase.addSelectionListener( new class SelectionAdapter{ | |
92 public void widgetSelected( SelectionEvent event ){ | |
93 auto preserveCase = (cast(Button)event.widget).getSelection(); | |
94 auto ftlp = cast(FileTreeLabelProvider) tv.getLabelProvider(); | |
95 ftlp.setPreserveCase(preserveCase); | |
96 } | |
97 }); | |
98 return composite; | |
99 } | |
100 } | |
101 | |
102 class FileTreeContentProvider : ITreeContentProvider { | |
103 public override Object[] getChildren( Object arg0 ){ | |
104 Trace.formatln( "trc line={} ", __LINE__ ); | |
105 try{ | |
106 auto fp = cast(FilePath)arg0; | |
107 Object[] res; | |
108 if( !fp.isFolder()) { | |
109 return null; | |
110 } | |
111 foreach( item; fp ){ | |
112 res ~= FilePath.from( item ); | |
113 } | |
114 | |
115 Trace.formatln( "trc line={} ", __LINE__ ); | |
116 return res; | |
117 } | |
118 catch( Exception e ){ | |
119 ExceptionPrintStackTrace(e); | |
120 return null; | |
121 } | |
122 } | |
123 | |
124 public override Object getParent(Object arg0 ){ | |
125 Trace.formatln( "trc line={} ", __LINE__ ); | |
126 auto fp = cast(FilePath)arg0; | |
127 return fp.pop; | |
128 } | |
129 | |
130 public override bool hasChildren(Object arg0 ){ | |
131 Trace.formatln( "trc line={} ", __LINE__ ); | |
132 auto obj = getChildren(arg0); | |
133 return obj is null ? false : obj.length > 0; | |
134 } | |
135 | |
136 public override Object[] getElements( Object arg0 ){ | |
137 Trace.formatln( "trc line={} ", __LINE__ ); | |
138 Object[] res; | |
139 res ~= new FilePath( "/" ); | |
140 // foreach( root; FileRoots.list()){ | |
141 // res ~= new FilePath( root ); | |
142 // } | |
143 return res; | |
144 } | |
145 | |
146 public override void dispose(){ | |
147 } | |
148 | |
149 public override void inputChanged(Viewer arg0, Object arg1, Object arg2 ){ | |
150 } | |
151 | |
152 } | |
153 | |
154 class FileTreeLabelProvider : ILabelProvider { | |
155 | |
156 private Seq!(ILabelProviderListener) listeners; | |
157 | |
158 private Image file; | |
159 private Image dir; | |
160 | |
161 private bool preserveCase; | |
162 | |
163 public this(){ | |
164 Trace.formatln( "trc line={} Label", __LINE__ ); | |
165 listeners = new ArraySeq!(ILabelProviderListener); | |
166 | |
167 file = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "file.png" )))); | |
168 dir = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "folder.png" )))); | |
169 } | |
170 | |
171 public void setPreserveCase(bool preserveCase){ | |
172 Trace.formatln( "trc line={} Label", __LINE__ ); | |
173 this.preserveCase = preserveCase; | |
174 auto event = new LabelProviderChangedEvent(this); | |
175 for( int i = 0, n = listeners.size(); i < n; i++ ){ | |
176 auto ilpl = listeners.get(i); | |
177 ilpl.labelProviderChanged(event); | |
178 } | |
179 } | |
180 | |
181 public override Image getImage(Object arg0){ | |
182 Trace.formatln( "trc line={} Label", __LINE__ ); | |
183 return (cast(FilePath)arg0).isFolder() ? dir : file; | |
184 } | |
185 | |
186 public override char[] getText(Object arg0){ | |
187 auto text = (cast(FilePath)arg0).name(); | |
188 // if( text.length is 0 ){ | |
189 // text = (cast(FilePath)arg0).pop.name(); | |
190 // } | |
191 Trace.formatln( "name={} ", text ); | |
192 Trace.formatln( "trc line={} Label", __LINE__ ); | |
193 return "x" ~ (preserveCase ? text : text.toUpperCase()); | |
194 } | |
195 | |
196 public void addListener( ILabelProviderListener arg0 ){ | |
197 listeners.append(arg0); | |
198 } | |
199 | |
200 public void dispose(){ | |
201 if( dir !is null ) dir.dispose(); | |
202 if( file !is null ) file.dispose(); | |
203 } | |
204 | |
205 public bool isLabelProperty(Object arg0, char[] arg1){ | |
206 return false; | |
207 } | |
208 | |
209 public void removeListener(ILabelProviderListener arg0){ | |
210 listeners.remove(arg0); | |
211 } | |
212 } | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 |