Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 80a1d8a78c528a7cafc99a5771d82c0dc577365f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others.
 * 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
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.internal.tcf.debug.ui.commands;

import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.debug.ui.IDebugView;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFModel;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNode;
import org.eclipse.tm.tcf.util.TCFTask;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.CompoundContributionItem;

public class UpdatePolicyMenu extends CompoundContributionItem {

    private static final String[] policy_names = {
        "Automatic",
        "Manual",
        "Breakpoint Hit",
    };

    @Override
    protected IContributionItem[] getContributionItems() {
        IContributionItem[] items = new IContributionItem[policy_names.length];
        for (int i = 0; i < items.length; i++) {
            final int n = i;
            items[i] = new ContributionItem() {
                @Override
                public void fill(final Menu menu, int index) {
                    final MenuItem item = new MenuItem(menu, SWT.RADIO);
                    item.setText(policy_names[n]);
                    final MenuListener menu_listener = new MenuListener() {
                        public void menuShown(MenuEvent e) {
                            item.setSelection(getPolicy() == n);
                        }
                        public void menuHidden(MenuEvent e) {
                        }
                    };
                    menu.addMenuListener(menu_listener);
                    item.addDisposeListener(new DisposeListener() {
                        public void widgetDisposed(DisposeEvent e) {
                            menu.removeMenuListener(menu_listener);
                        }
                    });
                    item.addSelectionListener(new SelectionListener() {
                        public void widgetSelected(SelectionEvent e) {
                            if (item.getSelection()) setPolicy(n);
                        }
                        public void widgetDefaultSelected(SelectionEvent e) {
                        }
                    });
                }
            };
        }
        return items;
    }

    private IWorkbenchPart getPart() {
        return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
    }

    private TCFNode getRootNode(IWorkbenchPart part) {
        IWorkbenchPartSite site = part.getSite();
        if (site == null || IDebugUIConstants.ID_DEBUG_VIEW.equals(site.getId())) {
            return null;
        }
        if (part instanceof IDebugView) {
            Object input = ((IDebugView)part).getViewer().getInput();
            if (input instanceof TCFNode) return (TCFNode)input;
        }
        return null;
    }

    private int getPolicy() {
        final IWorkbenchPart part = getPart();
        if (part == null) return 0;
        final TCFNode node = getRootNode(part);
        if (node == null) return 0;
        return new TCFTask<Integer>(node.getChannel()) {
            public void run() {
                TCFModel model = node.getModel();
                if (!model.isLocked(part)) done(TCFModel.UPDATE_POLICY_AUTOMATIC);
                else done(model.getLockPolicy(part));
            }
        }.getE();
    }

    private void setPolicy(final int n) {
        final IWorkbenchPart part = getPart();
        if (part == null) return;
        final TCFNode node = getRootNode(part);
        if (node == null) return;
        new TCFTask<Object>(node.getChannel()) {
            public void run() {
                TCFModel model = node.getModel();
                model.setLockPolicy(part, n);
                done(null);
            }
        }.getE();
    }
}

Back to the top