diff snippets/tree/Snippet8.d @ 78:4a04b6759f98

Clean up directory names
author John Reimer <terminal.node@gmail.com>
date Sat, 10 May 2008 13:32:45 -0700
parents
children 7b1c122b4128
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/tree/Snippet8.d	Sat May 10 13:32:45 2008 -0700
@@ -0,0 +1,79 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * D Port:
+ *     Jesse Phillips <Jesse.K.Phillips+D> gmail.com
+ *******************************************************************************/
+module snippets.tree.Snippet8;
+
+/*
+ * Tree example snippet: create a tree (lazy)
+ *
+ * For a list of all DWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ */
+
+import dwt.DWT;
+import dwt.layout.FillLayout;
+import dwt.graphics.Point;
+import dwt.widgets.Display;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.widgets.Shell;
+import dwt.widgets.Tree;
+import dwt.widgets.TreeItem;
+
+import dwt.dwthelper.utils;
+import tango.io.FilePath;
+import tango.io.FileRoots;
+
+void main () {
+    auto display = new Display ();
+    auto shell = new Shell (display);
+    shell.setText ("Lazy Tree");
+    shell.setLayout (new FillLayout ());
+    auto tree = new Tree (shell, DWT.BORDER);
+    auto roots = FileRoots.list();
+    foreach (file; roots) {
+        auto root = new TreeItem (tree, 0);
+        root.setText (file);
+        root.setData (new FilePath(file));
+        new TreeItem (root, 0);
+    }
+    tree.addListener (DWT.Expand, new class Listener {
+        public void handleEvent (Event event) {
+            auto root = cast(TreeItem) event.item;
+            auto items = root.getItems ();
+            foreach(item; items) {
+                if (item.getData () !is null) return;
+                item.dispose ();
+            }
+            auto file = cast(FilePath) root.getData ();
+            auto files = file.toList();
+            if (files is null) return;
+            foreach (f; files) {
+                TreeItem item = new TreeItem (root, 0);
+                item.setText (f.toString());
+                item.setData (f);
+                if (f.isFolder()) {
+                    new TreeItem (item, 0);
+                }
+            }
+        }
+    });
+    auto size = tree.computeSize (300, DWT.DEFAULT);
+    auto width = Math.max (300, size.x);
+    auto height = Math.max (300, size.y);
+    shell.setSize (shell.computeSize (width, height));
+    shell.open ();
+    while (!shell.isDisposed ()) {
+        if (!display.readAndDispatch ()) display.sleep ();
+    }
+    display.dispose ();
+}