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;
|
|
33 import tango.io.FilePath;
|
|
34 import tango.io.FileSystem;
|
|
35
|
|
36 void main () {
|
|
37 auto display = new Display ();
|
|
38 auto shell = new Shell (display);
|
|
39 shell.setText ("Lazy Tree");
|
|
40 shell.setLayout (new FillLayout ());
|
|
41 auto tree = new Tree (shell, SWT.BORDER);
|
|
42 auto roots = FileSystem.roots();
|
|
43 foreach (file; roots) {
|
|
44 auto root = new TreeItem (tree, 0);
|
|
45 root.setText (file);
|
|
46 root.setData (new FilePath(file));
|
|
47 new TreeItem (root, 0);
|
|
48 }
|
|
49 tree.addListener (SWT.Expand, new class Listener {
|
|
50 public void handleEvent (Event event) {
|
|
51 auto root = cast(TreeItem) event.item;
|
|
52 auto items = root.getItems ();
|
|
53 foreach(item; items) {
|
|
54 if (item.getData () !is null) return;
|
|
55 item.dispose ();
|
|
56 }
|
|
57 auto file = cast(FilePath) root.getData ();
|
|
58 auto files = file.toList();
|
|
59 if (files is null) return;
|
|
60 foreach (f; files) {
|
|
61 TreeItem item = new TreeItem (root, 0);
|
|
62 item.setText (f.toString());
|
|
63 item.setData (f);
|
|
64 if (f.isFolder()) {
|
|
65 new TreeItem (item, 0);
|
|
66 }
|
|
67 }
|
|
68 }
|
|
69 });
|
|
70 auto size = tree.computeSize (300, SWT.DEFAULT);
|
|
71 auto width = Math.max (300, size.x);
|
|
72 auto height = Math.max (300, size.y);
|
|
73 shell.setSize (shell.computeSize (width, height));
|
|
74 shell.open ();
|
|
75 while (!shell.isDisposed ()) {
|
|
76 if (!display.readAndDispatch ()) display.sleep ();
|
|
77 }
|
|
78 display.dispose ();
|
|
79 }
|