Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package ch.jgraber.blog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
/**
* Test Background Work for SWT based on tutorial on
* http://www.java2s.com/Code/JavaAPI
* /org.eclipse.swt.widgets/DisplayasyncExecRunnablerun.htm
*
* @author Johnny Graber
*/
public class SwtBackgroundDemo {
/**
* @param args
*/
public static void main(String[] args) {
final SwtBackgroundDemo window = new SwtBackgroundDemo();
window.open();
}
protected ProgressBar progress;
/**
* Open the window
*/
public void open() {
final Display s_display = Display.getDefault();
final Shell s_shell = new Shell(s_display);
s_shell.setSize(new Point(200, 200));
s_shell.setText("BackgroundWork Test");
s_shell.setLayout(new FillLayout());
s_shell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
if (event.detail == SWT.TRAVERSE_ESCAPE)
s_shell.dispose();
}
});
Composite s_composite = new Composite(s_shell, SWT.NONE);
s_composite.setLayout(new FillLayout(SWT.VERTICAL));
final Button workButton = new Button(s_composite, SWT.FLAT);
workButton.setText("Bereit!");
workButton.setLocation(new Point(0, 0));
workButton.setSize(100, 50);
progress = new ProgressBar(s_composite, SWT.HORIZONTAL | SWT.SMOOTH);
workButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
progress.setSelection(0);
workButton.setEnabled(false);
new LongRunningOperation(s_display, progress, workButton)
.start();
workButton.setText("SelectionListener beendet!");
}
});
final Button workButtonSlow = new Button(s_composite, SWT.FLAT);
workButtonSlow.setText("Bereit! [Blockiert]");
workButtonSlow.setLocation(new Point(0, 0));
workButtonSlow.setSize(100, 50);
workButtonSlow.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
progress.setSelection(0);
workButtonSlow.setEnabled(false);
// simuliert lange dauernde Aktion
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
progress.setSelection(progress.getSelection() + 1);
}
workButtonSlow.setText("Thread beendet");
workButtonSlow.setEnabled(true);
}
});
s_shell.layout();
s_shell.open();
while (!s_shell.isDisposed()) {
if (!s_display.readAndDispatch())
s_display.sleep();
}
}
class LongRunningOperation extends Thread {
private Display display;
private ProgressBar progressBar;
private Button workButton;
/**
* Alles übergeben was aus diesem Thread erreichbar sein soll
*/
public LongRunningOperation(Display display, ProgressBar progressBar,
Button workButton) {
this.display = display;
this.progressBar = progressBar;
this.workButton = workButton;
}
/**
* Länger laufende Methode um eine Verarbeitung zu simulieren
*/
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
progressBar.setSelection(progressBar.getSelection() + 1);
// ProgressBar kann nur via asyncExec aktualisiert werden!
display.asyncExec(new Runnable() {
public void run() {
if (progressBar.isDisposed())
return;
progressBar.setSelection(progressBar.getSelection() + 1);
}
});
}
// Gleiches gilt für alle GUI-Elemente
display.asyncExec(new Runnable() {
public void run() {
if (workButton.isDisposed())
return;
workButton.setText("Thread beendet");
workButton.setEnabled(true);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment