28
|
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 org.eclipse.swt.snippets.Snippet8;
|
|
14
|
|
15 /*
|
|
16 * Tree example snippet: create a tree (lazy)
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21
|
|
22 import org.eclipse.swt.SWT;
|
|
23 import org.eclipse.swt.layout.FillLayout;
|
|
24 import org.eclipse.swt.graphics.Point;
|
|
25 import org.eclipse.swt.widgets.Display;
|
|
26 import org.eclipse.swt.widgets.Event;
|
|
27 import org.eclipse.swt.widgets.Listener;
|
|
28 import org.eclipse.swt.widgets.Shell;
|
|
29 import org.eclipse.swt.widgets.Tree;
|
|
30 import org.eclipse.swt.widgets.TreeItem;
|
|
31
|
|
32 import java.lang.all;
|
112
|
33 version(Tango){
|
|
34 import tango.io.FilePath;
|
|
35 import tango.io.FileSystem;
|
|
36 } else { // Phobos
|
|
37 import std.file;
|
|
38 import std.path;
|
|
39 class FileSystem {
|
|
40 static string[] roots() {return [rel2abs(sep)];}
|
|
41 }
|
|
42 class FilePath {
|
|
43 string path;
|
|
44 this (string path) {this.path = path;}
|
|
45 FilePath[] toList() {
|
|
46 FilePath[] r;
|
|
47 foreach (file; listDir(path)) {
|
|
48 r ~= new FilePath(std.path.join(path, file));
|
|
49 }
|
|
50 return r;
|
|
51 }
|
|
52 bool isFolder() {return exists(path) && isDir(path);}
|
|
53 string toString() {return path;}
|
|
54 }
|
|
55 }
|
28
|
56
|
|
57 void main () {
|
|
58 auto display = new Display ();
|
|
59 auto shell = new Shell (display);
|
|
60 shell.setText ("Lazy Tree");
|
|
61 shell.setLayout (new FillLayout ());
|
|
62 auto tree = new Tree (shell, SWT.BORDER);
|
|
63 auto roots = FileSystem.roots();
|
|
64 foreach (file; roots) {
|
|
65 auto root = new TreeItem (tree, 0);
|
|
66 root.setText (file);
|
|
67 root.setData (new FilePath(file));
|
|
68 new TreeItem (root, 0);
|
|
69 }
|
|
70 tree.addListener (SWT.Expand, new class Listener {
|
|
71 public void handleEvent (Event event) {
|
|
72 auto root = cast(TreeItem) event.item;
|
|
73 auto items = root.getItems ();
|
|
74 foreach(item; items) {
|
|
75 if (item.getData () !is null) return;
|
|
76 item.dispose ();
|
|
77 }
|
|
78 auto file = cast(FilePath) root.getData ();
|
|
79 auto files = file.toList();
|
|
80 if (files is null) return;
|
|
81 foreach (f; files) {
|
|
82 TreeItem item = new TreeItem (root, 0);
|
|
83 item.setText (f.toString());
|
|
84 item.setData (f);
|
|
85 if (f.isFolder()) {
|
|
86 new TreeItem (item, 0);
|
|
87 }
|
|
88 }
|
|
89 }
|
|
90 });
|
|
91 auto size = tree.computeSize (300, SWT.DEFAULT);
|
|
92 auto width = Math.max (300, size.x);
|
|
93 auto height = Math.max (300, size.y);
|
|
94 shell.setSize (shell.computeSize (width, height));
|
|
95 shell.open ();
|
|
96 while (!shell.isDisposed ()) {
|
|
97 if (!display.readAndDispatch ()) display.sleep ();
|
|
98 }
|
|
99 display.dispose ();
|
|
100 }
|