Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d3f994f1a4ce6f45e8a43df341f2766862f826d (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*******************************************************************************
 * Copyright (c) 2011, 2012 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.internal.cdt.ui.commands;

import java.util.ArrayList;
import java.util.Map;

import org.eclipse.cdt.debug.ui.IPinProvider;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.PresentationContext;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.tcf.internal.debug.ui.model.TCFModel;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExecContext;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeStackFrame;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.util.TCFDataCache;
import org.eclipse.tcf.util.TCFTask;
import org.eclipse.ui.IWorkbenchPart;

@SuppressWarnings("restriction")
public class TCFPinViewCommand implements IPinProvider {

    public static final String ID_MEMORY_BROWSER_VIEW = "org.eclipse.cdt.debug.ui.memory.memorybrowser.MemoryBrowser"; //$NON-NLS-1$
    private final TCFModel model;
    private final ArrayList<PinnedView> list = new ArrayList<PinnedView>();

    private class PinnedView implements IPinElementHandle {

        @SuppressWarnings("unused")
        private final IPinModelListener listener;
        @SuppressWarnings("unused")
        private final IWorkbenchPart part;
        private final TCFNode node;
        private final IPresentationContext ctx;

        final IRunControl.RunControlListener rc_listener = new IRunControl.RunControlListener() {

            public void contextAdded(IRunControl.RunControlContext[] contexts) {
            }

            public void contextChanged(IRunControl.RunControlContext[] contexts) {
                for (IRunControl.RunControlContext ctx : contexts) {
                    if (node.getID().equals(ctx.getID())) updateLabel();
                }
            }

            public void contextRemoved(String[] context_ids) {
                for (String id : context_ids) {
                    if (node.getID().equals(id)) updateLabel();
                }
            }

            public void contextSuspended(String id, String pc, String reason, Map<String, Object> params) {
                if (node.getID().equals(id)) updateLabel();
            }

            public void contextResumed(String id) {
                if (node.getID().equals(id)) updateLabel();
            }

            public void containerSuspended(String context, String pc, String reason, Map<String, Object> params, String[] suspended_ids) {
                for (String id : suspended_ids) {
                    if (node.getID().equals(id)) updateLabel();
                }
            }

            public void containerResumed(String[] context_ids) {
                for (String id : context_ids) {
                    if (node.getID().equals(id)) updateLabel();
                }
            }

            public void contextException(String id, String msg) {
                if (node.getID().equals(id)) updateLabel();
            }
        };

        PinnedView(IWorkbenchPart part, TCFNode node, IPinModelListener listener) {
            this.part = part;
            this.node = node;
            this.listener = listener;
            ctx = new PresentationContext(TCFModel.ID_PINNED_VIEW, part);
            IRunControl rc = model.getChannel().getRemoteService(IRunControl.class);
            if (rc != null) rc.addListener(rc_listener);
        }

        void updateLabel() {
            // TODO: CDT does not support label update
            /*
            model.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    listener.modelChanged(new StructuredSelection(node));
                }
            });
            */
        }

        void dispose() {
            IRunControl rc = model.getChannel().getRemoteService(IRunControl.class);
            if (rc != null) rc.removeListener(rc_listener);
        }

        public Object getDebugContext() {
            return node;
        }

        public String getLabel() {
            return new TCFTask<String>() {
                public void run() {
                    model.update(new ILabelUpdate[]{ new ILabelUpdate() {

                        String text;

                        public IPresentationContext getPresentationContext() {
                            return ctx;
                        }

                        public Object getElement() {
                            return node;
                        }

                        public TreePath getElementPath() {
                            return null;
                        }

                        public Object getViewerInput() {
                            return null;
                        }

                        public void setStatus(IStatus status) {
                        }

                        public IStatus getStatus() {
                            return null;
                        }

                        public void done() {
                            done_update(text);
                        }

                        public void cancel() {
                        }

                        public boolean isCanceled() {
                            return false;
                        }

                        public String[] getColumnIds() {
                            return null;
                        }

                        public void setLabel(String text, int columnIndex) {
                            if (columnIndex == 0) this.text = text;
                        }

                        public void setFontData(FontData fontData, int columnIndex) {
                        }

                        public void setImageDescriptor(ImageDescriptor image, int columnIndex) {
                        }

                        public void setForeground(RGB foreground, int columnIndex) {
                        }

                        public void setBackground(RGB background, int columnIndex) {
                        }
                    }});
                }

                private void done_update(String text) {
                    if (text == null) text = node.getID();
                    done(text);
                }
            }.getE();
        }

        public IPinElementColorDescriptor getPinElementColorDescriptor() {
            return null;
        }
    }

    public TCFPinViewCommand(TCFModel model) {
        this.model = model;
    }

    public boolean isPinnable(IWorkbenchPart part, final Object obj) {
        if (obj instanceof TCFNode) {
            try {
                final String id = part.getSite().getId();
                return new TCFTask<Boolean>(model.getChannel()) {
                    public void run() {
                        boolean mem = false;
                        boolean vars = false;
                        boolean memBrowser = false;
                        if (obj instanceof TCFNodeExecContext) {
                            TCFNodeExecContext node = (TCFNodeExecContext)obj;
                            TCFDataCache<IRunControl.RunControlContext> ctx_cache = node.getRunContext();
                            if (!ctx_cache.validate(this)) return;
                            IRunControl.RunControlContext ctx_data = ctx_cache.getData();
                            if (ctx_data != null) {
                                vars = ctx_data.hasState();
                                mem = vars || ctx_data.getProcessID() != null;
                                memBrowser = mem;
                            }
                        }
                        if (obj instanceof TCFNodeStackFrame) {
                            vars = true;
                            mem = true;
                            memBrowser = false;
                        }
                        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(id)) done(mem);
                        else if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(id)) done(vars);
                        else if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(id)) done(mem);
                        else if (ID_MEMORY_BROWSER_VIEW.equals(id)) done(memBrowser);
                        else done(false);
                    }
                }.getE();
            }
            catch (Throwable x) {
                return false;
            }
        }
        return false;
    }

    public IPinElementHandle pin(final IWorkbenchPart part, Object obj, final IPinModelListener listener) {
        if (obj instanceof TCFNode) {
            final TCFNode node = (TCFNode)obj;
            return new TCFTask<IPinElementHandle>() {
                public void run() {
                    PinnedView p = new PinnedView(part, node, listener);
                    model.setPin(part, node);
                    list.add(p);
                    done(p);
                }
            }.getE();
        }
        return null;
    }

    public void unpin(final IWorkbenchPart part, final IPinElementHandle handle) {
        new TCFTask<Object>() {
            public void run() {
                model.setPin(part, null);
                if (list.remove(handle)) {
                    ((PinnedView)handle).dispose();
                }
                done(null);
            }
        };
    }

    public boolean isPinnedTo(Object obj, final IPinElementHandle handle) {
        if (obj instanceof TCFNode) {
            return new TCFTask<Boolean>() {
                public void run() {
                    done(list.contains(handle));
                }
            }.getE();
        }
        return false;
    }
}

Back to the top