01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.handlers;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: import org.eclipse.core.commands.AbstractHandler;
16: import org.eclipse.core.commands.ExecutionEvent;
17: import org.eclipse.core.commands.ExecutionException;
18: import org.eclipse.ui.IWorkbenchWindow;
19: import org.eclipse.ui.commands.ICommandService;
20: import org.eclipse.ui.commands.IElementUpdater;
21: import org.eclipse.ui.handlers.HandlerUtil;
22: import org.eclipse.ui.internal.WorkbenchMessages;
23: import org.eclipse.ui.internal.WorkbenchWindow;
24: import org.eclipse.ui.menus.UIElement;
25: import org.eclipse.ui.services.IServiceScopes;
26:
27: /**
28: * Handler that toggles the visibility of the coolbar/perspective bar in a given window.
29: *
30: * @since 3.3
31: */
32: public class ToggleCoolbarHandler extends AbstractHandler implements
33: IElementUpdater {
34:
35: /*
36: * (non-Javadoc)
37: *
38: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
39: */
40: public Object execute(ExecutionEvent event)
41: throws ExecutionException {
42: final IWorkbenchWindow activeWorkbenchWindow = HandlerUtil
43: .getActiveWorkbenchWindowChecked(event);
44: if (activeWorkbenchWindow instanceof WorkbenchWindow) {
45: WorkbenchWindow window = (WorkbenchWindow) activeWorkbenchWindow;
46: window.toggleToolbarVisibility();
47: ICommandService commandService = (ICommandService) activeWorkbenchWindow
48: .getService(ICommandService.class);
49: Map filter = new HashMap();
50: filter.put(IServiceScopes.WINDOW_SCOPE, window);
51: commandService.refreshElements(event.getCommand().getId(),
52: filter);
53: }
54:
55: return null;
56: }
57:
58: /*
59: * (non-Javadoc)
60: *
61: * @see org.eclipse.ui.commands.IElementUpdater#updateElement(org.eclipse.ui.menus.UIElement,
62: * java.util.Map)
63: */
64: public void updateElement(UIElement element, Map parameters) {
65: IWorkbenchWindow window = (IWorkbenchWindow) element
66: .getServiceLocator().getService(IWorkbenchWindow.class);
67: if (window == null || !(window instanceof WorkbenchWindow))
68: return;
69: element
70: .setText(isCoolbarVisible((WorkbenchWindow) window) ? WorkbenchMessages.ToggleCoolbarVisibilityAction_hide_text
71: : WorkbenchMessages.ToggleCoolbarVisibilityAction_show_text);
72: }
73:
74: /**
75: * Return whether the coolbar is currently visible.
76: *
77: * @param window
78: * the window to test
79: * @return whether or not the coolbar is visible
80: */
81: private boolean isCoolbarVisible(WorkbenchWindow window) {
82: return window.getCoolBarVisible()
83: || window.getPerspectiveBarVisible();
84: }
85: }
|