62
|
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 try{
|
|
105 auto fp = cast(FilePath)arg0;
|
|
106 Object[] res;
|
|
107 if( !fp.isFolder()) {
|
|
108 return null;
|
|
109 }
|
|
110 foreach( item; fp ){
|
|
111 res ~= FilePath.from( item );
|
|
112 }
|
|
113
|
|
114 return res;
|
|
115 }
|
|
116 catch( Exception e ){
|
|
117 ExceptionPrintStackTrace(e);
|
|
118 return null;
|
|
119 }
|
|
120 }
|
|
121
|
|
122 public override Object getParent(Object arg0 ){
|
|
123 auto fp = cast(FilePath)arg0;
|
|
124 return fp.pop;
|
|
125 }
|
|
126
|
|
127 public override bool hasChildren(Object arg0 ){
|
|
128 auto obj = getChildren(arg0);
|
|
129 return obj is null ? false : obj.length > 0;
|
|
130 }
|
|
131
|
|
132 public override Object[] getElements( Object arg0 ){
|
|
133 Object[] res;
|
63
|
134
|
|
135 foreach( root; FileRoots.list()){
|
|
136 res ~= new FilePath( root );
|
|
137 }
|
|
138
|
62
|
139 return res;
|
|
140 }
|
|
141
|
|
142 public override void dispose(){
|
|
143 }
|
|
144
|
|
145 public override void inputChanged(Viewer arg0, Object arg1, Object arg2 ){
|
|
146 }
|
|
147
|
|
148 }
|
|
149
|
|
150 class FileTreeLabelProvider : ILabelProvider {
|
|
151
|
|
152 private Seq!(ILabelProviderListener) listeners;
|
|
153
|
|
154 private Image file;
|
|
155 private Image dir;
|
|
156
|
|
157 private bool preserveCase;
|
|
158
|
|
159 public this(){
|
|
160 Trace.formatln( "trc line={} Label", __LINE__ );
|
|
161 listeners = new ArraySeq!(ILabelProviderListener);
|
|
162
|
|
163 file = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "file.png" ))));
|
|
164 dir = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "folder.png" ))));
|
|
165 }
|
|
166
|
|
167 public void setPreserveCase(bool preserveCase){
|
|
168 Trace.formatln( "trc line={} Label", __LINE__ );
|
|
169 this.preserveCase = preserveCase;
|
|
170 auto event = new LabelProviderChangedEvent(this);
|
|
171 for( int i = 0, n = listeners.size(); i < n; i++ ){
|
|
172 auto ilpl = listeners.get(i);
|
|
173 ilpl.labelProviderChanged(event);
|
|
174 }
|
|
175 }
|
|
176
|
|
177 public override Image getImage(Object arg0){
|
|
178 Trace.formatln( "trc line={} Label", __LINE__ );
|
|
179 return (cast(FilePath)arg0).isFolder() ? dir : file;
|
|
180 }
|
|
181
|
|
182 public override char[] getText(Object arg0){
|
|
183 auto text = (cast(FilePath)arg0).name();
|
|
184 // if( text.length is 0 ){
|
|
185 // text = (cast(FilePath)arg0).pop.name();
|
|
186 // }
|
|
187 Trace.formatln( "name={} ", text );
|
|
188 Trace.formatln( "trc line={} Label", __LINE__ );
|
|
189 return "x" ~ (preserveCase ? text : text.toUpperCase());
|
|
190 }
|
|
191
|
|
192 public void addListener( ILabelProviderListener arg0 ){
|
|
193 listeners.append(arg0);
|
|
194 }
|
|
195
|
|
196 public void dispose(){
|
|
197 if( dir !is null ) dir.dispose();
|
|
198 if( file !is null ) file.dispose();
|
|
199 }
|
|
200
|
|
201 public bool isLabelProperty(Object arg0, char[] arg1){
|
|
202 return false;
|
|
203 }
|
|
204
|
|
205 public void removeListener(ILabelProviderListener arg0){
|
|
206 listeners.remove(arg0);
|
|
207 }
|
|
208 }
|
|
209
|
|
210
|
|
211
|
|
212
|
|
213
|
|
214
|
|
215
|
|
216
|
|
217
|
|
218
|
|
219
|
|
220
|
|
221
|
|
222
|