001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.quickaccess;
011:
012: import org.eclipse.core.commands.Command;
013: import org.eclipse.core.commands.ParameterizedCommand;
014: import org.eclipse.core.commands.common.NotDefinedException;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.ui.IWorkbenchWindow;
017: import org.eclipse.ui.PlatformUI;
018: import org.eclipse.ui.handlers.IHandlerService;
019: import org.eclipse.ui.internal.misc.StatusUtil;
020: import org.eclipse.ui.statushandlers.StatusManager;
021:
022: /**
023: * @since 3.3
024: *
025: */
026: public class CommandElement extends QuickAccessElement {
027:
028: private static final String separator = " - "; //$NON-NLS-1$
029:
030: private ParameterizedCommand command;
031:
032: private String id;
033:
034: /* package */CommandElement(ParameterizedCommand command,
035: String id, CommandProvider commandProvider) {
036: super (commandProvider);
037: this .id = id;
038: this .command = command;
039: }
040:
041: public void execute() {
042: Object o = getProvider();
043: if (o instanceof CommandProvider) {
044: CommandProvider provider = (CommandProvider) o;
045: if (provider.getRealHandlerService() != null) {
046: try {
047: provider.getRealHandlerService()
048: .executeCommandInContext(command, null,
049: provider.getContextSnapshot());
050: } catch (Exception ex) {
051: StatusUtil.handleStatus(ex, StatusManager.SHOW
052: | StatusManager.LOG);
053: }
054: return;
055: }
056: }
057:
058: // let's try the old fashioned way
059: IWorkbenchWindow window = PlatformUI.getWorkbench()
060: .getActiveWorkbenchWindow();
061: if (window != null) {
062: IHandlerService handlerService = (IHandlerService) window
063: .getWorkbench().getService(IHandlerService.class);
064: try {
065: handlerService.executeCommand(command, null);
066: } catch (Exception ex) {
067: StatusUtil.handleStatus(ex, StatusManager.SHOW
068: | StatusManager.LOG);
069: }
070: }
071: }
072:
073: public String getId() {
074: return id;
075: }
076:
077: public ImageDescriptor getImageDescriptor() {
078: return null;
079: }
080:
081: public String getLabel() {
082: try {
083: Command nestedCommand = command.getCommand();
084: if (nestedCommand != null
085: && nestedCommand.getDescription() != null
086: && nestedCommand.getDescription().length() != 0) {
087: return command.getName() + separator
088: + nestedCommand.getDescription();
089: }
090: return command.getName();
091: } catch (NotDefinedException e) {
092: return command.toString();
093: }
094: }
095:
096: public int hashCode() {
097: final int prime = 31;
098: int result = 1;
099: result = prime * result
100: + ((command == null) ? 0 : command.hashCode());
101: return result;
102: }
103:
104: public boolean equals(Object obj) {
105: if (this == obj)
106: return true;
107: if (obj == null)
108: return false;
109: if (getClass() != obj.getClass())
110: return false;
111: final CommandElement other = (CommandElement) obj;
112: if (command == null) {
113: if (other.command != null)
114: return false;
115: } else if (!command.equals(other.command))
116: return false;
117: return true;
118: }
119: }
|