KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collection JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.commands.Command;
20 import org.eclipse.core.commands.ParameterizedCommand;
21 import org.eclipse.core.commands.common.NotDefinedException;
22 import org.eclipse.core.expressions.IEvaluationContext;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.commands.ICommandService;
26 import org.eclipse.ui.handlers.IHandlerService;
27 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
28 import org.eclipse.ui.internal.WorkbenchImages;
29 import org.eclipse.ui.internal.handlers.HandlerService;
30
31 /**
32  * @since 3.3
33  *
34  */

35 public class CommandProvider extends QuickAccessProvider {
36
37     private Map JavaDoc idToElement;
38     private IEvaluationContext contextSnapshot;
39     private HandlerService realHandlerService;
40     
41     public CommandProvider() {
42         // initialize eagerly
43
saveApplicationContext();
44         getElements();
45     }
46
47     public String JavaDoc getId() {
48         return "org.eclipse.ui.commands"; //$NON-NLS-1$
49
}
50
51     public QuickAccessElement getElementForId(String JavaDoc id) {
52         getElements();
53         return (CommandElement) idToElement.get(id);
54     }
55
56     public QuickAccessElement[] getElements() {
57         if (idToElement == null) {
58             idToElement = new HashMap JavaDoc();
59             ICommandService commandService = (ICommandService) PlatformUI
60                     .getWorkbench().getService(ICommandService.class);
61             final Collection JavaDoc commandIds = commandService.getDefinedCommandIds();
62             final Iterator JavaDoc commandIdItr = commandIds.iterator();
63             while (commandIdItr.hasNext()) {
64                 final String JavaDoc currentCommandId = (String JavaDoc) commandIdItr.next();
65                 final Command command = commandService
66                         .getCommand(currentCommandId);
67                 if (command != null && command.isHandled()
68                         && command.isEnabled()) {
69                     try {
70                         Collection JavaDoc combinations = ParameterizedCommand
71                                 .generateCombinations(command);
72                         for (Iterator JavaDoc it = combinations.iterator(); it
73                                 .hasNext();) {
74                             ParameterizedCommand pc = (ParameterizedCommand) it.next();
75                             String JavaDoc id = pc.serialize();
76                             idToElement.put(id,
77                                     new CommandElement(pc, id, this));
78                         }
79                     } catch (final NotDefinedException e) {
80                         // It is safe to just ignore undefined commands.
81
}
82                 }
83             }
84         }
85         return (QuickAccessElement[]) idToElement.values().toArray(
86                 new QuickAccessElement[idToElement.values().size()]);
87     }
88
89     public ImageDescriptor getImageDescriptor() {
90         return WorkbenchImages
91                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE);
92     }
93
94     public String JavaDoc getName() {
95         return QuickAccessMessages.QuickAccess_Commands;
96     }
97     
98     private void saveApplicationContext() {
99         realHandlerService = (HandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
100         contextSnapshot = realHandlerService.getFullContextSnapshot();
101     }
102     
103     HandlerService getRealHandlerService() {
104         return realHandlerService;
105     }
106     
107     IEvaluationContext getContextSnapshot() {
108         return contextSnapshot;
109     }
110 }
111
Popular Tags