Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7fb81c4bd7b159eded7296170accb41579771bc (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*******************************************************************************
 * Copyright (c) 2006, 2015 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.filebrowser;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.ui.viewmodel.IVMContext;
import org.eclipse.cdt.dsf.ui.viewmodel.IVMNode;
import org.eclipse.cdt.dsf.ui.viewmodel.IVMProvider;
import org.eclipse.cdt.dsf.ui.viewmodel.VMDelta;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementLabelProvider;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
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.IViewerUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;


/**
 * File view model node which returns file elements that are found in the directory 
 * specified by the parent element.  The child nodes of this node are fixed to 
 * reference this element, and therefore this node will recursively populate 
 * the contents of the tree reflecting the underlying filesystem directories.
 * <br>
 * Note: this node does NOT sub-class the {@link org.eclipse.cdt.dsf.ui.viewmodel.AbstractVMNode}
 */
@SuppressWarnings("restriction")
class FileVMNode 
    implements IElementLabelProvider, IVMNode
{
    /**
     * Reference to the viewer model provider.  It's mainly used to access the
     * viewer model adapter and its executor.
     */
    private final FileBrowserVMProvider fProvider;
    
    public FileVMNode(FileBrowserVMProvider provider) {
        fProvider = provider;
    }

    @Override
    public String toString() {
        return "FileVMNode";  
    }


    @Override
    public void dispose() {
        // All resources garbage collected.
    }
    
    public void setChildNodes(IVMNode[] childNodes) {
        throw new UnsupportedOperationException("This node does not support children."); //$NON-NLS-1$
    }

    /** 
     * List of child nodes containing only a reference to this.
     */
    private final IVMNode[] fChildNodes = { this };

    public IVMNode[] getChildNodes() {
        return fChildNodes;
    }
    
    @Override
    public void update(final IHasChildrenUpdate[] updates) {
        new Job("") { //$NON-NLS-1$
            {
                setSystem(true);
                setPriority(INTERACTIVE);
            }
                
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                for (IHasChildrenUpdate update : updates) {
                    /*
                     * Do not retrieve directory contents just to mark the plus
                     * sign in the tree.  If it's a directory, just assume that
                     * it has children.
                     */
                    FileVMContext vmc = (FileVMContext)update.getElement();
                    update.setHasChilren(vmc.getFile().isDirectory());
                    update.done();
                }
                
                return Status.OK_STATUS;
            }
        }.schedule();
    }
    
    @Override
    public void update(final IChildrenCountUpdate[] updates) {
        new Job("") { //$NON-NLS-1$
            {
                setSystem(true);
                setPriority(INTERACTIVE);
            }
                
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                for (IChildrenCountUpdate update : updates) {
                    update.setChildCount(getFiles(update).length);
                    update.done();
                }                
                return Status.OK_STATUS;
            }
        }.schedule();
    }
    
    @Override
    public void update(final IChildrenUpdate[] updates) {
        new Job("") { //$NON-NLS-1$
            {
                setSystem(true);
                setPriority(INTERACTIVE);
            }
                
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                for (IChildrenUpdate update : updates) {
                    File[] files = getFiles(update);
                    int offset = update.getOffset() != -1 ? update.getOffset() : 0;
                    int length = update.getLength() != -1 ? update.getLength() : files.length;
                    for (int i = offset; (i < files.length) && (i < (offset + length)); i++) {
                        update.setChild(new FileVMContext(FileVMNode.this, files[i]), i);
                    }
                    update.done();
                }                
                return Status.OK_STATUS;
            }
        }.schedule();
    }
    
    @Override
    public void update(final ILabelUpdate[] updates) {
        new Job("") { //$NON-NLS-1$
            {
                setSystem(true);
                setPriority(INTERACTIVE);
            }
                
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                for (ILabelUpdate update : updates) {
                    update.setLabel(getLabel((FileVMContext)update.getElement()), 0);
                    update.done();
                }
                
                return Status.OK_STATUS;
            }
        }.schedule();
    }

    private static final File[] EMPTY_FILE_LIST = new File[0];

    /**
     * Retrieves the list of files for this node.  The list of files is based 
     * on the parent element in the tree, which must be of type FileVMC. 
     * 
     * @param update Update object containing the path (and the parent element) 
     * in the tree viewer.
     * @return List of files contained in the directory specified in the 
     * update object.  An empty list if the parent element is not a directory. 
     * @throws ClassCastException If the parent element contained in the update 
     * is NOT of type FileVMC. 
     */
    private File[] getFiles(IViewerUpdate update) {
        FileVMContext vmc = (FileVMContext)update.getElement();
        File[] files =  vmc.getFile().listFiles();
        return files != null ? files : EMPTY_FILE_LIST;
    }

    /**
     * Returs the text label to show in the tree for given element.
     */
    private  String getLabel(FileVMContext vmc) {
        return vmc.getFile().getName();     
    }

    @Override
    public void getContextsForEvent(VMDelta parentDelta, Object event, DataRequestMonitor<IVMContext[]> rm) {
        rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, IDsfStatusConstants.NOT_SUPPORTED, "", null)); //$NON-NLS-1$
        rm.done();
    }
    
    @Override
    public int getDeltaFlags(Object e) {
        /*
         * @see buildDelta()
         */
        int retVal = IModelDelta.NO_CHANGE;
        if (e instanceof String) {
            retVal |= IModelDelta.SELECT | IModelDelta.EXPAND; 
        }
        
        return retVal;
    }

    @Override
    public void buildDelta(final Object event, final VMDelta parentDelta, final int nodeOffset, final RequestMonitor requestMonitor) {
        /*
         * The FileLayoutNode is recursive, with itself as the only child.  In this 
         * method the delta is calculated for a full path VMContext elements, and the
         * implementation of this method is not recursive.
         */
        if (event instanceof String) {
            new Job("") { //$NON-NLS-1$
                {
                    setSystem(true);
                    setPriority(INTERACTIVE);
                }
                    
                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    /*
                     * Requirements for a selection event to be issued is that the file exist, and
                     * that the parentDelta contain a FileVMC of a parent directory as its element.
                     *  
                     * The test for first the former requirement could be performed inside getDeltaFlags() 
                     * but getDeltaFlags() is synchronous, so it is better to perform this test here using 
                     * a background thread (job).
                     * 
                     *  The latter is requirement is needed because this node does not have the algorithm 
                     *  calculate the complete list of root nodes.  That algorithm is implemented inside the
                     *  {@link FileSystemRootsLayoutNode#updateElements} method.
                     */
                    
                    final File eventFile = new File((String)event);
                    File parentFile = null;
                    if (parentDelta.getElement() instanceof FileVMContext) {
                        parentFile = ((FileVMContext)parentDelta.getElement()).getFile();
                    }

                    // The file has to exist in order for us to be able to select 
                    // it in the tree. 
                    if (eventFile.exists() && parentFile != null) {
                        // Create a list containing all files in path
                        List<File> filePath = new LinkedList<File>();
                        for (File file = eventFile; file != null && !file.equals(parentFile); file = file.getParentFile()) {
                            filePath.add(0, file);
                        }

                        if (filePath.size() != 0) {
                            // Build the delta for all files in path.
                            ModelDelta delta = parentDelta;
                            File[] allFilesInDirectory = parentFile.listFiles();
                            for (File pathSegment : filePath) {
                                // All files in path should be directories, and should therefore
                                // have a valid list of elements.
                                assert allFilesInDirectory != null;
                                
                                File[] pathSegmentDirectoryFiles = pathSegment.listFiles();
                                delta = delta.addNode(
                                    new FileVMContext(FileVMNode.this, pathSegment), 
                                    nodeOffset + Arrays.asList(allFilesInDirectory).indexOf(pathSegment), 
                                    IModelDelta.NO_CHANGE, 
                                    pathSegmentDirectoryFiles != null ? pathSegmentDirectoryFiles.length : 0);
                                allFilesInDirectory = pathSegmentDirectoryFiles;
                            }
                            
                            // The last file in path gets the EXPAND | SELECT flags.
                            delta.setFlags(delta.getFlags() | IModelDelta.SELECT | IModelDelta.EXPAND);
                        }
                    }
                    
                    // Invoke the request monitor.
                    
                    requestMonitor.done();

                    return Status.OK_STATUS;
                }
            }.schedule();
        } else {
            requestMonitor.done();
        }            
    }
    
    /**
     * Override the behavior which checks for delta flags of all the child nodes, 
     * because we would get stuck in a recursive loop.  Instead call only the child 
     * nodes which are not us.
     */
    protected Map<IVMNode, Integer> getChildNodesWithDeltas(Object e) {
        Map<IVMNode, Integer> nodes = new HashMap<IVMNode, Integer>(); 
        for (final IVMNode childNode : getChildNodes()) {
            int delta = childNode.getDeltaFlags(e);
            if (delta != IModelDelta.NO_CHANGE) {
                nodes.put(childNode, delta);
            }
        }
        return nodes;
    }

    @Override
    public IVMProvider getVMProvider() {
        return fProvider;
    }

    
}

Back to the top