]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.rap/src/main/java/org/argeo/security/ui/rap/RapActionBarAdvisor.java
8b9ac834bd7f39a0a02bfe423674435b89cc1e5a
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.rap / src / main / java / org / argeo / security / ui / rap / RapActionBarAdvisor.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.rap;
17
18 import org.argeo.security.ui.commands.OpenHomePerspective;
19 import org.eclipse.core.commands.Category;
20 import org.eclipse.core.commands.Command;
21 import org.eclipse.jface.action.ICoolBarManager;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.IToolBarManager;
24 import org.eclipse.jface.action.ToolBarManager;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.actions.ActionFactory;
28 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
29 import org.eclipse.ui.application.ActionBarAdvisor;
30 import org.eclipse.ui.application.IActionBarConfigurer;
31 import org.eclipse.ui.commands.ICommandService;
32
33 /** Eclipse rap specific action bar advisor */
34 public class RapActionBarAdvisor extends ActionBarAdvisor {
35 private final static String ID_BASE = "org.argeo.security.ui.rap";
36 // private final static Log log = LogFactory
37 // .getLog(SecureActionBarAdvisor.class);
38
39 /** Null means anonymous */
40 private String username = null;
41
42 // private IAction logoutAction;
43 // private IWorkbenchAction openPerspectiveDialogAction;
44 // private IWorkbenchAction showViewMenuAction;
45 // private IWorkbenchAction preferences;
46 private IWorkbenchAction saveAction;
47 private IWorkbenchAction saveAllAction;
48
49 // private IWorkbenchAction closeAllAction;
50
51 public RapActionBarAdvisor(IActionBarConfigurer configurer, String username) {
52 super(configurer);
53 this.username = username;
54 }
55
56 protected void makeActions(IWorkbenchWindow window) {
57 // preferences = ActionFactory.PREFERENCES.create(window);
58 // register(preferences);
59 // openPerspectiveDialogAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG
60 // .create(window);
61 // register(openPerspectiveDialogAction);
62 // showViewMenuAction = ActionFactory.SHOW_VIEW_MENU.create(window);
63 // register(showViewMenuAction);
64 //
65 // // logout
66 // logoutAction = ActionFactory.QUIT.create(window);
67 // // logoutAction = createLogoutAction();
68 // register(logoutAction);
69 //
70 // Save semantics
71 saveAction = ActionFactory.SAVE.create(window);
72 register(saveAction);
73 saveAllAction = ActionFactory.SAVE_ALL.create(window);
74 register(saveAllAction);
75 // closeAllAction = ActionFactory.CLOSE_ALL.create(window);
76 // register(closeAllAction);
77
78 }
79
80 protected void fillMenuBar(IMenuManager menuBar) {
81 // MenuManager fileMenu = new MenuManager("&File",
82 // IWorkbenchActionConstants.M_FILE);
83 // MenuManager editMenu = new MenuManager("&Edit",
84 // IWorkbenchActionConstants.M_EDIT);
85 // MenuManager windowMenu = new MenuManager("&Window",
86 // IWorkbenchActionConstants.M_WINDOW);
87 //
88 // menuBar.add(fileMenu);
89 // menuBar.add(editMenu);
90 // menuBar.add(windowMenu);
91 // // Add a group marker indicating where action set menus will appear.
92 // menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
93 //
94 // // File
95 // fileMenu.add(saveAction);
96 // fileMenu.add(saveAllAction);
97 // fileMenu.add(closeAllAction);
98 // fileMenu.add(new
99 // GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
100 // fileMenu.add(new Separator());
101 // fileMenu.add(logoutAction);
102 //
103 // // Edit
104 // editMenu.add(preferences);
105 //
106 // // Window
107 // windowMenu.add(openPerspectiveDialogAction);
108 // windowMenu.add(showViewMenuAction);
109 }
110
111 @Override
112 protected void fillCoolBar(ICoolBarManager coolBar) {
113 if (username != null) {
114 ICommandService cmdService = (ICommandService) getActionBarConfigurer()
115 .getWindowConfigurer().getWorkbenchConfigurer()
116 .getWorkbench().getService(ICommandService.class);
117 Category userMenus = cmdService.getCategory(ID_BASE + ".userMenus");
118 if (!userMenus.isDefined())
119 userMenus.define("User Menus", "User related menus");
120
121 Command userMenu = cmdService.getCommand(ID_BASE
122 + ".userMenuCommand");
123 if (userMenu.isDefined())
124 userMenu.undefine();
125 userMenu.define(username, "User menu actions", userMenus);
126 userMenu.setHandler(new OpenHomePerspective());
127
128 // userToolbar.add(new UserMenuAction());
129 // coolBar.add(userToolbar);
130 } else {// anonymous
131 IToolBarManager userToolbar = new ToolBarManager(SWT.FLAT
132 | SWT.RIGHT);
133 // userToolbar.add(logoutAction);
134 coolBar.add(userToolbar);
135 }
136 // IToolBarManager saveToolbar = new ToolBarManager(SWT.FLAT |
137 // SWT.RIGHT);
138 // saveToolbar.add(saveAction);
139 // saveToolbar.add(saveAllAction);
140 // coolBar.add(saveToolbar);
141 }
142
143 // class UserMenuAction extends Action implements IWorkbenchAction {
144 //
145 // public UserMenuAction() {
146 // super(username, IAction.AS_DROP_DOWN_MENU);
147 // // setMenuCreator(new UserMenu());
148 // }
149 //
150 // @Override
151 // public String getId() {
152 // return "org.argeo.security.ui.rap.userMenu";
153 // }
154 //
155 // @Override
156 // public void dispose() {
157 // }
158 //
159 // }
160
161 // class UserMenu implements IMenuCreator {
162 // private Menu menu;
163 //
164 // public Menu getMenu(Control parent) {
165 // Menu menu = new Menu(parent);
166 // addActionToMenu(menu, logoutAction);
167 // return menu;
168 // }
169 //
170 // private void addActionToMenu(Menu menu, IAction action) {
171 // ActionContributionItem item = new ActionContributionItem(action);
172 // item.fill(menu, -1);
173 // }
174 //
175 // public void dispose() {
176 // if (menu != null) {
177 // menu.dispose();
178 // }
179 // }
180 //
181 // public Menu getMenu(Menu parent) {
182 // // Not use
183 // return null;
184 // }
185 //
186 // }
187
188 // protected IAction createLogoutAction() {
189 // Subject subject = Subject.getSubject(AccessController.getContext());
190 // final String username = subject.getPrincipals().iterator().next()
191 // .getName();
192 //
193 // IAction logoutAction = new Action() {
194 // public String getId() {
195 // return SecureRapActivator.ID + ".logoutAction";
196 // }
197 //
198 // public String getText() {
199 // return "Logout " + username;
200 // }
201 //
202 // public void run() {
203 // // try {
204 // // Subject subject = SecureRapActivator.getLoginContext()
205 // // .getSubject();
206 // // String subjectStr = subject.toString();
207 // // subject.getPrincipals().clear();
208 // // SecureRapActivator.getLoginContext().logout();
209 // // log.info(subjectStr + " logged out");
210 // // } catch (LoginException e) {
211 // // log.error("Error when logging out", e);
212 // // }
213 // // SecureEntryPoint.logout(username);
214 // // PlatformUI.getWorkbench().close();
215 // // try {
216 // // RWT.getRequest().getSession().setMaxInactiveInterval(1);
217 // // } catch (Exception e) {
218 // // if (log.isTraceEnabled())
219 // // log.trace("Error when invalidating session", e);
220 // // }
221 // }
222 //
223 // };
224 // return logoutAction;
225 // }
226
227 }