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 java.util.Collection;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.Map;
016:
017: import org.eclipse.core.commands.Command;
018: import org.eclipse.core.commands.ParameterizedCommand;
019: import org.eclipse.core.commands.common.NotDefinedException;
020: import org.eclipse.core.expressions.IEvaluationContext;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.commands.ICommandService;
024: import org.eclipse.ui.handlers.IHandlerService;
025: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
026: import org.eclipse.ui.internal.WorkbenchImages;
027: import org.eclipse.ui.internal.handlers.HandlerService;
028:
029: /**
030: * @since 3.3
031: *
032: */
033: public class CommandProvider extends QuickAccessProvider {
034:
035: private Map idToElement;
036: private IEvaluationContext contextSnapshot;
037: private HandlerService realHandlerService;
038:
039: public CommandProvider() {
040: // initialize eagerly
041: saveApplicationContext();
042: getElements();
043: }
044:
045: public String getId() {
046: return "org.eclipse.ui.commands"; //$NON-NLS-1$
047: }
048:
049: public QuickAccessElement getElementForId(String id) {
050: getElements();
051: return (CommandElement) idToElement.get(id);
052: }
053:
054: public QuickAccessElement[] getElements() {
055: if (idToElement == null) {
056: idToElement = new HashMap();
057: ICommandService commandService = (ICommandService) PlatformUI
058: .getWorkbench().getService(ICommandService.class);
059: final Collection commandIds = commandService
060: .getDefinedCommandIds();
061: final Iterator commandIdItr = commandIds.iterator();
062: while (commandIdItr.hasNext()) {
063: final String currentCommandId = (String) commandIdItr
064: .next();
065: final Command command = commandService
066: .getCommand(currentCommandId);
067: if (command != null && command.isHandled()
068: && command.isEnabled()) {
069: try {
070: Collection combinations = ParameterizedCommand
071: .generateCombinations(command);
072: for (Iterator it = combinations.iterator(); it
073: .hasNext();) {
074: ParameterizedCommand pc = (ParameterizedCommand) it
075: .next();
076: String id = pc.serialize();
077: idToElement.put(id, new CommandElement(pc,
078: id, this ));
079: }
080: } catch (final NotDefinedException e) {
081: // It is safe to just ignore undefined commands.
082: }
083: }
084: }
085: }
086: return (QuickAccessElement[]) idToElement.values().toArray(
087: new QuickAccessElement[idToElement.values().size()]);
088: }
089:
090: public ImageDescriptor getImageDescriptor() {
091: return WorkbenchImages
092: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE);
093: }
094:
095: public String getName() {
096: return QuickAccessMessages.QuickAccess_Commands;
097: }
098:
099: private void saveApplicationContext() {
100: realHandlerService = (HandlerService) PlatformUI.getWorkbench()
101: .getService(IHandlerService.class);
102: contextSnapshot = realHandlerService.getFullContextSnapshot();
103: }
104:
105: HandlerService getRealHandlerService() {
106: return realHandlerService;
107: }
108:
109: IEvaluationContext getContextSnapshot() {
110: return contextSnapshot;
111: }
112: }
|