Mercurial > projects > dwt-samples
comparison snippets/busyindicator/Snippet130.d @ 113:7194dba256b8
Add Snippet130. Thanks TomD.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Jul 2008 01:42:44 +0200 |
parents | |
children | 8cdaac0dc743 |
comparison
equal
deleted
inserted
replaced
112:cfb304d22f28 | 113:7194dba256b8 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2004 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 | |
12 /* | |
13 * BusyIndicator example snippet: display busy cursor during long running task | |
14 * | |
15 * For a list of all SWT example snippets see | |
16 * http://www.eclipse.org/swt/snippets/ | |
17 */ | |
18 module busyindicator.Snippet130; | |
19 | |
20 import dwt.DWT; | |
21 import dwt.events.SelectionAdapter; | |
22 import dwt.events.SelectionEvent; | |
23 import dwt.layout.GridLayout; | |
24 import dwt.layout.GridData; | |
25 import dwt.widgets.Shell; | |
26 import dwt.widgets.Button; | |
27 import dwt.widgets.Display; | |
28 import dwt.widgets.Shell; | |
29 import dwt.widgets.Text; | |
30 | |
31 | |
32 import dwt.custom.BusyIndicator; | |
33 | |
34 import dwt.dwthelper.utils; | |
35 import dwt.dwthelper.Runnable; | |
36 import dwt.dwthelper.System; | |
37 | |
38 import tango.core.Thread; | |
39 import tango.util.Convert; | |
40 import tango.util.log.Trace; | |
41 | |
42 void main(String[] args){ | |
43 Snippet130.main(args); | |
44 } | |
45 | |
46 public class Snippet130 { | |
47 | |
48 public static void main(String[] args) { | |
49 Display display = new Display(); | |
50 Shell shell = new Shell(display); | |
51 shell.setLayout(new GridLayout()); | |
52 Text text = new Text(shell, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL); | |
53 text.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
54 int[1] nextId; | |
55 Button b = new Button(shell, DWT.PUSH); | |
56 b.setText("invoke long running job"); | |
57 | |
58 b.addSelectionListener(new class() SelectionAdapter { | |
59 public void widgetSelected(SelectionEvent e) { | |
60 Runnable longJob = new class() Runnable { | |
61 bool done = false; | |
62 int id; | |
63 private void runThread(){ | |
64 id = nextId[0]++; | |
65 display.syncExec( dgRunnable( &printStart, text, id )); | |
66 for (int i = 0; i < 6; i++) { | |
67 if (display.isDisposed()) return; | |
68 Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i); | |
69 Thread.sleep(0.500); | |
70 } | |
71 if (display.isDisposed()) return; | |
72 display.syncExec( dgRunnable( &printEnd , text, id )); // display.syncExec | |
73 done = true; | |
74 display.wake(); | |
75 } | |
76 public void run() { | |
77 Thread thread = new Thread( &runThread ); | |
78 thread.start(); | |
79 | |
80 while (!done && !shell.isDisposed()) { | |
81 if (!display.readAndDispatch()) | |
82 display.sleep(); | |
83 } | |
84 } | |
85 }; // Runnable longJob = ... | |
86 BusyIndicator.showWhile(display, longJob); | |
87 } // widgetSelected(); | |
88 }); // addSelectionListener | |
89 | |
90 | |
91 shell.setSize(250, 150); | |
92 shell.open(); | |
93 while (!shell.isDisposed()) { | |
94 if (!display.readAndDispatch()) | |
95 display.sleep(); | |
96 } | |
97 display.dispose(); | |
98 } | |
99 private void printStart(Text text, int id ) { | |
100 if (text.isDisposed()) return; | |
101 Trace.formatln( "Start long running task {}", id ); | |
102 text.append("\nStart long running task "~to!(char[])(id)); | |
103 } | |
104 private void printEnd(Text text, int id ) { | |
105 if (text.isDisposed()) return; | |
106 Trace.formatln( "Completed long running task {}", id ); | |
107 text.append("\nCompleted long running task "~to!(char[])(id)); | |
108 } | |
109 } | |
110 | |
111 |