KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > ToggleCoolbarHandler


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.handlers;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.commands.ICommandService;
22 import org.eclipse.ui.commands.IElementUpdater;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.eclipse.ui.internal.WorkbenchMessages;
25 import org.eclipse.ui.internal.WorkbenchWindow;
26 import org.eclipse.ui.menus.UIElement;
27 import org.eclipse.ui.services.IServiceScopes;
28
29 /**
30  * Handler that toggles the visibility of the coolbar/perspective bar in a given window.
31  *
32  * @since 3.3
33  */

34 public class ToggleCoolbarHandler extends AbstractHandler implements
35         IElementUpdater {
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
41      */

42     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
43         final IWorkbenchWindow activeWorkbenchWindow = HandlerUtil
44                 .getActiveWorkbenchWindowChecked(event);
45         if (activeWorkbenchWindow instanceof WorkbenchWindow) {
46             WorkbenchWindow window = (WorkbenchWindow) activeWorkbenchWindow;
47             window.toggleToolbarVisibility();
48             ICommandService commandService = (ICommandService) activeWorkbenchWindow
49                     .getService(ICommandService.class);
50             Map JavaDoc filter = new HashMap JavaDoc();
51             filter.put(IServiceScopes.WINDOW_SCOPE, window);
52             commandService.refreshElements(event.getCommand().getId(), 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 JavaDoc 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() || window.getPerspectiveBarVisible();
83     }
84 }
85
Popular Tags