1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet8" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2004 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * D Port: 46 * Jesse Phillips <Jesse.K.Phillips+D> gmail.com 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet8; 49 50 /* 51 * Tree example snippet: create a tree (lazy) 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 */ 56 57 import org.eclipse.swt.SWT; 58 import org.eclipse.swt.layout.FillLayout; 59 import org.eclipse.swt.graphics.Point; 60 import org.eclipse.swt.widgets.Display; 61 import org.eclipse.swt.widgets.Event; 62 import org.eclipse.swt.widgets.Listener; 63 import org.eclipse.swt.widgets.Shell; 64 import org.eclipse.swt.widgets.Tree; 65 import org.eclipse.swt.widgets.TreeItem; 66 67 import java.lang.all; 68 version(Tango){ 69 import tango.io.FilePath; 70 import tango.io.FileSystem; 71 } else { // Phobos 72 import std.file; 73 import std.path; 74 class FileSystem { 75 static string[] roots() {return [absolutePath(dirSeparator)];} 76 } 77 class FilePath { 78 string path; 79 this (string path) {this.path = path;} 80 FilePath[] toList() { 81 FilePath[] r; 82 foreach (string file; dirEntries(path, SpanMode.shallow)) { 83 r ~= new FilePath(std.path.buildPath(path, file)); 84 } 85 return r; 86 } 87 bool isFolder() {return exists(path) && isDir(path);} 88 override 89 string toString() {return path;} 90 } 91 } 92 93 void main () { 94 auto display = new Display (); 95 auto shell = new Shell (display); 96 shell.setText ("Lazy Tree"); 97 shell.setLayout (new FillLayout ()); 98 auto tree = new Tree (shell, SWT.BORDER); 99 auto roots = FileSystem.roots(); 100 foreach (file; roots) { 101 auto root = new TreeItem (tree, 0); 102 root.setText (file); 103 root.setData (new FilePath(file)); 104 new TreeItem (root, 0); 105 } 106 tree.addListener (SWT.Expand, new class Listener { 107 public void handleEvent (Event event) { 108 auto root = cast(TreeItem) event.item; 109 auto items = root.getItems (); 110 foreach(item; items) { 111 if (item.getData () !is null) return; 112 item.dispose (); 113 } 114 auto file = cast(FilePath) root.getData (); 115 auto files = file.toList(); 116 if (files is null) return; 117 foreach (f; files) { 118 TreeItem item = new TreeItem (root, 0); 119 item.setText (f.toString()); 120 item.setData (f); 121 if (f.isFolder()) { 122 new TreeItem (item, 0); 123 } 124 } 125 } 126 }); 127 auto size = tree.computeSize (300, SWT.DEFAULT); 128 auto width = Math.max (300, size.x); 129 auto height = Math.max (300, size.y); 130 shell.setSize (shell.computeSize (width, height)); 131 shell.open (); 132 while (!shell.isDisposed ()) { 133 if (!display.readAndDispatch ()) display.sleep (); 134 } 135 display.dispose (); 136 }