1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet130a"
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  *     Thomas Demmer <t_demmer AT web DOT de>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet130a;
49 /*
50  * BusyIndicator example snippet: display busy cursor during long running task
51  *
52  * For a list of all SWT example snippets see
53  * http://www.eclipse.org/swt/snippets/
54  */
55 import org.eclipse.swt.SWT;
56 import org.eclipse.swt.events.SelectionAdapter;
57 import org.eclipse.swt.events.SelectionEvent;
58 import org.eclipse.swt.layout.GridLayout;
59 import org.eclipse.swt.layout.GridData;
60 import org.eclipse.swt.widgets.Shell;
61 import org.eclipse.swt.widgets.Button;
62 import org.eclipse.swt.widgets.Display;
63 import org.eclipse.swt.widgets.Shell;
64 import org.eclipse.swt.widgets.Text;
65 
66 
67 import org.eclipse.swt.custom.BusyIndicator;
68 
69 import java.lang.all;
70 
71 version(Tango){
72     //import tango.core.Thread;
73     import tango.io.Stdout;
74     import tango.util.Convert;
75     import tango.util.log.Trace;
76 } else { // Phobos
77     import std.conv;
78     import std.stdio;
79 }
80 
81 
82 void main() {
83     Display display = new Display();
84     Shell shell = new Shell(display);
85     shell.setLayout(new GridLayout());
86     Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
87     text.setLayoutData(new GridData(GridData.FILL_BOTH));
88     int[] nextId = new int[1];
89     Button b = new Button(shell, SWT.PUSH);
90     b.setText("invoke long running job");
91 
92     b.addSelectionListener(new class SelectionAdapter {
93         override
94         public void widgetSelected(SelectionEvent e) {
95             Runnable longJob = new class Runnable {
96                 bool done = false;
97                 int id;
98                 public void run() {
99                     Thread thread = new Thread({
100                         id = nextId[0]++;
101                         display.syncExec( dgRunnable( &printStart, text, id ));
102                         for (int i = 0; i < 6; i++) {
103                         if (display.isDisposed()) return;
104                         version(Tango){
105                             Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i);
106                         } else { // Phobos
107                             writefln("do task that takes a long time in a separate thread %s %s/6", id, i);
108                         }
109                         Thread.sleep(500);
110                         }
111                         /*
112                         for (int i = 0; i < 100000; i++) {
113                             if (display.isDisposed()) return;
114                             Stdout.formatln("do task that takes a long time in a separate thread {}", id);
115                         }
116                         */
117                         if (display.isDisposed()) return;
118                         display.syncExec( dgRunnable( &printEnd, text, id ));
119                         done = true;
120                         display.wake();
121                     }); // thread = ...
122                     thread.start();
123 
124                     while (!done && !shell.isDisposed()) {
125                         if (!display.readAndDispatch())
126                             display.sleep();
127                     }
128                 }
129             };  // Runnable longJob = ...
130             BusyIndicator.showWhile(display, longJob);
131         } // widgetSelected();
132     }); // addSelectionListener
133 
134 
135     shell.setSize(250, 150);
136     shell.open();
137     while (!shell.isDisposed()) {
138         if (!display.readAndDispatch())
139             display.sleep();
140     }
141     display.dispose();
142 }
143 
144 void printStart(Text text, int id ) {
145     if (text.isDisposed()) return;
146     version(Tango){
147         Trace.formatln( "Start long running task {}", id );
148     } else { // Phobos
149         writefln( "Start long running task %s", id );
150     }
151     text.append("\nStart long running task "~to!(String)(id));
152 }
153 
154 void printEnd(Text text, int id ) {
155     if (text.isDisposed()) return;
156     version(Tango){
157         Trace.formatln( "Completed long running task {}", id );
158     } else { // Phobos
159         writefln( "Completed long running task %s", id );
160     }
161     text.append("\nCompleted long running task "~to!(String)(id));
162 }