KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > quickaccess > CommandElement


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.quickaccess;
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.commands.ParameterizedCommand;
16 import org.eclipse.core.commands.common.NotDefinedException;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.IWorkbenchWindow;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.handlers.IHandlerService;
21 import org.eclipse.ui.internal.misc.StatusUtil;
22 import org.eclipse.ui.statushandlers.StatusManager;
23
24 /**
25  * @since 3.3
26  *
27  */

28 public class CommandElement extends QuickAccessElement {
29
30     private static final String JavaDoc separator = " - "; //$NON-NLS-1$
31

32     private ParameterizedCommand command;
33
34     private String JavaDoc id;
35
36     /* package */CommandElement(ParameterizedCommand command, String JavaDoc id,
37             CommandProvider commandProvider) {
38         super(commandProvider);
39         this.id = id;
40         this.command = command;
41     }
42
43     public void execute() {
44         Object JavaDoc o = getProvider();
45         if (o instanceof CommandProvider) {
46             CommandProvider provider = (CommandProvider) o;
47             if (provider.getRealHandlerService()!=null) {
48                 try {
49                     provider.getRealHandlerService().executeCommandInContext(
50                             command, null, provider.getContextSnapshot());
51                 } catch (Exception JavaDoc ex) {
52                     StatusUtil.handleStatus(ex, StatusManager.SHOW
53                             | StatusManager.LOG);
54                 }
55                 return;
56             }
57         }
58         
59         // let's try the old fashioned way
60
IWorkbenchWindow window = PlatformUI.getWorkbench()
61                 .getActiveWorkbenchWindow();
62         if (window != null) {
63             IHandlerService handlerService = (IHandlerService) window
64                     .getWorkbench().getService(IHandlerService.class);
65             try {
66                 handlerService.executeCommand(command, null);
67             } catch (Exception JavaDoc ex) {
68                 StatusUtil.handleStatus(ex, StatusManager.SHOW
69                         | StatusManager.LOG);
70             }
71         }
72     }
73
74     public String JavaDoc getId() {
75         return id;
76     }
77
78     public ImageDescriptor getImageDescriptor() {
79         return null;
80     }
81
82     public String JavaDoc getLabel() {
83         try {
84             Command nestedCommand = command.getCommand();
85             if (nestedCommand != null && nestedCommand.getDescription() != null
86                     && nestedCommand.getDescription().length() != 0) {
87                 return command.getName() + separator
88                         + nestedCommand.getDescription();
89             }
90             return command.getName();
91         } catch (NotDefinedException e) {
92             return command.toString();
93         }
94     }
95
96     public int hashCode() {
97         final int prime = 31;
98         int result = 1;
99         result = prime * result + ((command == null) ? 0 : command.hashCode());
100         return result;
101     }
102
103     public boolean equals(Object JavaDoc obj) {
104         if (this == obj)
105             return true;
106         if (obj == null)
107             return false;
108         if (getClass() != obj.getClass())
109             return false;
110         final CommandElement other = (CommandElement) obj;
111         if (command == null) {
112             if (other.command != null)
113                 return false;
114         } else if (!command.equals(other.command))
115             return false;
116         return true;
117     }
118 }
119
Popular Tags