Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33d4a861e1d545d0160bccdd59ae08bfee530223 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
 * Copyright (c) 2009, 2013 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.tcf.internal.debug.ui.commands;

import java.util.ArrayList;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.core.commands.IHandler2;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.commands.common.EventManager;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.ui.model.TCFModelManager;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.ui.IActionDelegate2;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

public abstract class AbstractActionDelegate extends EventManager implements
        IViewActionDelegate, IActionDelegate2, IWorkbenchWindowActionDelegate,
        IObjectActionDelegate, IHandler2 {

    private IAction action;
    private IWorkbenchPart part;
    private IWorkbenchWindow window;
    private ExecutionEvent event;
    private ISelection selection;
    private ISelection event_selection;
    @SuppressWarnings("unused")
    private Object context;
    private boolean enabled;

    public void init(IAction action) {
        this.action = action;
    }

    public void init(IViewPart view) {
        this.part = view;
    }

    public void init(IWorkbenchWindow window) {
        this.window = window;
    }

    public void dispose() {
        action = null;
        part = null;
        window = null;
    }

    public void addHandlerListener(IHandlerListener listener) {
        addListenerObject(listener);
    }

    public void removeHandlerListener(IHandlerListener listener) {
        removeListenerObject(listener);
    }

    protected void fireHandlerChanged(HandlerEvent event) {
        if (event == null) throw new NullPointerException();

        Object[] listeners = getListeners();
        for (int i = 0; i < listeners.length; i++) {
            IHandlerListener listener = (IHandlerListener)listeners[i];
            listener.handlerChanged(event);
        }
    }

    public void setActivePart(IAction action, IWorkbenchPart part) {
        this.action = action;
        this.part = part;
        window = part.getSite().getWorkbenchWindow();
    }

    public void run(IAction action) {
        IAction action0 = this.action;
        try {
            this.action = action;
            run();
        }
        finally {
            this.action = action0;
        }
    }

    public void runWithEvent(IAction action, Event event) {
        run(action);
    }

    public void selectionChanged(IAction action, ISelection selection) {
        this.selection = selection;
        IAction action0 = this.action;
        try {
            this.action = action;
            selectionChanged();
        }
        finally {
            this.action = action0;
        }
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        if (action != null) action.setEnabled(enabled);
    }

    public void setEnabled(Object context) {
        this.context = context;
    }

    public boolean isEnabled() {
        window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (window == null) return false;
        part = window.getActivePage().getActivePart();
        if (part == null) return false;
        ISelectionProvider selection_provider = part.getSite().getSelectionProvider();
        if (selection_provider == null) return false;
        selection = selection_provider.getSelection();
        if (selection == null) return false;
        selectionChanged();
        return enabled;
    }

    public IWorkbenchPart getPart() {
        if (event != null) return HandlerUtil.getActivePart(event);
        return part;
    }

    public IWorkbenchWindow getWindow() {
        // In Eclipse 4.x, the HandlerUtil.getActiveWorkbenchWindow(event) may return null
        if (event != null && HandlerUtil.getActiveWorkbenchWindow(event) != null) return HandlerUtil.getActiveWorkbenchWindow(event);
        if (part != null) return part.getSite().getWorkbenchWindow();
        if (window != null) return window;
        if (PlatformUI.getWorkbench() != null) return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        return null;
    }

    public ISelection getSelection() {
        if (event != null) return event_selection;
        return selection;
    }

    public Object execute(ExecutionEvent event) throws ExecutionException {
        try {
            this.event = event;
            event_selection = selection;
            run();
        }
        catch (Throwable x) {
            throw new ExecutionException("Command aborted", x);
        }
        finally {
            this.event = null;
            event_selection = null;
        }
        return null;
    }

    public boolean isHandled() {
        return true;
    }

    public TCFNode getSelectedNode() {
        ISelection s = getSelection();
        if (s instanceof IStructuredSelection) {
            final Object o = ((IStructuredSelection)s).getFirstElement();
            if (o instanceof TCFNode) return (TCFNode)o;
            if (o instanceof TCFLaunch) return TCFModelManager.getRootNodeSync((TCFLaunch)o);
        }
        return null;
    }

    public TCFNode[] getSelectedNodes() {
        ISelection s0 = getSelection();
        ArrayList<TCFNode> list = new ArrayList<TCFNode>();
        if (s0 instanceof IStructuredSelection) {
            IStructuredSelection s = (IStructuredSelection)s0;
            if (s.size() > 0) {
                for (final Object o : s.toArray()) {
                    if (o instanceof TCFNode) {
                        list.add((TCFNode)o);
                    }
                    else if (o instanceof TCFLaunch) {
                        TCFNode n = TCFModelManager.getRootNodeSync((TCFLaunch)o);
                        if (n != null) list.add(n);
                    }
                }
            }
        }
        return list.toArray(new TCFNode[list.size()]);
    }

    protected abstract void selectionChanged();

    protected abstract void run();
}

Back to the top