Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f30d37378a1acf3839d8543c1201d0e89c1bf846 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     ARTAL Technologies <simon.chemouil@artal.fr> - Bug 293044 added keybindings display
 *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 440810
 *     Patrik Suzzi <psuzzi@gmail.com> - Bug 476045
 *******************************************************************************/

package org.eclipse.ui.internal.quickaccess;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.bindings.TriggerSequence;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandImageService;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.internal.keys.BindingService;
import org.eclipse.ui.internal.menus.CommandMessages;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.quickaccess.QuickAccessElement;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * @since 3.3
 *
 */
public class CommandElement extends QuickAccessElement {

	private ParameterizedCommand command;

	private String id;

	/* package */ CommandElement(ParameterizedCommand command, String id, CommandProvider commandProvider) {
		super(commandProvider);
		this.id = id;
		this.command = command;
	}

	@Override
	public void execute() {
		Object o = getProvider();
		if (o instanceof CommandProvider) {
			CommandProvider provider = (CommandProvider) o;
			if (provider.getHandlerService() != null && provider.getContextSnapshot() != null) {
				try {
					provider.getHandlerService().executeCommandInContext(command, null, provider.getContextSnapshot());
				} catch (Exception ex) {
					StatusUtil.handleStatus(ex, StatusManager.SHOW | StatusManager.LOG);
				}
				return;
			}
		}

		// let's try the old fashioned way
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			IHandlerService handlerService = window.getWorkbench().getService(IHandlerService.class);
			try {
				handlerService.executeCommand(command, null);
			} catch (Exception ex) {
				StatusUtil.handleStatus(ex, StatusManager.SHOW | StatusManager.LOG);
			}
		}
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		ICommandImageService imgService = ((CommandProvider) getProvider()).getCommandImageService();
		return (imgService == null) ? null : imgService.getImageDescriptor(getId());
	}

	/**
	 * Returns a formatted string describes this command.
	 *
	 * @return a description of the command of this element
	 * @since 3.6
	 */
	public String getCommand() {
		final StringBuilder label = new StringBuilder();

		try {
			Command nestedCommand = command.getCommand();
			label.append(command.getName());
			if (nestedCommand != null && nestedCommand.getDescription() != null
					&& nestedCommand.getDescription().length() != 0) {
				label.append(separator).append(nestedCommand.getDescription());
			}
		} catch (NotDefinedException e) {
			label.append(command.toString());
		}

		return label.toString();
	}

	@Override
	public String getLabel() {
		String command = getCommand();
		String binding = getBinding();
		if (binding != null) {
			return NLS.bind(CommandMessages.Tooltip_Accelerator, command, binding);
		}
		return command;
	}

	/**
	 * Returns a formatted string that can be used to invoke this element's command.
	 * <code>null</code> may be returned if a binding cannot be found.
	 *
	 * @return the string keybinding for invoking this element's command, may be
	 *         <code>null</code>
	 * @since 3.6
	 */
	public String getBinding() {
		BindingService service = (BindingService) PlatformUI.getWorkbench().getService(IBindingService.class);
		TriggerSequence[] triggerSeq = service.getBindingManager().getActiveBindingsDisregardingContextFor(command);
		if (triggerSeq != null && triggerSeq.length > 0) {
			return triggerSeq[0].format();
		}
		return null;
	}

	@Override
	public String getSortLabel() {
		try {
			return command.getName();
		} catch (NotDefinedException e) {
			return command.toString();
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((command == null) ? 0 : command.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final CommandElement other = (CommandElement) obj;
		if (command == null) {
			if (other.command != null)
				return false;
		} else if (!command.equals(other.command))
			return false;
		return true;
	}
}

Back to the top