]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/rap/SecureActionBarAdvisor.java
Prepare next development cycle
[lgpl/argeo-commons.git] / ui / rap / SecureActionBarAdvisor.java
1 package org.argeo.security.ui.rap;
2
3 import java.security.Principal;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginException;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.ArgeoException;
11 import org.eclipse.jface.action.Action;
12 import org.eclipse.jface.action.GroupMarker;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.ICoolBarManager;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.Separator;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.rwt.RWT;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.ui.IWorkbenchActionConstants;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
27 import org.eclipse.ui.application.ActionBarAdvisor;
28 import org.eclipse.ui.application.IActionBarConfigurer;
29
30 public class SecureActionBarAdvisor extends ActionBarAdvisor {
31 private final static Log log = LogFactory
32 .getLog(SecureActionBarAdvisor.class);
33
34 private IAction logoutAction;
35 private IWorkbenchAction openPerspectiveDialogAction;
36 private IWorkbenchAction showViewMenuAction;
37 private IWorkbenchAction preferences;
38 private IWorkbenchAction saveAction;
39 private IWorkbenchAction saveAllAction;
40 private IWorkbenchAction closeAllAction;
41
42 public SecureActionBarAdvisor(IActionBarConfigurer configurer, Boolean isRcp) {
43 super(configurer);
44 }
45
46 protected void makeActions(IWorkbenchWindow window) {
47 preferences = ActionFactory.PREFERENCES.create(window);
48 register(preferences);
49 openPerspectiveDialogAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG
50 .create(window);
51 register(openPerspectiveDialogAction);
52 showViewMenuAction = ActionFactory.SHOW_VIEW_MENU.create(window);
53 register(showViewMenuAction);
54
55 // logoutAction = ActionFactory.QUIT.create(window);
56
57 Subject subject = null;
58 try {
59 subject = SecureRapActivator.getLoginContext().getSubject();
60 } catch (LoginException e1) {
61 throw new ArgeoException("Cannot retrieve subject", e1);
62 }
63 final Principal principal = subject.getPrincipals().iterator().next();
64
65 logoutAction = new Action() {
66 public String getId() {
67 return SecureRapActivator.ID + ".logoutAction";
68 }
69
70 public String getText() {
71 return "Logout " + principal.getName();
72 }
73
74 public void run() {
75 try {
76 Subject subject = SecureRapActivator.getLoginContext()
77 .getSubject();
78 String subjectStr = subject.toString();
79 SecureRapActivator.getLoginContext().logout();
80 log.info(subjectStr + " logged out");
81 } catch (LoginException e) {
82 log.error("Error when logging out", e);
83 }
84 try {
85 RWT.getRequest().getSession().setMaxInactiveInterval(1);
86 PlatformUI.getWorkbench().close();
87 } catch (Exception e) {
88 if (log.isTraceEnabled())
89 log.trace("Error when invalidating session", e);
90 }
91 }
92
93 };
94 register(logoutAction);
95
96 // Save semantics
97 saveAction = ActionFactory.SAVE.create(window);
98 register(saveAction);
99 saveAllAction = ActionFactory.SAVE_ALL.create(window);
100 register(saveAllAction);
101 closeAllAction = ActionFactory.CLOSE_ALL.create(window);
102 register(closeAllAction);
103
104 }
105
106 protected void fillMenuBar(IMenuManager menuBar) {
107 MenuManager fileMenu = new MenuManager("&File",
108 IWorkbenchActionConstants.M_FILE);
109 MenuManager editMenu = new MenuManager("&Edit",
110 IWorkbenchActionConstants.M_EDIT);
111 MenuManager windowMenu = new MenuManager("&Window",
112 IWorkbenchActionConstants.M_WINDOW);
113
114 menuBar.add(fileMenu);
115 menuBar.add(editMenu);
116 menuBar.add(windowMenu);
117 // Add a group marker indicating where action set menus will appear.
118 menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
119
120 // File
121 fileMenu.add(saveAction);
122 fileMenu.add(saveAllAction);
123 fileMenu.add(closeAllAction);
124 fileMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
125 fileMenu.add(new Separator());
126 fileMenu.add(logoutAction);
127
128 // Edit
129 editMenu.add(preferences);
130
131 // Window
132 windowMenu.add(openPerspectiveDialogAction);
133 windowMenu.add(showViewMenuAction);
134 }
135
136 @Override
137 protected void fillCoolBar(ICoolBarManager coolBar) {
138 IToolBarManager saveToolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
139 saveToolbar.add(saveAction);
140 saveToolbar.add(saveAllAction);
141 coolBar.add(saveToolbar);
142 }
143
144 }