117
|
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 * D Port:
|
|
11 * Thomas Demmer <t_demmer AT web DOT de>
|
|
12 *******************************************************************************/
|
|
13 module busyindicator.Snippet130;
|
|
14 /*
|
|
15 * BusyIndicator example snippet: display busy cursor during long running task
|
|
16 *
|
|
17 * For a list of all SWT example snippets see
|
|
18 * http://www.eclipse.org/swt/snippets/
|
|
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.io.Stdout;
|
|
40 import tango.util.Convert;
|
|
41 import tango.util.log.Trace;
|
|
42
|
|
43
|
|
44 void main(String[] args){
|
|
45 Snippet130.main(args);
|
|
46 }
|
|
47
|
|
48 public class Snippet130 {
|
|
49
|
|
50 public static void main(String[] args) {
|
|
51 Display display = new Display();
|
|
52 Shell shell = new Shell(display);
|
|
53 shell.setLayout(new GridLayout());
|
|
54 Text text = new Text(shell, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL);
|
|
55 text.setLayoutData(new GridData(GridData.FILL_BOTH));
|
|
56 int[] nextId = new int[1];
|
|
57 Button b = new Button(shell, DWT.PUSH);
|
|
58 b.setText("invoke long running job");
|
|
59
|
|
60 b.addSelectionListener(new class() SelectionAdapter {
|
|
61 public void widgetSelected(SelectionEvent e) {
|
|
62 Runnable longJob = new class() Runnable {
|
|
63 bool done = false;
|
|
64 int id;
|
|
65 public void run() {
|
|
66 Thread thread = new Thread({
|
|
67 id = nextId[0]++;
|
|
68 display.syncExec( dgRunnable( &printStart, text, id ));
|
|
69 for (int i = 0; i < 6; i++) {
|
|
70 if (display.isDisposed()) return;
|
|
71 Trace.formatln("do task that takes a long time in a separate thread {} {}/6", id, i);
|
|
72 Thread.sleep(0.500);
|
|
73 }
|
|
74 /*
|
|
75 for (int i = 0; i < 100000; i++) {
|
|
76 if (display.isDisposed()) return;
|
|
77 Stdout.formatln("do task that takes a long time in a separate thread {}", id);
|
|
78 }
|
|
79 */
|
|
80 if (display.isDisposed()) return;
|
|
81 display.syncExec( dgRunnable( &printEnd, text, id ));
|
|
82 done = true;
|
|
83 display.wake();
|
|
84 }); // thread = ...
|
|
85 thread.start();
|
|
86
|
|
87 while (!done && !shell.isDisposed()) {
|
|
88 if (!display.readAndDispatch())
|
|
89 display.sleep();
|
|
90 }
|
|
91 }
|
|
92 }; // Runnable longJob = ...
|
|
93 BusyIndicator.showWhile(display, longJob);
|
|
94 } // widgetSelected();
|
|
95 }); // addSelectionListener
|
|
96
|
|
97
|
|
98 shell.setSize(250, 150);
|
|
99 shell.open();
|
|
100 while (!shell.isDisposed()) {
|
|
101 if (!display.readAndDispatch())
|
|
102 display.sleep();
|
|
103 }
|
|
104 display.dispose();
|
|
105 }
|
|
106 private void printStart(Text text, int id ) {
|
|
107 if (text.isDisposed()) return;
|
|
108 Trace.formatln( "Start long running task {}", id );
|
|
109 text.append("\nStart long running task "~to!(char[])(id));
|
|
110 }
|
|
111 private void printEnd(Text text, int id ) {
|
|
112 if (text.isDisposed()) return;
|
|
113 Trace.formatln( "Completed long running task {}", id );
|
|
114 text.append("\nCompleted long running task "~to!(char[])(id));
|
|
115 }
|
|
116 }
|