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;
}
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;
}
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"));
}
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());
}
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);
}
Aggregations