View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.cms.ui.workbench.rap;
17  
18  import org.argeo.cms.auth.CurrentUser;
19  import org.argeo.cms.ui.workbench.commands.OpenHomePerspective;
20  import org.eclipse.core.commands.Category;
21  import org.eclipse.core.commands.Command;
22  import org.eclipse.jface.action.ICoolBarManager;
23  import org.eclipse.jface.action.IMenuManager;
24  import org.eclipse.jface.action.IToolBarManager;
25  import org.eclipse.jface.action.ToolBarManager;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.ui.IWorkbenchWindow;
28  import org.eclipse.ui.actions.ActionFactory;
29  import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
30  import org.eclipse.ui.application.ActionBarAdvisor;
31  import org.eclipse.ui.application.IActionBarConfigurer;
32  import org.eclipse.ui.commands.ICommandService;
33  
34  /** Eclipse rap specific action bar advisor */
35  public class RapActionBarAdvisor extends ActionBarAdvisor {
36  	private final static String ID_BASE = SecureRapActivator.ID;
37  	// private final static Log log = LogFactory
38  	// .getLog(SecureActionBarAdvisor.class);
39  
40  	/** Null means anonymous */
41  	private String username = null;
42  
43  	// private IAction logoutAction;
44  	// private IWorkbenchAction openPerspectiveDialogAction;
45  	// private IWorkbenchAction showViewMenuAction;
46  	// private IWorkbenchAction preferences;
47  	private IWorkbenchAction saveAction;
48  	private IWorkbenchAction saveAllAction;
49  
50  	// private IWorkbenchAction closeAllAction;
51  
52  	public RapActionBarAdvisor(IActionBarConfigurer configurer, String username) {
53  		super(configurer);
54  		this.username = username;
55  	}
56  
57  	protected void makeActions(IWorkbenchWindow window) {
58  		// preferences = ActionFactory.PREFERENCES.create(window);
59  		// register(preferences);
60  		// openPerspectiveDialogAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG
61  		// .create(window);
62  		// register(openPerspectiveDialogAction);
63  		// showViewMenuAction = ActionFactory.SHOW_VIEW_MENU.create(window);
64  		// register(showViewMenuAction);
65  		//
66  		// // logout
67  		// logoutAction = ActionFactory.QUIT.create(window);
68  		// // logoutAction = createLogoutAction();
69  		// register(logoutAction);
70  		//
71  		// Save semantics
72  		saveAction = ActionFactory.SAVE.create(window);
73  		register(saveAction);
74  		saveAllAction = ActionFactory.SAVE_ALL.create(window);
75  		register(saveAllAction);
76  		// closeAllAction = ActionFactory.CLOSE_ALL.create(window);
77  		// register(closeAllAction);
78  
79  	}
80  
81  	protected void fillMenuBar(IMenuManager menuBar) {
82  		// MenuManager fileMenu = new MenuManager("&File",
83  		// IWorkbenchActionConstants.M_FILE);
84  		// MenuManager editMenu = new MenuManager("&Edit",
85  		// IWorkbenchActionConstants.M_EDIT);
86  		// MenuManager windowMenu = new MenuManager("&Window",
87  		// IWorkbenchActionConstants.M_WINDOW);
88  		//
89  		// menuBar.add(fileMenu);
90  		// menuBar.add(editMenu);
91  		// menuBar.add(windowMenu);
92  		// // Add a group marker indicating where action set menus will appear.
93  		// menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
94  		//
95  		// // File
96  		// fileMenu.add(saveAction);
97  		// fileMenu.add(saveAllAction);
98  		// fileMenu.add(closeAllAction);
99  		// fileMenu.add(new
100 		// GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
101 		// fileMenu.add(new Separator());
102 		// fileMenu.add(logoutAction);
103 		//
104 		// // Edit
105 		// editMenu.add(preferences);
106 		//
107 		// // Window
108 		// windowMenu.add(openPerspectiveDialogAction);
109 		// windowMenu.add(showViewMenuAction);
110 	}
111 
112 	@Override
113 	protected void fillCoolBar(ICoolBarManager coolBar) {
114 		// Add a command which label is the display name of the current
115 		// logged-in user
116 		if (username != null) {
117 			ICommandService cmdService = (ICommandService) getActionBarConfigurer()
118 					.getWindowConfigurer().getWorkbenchConfigurer()
119 					.getWorkbench().getService(ICommandService.class);
120 			Category userMenus = cmdService.getCategory(ID_BASE + ".userMenus");
121 			if (!userMenus.isDefined())
122 				userMenus.define("User Menus", "User related menus");
123 			Command userMenu = cmdService.getCommand(ID_BASE
124 					+ ".userMenuCommand");
125 			if (userMenu.isDefined())
126 				userMenu.undefine();
127 			userMenu.define(CurrentUser.getDisplayName(), "User menu actions",
128 					userMenus);
129 			// userMenu.define(username, "User menu actions", userMenus);
130 			
131 			userMenu.setHandler(new OpenHomePerspective());
132 
133 			// userToolbar.add(new UserMenuAction());
134 			// coolBar.add(userToolbar);
135 		} else {// anonymous
136 			IToolBarManager userToolbar = new ToolBarManager(SWT.FLAT
137 					| SWT.RIGHT);
138 			// userToolbar.add(logoutAction);
139 			coolBar.add(userToolbar);
140 		}
141 		// IToolBarManager saveToolbar = new ToolBarManager(SWT.FLAT |
142 		// SWT.RIGHT);
143 		// saveToolbar.add(saveAction);
144 		// saveToolbar.add(saveAllAction);
145 		// coolBar.add(saveToolbar);
146 	}
147 
148 	// class UserMenuAction extends Action implements IWorkbenchAction {
149 	//
150 	// public UserMenuAction() {
151 	// super(username, IAction.AS_DROP_DOWN_MENU);
152 	// // setMenuCreator(new UserMenu());
153 	// }
154 	//
155 	// @Override
156 	// public String getId() {
157 	// return "org.argeo.cms.ui.workbench.rap.userMenu";
158 	// }
159 	//
160 	// @Override
161 	// public void dispose() {
162 	// }
163 	//
164 	// }
165 
166 	// class UserMenu implements IMenuCreator {
167 	// private Menu menu;
168 	//
169 	// public Menu getMenu(Control parent) {
170 	// Menu menu = new Menu(parent);
171 	// addActionToMenu(menu, logoutAction);
172 	// return menu;
173 	// }
174 	//
175 	// private void addActionToMenu(Menu menu, IAction action) {
176 	// ActionContributionItem item = new ActionContributionItem(action);
177 	// item.fill(menu, -1);
178 	// }
179 	//
180 	// public void dispose() {
181 	// if (menu != null) {
182 	// menu.dispose();
183 	// }
184 	// }
185 	//
186 	// public Menu getMenu(Menu parent) {
187 	// // Not use
188 	// return null;
189 	// }
190 	//
191 	// }
192 
193 	// protected IAction createLogoutAction() {
194 	// Subject subject = Subject.getSubject(AccessController.getContext());
195 	// final String username = subject.getPrincipals().iterator().next()
196 	// .getName();
197 	//
198 	// IAction logoutAction = new Action() {
199 	// public String getId() {
200 	// return SecureRapActivator.ID + ".logoutAction";
201 	// }
202 	//
203 	// public String getText() {
204 	// return "Logout " + username;
205 	// }
206 	//
207 	// public void run() {
208 	// // try {
209 	// // Subject subject = SecureRapActivator.getLoginContext()
210 	// // .getSubject();
211 	// // String subjectStr = subject.toString();
212 	// // subject.getPrincipals().clear();
213 	// // SecureRapActivator.getLoginContext().logout();
214 	// // log.info(subjectStr + " logged out");
215 	// // } catch (LoginException e) {
216 	// // log.error("Error when logging out", e);
217 	// // }
218 	// // SecureEntryPoint.logout(username);
219 	// // PlatformUI.getWorkbench().close();
220 	// // try {
221 	// // RWT.getRequest().getSession().setMaxInactiveInterval(1);
222 	// // } catch (Exception e) {
223 	// // if (log.isTraceEnabled())
224 	// // log.trace("Error when invalidating session", e);
225 	// // }
226 	// }
227 	//
228 	// };
229 	// return logoutAction;
230 	// }
231 
232 }