Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Tray icon/menu
Tray icon/menu [message #906349] Sat, 01 September 2012 07:41 Go to next message
Wolfram Swoboda is currently offline Wolfram Swoboda
Messages: 2
Registered: September 2012
Junior Member
Hello,

I have a problem concerning a tray icon with menu in an E4 RCP application. I want to 'minimize' my main window to the tray and show it again on a tray-icon click.

- How do i define a tray icon
I tried it with creating an invisible window in the Application.e4xmi, added a Part to it which contains the code for the tray item:
...
final Tray tray = display.getSystemTray();
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
...

This works fine, the icon shows, BUT when i try to make the main window invisible it closes the whole application, which is logical because there is no more visible window left.
trayItem.addSelectionListener(new SelectionAdapter() {
	 @Override
	 public void widgetSelected(SelectionEvent e) {
	      mainWindow.setVisible(!mainWindow.isVisible());
	 }
);

Is there a way to prohibit the workbench from closing when the last window is set invisible?
Is there a better way/a best practice to create a tray icon/menu with E4?

Any ideas?

[Updated on: Sat, 01 September 2012 07:41]

Report message to a moderator

Re: Tray icon/menu [message #906475 is a reply to message #906349] Sat, 01 September 2012 15:51 Go to previous message
Wolfram Swoboda is currently offline Wolfram Swoboda
Messages: 2
Registered: September 2012
Junior Member
I found a solution but i'ts not pretty, more an evil hack, but it works. Better solutions are most welcome Wink.
The trick is to create the Tray in a new Display+Shell, in a new Thread and stall the UI Thread of the main Window:

private final ExecutorService trayRunner = Executors.newSingleThreadExecutor();
	private Thread uiThread;
	
	@PostConstruct
	public void createControls(final MTrimmedWindow window, EModelService modelservice,MApplication application, Display display,IWorkbench workbench){
		
		display.asyncExec(new Runnable() {
			
			@Override
			public void run() {
				uiThread = Thread.currentThread();				
			}
		});
		createSystemTray(window,display);
		
		
	}
	private void createSystemTray(final MTrimmedWindow window,final Display uiDisplay){
		trayRunner.execute(new Runnable() {
			
			@Override
			public void run() {
				
				Display trayRunnerDisplay = new Display();
				
				final Shell shell = new Shell(trayRunnerDisplay);
				//shell.setVisible(false);
				//shell.open();
				
				
				Tray tray = trayRunnerDisplay.getSystemTray();
				final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
				
				ImageDescriptor icon = null;
				try {
					icon = ImageDescriptor.createFromURL(new URL("platform:/plugin/at.swoboda.bakkup/icons/bakkup_icon.gif"));
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				trayItem.setImage(icon.createImage());
				trayItem.setText("Bakkup");
				trayItem.setToolTipText("Bakkup Trayitem");
				System.out.println("Tray thread ="+Thread.currentThread().getId());
			
				trayItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						if(window.isVisible()){
							uiDisplay.asyncExec(new Runnable() {

								@Override
								public void run() {


									window.setVisible(false);
									try {
										while(true){
											uiThread.sleep(10000);
										}
									} catch (InterruptedException e) {
										window.setVisible(true);
										System.out.println("interrupted");
									}


								}
							});
						}else{
							uiThread.interrupt();
						}
					}

				});

				while (!shell.isDisposed ()) {
					
					if (!trayRunnerDisplay.readAndDispatch ()) trayRunnerDisplay.sleep ();
				}
				
				
			}
		});
		
	}
	
Previous Topic:File Browser in a Part
Next Topic:Graphiti and E4
Goto Forum:
  


Current Time: Thu Oct 02 12:26:51 GMT 2014

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

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