Search in sources :

Example 6 with Category

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));
}
Also used : Category(org.eclipse.core.commands.Category) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) ECommandService(org.eclipse.e4.core.commands.ECommandService) Test(org.junit.Test)

Example 7 with Category

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);
}
Also used : Category(org.eclipse.core.commands.Category) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) State(org.eclipse.core.commands.State) RadioState(org.eclipse.jface.commands.RadioState) ToggleState(org.eclipse.jface.commands.ToggleState) IActionCommandMappingService(org.eclipse.ui.internal.handlers.IActionCommandMappingService) ToggleState(org.eclipse.jface.commands.ToggleState) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) RadioState(org.eclipse.jface.commands.RadioState)

Example 8 with Category

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));
    }
}
Also used : Context(org.eclipse.core.commands.contexts.Context) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) Category(org.eclipse.core.commands.Category) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command)

Example 9 with Category

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);
}
Also used : Context(org.eclipse.core.commands.contexts.Context) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) Binding(org.eclipse.jface.bindings.Binding) TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) IParameter(org.eclipse.core.commands.IParameter) Scheme(org.eclipse.jface.bindings.Scheme) Category(org.eclipse.core.commands.Category) HashMap(java.util.HashMap) KeyBinding(org.eclipse.jface.bindings.keys.KeyBinding) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) IParameterValues(org.eclipse.core.commands.IParameterValues) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with Category

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?
    }
}
Also used : Category(org.eclipse.core.commands.Category) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) TreeMap(java.util.TreeMap) Command(org.eclipse.core.commands.Command) Viewer(org.eclipse.jface.viewers.Viewer) ICommandService(org.eclipse.ui.commands.ICommandService) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) ArrayList(java.util.ArrayList) Category(org.eclipse.core.commands.Category) Command(org.eclipse.core.commands.Command) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException)

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