Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet130.d @ 44:ed96ea2a2764
First swt win snippets are buiding with d2+phobos
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 25 Mar 2009 18:31:01 +0100 |
parents | 4e5843b771cc |
children | 536e43f63c81 |
rev | line source |
---|---|
28 | 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 org.eclipse.swt.snippets.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 org.eclipse.swt.SWT; | |
21 import org.eclipse.swt.events.SelectionAdapter; | |
22 import org.eclipse.swt.events.SelectionEvent; | |
23 import org.eclipse.swt.layout.GridLayout; | |
24 import org.eclipse.swt.layout.GridData; | |
25 import org.eclipse.swt.widgets.Shell; | |
26 import org.eclipse.swt.widgets.Button; | |
27 import org.eclipse.swt.widgets.Display; | |
28 import org.eclipse.swt.widgets.Shell; | |
29 import org.eclipse.swt.widgets.Text; | |
30 | |
31 | |
32 import org.eclipse.swt.custom.BusyIndicator; | |
33 | |
34 import java.lang.all; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
35 import java.lang.Thread; |
28 | 36 |
37 void main(String[] args){ | |
38 Snippet130.main(args); | |
39 } | |
40 | |
41 public class Snippet130 { | |
42 | |
43 public static void main(String[] args) { | |
44 Display display = new Display(); | |
45 Shell shell = new Shell(display); | |
46 shell.setLayout(new GridLayout()); | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
47 Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); |
28 | 48 text.setLayoutData(new GridData(GridData.FILL_BOTH)); |
49 int[] nextId = new int[1]; | |
50 Button b = new Button(shell, SWT.PUSH); | |
51 b.setText("invoke long running job"); | |
52 | |
53 b.addSelectionListener(new class() SelectionAdapter { | |
54 public void widgetSelected(SelectionEvent e) { | |
55 Runnable longJob = new class() Runnable { | |
56 bool done = false; | |
57 int id; | |
58 public void run() { | |
59 Thread thread = new Thread({ | |
60 id = nextId[0]++; | |
61 display.syncExec(new class() Runnable { | |
62 public void run() { | |
63 if (text.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
64 text.append(Format("\nStart long running task {}", id)); |
28 | 65 } |
66 }); // display.syncExec | |
67 /* | |
68 * This crashes when more than 1 thread gets created. THD | |
69 for (int i = 0; i < 100000; i++) { | |
70 if (display.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
71 getDwtLogger().info(__FILE__, __LINE__, "do task that takes a long time in a separate thread {}", id); |
28 | 72 } |
73 */ | |
74 // This runs fine | |
75 for (int i = 0; i < 6; i++) { | |
76 if (display.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
77 getDwtLogger().info( __FILE__, __LINE__, "do task that takes a long time in a separate thread {} {}/6", id, i); |
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
78 Thread.sleep(500); |
28 | 79 } |
80 | |
81 if (display.isDisposed()) return; | |
82 display.syncExec(new class() Runnable { | |
83 public void run() { | |
84 if (text.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
85 text.append(Format("\nCompleted long running task {}", id)); |
28 | 86 } |
87 }); // display.syncExec | |
88 done = true; | |
89 display.wake(); | |
90 }); // thread = ... | |
91 thread.start(); | |
92 | |
93 while (!done && !shell.isDisposed()) { | |
94 if (!display.readAndDispatch()) | |
95 display.sleep(); | |
96 } | |
97 } | |
98 }; // Runnable longJob = ... | |
99 BusyIndicator.showWhile(display, longJob); | |
100 } // widgetSelected(); | |
101 }); // addSelectionListener | |
102 | |
103 | |
104 shell.setSize(250, 150); | |
105 shell.open(); | |
106 while (!shell.isDisposed()) { | |
107 if (!display.readAndDispatch()) | |
108 display.sleep(); | |
109 } | |
110 display.dispose(); | |
111 } | |
112 private void printStart(Text text, int id ) { | |
113 if (text.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
114 getDwtLogger().info( __FILE__, __LINE__, "Start long running task {}", id ); |
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
115 text.append(Format("\nStart long running task {}", id)); |
28 | 116 } |
117 private void printEnd(Text text, int id ) { | |
118 if (text.isDisposed()) return; | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
119 getDwtLogger().info( __FILE__, __LINE__, "Completed long running task {}", id ); |
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
120 text.append(Format("\nCompleted long running task {}", id)); |
28 | 121 } |
122 } |