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