| Showing the system out (from logger) into a text. [message #875988] |
Wed, 23 May 2012 13:28  |
Chung Ley Messages: 5 Registered: May 2010 |
Junior Member |
|
|
Hi,
I have a SWT UI that collects inputs from the user and then execute some jobs for them when they click on the Go button.
To help the user, I create a modal dialog (shell) with a multi-line text and redirect the system.out and System.err so that they will show up into this text object. I was able to find help with some code snippet on that.
The code seems to work where the text component will get the updates from the logger. However, if I try to re-size, move, or switching between the windows before the job is completed, the entire shell will be "blank" until the job is finished AND then shell will show up with ALL the text into the Text area. My guess is that the thread that I am creating is "blocking" the regular UI update thread; but I don't know any workaround for it. Or there is something wrong that I am doing... Can someone help?>
Here is a snippet of the code itself...
goButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String argumentList = validateInputs();
if (argumentList == null) {
return;
}
dialogShell = new Shell(shell, SWT.PRIMARY_MODAL | SWT.SHEET
| SWT.RESIZE);
GridData gridData;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
dialogShell.setLayout(gridLayout);
final Text logText = new Text(dialogShell, SWT.MULTI | SWT.BORDER
| SWT.READ_ONLY | SWT.V_SCROLL);
logText.setLayoutData(new GridData(GridData.FILL,
GridData.FILL, true, true));
origOutPS = System.out;
origErrPS = System.err;
final Button closeButton = new Button(dialogShell, SWT.PUSH);
gridData = new GridData(GridData.END, GridData.CENTER, false,
false);
closeButton.setLayoutData(gridData);
closeButton.setText("Close");
closeButton.setEnabled(false);
closeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogShell.dispose();
}
});
dialogShell.setDefaultButton(closeButton);
dialogShell.setImage(imageGF);
dialogShell.addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
// TODO
}
});
dialogShell.setSize(550, 250);
dialogShell.setText("Processing....");
dialogShell.addShellListener(new ShellAdapter() {
public void shellActivated(ShellEvent event) {
myThread = new Thread(new Runnable() {
public void run() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
OutputStream out = new OutputStream() {
public void write(int b)
throws IOException {
logText.append(String
.valueOf((char) b));
}
public void write(byte[] b,
int off, int len) {
logText.append(new String(b,
off, len));
}
};
PrintStream ps = new PrintStream(out,
true);
System.setOut(ps);
System.setErr(ps);
try {
myHugeDummyJob(argumentList.split(","));
closeButton.setEnabled(true);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
});
}
});
myThread.start();
}
});
dialogShell.open();
// Setting the Out and Err stream back to the original...
System.setOut(origOutPS);
System.setErr(origErrPS);
}
});
|
|
|