Search in sources :

Example 1 with Category

use of org.eclipse.core.commands.Category in project lsp4e by eclipse.

the class CommandExecutor method createEclipseCoreCommand.

private static ParameterizedCommand createEclipseCoreCommand(@NonNull Command command, IPath context, @NonNull IWorkbench workbench) {
    // Usually commands are defined via extension point, but we synthesize one on
    // the fly for the command ID, since we do not want downstream users
    // having to define them.
    String commandId = command.getCommand();
    @Nullable ICommandService commandService = workbench.getService(ICommandService.class);
    org.eclipse.core.commands.Command coreCommand = commandService.getCommand(commandId);
    if (!coreCommand.isDefined()) {
        ParameterType commandParamType = commandService.getParameterType(LSP_COMMAND_PARAMETER_TYPE_ID);
        ParameterType pathParamType = commandService.getParameterType(LSP_PATH_PARAMETER_TYPE_ID);
        Category category = commandService.getCategory(LSP_COMMAND_CATEGORY_ID);
        IParameter[] parameters = { new CommandEventParameter(commandParamType, command.getTitle(), LSP_COMMAND_PARAMETER_ID), new CommandEventParameter(pathParamType, command.getTitle(), LSP_PATH_PARAMETER_ID) };
        coreCommand.define(commandId, null, category, parameters);
    }
    Map<Object, Object> parameters = new HashMap<>();
    parameters.put(LSP_COMMAND_PARAMETER_ID, command);
    parameters.put(LSP_PATH_PARAMETER_ID, context);
    ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(coreCommand, parameters);
    return parameterizedCommand;
}
Also used : ParameterType(org.eclipse.core.commands.ParameterType) IParameter(org.eclipse.core.commands.IParameter) Category(org.eclipse.core.commands.Category) HashMap(java.util.HashMap) CommandEventParameter(org.eclipse.lsp4e.command.internal.CommandEventParameter) ICommandService(org.eclipse.ui.commands.ICommandService) JsonObject(com.google.gson.JsonObject) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with Category

use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.

the class CommandPersistence method readCategoriesFromRegistry.

/**
 * Reads all of the category definitions from the commands extension point.
 *
 * @param configurationElements     The configuration elements in the commands
 *                                  extension point; must not be
 *                                  <code>null</code>, but may be empty.
 * @param configurationElementCount The number of configuration elements that
 *                                  are really in the array.
 * @param commandManager            The command service to which the categories
 *                                  should be added; must not be
 *                                  <code>null</code>.
 */
private static void readCategoriesFromRegistry(final IConfigurationElement[] configurationElements, final int configurationElementCount, final CommandManager commandManager) {
    Category undefCat = commandManager.getCategory(null);
    if (!undefCat.isDefined()) {
        // Define the uncategorized category.
        commandManager.defineUncategorizedCategory(WorkbenchMessages.CommandService_AutogeneratedCategoryName, WorkbenchMessages.CommandService_AutogeneratedCategoryDescription);
    }
    final List<IStatus> warningsToLog = new ArrayList<>(1);
    for (int i = 0; i < configurationElementCount; i++) {
        final IConfigurationElement configurationElement = configurationElements[i];
        // Read out the category identifier.
        final String categoryId = readRequired(configurationElement, ATT_ID, warningsToLog, // $NON-NLS-1$
        "Categories need an id");
        if (categoryId == null) {
            continue;
        }
        // Read out the name.
        final String name = readRequired(// $NON-NLS-1$
        configurationElement, // $NON-NLS-1$
        ATT_NAME, // $NON-NLS-1$
        warningsToLog, // $NON-NLS-1$
        "Categories need a name", categoryId);
        if (name == null) {
            continue;
        }
        // Read out the description.
        final String description = readOptional(configurationElement, ATT_DESCRIPTION);
        final Category category = commandManager.getCategory(categoryId);
        if (!category.isDefined()) {
            category.define(name, description);
        }
    }
    // If there were any warnings, then log them now.
    logWarnings(warningsToLog, // $NON-NLS-1$
    "Warnings while parsing the commands from the 'org.eclipse.ui.commands' and 'org.eclipse.ui.actionDefinitions' extension points.");
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Category(org.eclipse.core.commands.Category) ArrayList(java.util.ArrayList) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 3 with Category

use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.

the class BindingLookupTest method defineCommands.

private void defineCommands(IEclipseContext context) {
    ECommandService cs = workbenchContext.get(ECommandService.class);
    Category category = cs.defineCategory(TEST_CAT1, "CAT1", null);
    cs.defineCommand(TEST_ID1, "ID1", null, category, null);
    cs.defineCommand(TEST_ID2, "ID2", null, category, null);
}
Also used : Category(org.eclipse.core.commands.Category) ECommandService(org.eclipse.e4.core.commands.ECommandService)

Example 4 with Category

use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.

the class HandlerTest method defineCommands.

private void defineCommands(IEclipseContext context) {
    ECommandService cs = workbenchContext.get(ECommandService.class);
    Category category = cs.defineCategory(TEST_CAT1, "CAT1", null);
    cs.defineCommand(TEST_ID1, "ID1", null, category, null);
    cs.defineCommand(TEST_ID2, "ID2", null, category, null);
    cs.defineCommand(TEST_ID3, "ID3", null, category, new IParameter[] { new IParameter() {

        @Override
        public boolean isOptional() {
            return true;
        }

        @Override
        public IParameterValues getValues() throws ParameterValuesException {
            return Collections::emptyMap;
        }

        @Override
        public String getName() {
            return ACTIVE_INFO_ID;
        }

        @Override
        public String getId() {
            return ACTIVE_INFO_ID;
        }
    } });
}
Also used : IParameter(org.eclipse.core.commands.IParameter) Category(org.eclipse.core.commands.Category) ParameterValuesException(org.eclipse.core.commands.ParameterValuesException) Collections(java.util.Collections) IParameterValues(org.eclipse.core.commands.IParameterValues) ECommandService(org.eclipse.e4.core.commands.ECommandService)

Example 5 with Category

use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.

the class DefineCommandsTest method testParamizedCommandsSimple.

@Test
public void testParamizedCommandsSimple() {
    ECommandService cs = workbenchContext.get(ECommandService.class);
    IParameter[] parms = new IParameter[1];
    parms[0] = new IParameter() {

        @Override
        public String getId() {
            return "viewId";
        }

        @Override
        public String getName() {
            return "View Id";
        }

        @Override
        public IParameterValues getValues() {
            return null;
        }

        @Override
        public boolean isOptional() {
            return false;
        }
    };
    // command needs to be defined
    Category defineCategory = cs.defineCategory(TEST_CAT1, "CAT1", null);
    Command command = cs.defineCommand(TEST_ID1_WITH_PARAMETERS, "TEST_ID1_WITH_PARAMETERS", null, defineCategory, parms);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("viewId", "Testing");
    // afterwards it is possible to create a ParameterizedCommand
    ParameterizedCommand createdParamizedCommand = cs.createCommand(TEST_ID1_WITH_PARAMETERS, parameters);
    assertNotNull(command);
    assertNotNull(createdParamizedCommand);
    Command cmd1 = cs.getCommand(TEST_ID1_WITH_PARAMETERS);
    assertNotNull("get command1", cmd1);
}
Also used : IParameter(org.eclipse.core.commands.IParameter) Category(org.eclipse.core.commands.Category) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) HashMap(java.util.HashMap) IParameterValues(org.eclipse.core.commands.IParameterValues) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ECommandService(org.eclipse.e4.core.commands.ECommandService) Test(org.junit.Test)

Aggregations

Category (org.eclipse.core.commands.Category)23 Command (org.eclipse.core.commands.Command)14 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)13 ECommandService (org.eclipse.e4.core.commands.ECommandService)10 Test (org.junit.Test)7 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 IParameter (org.eclipse.core.commands.IParameter)5 Context (org.eclipse.core.commands.contexts.Context)4 HashSet (java.util.HashSet)3 IParameterValues (org.eclipse.core.commands.IParameterValues)3 ParameterType (org.eclipse.core.commands.ParameterType)3 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)3 Binding (org.eclipse.jface.bindings.Binding)3 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)3 CommandManager (org.eclipse.core.commands.CommandManager)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)2 IStatus (org.eclipse.core.runtime.IStatus)2