Skip to main content
summaryrefslogtreecommitdiffstats
blob: 307877527c18e4dd02deb5af5a4ea5298c4eabd9 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 Wind River Systems 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.cdt.examples.dsf.timers;

import java.util.concurrent.RejectedExecutionException;

import org.eclipse.cdt.dsf.concurrent.ConfinedToDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.ui.viewmodel.VMDelta;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.AbstractDMVMNode;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.AbstractDMVMProvider;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.IElementPropertiesProvider;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.IPropertiesUpdate;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.LabelAttribute;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.LabelColumnInfo;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.LabelImage;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.LabelText;
import org.eclipse.cdt.dsf.ui.viewmodel.properties.PropertiesBasedLabelProvider;
import org.eclipse.cdt.examples.dsf.DsfExamplesPlugin;
import org.eclipse.cdt.examples.dsf.timers.AlarmService.TriggerDMContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementEditor;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Composite;


/**
 * View model node that defines how alarm DMContexts are displayed in the view.  Alarm
 * nodes are fairly static, once they are created their label doesn't change.
 * @see TriggerDMContext 
 */
@SuppressWarnings("restriction")
class TriggersVMNode extends AbstractDMVMNode 
    implements IElementEditor, IElementPropertiesProvider, IElementLabelProvider
{
    private static final String PROP_TRIGGER_NUMBER = "alarmNumber"; 
    private static final String PROP_TRIGGER_VALUE = "alarmTriggerValue"; 
    
    // Create and configure the label provider.
    private static final PropertiesBasedLabelProvider fgLabelProvider;
    static {
        fgLabelProvider = new PropertiesBasedLabelProvider();

        LabelColumnInfo idCol = new LabelColumnInfo(
            new LabelAttribute[] { 
                new LabelText("Trigger #{0}", new String[] { PROP_TRIGGER_NUMBER }), 
                new LabelImage(DsfExamplesPlugin.getDefault().getImageRegistry().
                    getDescriptor(DsfExamplesPlugin.IMG_ALARM))
            });
        fgLabelProvider.setColumnInfo(TimersViewColumnPresentation.COL_ID, idCol);
        
        LabelColumnInfo valueCol = new LabelColumnInfo(
            new LabelAttribute[] {
                new LabelText("{0}", new String[] { PROP_TRIGGER_VALUE }) 
            });
        fgLabelProvider.setColumnInfo(TimersViewColumnPresentation.COL_VALUE, 
            valueCol);            
    }

    private TriggerCellModifier fAlarmCellModifier;
    
    public TriggersVMNode(AbstractDMVMProvider provider, DsfSession session) {
        super(provider, session, TriggerDMContext.class);
    }
    
    @Override
    public String toString() {
        return "TriggersVMNode(" + getSession().getId() + ")"; 
    }
    
    @Override
    protected void updateElementsInSessionThread(final IChildrenUpdate update) {
        AlarmService alarmService = getServicesTracker().getService(AlarmService.class, null); 
        if ( alarmService == null ) {
            handleFailedUpdate(update);
            return;
    	}

        TriggerDMContext[] triggers = alarmService.getTriggers();
        fillUpdateWithVMCs(update, triggers);
        update.done();
    }

    public void update(ILabelUpdate[] updates) {
        fgLabelProvider.update(updates);
    }
    
    public void update(final IPropertiesUpdate[] updates) {
        // Switch to the session thread before processing the updates.
        try {
            getSession().getExecutor().execute(new DsfRunnable() {
                public void run() {
                    for (IPropertiesUpdate update : updates) {
                        updatePropertiesInSessionThread(update);
                    }
                }});
        } catch (RejectedExecutionException e) {
            for (IViewerUpdate update : updates) {
                handleFailedUpdate(update);
            }
        }
    }

    @ConfinedToDsfExecutor("getSession#getExecutor")
    private void updatePropertiesInSessionThread(final IPropertiesUpdate update) {
        // Find the trigger context in the element being updated
        TriggerDMContext triggerCtx = findDmcInPath( 
            update.getViewerInput(), update.getElementPath(), TriggerDMContext.class);
        AlarmService alarmService = getServicesTracker().getService(AlarmService.class, null); 
        
        // If either update or service are not valid, fail the update and return.
        if ( triggerCtx == null || alarmService == null) {
        	handleFailedUpdate(update);
            return;
        }
        
        // Calculate and set the update properties.
        int value = alarmService.getTriggerValue(triggerCtx);
        
        if (value == -1) {
            handleFailedUpdate(update);
            return;
        } 

        update.setProperty(PROP_TRIGGER_NUMBER, triggerCtx.getTriggerNumber());
        update.setProperty(PROP_TRIGGER_VALUE, value);
        update.done();
    }
    
    public CellEditor getCellEditor(IPresentationContext context, String columnId, 
        Object element, Composite parent) 
    {
        // Create a cell editor to modify the trigger value.
        if (TimersViewColumnPresentation.COL_VALUE.equals(columnId)) { 
            return new TextCellEditor(parent);
        } 
        return null;
    }

    // Note: this method is synchronized because IElementEditor.getCellModifier can be called
    // on any thread, even though in practice it should be only called on the UI thread.
    public ICellModifier getCellModifier(IPresentationContext context, 
        Object element) 
    {
        // Create the cell modifier if needed.
        if (fAlarmCellModifier == null) {
            fAlarmCellModifier = new TriggerCellModifier(getSession());
        }
        return fAlarmCellModifier; 
    }
    
    public int getDeltaFlags(Object e) {
        // Since the label for triggers doesn't change, this node will generate 
        // delta info only if the list of alarms is changed.
        if (e instanceof AlarmService.TriggersChangedEvent) {
            return IModelDelta.CONTENT;
        }
        return IModelDelta.NO_CHANGE;
    }
    
    
    public void buildDelta(Object event, VMDelta parentDelta, int nodeOffset, 
        RequestMonitor requestMonitor) 
    {
        if (event instanceof AlarmService.TriggersChangedEvent) {
            // The list of alarms has changed, which means that the parent 
            // node needs to refresh its contents, which in turn will re-fetch the
            // elements from this node.
            parentDelta.setFlags(parentDelta.getFlags() | IModelDelta.CONTENT);
        }
        requestMonitor.done();
    } 
    
    @Override
    public void dispose() {
        if (fAlarmCellModifier != null) {
            fAlarmCellModifier.dispose();
        }
        super.dispose();
    }
}

Back to the top