View Javadoc

1   package org.eparapher.rcp.tray;
2   
3   import org.eclipse.swt.SWT;
4   import org.eclipse.swt.events.SelectionAdapter;
5   import org.eclipse.swt.events.SelectionEvent;
6   import org.eclipse.swt.graphics.Image;
7   import org.eclipse.swt.widgets.Display;
8   import org.eclipse.swt.widgets.Event;
9   import org.eclipse.swt.widgets.Listener;
10  import org.eclipse.swt.widgets.Menu;
11  import org.eclipse.swt.widgets.MenuItem;
12  import org.eclipse.swt.widgets.Shell;
13  import org.eclipse.swt.widgets.TrayItem;
14  import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
15  import org.eparapher.core.EParapherManager;
16  import org.eparapher.rcp.actions.CMSSignDocumentAction;
17  import org.eparapher.rcp.actions.ToPDFAndSignDocumentAction;
18  import org.eparapher.rcp.actions.XMLSignDocumentAction;
19  import org.eparapher.rcp.tools.GUIIcons;
20  
21  public class TrayIconManager {
22  
23  	private boolean started;
24  	
25  	private TrayItem trayItem;
26  	private Image    trayIconImage;
27  	private Image    trayIconImageGrey;
28  	
29  	private IWorkbenchWindowConfigurer windowConfigurer;
30  	
31  	private Menu     menu;
32  	private MenuItem signfile;
33  	private MenuItem signpdffile;
34  	private MenuItem signxmlfile;
35  	//private MenuItem encfile;
36  	private MenuItem preferences;
37  	private MenuItem open;
38  	private MenuItem exit;
39  	
40  	public TrayIconManager(IWorkbenchWindowConfigurer wwconfig) {
41  		//get the display and image using standard eclipse mechanisms
42  		windowConfigurer = wwconfig;
43  		started=false;
44  		initSystemTray();
45  	}
46  
47  	private void initSystemTray() {
48  
49  		trayIconImage = GUIIcons.TRAY_ICON_IMG;
50  		trayIconImageGrey = new Image(trayIconImage.getDevice(), trayIconImage, SWT.IMAGE_GRAY);
51  		
52  		Display display = windowConfigurer.getWindow().getWorkbench().getDisplay();
53  		trayItem = new TrayItem(display.getSystemTray(), SWT.NONE);
54  		trayItem.setImage(trayIconImageGrey);
55  		trayItem.setToolTipText("eParapher");
56  
57  		//Action Double Click : Show/Hide Main window
58  		trayItem.addSelectionListener(new clickOnSystrayIcon());
59  
60  		//Action right clik : show Menu
61  		Shell workbenchWindowShell = new Shell();
62  		//windowConfigurer.getWindow().getShell();
63  		
64  		// Create a Menu & items.
65  		menu = new Menu(workbenchWindowShell, SWT.POP_UP);
66  		menu.setEnabled(true);
67  		
68  		signfile = new MenuItem(menu, SWT.PUSH);
69  		signpdffile = new MenuItem(menu, SWT.PUSH);
70  		signxmlfile = new MenuItem(menu, SWT.PUSH);
71  		//new MenuItem(menu, SWT.SEPARATOR);
72  		//encfile  = new MenuItem(menu, SWT.PUSH);
73  		//menu.add(new Separator());
74  		new MenuItem(menu, SWT.SEPARATOR);
75  		preferences = new MenuItem(menu, SWT.PUSH);
76  		new MenuItem(menu, SWT.SEPARATOR);
77  		open = new MenuItem(menu, SWT.PUSH);
78  		exit = new MenuItem(menu, SWT.PUSH);
79  		
80  		exit.setText("Exit");
81  		exit.setImage(GUIIcons.TRAY_EXIT_IMG);
82  		// Do a workbench close in the event handler for exit menu item.
83  		exit.addListener(SWT.Selection, new ExitEParapher());
84  
85  		open.setText("Open");
86  		open.setImage(GUIIcons.TRAY_ICON_IMG);
87  		// make the workbench visible in the event handler for exit menu item.
88  		open.addListener(SWT.Selection, new OpenEParapherGUI());
89  		
90  		signfile.setText("Sign a file ...");
91  		signfile.setImage(GUIIcons.FILE_ICON_CMS);
92  		signfile.setEnabled(true);
93  		signfile.addListener(SWT.Selection, new Listener() {
94  			public void handleEvent(Event event) {
95  				CMSSignDocumentAction cmssigner = new CMSSignDocumentAction();
96  				cmssigner.run();
97  			}
98  		});
99  		
100 		signpdffile.setText("Sign a PDF file ...");
101 		signpdffile.setImage(GUIIcons.FILE_ICON_CMS);
102 		signpdffile.setEnabled(true);
103 		signpdffile.addListener(SWT.Selection, new Listener() {
104 			public void handleEvent(Event event) {
105 				ToPDFAndSignDocumentAction pdfsigner = new ToPDFAndSignDocumentAction();
106 				pdfsigner.run();
107 			}
108 		});
109 		
110 		signxmlfile.setText("Sign a XML file ...");
111 		signxmlfile.setImage(GUIIcons.FILE_ICON_CMS);
112 		signxmlfile.setEnabled(true);
113 		signxmlfile.addListener(SWT.Selection, new Listener() {
114 			public void handleEvent(Event event) {
115 				XMLSignDocumentAction xmlsigner = new XMLSignDocumentAction();
116 				xmlsigner.run();
117 			}
118 		});
119 		
120 		//encfile.setText("Encrypt a file ...");
121 		//encfile.setImage(GUIIcons.FILE_ICON_ENC);
122 		//encfile.setEnabled(false);
123 		//encfile.addListener(SWT.Selection, new Listener() {
124 		//	public void handleEvent(Event event) {
125 		//		eParapherTools.infoMessage("Select a file and encrypt it","This shortcut has not been implemented yet.\r\nSorry :p");
126 		//	}
127 		//});
128 		preferences.setText("Preferences");
129 		preferences.setImage(GUIIcons.PREF_ICON_IMAGE);
130 		preferences.addListener(SWT.Selection, new Listener() {
131 			public void handleEvent(Event event) {
132 				EParapherManager.getInstance().getUI().showeParapherSettings();
133 			}
134 		});
135 		trayItem.addListener(SWT.MenuDetect, new Listener() {
136 			public void handleEvent(Event event) {
137 				menu.setVisible(true);
138 			}
139 		});
140 	}
141 	
142 	public void StartupFinished() {
143 		trayItem.setImage(trayIconImage);
144 		menu.setEnabled(true);
145 	}
146 	
147 	/**
148 	 * Disable 
149 	 * @param locked
150 	 */
151 	public void switchState(boolean locked) {
152 		if (locked) {
153 			trayItem.setImage(trayIconImageGrey);
154 			trayItem.setToolTipText("eParapher is locked");
155 			menu.setVisible(false);
156 			hideMainGUI();
157 		} else {
158 			trayItem.setImage(trayIconImage);
159 			trayItem.setToolTipText("eParapher");
160 			menu.setVisible(true);
161 			showMainGUI();
162 		}
163 	}
164 
165 	public void showMainGUI() {
166 		Shell workbenchWindowShell = windowConfigurer.getWindow().getShell();
167 		workbenchWindowShell.setVisible(true);
168 		workbenchWindowShell.setActive();
169 		workbenchWindowShell.setFocus();
170 		workbenchWindowShell.setMinimized(false);				//trayItemIconImage.dispose();
171 		//trayItem.dispose();
172 	}
173 	public void hideMainGUI() {
174 		Shell workbenchWindowShell = windowConfigurer.getWindow().getShell();
175 		workbenchWindowShell.setVisible(false);
176 		workbenchWindowShell.setMinimized(true);
177 	}
178 	
179 	public class clickOnSystrayIcon extends SelectionAdapter {
180 		public void widgetDefaultSelected(SelectionEvent e) {
181 			Shell workbenchWindowShell = windowConfigurer.getWindow().getShell();
182 			if (!workbenchWindowShell.getVisible())
183 				showMainGUI();
184 			else
185 				hideMainGUI();
186 		}
187 	}
188 	
189 	public class ExitEParapher implements Listener {
190 		public void handleEvent(Event event) {
191 		    trayIconImage.dispose();
192 			trayItem.dispose();
193 			open.dispose();
194 			exit.dispose();
195 			menu.dispose();
196 			windowConfigurer.getWorkbenchConfigurer().getWorkbench().close();
197 		}
198 	}
199 	
200 	public class OpenEParapherGUI implements Listener {
201 		public void handleEvent(Event event) {
202 			Shell workbenchWindowShell = windowConfigurer.getWindow().getShell();
203 			workbenchWindowShell.setVisible(true);
204 			workbenchWindowShell.setActive();
205 			workbenchWindowShell.setFocus();
206 			workbenchWindowShell.setMinimized(false);
207 			/*trayItemIconImage.dispose();
208 			trayItem.dispose();
209 			trayItem.setVisible(true);
210 			open.dispose();
211 			exit.dispose();
212 			menu.dispose();*/
213 		}
214 	}
215 }