use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.
the class DefineCommandsTest method testCreateWithSecondContexts.
@Test
public void testCreateWithSecondContexts() {
IEclipseContext localContext = workbenchContext.createChild();
ECommandService cs = localContext.get(ECommandService.class);
assertNotNull(cs);
assertNotNull(cs.defineCategory(TEST_CAT1, "CAT1", null));
Category category = cs.getCategory(TEST_CAT1);
assertNotNull("need category", category);
assertNotNull("command1", cs.defineCommand(TEST_ID1, "ID1", null, category, null));
assertNotNull("command2", cs.defineCommand(TEST_ID2, "ID2", null, category, null));
Command cmd1 = cs.getCommand(TEST_ID1);
assertNotNull("get command1", cmd1);
try {
assertEquals("ID1", cmd1.getName());
} catch (NotDefinedException e) {
fail(e.getMessage());
}
assertNotNull("get command2", cs.getCommand(TEST_ID2));
}
use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.
the class LegacyActionPersistence method convertActionToCommand.
/**
* Determine which command to use. This is slightly complicated as actions do
* not have to have commands, but the new architecture requires it. As such, we
* will auto-generate a command for the action if the definitionId is missing or
* points to a command that does not yet exist. All such command identifiers are
* prefixed with AUTOGENERATED_COMMAND_ID_PREFIX.
*
* @param element The action element from which a command must be
* generated; must not be <code>null</code>.
* @param primaryId The primary identifier to use when auto-generating a
* command; must not be <code>null</code>.
* @param secondaryId The secondary identifier to use when auto-generating a
* command; must not be <code>null</code>.
* @param warningsToLog The collection of warnings logged while reading the
* extension point; must not be <code>null</code>.
* @return the fully-parameterized command; <code>null</code> if an error
* occurred.
*/
private ParameterizedCommand convertActionToCommand(final IConfigurationElement element, final String primaryId, final String secondaryId, final List<IStatus> warningsToLog) {
String commandId = readOptional(element, ATT_DEFINITION_ID);
Command command = null;
if (commandId != null) {
command = commandService.getCommand(commandId);
}
final IActionCommandMappingService mappingService = window.getService(IActionCommandMappingService.class);
String label = null;
if ((commandId == null) || (!command.isDefined())) {
// Add a mapping from this action id to the command id.
if (commandId == null && mappingService != null) {
commandId = mappingService.getGeneratedCommandId(primaryId, secondaryId);
}
if (commandId == null) {
// $NON-NLS-1$
WorkbenchPlugin.log("MappingService unavailable");
return null;
}
// Read the label attribute.
label = readRequired(// $NON-NLS-1$
element, // $NON-NLS-1$
ATT_LABEL, // $NON-NLS-1$
warningsToLog, // $NON-NLS-1$
"Actions require a non-empty label or definitionId", commandId);
if (label == null) {
label = WorkbenchMessages.LegacyActionPersistence_AutogeneratedCommandName;
}
/*
* Read the tooltip attribute. The tooltip is really the description of the
* command.
*/
final String tooltip = readOptional(element, ATT_TOOLTIP);
// Define the command.
command = commandService.getCommand(commandId);
final Category category = commandService.getCategory(null);
final String name = LegacyActionTools.removeAcceleratorText(Action.removeMnemonics(label));
command.define(name, tooltip, category, null);
// TODO Decide the command state.
final String style = readOptional(element, ATT_STYLE);
if (STYLE_RADIO.equals(style)) {
final State state = new RadioState();
// TODO How to set the id?
final boolean checked = readBoolean(element, ATT_STATE, false);
state.setValue((checked) ? Boolean.TRUE : Boolean.FALSE);
command.addState(IMenuStateIds.STYLE, state);
} else if (STYLE_TOGGLE.equals(style)) {
final State state = new ToggleState();
final boolean checked = readBoolean(element, ATT_STATE, false);
state.setValue((checked) ? Boolean.TRUE : Boolean.FALSE);
command.addState(IMenuStateIds.STYLE, state);
}
}
// and the actionId
if (mappingService != null) {
mappingService.map(mappingService.getGeneratedCommandId(primaryId, secondaryId), commandId);
}
return new ParameterizedCommand(command, null);
}
use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.
the class HandlerActivationTest method doSetUp.
@Override
protected void doSetUp() throws Exception {
for (final String[] contextInfo : CREATE_CONTEXTS) {
final Context context = contextService.getContext(contextInfo[0]);
if (!context.isDefined()) {
context.define(contextInfo[1], contextInfo[2], contextInfo[3]);
}
}
Command cmd = commandService.getCommand(CMD_ID);
if (!cmd.isDefined()) {
Category cat = commandService.getCategory(CATEGORY_ID);
cmd.define("Test Handler", "Test handler activation", cat);
cmd.setHandler(HandlerServiceImpl.getHandler(CMD_ID));
}
}
use of org.eclipse.core.commands.Category in project eclipse.platform.ui by eclipse-platform.
the class BindingManagerTest method testGetBestActiveBindingFor.
/**
* Tests whether the method works with a null argument. Tests that it works
* in a simple case.
*
* @throws NotDefinedException
* If the scheme we try to activate is not defined.
*/
@Test
public void testGetBestActiveBindingFor() throws Exception {
// Test with a null argument.
final TriggerSequence[] activeBindingsForNull = bindingManager.getActiveBindingsFor((ParameterizedCommand) null);
assertNotNull("The active bindings for a command should never be null", activeBindingsForNull);
assertTrue("The active binding for a null command should always be empty", activeBindingsForNull.length == 0);
// Test a simple case.
final Context context = contextManager.getContext("na");
context.define("name", "description", null);
final Scheme scheme = bindingManager.getScheme("na");
scheme.define("name", "description", null);
bindingManager.setActiveScheme(scheme);
final Set<String> activeContextIds = new HashSet<>();
activeContextIds.add("na");
contextManager.setActiveContextIds(activeContextIds);
final String commandId = "commandId";
final String categoryId = "cat";
Category cat = commandManager.getCategory(categoryId);
cat.define("cat", "cat");
Command cmd = commandManager.getCommand(commandId);
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;
}
};
cmd.define("na", "NA", cat, parms);
Map<String, String> map = new HashMap<>();
map.put("viewId", "outline");
ParameterizedCommand outline = ParameterizedCommand.generateCommand(cmd, map);
map = new HashMap<>();
map.put("viewId", "console");
ParameterizedCommand console = ParameterizedCommand.generateCommand(cmd, map);
assertFalse(outline.equals(console));
final Binding b2 = new KeyBinding(KeySequence.getInstance("M1+M2+V"), outline, "na", "na", null, null, null, Binding.SYSTEM);
bindingManager.addBinding(b2);
final Binding binding = new KeyBinding(KeySequence.getInstance("M1+V"), outline, "na", "na", null, null, null, Binding.SYSTEM);
bindingManager.addBinding(binding);
final Binding b3 = new KeyBinding(KeySequence.getInstance("M1+M2+C"), console, "na", "na", null, null, null, Binding.SYSTEM);
bindingManager.addBinding(b3);
// - above is all done as part of startup
final TriggerSequence[] bindings = bindingManager.getActiveBindingsFor(binding.getParameterizedCommand());
assertEquals(2, bindings.length);
final TriggerSequence bestBinding = bindingManager.getBestActiveBindingFor(outline);
assertEquals(binding.getTriggerSequence(), bestBinding);
final TriggerSequence bestBinding2 = bindingManager.getBestActiveBindingFor(console);
assertEquals(b3.getTriggerSequence(), bestBinding2);
}
use of org.eclipse.core.commands.Category in project eclipse.pde by eclipse-pde.
the class CommandTreeContentProvider method init.
private void init() {
fCatMap = new TreeMap<>((arg0, arg1) -> {
String comA = CommandList.getText(arg0);
String comB = CommandList.getText(arg1);
if (comA != null)
return comA.compareTo(comB);
// undefined ids should go last
return +1;
});
Command[] commands = fComServ.getDefinedCommands();
for (Command command : commands) {
// skip commands with autogenerated id's
if (// $NON-NLS-1$
command.getId().startsWith("AUTOGEN:::"))
continue;
// populate category map
try {
Category cat = command.getCategory();
ArrayList<Command> list = fCatMap.get(cat);
if (list == null)
fCatMap.put(cat, list = new ArrayList<>());
list.add(command);
} catch (NotDefinedException e) {
continue;
}
// TODO: populate context map
// can we easily group commands by context?
}
}
Aggregations