Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Flashing Window Title Text using Display.async(Creating a window where the Title of the window can be changed continuously)
Flashing Window Title Text using Display.async [message #486917] Mon, 21 September 2009 00:26 Go to next message
Ricky Patel is currently offline Ricky PatelFriend
Messages: 33
Registered: July 2009
Member
After looking and figuring out that getting a window to request attention of the user without bringing the window to the front (aka. flash in the taskbar in windows, jump up and down on a mac, etc) I decided to explore other routes and ideas.

I was messing around and thought it would be cool to try and make a rotating window title or a flashing window title, just something of that nature because if I cant make the item flash, I can at least be as annoying as can be. I tried the following just for a generic text changing tool:

Display.getCurrent().asyncExec(new Runnable (){
	@Override
	public void run ()
	{
		String text = tempWindow.getText();
		//flash 10 times and thats it
		for (int x=0;x<10;x++)
		{
			if (tempWindow.getDisplay().getActiveShell() == tempWindow.getShell())
				break;
			tempWindow.setText("SEE ME!!!");
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
				if (tempWindow.getDisplay().getActiveShell() == tempWindow.getShell())
					break;
			tempWindow.setText(text);
			try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
});


Overall what it is supposed to do is in every second, switch between the current window text to the text "SEE ME!" and then in another second switch back.

The issue is, this code freezes up the UI thread so that all the threads in the Display cannot refresh. This means that Windows will give a lovely "Not responding" on the title bar since the UI isn't responding.

And I'm assuming once we get this to run truly in another thread, that adding a ShellListener will allow knowledge that the user has clicked on a window and we can kill the async method which will be the next step for this project.

Anyone have any ideas or suggestions on how to make this work? Or how to make a window request attention?
Re: Flashing Window Title Text using Display.async [message #486918 is a reply to message #486917] Mon, 21 September 2009 00:42 Go to previous messageGo to next message
Remy Suen is currently offline Remy SuenFriend
Messages: 462
Registered: July 2009
Senior Member
On Sun, 20 Sep 2009 20:26:48 -0400, Ricky Patel wrote:
> The issue is, this code freezes up the UI thread so that all the threads
> in the Display cannot refresh. This means that Windows will give a
> lovely "Not responding" on the title bar since the UI isn't responding.

You're sleeping on the UI thread (via your Thread.sleep(1000) method
call), this is naturally going to freeze the UI thread.

Use Display's timerExec(int, Runnable) method or a job or whatever to
enqueue multiple requests over a set timed interval instead.

Regards,
Remy
Re: Flashing Window Title Text using Display.async [message #486919 is a reply to message #486918] Mon, 21 September 2009 01:36 Go to previous messageGo to next message
Ricky Patel is currently offline Ricky PatelFriend
Messages: 33
Registered: July 2009
Member
Seems to work quite well, thanks!

For anyone who is interested, here is a generic version of it that uses a shell and a string


	/**
	 * makes a window request a users attention
	 * @param tempWindow a shell of which title will be changed
	 * @param tempMessage a message for the user to know why attention is needed
	 */
	protected void requestUserAttention (Shell tempWindow, String tempMessage)
	{
		//rate at which the title will change in milliseconds
		int rateOfChange = 1000;
		
		final Shell window = tempWindow;
		//flash 10 times and thats it
		final String orgText = window.getText();
		final String message = tempMessage;
		for (int x=0;x<10;x++)
		{
			window.getDisplay().timerExec(2*rateOfChange*x-rateOfChange, new Runnable (){
				@Override
				public void run ()
				{
					if (window.getDisplay().getActiveShell() != window.getShell())
					{
						window.setText(message);
					}
				}});
			window.getDisplay().timerExec(2*rateOfChange*x, new Runnable (){
				@Override
				public void run ()
				{
					if (window.getDisplay().getActiveShell() != window.getShell() ||
						window.getText().equals(message))
					{
						window.setText(orgText);
					}
				}});
		}
	}
Re: Flashing Window Title Text using Display.async [message #486920 is a reply to message #486919] Mon, 21 September 2009 02:17 Go to previous messageGo to next message
Remy Suen is currently offline Remy SuenFriend
Messages: 462
Registered: July 2009
Senior Member
On Sun, 20 Sep 2009 21:37:00 -0400, Ricky Patel wrote:
> Seems to work quite well, thanks!

Okilydokily.

> For anyone who is interested, here is a generic version of it that uses
> a shell and a string

Note that you're going to want to do some kind of checks within your
runnables since the code only considers the _current_ active shell at any
one moment in time and doesn't concern itself over how focus is being
shofted around.

Consider the scenario where you have window A and window B with the focus
on B, A starts blinking, you click on it, it stops blinking, then you go
back to B all within the ten seconds, A will start blinking again. I'm
not sure if this is the behaviour you want or not but thought I would
mention it.

Regards,
Remy
Re: Flashing Window Title Text using Display.async [message #490694 is a reply to message #486917] Fri, 09 October 2009 17:51 Go to previous message
Ricky Patel is currently offline Ricky PatelFriend
Messages: 33
Registered: July 2009
Member
Noting that my solution really doesnt handle the situation that Remy brought up I thought I would remake the solution for anyone who would like to use it:

/**
 * makes a window request a users attention
 * @param tempWindow a shell of some sort
 * @param tempMessage a message for the user to know why attention is needed
 */
protected void requestUserAttention (Shell tempWindow, String tempMessage)
{
	//rate at which the title will change in milliseconds
	int rateOfChange = 1000;
	
	final Shell window = tempWindow;
	//flash 10 times and thats it
	final String orgText = window.getText();
	final String message = tempMessage;
	
	window.setData("requestUserAttention", true);
	window.addShellListener(new ShellAdapter (){
		@Override
		public void shellActivated (ShellEvent e)
		{
			window.setData("requestUserAttention", false);
		}
	});
	for (int x=0;x<10;x++)
	{
		window.getDisplay().timerExec(2*rateOfChange*x-rateOfChange, new Runnable (){
			@Override
			public void run ()
			{
				if (((Boolean)window.getData("requestUserAttention")))
				{
					window.setText(message);
				}
			}});
		window.getDisplay().timerExec(2*rateOfChange*x, new Runnable (){
			@Override
			public void run ()
			{
				if (((Boolean)window.getData("requestUserAttention")) || window.getText().equals(message))
				{
					window.setText(orgText);
				}
			}});
	}
}
Previous Topic:Programmatically DB access connection via DTP on Eclipse 3.5 (Galileo)
Next Topic:Dynamic View throws error on restart
Goto Forum:
  


Current Time: Sun Nov 23 10:09:44 GMT 2014

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

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