Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5b337a7c1cb30e498f6142bb5942ab0dec2127b5 (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
/*******************************************************************************
 * Copyright (c) 2007, 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.dsf.ui.viewmodel;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.cdt.dsf.concurrent.DsfExecutable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementMementoProvider;
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.TreePath;

/** 
 * Helper class implementation of the update objects used with 
 * {@link IElementContentProvider}, {@link IElementLabelProvider}, 
 * and {@link IElementMementoProvider}.  The viewer update can be constructed 
 * using a higher level update object or a set of parameters to fulfill the 
 * <code>IViewerUpdate</code> interface. 
 * 
 * @since 1.0
 */
public class VMViewerUpdate extends DsfExecutable implements IViewerUpdate {
    
    /**
     * The request monitor to be called when this update is completed.
     */
    final private RequestMonitor fRequestMonitor;
    
    /**
     * A higher-level update that this update is based on.  If specified, the given
     * update is used to delegate calls to {@link #cancel()} and {@link #isCanceled()}.
     */
    final private IViewerUpdate fClientUpdate;
    
    /**
     * Place holder for the client update.  It is only used if the client update is
     * not specified.
     */
    private class ClientUpdatePlaceHolder implements IViewerUpdate {
        
        ClientUpdatePlaceHolder(TreePath elementPath, Object viewerInput, IPresentationContext presentationContext)
        {
            fViewerInput = viewerInput;
            fElementPath = elementPath;
            fPresentationContext = presentationContext;
        }
        /**
         * The flag indicating whether this update was cancelled.  This flag is not used
         * if the {@link #fClientUpdate} is initialized.
         */
        final private AtomicBoolean fCanceled = new AtomicBoolean(false);
        
        /**
         * The viewer input object for this update.
         */
        final private Object fViewerInput;
        
        /**
         * The element path of this update.
         */
        final private TreePath fElementPath;
        
        /**
         * The presentation context of this update.
         */
        final private IPresentationContext fPresentationContext;

        public void cancel() {
            fCanceled.set(true);
        }
        
        public boolean isCanceled() { 
            return fCanceled.get(); 
        }

        public IPresentationContext getPresentationContext() {
            return fPresentationContext;
        }

        public Object getElement() {
            return fElementPath.getSegmentCount() != 0 ? fElementPath.getLastSegment() : fViewerInput;
        }

        public TreePath getElementPath() {
            return fElementPath;
        }

        public Object getViewerInput() {
            return fViewerInput;
        }
        
        public void done() { assert false; } // not used
        public void setStatus(IStatus status) {assert false; } // not used
        public IStatus getStatus() { assert false; return null; } // not used


    }
    
    /**
     * Creates a viewer update based on a higher-level update.  The update element
     * information as well as cancel requests are delegated to the given client
     * update.
     * <p/>
     * Note: this update will not automatically call the client update's 
     * {@link IRequest#done()} method.  The user of this update should supply
     * a request monitor which properly completes the client update. 
     * 
     * @param clientUpdate Client update that this update is based on.
     * @param requestMonitor Call-back invoked when this update completes.  
     */
    public VMViewerUpdate(IViewerUpdate clientUpdate, RequestMonitor requestMonitor) {
        fRequestMonitor = requestMonitor;
        fClientUpdate = clientUpdate;
    }

    /**
     * Request monitor which uses a model delta to calculate the element information.
     * This update is useful when calculating a model delta for a given view model node.
     * 
     * @param delta Model delta of a parent element.
     * @param presentationContext Presentation context for this update.
     * @param requestMonitor Call-back invoked when this update completes.  
     */
    public VMViewerUpdate(IModelDelta delta, IPresentationContext presentationContext, RequestMonitor requestMonitor) {
        List<Object> elementList = new LinkedList<Object>();
        IModelDelta listDelta = delta;
        elementList.add(0, listDelta.getElement());
        while (listDelta.getParentDelta() != null) {
            listDelta = listDelta.getParentDelta();
            elementList.add(0, listDelta.getElement());
        }
        fClientUpdate = new ClientUpdatePlaceHolder(
            new TreePath(elementList.toArray()), elementList.get(elementList.size() - 1), presentationContext);
        fRequestMonitor = requestMonitor;
    }

    /**
     * Creates a viewer update with the given parameters.
     * 
     * @param elementPath The path to the element for which the update is generated.
     * @param viewerInput Input into the viewer of the update.
     * @param presentationContext Presentation context for this update.
     * @param requestMonitor Call-back invoked when this update completes.  
     */
    public VMViewerUpdate(TreePath elementPath, Object viewerInput, IPresentationContext presentationContext, RequestMonitor requestMonitor) {
        fRequestMonitor = requestMonitor;
        fClientUpdate = new ClientUpdatePlaceHolder(elementPath, viewerInput, presentationContext);
    }
    
    protected RequestMonitor getRequestMonitor() {
        return fRequestMonitor;
    }
    
    public Object getViewerInput() { return fClientUpdate.getViewerInput(); }
    public Object getElement() { return fClientUpdate.getElement(); }
    public TreePath getElementPath() { return fClientUpdate.getElementPath(); }
    public IPresentationContext getPresentationContext() { return fClientUpdate.getPresentationContext(); }
    public IStatus getStatus() { return fRequestMonitor.getStatus(); }
    public void setStatus(IStatus status) { fRequestMonitor.setStatus(status); }
    
    public boolean isCanceled() { 
        return fClientUpdate.isCanceled();
    }
    public void cancel() {
        fClientUpdate.cancel();
    }

    public void done() { 
    	setSubmitted();
        if ( isCanceled() ) {
            fRequestMonitor.cancel();
            fRequestMonitor.setStatus(new Status( IStatus.CANCEL, DsfUIPlugin.PLUGIN_ID," Update was cancelled") ); //$NON-NLS-1$
        }
        fRequestMonitor.done();
    }

}

Back to the top