Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Showing the system out (from logger) into a text.(This works if I don't move, resize, iconified or switching between windows.)
Showing the system out (from logger) into a text. [message #875988] Wed, 23 May 2012 17:28 Go to next message
Chung Ley is currently offline Chung LeyFriend
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);

	}
});
Re: Showing the system out (from logger) into a text. [message #876345 is a reply to message #875988] Thu, 24 May 2012 12:33 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 242
Registered: July 2009
Senior Member
As you surmised, your job is currently executing on the SWT thread: although you're starting a new thread, you end up performing your long-running operation within the asyncExec() runnable. If you put a println at the end of your myThread's runnable, you will see that your myThread finishes almost immediately.

Change your code to remove the big asyncExec() wrapper and instead cause your OutputStream wrapper to dispatch each write() call through an asyncExec to perform the logText.append(), and your final closeButton too.

Brian.
Re: Showing the system out (from logger) into a text. [message #876451 is a reply to message #876345] Thu, 24 May 2012 16:03 Go to previous message
Chung Ley is currently offline Chung LeyFriend
Messages: 5
Registered: May 2010
Junior Member
Thank you, Brian...

What you suggested made sense, but I might not have fully understand or implement it correctly. After I made the change, the log messages from the logger goes back to the screen instead of the component logText, and the last message was invalid thread access.

Here is the code with the change (I didn't bother with the closeButton yet).
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() {
						OutputStream out = new OutputStream() {
							public void write(int b)
									throws IOException {
								Display.getDefault().asyncExec(new Runnable() {
									finat int b_ = b;
									public void run() {
										logText.append(String
												.valueOf((char) b_));
									}
								});
							}

							public void write(byte[] b,
									int off, int len) {
								Display.getDefault().asyncExec(new Runnable() {
									final byte[] b_ = b;
									final int off_ = off;
									final int len_ = len;

									public void run() {
									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);

	}
});
Previous Topic:Changing focus when the component is being activated.
Next Topic:SWT Spinner internationalize
Goto Forum:
  


Current Time: Fri Mar 29 01:49:40 GMT 2024

Powered by FUDForum. Page generated in 0.02372 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top