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