78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 IBM Corporation 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 * IBM Corporation - initial API and implementation
|
|
10 * D Port:
|
|
11 * Jesse Phillips <Jesse.K.Phillips+D> gmail.com
|
|
12 *******************************************************************************/
|
|
13 module snippets.tree.Snippet8;
|
|
14
|
|
15 /*
|
|
16 * Tree example snippet: create a tree (lazy)
|
|
17 *
|
|
18 * For a list of all DWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21
|
|
22 import dwt.DWT;
|
|
23 import dwt.layout.FillLayout;
|
|
24 import dwt.graphics.Point;
|
|
25 import dwt.widgets.Display;
|
|
26 import dwt.widgets.Event;
|
|
27 import dwt.widgets.Listener;
|
|
28 import dwt.widgets.Shell;
|
|
29 import dwt.widgets.Tree;
|
|
30 import dwt.widgets.TreeItem;
|
|
31
|
|
32 import dwt.dwthelper.utils;
|
|
33 import tango.io.FilePath;
|
118
|
34 version(TANGOSVN){
|
|
35 import tango.io.FileSystem;
|
|
36 }
|
|
37 else{
|
78
|
38 import tango.io.FileRoots;
|
118
|
39 }
|
78
|
40
|
|
41 void main () {
|
|
42 auto display = new Display ();
|
|
43 auto shell = new Shell (display);
|
|
44 shell.setText ("Lazy Tree");
|
|
45 shell.setLayout (new FillLayout ());
|
|
46 auto tree = new Tree (shell, DWT.BORDER);
|
118
|
47 version(TANGOSVN){
|
|
48 auto roots = FileSystem.roots();
|
|
49 }
|
|
50 else{
|
78
|
51 auto roots = FileRoots.list();
|
118
|
52 }
|
78
|
53 foreach (file; roots) {
|
|
54 auto root = new TreeItem (tree, 0);
|
|
55 root.setText (file);
|
|
56 root.setData (new FilePath(file));
|
|
57 new TreeItem (root, 0);
|
|
58 }
|
|
59 tree.addListener (DWT.Expand, new class Listener {
|
|
60 public void handleEvent (Event event) {
|
|
61 auto root = cast(TreeItem) event.item;
|
|
62 auto items = root.getItems ();
|
|
63 foreach(item; items) {
|
|
64 if (item.getData () !is null) return;
|
|
65 item.dispose ();
|
|
66 }
|
|
67 auto file = cast(FilePath) root.getData ();
|
|
68 auto files = file.toList();
|
|
69 if (files is null) return;
|
|
70 foreach (f; files) {
|
|
71 TreeItem item = new TreeItem (root, 0);
|
|
72 item.setText (f.toString());
|
|
73 item.setData (f);
|
|
74 if (f.isFolder()) {
|
|
75 new TreeItem (item, 0);
|
|
76 }
|
|
77 }
|
|
78 }
|
|
79 });
|
|
80 auto size = tree.computeSize (300, DWT.DEFAULT);
|
|
81 auto width = Math.max (300, size.x);
|
|
82 auto height = Math.max (300, size.y);
|
|
83 shell.setSize (shell.computeSize (width, height));
|
|
84 shell.open ();
|
|
85 while (!shell.isDisposed ()) {
|
|
86 if (!display.readAndDispatch ()) display.sleep ();
|
|
87 }
|
|
88 display.dispose ();
|
|
89 }
|