1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet130"
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.Snippet130;
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 import java.lang.Thread;
71 
72 void main() {
73     Display display = new Display();
74     Shell shell = new Shell(display);
75     shell.setLayout(new GridLayout());
76     Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
77     text.setLayoutData(new GridData(GridData.FILL_BOTH));
78     int[] nextId = new int[1];
79     Button b = new Button(shell, SWT.PUSH);
80     b.setText("invoke long running job");
81 
82     b.addSelectionListener(new class SelectionAdapter {
83         override
84         public void widgetSelected(SelectionEvent e) {
85             Runnable longJob = new class Runnable {
86                 bool done = false;
87                 int id;
88                 public void run() {
89                     Thread thread = new Thread({
90                         id = nextId[0]++;
91                         display.syncExec(new class Runnable {
92                             public void run() {
93                             if (text.isDisposed()) return;
94                             text.append(Format("\nStart long running task {}", id));
95                             }
96                             }); // display.syncExec
97                         /*
98                          * This crashes when more than 1 thread gets created. THD
99                          for (int i = 0; i < 100000; i++) {
100                          if (display.isDisposed()) return;
101                          getDwtLogger().info(__FILE__, __LINE__, "do task that takes a long time in a separate thread {}", id);
102                          }
103                          */
104                         // This runs fine
105                         for (int i = 0; i < 6; i++) {
106                             if (display.isDisposed()) return;
107                             getDwtLogger().info( __FILE__, __LINE__, "do task that takes a long time in a separate thread {} {}/6", id, i);
108                             Thread.sleep(500);
109                         }
110 
111                         if (display.isDisposed()) return;
112                         display.syncExec(new class Runnable {
113                             public void run() {
114                                 if (text.isDisposed()) return;
115                                 text.append(Format("\nCompleted long running task {}", id));
116                             }
117                         }); // display.syncExec
118                         done = true;
119                         display.wake();
120                     }); // thread = ...
121                     thread.start();
122 
123                     while (!done && !shell.isDisposed()) {
124                         if (!display.readAndDispatch())
125                             display.sleep();
126                     }
127                 }
128             };  // Runnable longJob = ...
129             BusyIndicator.showWhile(display, longJob);
130         } // widgetSelected();
131     }); // addSelectionListener
132 
133 
134     shell.setSize(250, 150);
135     shell.open();
136     while (!shell.isDisposed()) {
137         if (!display.readAndDispatch())
138             display.sleep();
139     }
140     display.dispose();
141 }
142 
143 void printStart(Text text, int id ) {
144     if (text.isDisposed()) return;
145     getDwtLogger().info( __FILE__, __LINE__, "Start long running task {}", id );
146     text.append(Format("\nStart long running task {}", id));
147 }
148 
149 void printEnd(Text text, int id ) {
150     if (text.isDisposed()) return;
151     getDwtLogger().info( __FILE__, __LINE__, "Completed long running task {}", id );
152     text.append(Format("\nCompleted long running task {}", id));
153 }