EMMA Coverage Report (generated Mon Sep 29 15:05:28 EDT 2008)
[all classes][org.eclipse.ui.internal.actions]

COVERAGE SUMMARY FOR SOURCE FILE [CommandAction.java]

nameclass, %method, %block, %line, %
CommandAction.java100% (2/2)85%  (11/13)76%  (226/297)79%  (61.4/78)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CommandAction100% (1/1)82%  (9/11)74%  (203/274)77%  (55.4/72)
CommandAction (): void 0%   (0/1)0%   (0/9)0%   (0/4)
getParameterizedCommand (): ParameterizedCommand 0%   (0/1)0%   (0/3)0%   (0/1)
runWithEvent (Event): void 100% (1/1)39%  (15/38)36%  (4/11)
createCommand (ICommandService, String, Map): void 100% (1/1)68%  (23/34)80%  (8/10)
CommandAction (IServiceLocator, String, Map): void 100% (1/1)76%  (16/21)86%  (6/7)
init (IServiceLocator, String, Map): void 100% (1/1)84%  (105/125)90%  (23.3/26)
CommandAction (IServiceLocator, String): void 100% (1/1)100% (6/6)100% (2/2)
dispose (): void 100% (1/1)100% (19/19)100% (7/7)
getActionDefinitionId (): String 100% (1/1)100% (3/3)100% (1/1)
getCommandListener (): ICommandListener 100% (1/1)100% (12/12)100% (3/3)
run (): void 100% (1/1)100% (4/4)100% (2/2)
     
class CommandAction$1100% (1/1)100% (2/2)100% (23/23)100% (7/7)
CommandAction$1 (CommandAction): void 100% (1/1)100% (6/6)100% (2/2)
commandChanged (CommandEvent): void 100% (1/1)100% (17/17)100% (5/5)

1/*******************************************************************************
2 * Copyright (c) 2007, 2008 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 
12package org.eclipse.ui.internal.actions;
13 
14import java.util.Map;
15 
16import org.eclipse.core.commands.Command;
17import org.eclipse.core.commands.CommandEvent;
18import org.eclipse.core.commands.ICommandListener;
19import org.eclipse.core.commands.ParameterizedCommand;
20import org.eclipse.core.commands.common.NotDefinedException;
21import org.eclipse.jface.action.Action;
22import org.eclipse.swt.widgets.Event;
23import org.eclipse.ui.commands.ICommandImageService;
24import org.eclipse.ui.commands.ICommandService;
25import org.eclipse.ui.handlers.IHandlerService;
26import org.eclipse.ui.internal.WorkbenchPlugin;
27import org.eclipse.ui.services.IServiceLocator;
28 
29/**
30 * Instantiate an action that will execute the command.
31 * <p>
32 * This is a legacy bridge class, and should not be used outside of the
33 * framework. Please use menu contributions to display a command in a menu or
34 * toolbar.
35 * </p>
36 * <p>
37 * <b>Note:</b> Clients my instantiate, but they must not subclass.
38 * </p>
39 * 
40 * @since 3.3
41 */
42public class CommandAction extends Action {
43 
44        private IHandlerService handlerService = null;
45 
46        private ParameterizedCommand parameterizedCommand = null;
47 
48        private ICommandListener commandListener;
49 
50        protected CommandAction() {
51 
52        }
53 
54        /**
55         * Creates the action backed by a command. For commands that don't take
56         * parameters.
57         * 
58         * @param serviceLocator
59         *            The service locator that is closest in lifecycle to this
60         *            action.
61         * @param commandIdIn
62         *            the command id. Must not be <code>null</code>.
63         */
64        public CommandAction(IServiceLocator serviceLocator, String commandIdIn) {
65                this(serviceLocator, commandIdIn, null);
66        }
67 
68        /**
69         * Creates the action backed by a parameterized command. The parameterMap
70         * must contain only all required parameters, and may contain the optional
71         * parameters.
72         * 
73         * @param serviceLocator
74         *            The service locator that is closest in lifecycle to this
75         *            action.
76         * @param commandIdIn
77         *            the command id. Must not be <code>null</code>.
78         * @param parameterMap
79         *            the parameter map. May be <code>null</code>.
80         */
81        public CommandAction(IServiceLocator serviceLocator, String commandIdIn,
82                        Map parameterMap) {
83                if (commandIdIn == null) {
84                        throw new NullPointerException("commandIdIn must not be null"); //$NON-NLS-1$
85                }
86                init(serviceLocator, commandIdIn, parameterMap);
87        }
88 
89        protected ICommandListener getCommandListener() {
90                if (commandListener == null) {
91                        commandListener = new ICommandListener() {
92                                public void commandChanged(CommandEvent commandEvent) {
93                                        if (commandEvent.isHandledChanged()
94                                                        || commandEvent.isEnabledChanged()) {
95                                                if (commandEvent.getCommand().isDefined()) {
96                                                        setEnabled(commandEvent.getCommand().isEnabled());
97                                                }
98                                        }
99                                }
100                        };
101                }
102                return commandListener;
103        }
104 
105        /**
106         * Build a command from the executable extension information.
107         * 
108         * @param commandService
109         *            to get the Command object
110         * @param commandId
111         *            the command id for this action
112         * @param parameterMap
113         */
114        private void createCommand(ICommandService commandService,
115                        String commandId, Map parameterMap) {
116                Command cmd = commandService.getCommand(commandId);
117                if (!cmd.isDefined()) {
118                        WorkbenchPlugin.log("Command " + commandId + " is undefined"); //$NON-NLS-1$//$NON-NLS-2$
119                        return;
120                }
121 
122                if (parameterMap == null) {
123                        parameterizedCommand = new ParameterizedCommand(cmd, null);
124                        return;
125                }
126 
127                parameterizedCommand = ParameterizedCommand.generateCommand(cmd,
128                                parameterMap);
129        }
130 
131        public void dispose() {
132                // not important for command ID, maybe for command though.
133                handlerService = null;
134                if (commandListener != null) {
135                        parameterizedCommand.getCommand().removeCommandListener(
136                                        commandListener);
137                        commandListener = null;
138                }
139                parameterizedCommand = null;
140        }
141 
142        /*
143         * (non-Javadoc)
144         * 
145         * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
146         */
147        public void runWithEvent(Event event) {
148                if (handlerService == null) {
149                        String commandId = (parameterizedCommand == null ? "unknownCommand" //$NON-NLS-1$
150                                        : parameterizedCommand.getId());
151                        WorkbenchPlugin.log("Cannot run " + commandId //$NON-NLS-1$
152                                        + " before command action has been initialized"); //$NON-NLS-1$
153                        return;
154                }
155                try {
156                        if (parameterizedCommand != null) {
157                                handlerService.executeCommand(parameterizedCommand, event);
158                        }
159                } catch (Exception e) {
160                        WorkbenchPlugin.log(e);
161                }
162        }
163 
164        /*
165         * (non-Javadoc)
166         * 
167         * @see org.eclipse.jface.action.Action#run()
168         */
169        public void run() {
170                // hopefully this is never called
171                runWithEvent(null);
172        }
173 
174        protected void init(IServiceLocator serviceLocator, String commandIdIn,
175                        Map parameterMap) {
176                if (handlerService != null) {
177                        // already initialized
178                        return;
179                }
180                handlerService = (IHandlerService) serviceLocator
181                                .getService(IHandlerService.class);
182                ICommandService commandService = (ICommandService) serviceLocator
183                                .getService(ICommandService.class);
184                ICommandImageService commandImageService = (ICommandImageService) serviceLocator
185                                .getService(ICommandImageService.class);
186 
187                createCommand(commandService, commandIdIn, parameterMap);
188                if (parameterizedCommand != null) {
189                        setId(parameterizedCommand.getId());
190                        setActionDefinitionId(parameterizedCommand.getId());
191                        try {
192                                setText(parameterizedCommand.getName());
193                        } catch (NotDefinedException e) {
194                                // if we get this far it shouldn't be a problem
195                        }
196                        parameterizedCommand.getCommand().addCommandListener(
197                                        getCommandListener());
198                        parameterizedCommand.getCommand().setEnabled(
199                                        handlerService.getCurrentState());
200                        setEnabled(parameterizedCommand.getCommand().isEnabled());
201                        setImageDescriptor(commandImageService.getImageDescriptor(
202                                        commandIdIn, ICommandImageService.TYPE_DEFAULT));
203                        setDisabledImageDescriptor(commandImageService.getImageDescriptor(
204                                        commandIdIn, ICommandImageService.TYPE_DISABLED));
205                        setHoverImageDescriptor(commandImageService.getImageDescriptor(
206                                        commandIdIn, ICommandImageService.TYPE_HOVER));
207                }
208        }
209 
210        protected ParameterizedCommand getParameterizedCommand() {
211                return parameterizedCommand;
212        }
213 
214        public String getActionDefinitionId() {
215                return super.getActionDefinitionId();
216        }
217}

[all classes][org.eclipse.ui.internal.actions]
EMMA 2.0.5312 EclEmma Fix 1 (C) Vladimir Roubtsov