Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43cdd46cfb15277a885bb90ce4ca94a715065fde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*******************************************************************************
 * Copyright (c) 2019.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.commands;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExecContext;
import org.eclipse.tcf.services.IContextReset;
import org.eclipse.tcf.util.TCFDataCache;
import org.eclipse.tcf.util.TCFTask;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;

public class ResetMenu extends CompoundContributionItem implements IWorkbenchContribution {

    private static IContributionItem[] EMPTY_MENU = new IContributionItem[0];

    private IServiceLocator serviceLocator;

    @Override
    public void initialize(IServiceLocator serviceLocator) {
        this.serviceLocator = serviceLocator;
    }

    static IStructuredSelection getDebugContext() {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        ISelection selection = DebugUITools.getDebugContextManager().getContextService(window).getActiveContext();
        if (selection != null && selection instanceof IStructuredSelection) {
            return (IStructuredSelection) selection;
        }
        return StructuredSelection.EMPTY;
    }

    private IContributionItem makeContributionItem(TCFNodeExecContext exec, Map<String, Object> capability) {
        String type = capability.get(IContextReset.CAPABILITY_TYPE).toString();
        String desc = capability.get(IContextReset.CAPABILITY_DESCRIPTION).toString();
        Map<String, Object> params = new HashMap<String, Object>();
        CommandContributionItemParameter itemParameter;

        params.put("org.eclipse.tcf.debug.ui.commands.reset.param.type", type);
        itemParameter = new CommandContributionItemParameter(serviceLocator, null, null, 0);
        itemParameter.commandId = "org.eclipse.tcf.debug.ui.commands.reset";
        itemParameter.parameters = params;
        itemParameter.label = desc;
        return new CommandContributionItem(itemParameter);
    }

    @Override
    protected IContributionItem[] getContributionItems() {
        IStructuredSelection sselection = getDebugContext();
        Object obj = sselection.getFirstElement();
        IContributionItem[] items = EMPTY_MENU;

        if (obj instanceof TCFNode) {
            TCFNode node = (TCFNode) obj;
            while (node != null) {
                if (node instanceof TCFNodeExecContext) {
                    final TCFNodeExecContext exec = (TCFNodeExecContext) node;
                    Collection<Map<String, Object>> capabilities;
                    capabilities = new TCFTask<Collection<Map<String, Object>>>(exec.getChannel()) {
                        @Override
                        public void run() {
                            TCFDataCache<Collection<Map<String, Object>>> cache = exec.getResetCapabilities();
                            if (!cache.validate(this)) return;
                            done(cache.getData());
                        }
                    }.getE();
                    items = new IContributionItem[capabilities.size()];
                    int i = 0;
                    for (Map<String, Object> c : capabilities) {
                        items[i++] = makeContributionItem(exec, c);
                    }
                    break;
                }
                node = node.getParent();
            }
        }
        return items;
    }
}

Back to the top