Search in sources :

Example 1 with ParameterType

use of org.eclipse.core.commands.ParameterType 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 ParameterType

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

the class CommandPersistence method readParameters.

/**
 * Reads the parameters from a parent configuration element. This is used to
 * read the parameter sub-elements from a command element. Each parameter is
 * guaranteed to be valid. If invalid parameters are found, then a warning
 * status will be appended to the <code>warningsToLog</code> list.
 *
 * @param configurationElement The configuration element from which the
 *                             parameters should be read; must not be
 *                             <code>null</code>.
 * @param warningsToLog        The list of warnings found during parsing.
 *                             Warnings found while parsing the parameters will
 *                             be appended to this list. This value must not be
 *                             <code>null</code>.
 * @param commandManager       The command service from which the parameter can
 *                             get parameter types; must not be
 *                             <code>null</code>.
 * @return The array of parameters found for this configuration element;
 *         <code>null</code> if none can be found.
 */
private static Parameter[] readParameters(final IConfigurationElement configurationElement, final List<IStatus> warningsToLog, final CommandManager commandManager) {
    final IConfigurationElement[] parameterElements = configurationElement.getChildren(TAG_COMMAND_PARAMETER);
    if ((parameterElements == null) || (parameterElements.length == 0)) {
        return null;
    }
    int insertionIndex = 0;
    Parameter[] parameters = new Parameter[parameterElements.length];
    for (final IConfigurationElement parameterElement : parameterElements) {
        // Read out the id
        // $NON-NLS-1$
        final String id = readRequired(parameterElement, ATT_ID, warningsToLog, "Parameters need an id");
        if (id == null) {
            continue;
        }
        // Read out the name.
        // $NON-NLS-1$
        final String name = readRequired(parameterElement, ATT_NAME, warningsToLog, "Parameters need a name");
        if (name == null) {
            continue;
        }
        /*
			 * The IParameterValues will be initialized lazily as an IExecutableExtension.
			 */
        // Read out the typeId attribute, if present.
        final String typeId = readOptional(parameterElement, ATT_TYPE_ID);
        // Read out the optional attribute, if present.
        final boolean optional = readBoolean(parameterElement, ATT_OPTIONAL, true);
        final ParameterType type;
        if (typeId == null) {
            type = null;
        } else {
            type = commandManager.getParameterType(typeId);
        }
        final Parameter parameter = new Parameter(id, name, parameterElement, type, optional);
        parameters[insertionIndex++] = parameter;
    }
    if (insertionIndex != parameters.length) {
        final Parameter[] compactedParameters = new Parameter[insertionIndex];
        System.arraycopy(parameters, 0, compactedParameters, 0, insertionIndex);
        parameters = compactedParameters;
    }
    return parameters;
}
Also used : ParameterType(org.eclipse.core.commands.ParameterType) Parameter(org.eclipse.e4.ui.internal.workbench.Parameter) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement)

Example 3 with ParameterType

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

the class CommandParameterTypeTest method testIsCompatible.

/**
 * Tests ParameterType.isCompatible for various values with the Integer
 * parameter type used in this test suite.
 */
@Test
public void testIsCompatible() throws CommandException {
    ICommandService commandService = getCommandService();
    ParameterType type = commandService.getParameterType(TYPE);
    assertTrue(type.isCompatible(Integer.valueOf(4)));
    assertTrue(type.isCompatible(Integer.valueOf(0)));
    assertTrue(type.isCompatible(Integer.valueOf(-434)));
    assertFalse(type.isCompatible(null));
    assertFalse(type.isCompatible("4"));
}
Also used : ParameterType(org.eclipse.core.commands.ParameterType) ICommandService(org.eclipse.ui.commands.ICommandService) Test(org.junit.Test)

Example 4 with ParameterType

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

the class CommandParameterTypeTest method testGetReturnType.

/**
 * Test {@link Command#getReturnType()}, making sure we can get the return
 * type of the subtract command.
 */
@Test
public void testGetReturnType() throws CommandException {
    ICommandService commandService = getCommandService();
    Command command = commandService.getCommand(SUBTRACT);
    ParameterType returnType = command.getReturnType();
    assertNotNull(returnType);
    assertEquals(TYPE, returnType.getId());
}
Also used : ParameterType(org.eclipse.core.commands.ParameterType) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ICommandService(org.eclipse.ui.commands.ICommandService) Test(org.junit.Test)

Example 5 with ParameterType

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

the class CommandParameterTypeTest method testConvertIntegerToString.

private void testConvertIntegerToString(Object value, String expected, boolean expectFail) throws CommandException {
    ICommandService commandService = getCommandService();
    ParameterType type = commandService.getParameterType(TYPE);
    String converted = null;
    if (expectFail) {
        try {
            converted = type.getValueConverter().convertToString(value);
            fail("expected ParameterValueConversionException");
        } catch (ParameterValueConversionException ex) {
            // passed
            return;
        } catch (Exception ex) {
            fail("expected ParameterValueConversionException");
        }
    } else {
        converted = type.getValueConverter().convertToString(value);
    }
    assertEquals(expected, converted);
}
Also used : ParameterType(org.eclipse.core.commands.ParameterType) ParameterValueConversionException(org.eclipse.core.commands.ParameterValueConversionException) ICommandService(org.eclipse.ui.commands.ICommandService) CommandException(org.eclipse.core.commands.common.CommandException) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ParameterValueConversionException(org.eclipse.core.commands.ParameterValueConversionException)

Aggregations

ParameterType (org.eclipse.core.commands.ParameterType)10 ICommandService (org.eclipse.ui.commands.ICommandService)5 ArrayList (java.util.ArrayList)3 Category (org.eclipse.core.commands.Category)3 IParameter (org.eclipse.core.commands.IParameter)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 Parameter (org.eclipse.e4.ui.internal.workbench.Parameter)3 Command (org.eclipse.core.commands.Command)2 ParameterValueConversionException (org.eclipse.core.commands.ParameterValueConversionException)2 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)2 CommandException (org.eclipse.core.commands.common.CommandException)2 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)2 IStatus (org.eclipse.core.runtime.IStatus)2 MCommandParameter (org.eclipse.e4.ui.model.application.commands.MCommandParameter)2 Test (org.junit.Test)2 JsonObject (com.google.gson.JsonObject)1 HashMap (java.util.HashMap)1 AbstractParameterValueConverter (org.eclipse.core.commands.AbstractParameterValueConverter)1 MCategory (org.eclipse.e4.ui.model.application.commands.MCategory)1 MCommand (org.eclipse.e4.ui.model.application.commands.MCommand)1