Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 35b8ad64e741b1c4be804c768e15b8a0a0135ed2 (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
/*******************************************************************************
 * 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 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.te.tcf.filesystem.core.internal.callbacks;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DirEntry;
import org.eclipse.tcf.services.IFileSystem.DoneClose;
import org.eclipse.tcf.services.IFileSystem.DoneReadDir;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.tcf.filesystem.core.activator.CorePlugin;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;

/**
 * The callback handler that handles the event when a directory is read.
 */
public class QueryDoneReadDir extends CallbackBase implements DoneReadDir {
	// The tcf channel.
	IChannel channel;
	// The file system service.
	IFileSystem service;
	// The file handle of the parent directory.
	IFileSystem.IFileHandle handle;
	// The parent node being queried.
	FSTreeNode parentNode;
	// The callback object.
	ICallback callback;
	/**
	 * Create an instance with parameters to initialize the fields.
	 * 
	 * @param channel The tcf channel.
	 * @param service The file system service.
	 * @param handle The directory's file handle.
	 * @param parentNode The parent directory.
	 */
	public QueryDoneReadDir(ICallback callback, IChannel channel, IFileSystem service, IFileSystem.IFileHandle handle, FSTreeNode parentNode) {
		this.callback = callback;
		this.channel = channel;
		this.service = service;
		this.handle = handle;
		this.parentNode = parentNode;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.services.IFileSystem.DoneReadDir#doneReadDir(org.eclipse.tcf.protocol.IToken, org.eclipse.tcf.services.IFileSystem.FileSystemException, org.eclipse.tcf.services.IFileSystem.DirEntry[], boolean)
	 */
	@Override
	public void doneReadDir(IToken token, FileSystemException error, DirEntry[] entries, boolean eof) {
		// Process the returned data
		if (error == null) {
			if (entries != null && entries.length > 0) {
				for (DirEntry entry : entries) {
					FSTreeNode node = new FSTreeNode(parentNode, entry, false);
					parentNode.addChild(node);
				}
			}

			if (eof) {
				// Close the handle and channel if EOF is signaled or an error occurred.
				service.close(handle, new DoneClose() {
					@Override
                    public void doneClose(IToken token, FileSystemException error) {
						if(callback != null) {
							IStatus status = error == null ? Status.OK_STATUS : new Status(IStatus.ERROR, CorePlugin.getUniqueIdentifier(), error.getMessage(), error);
							callback.done(this, status);
						}
                    }});
			}
			else {
				// And invoke ourself again
				service.readdir(handle, new QueryDoneReadDir(callback, channel, service, handle, parentNode));
			}
		} else if(callback != null) {
			// Close the handle and channel if EOF is signaled or an error occurred.
			service.close(handle, new DoneClose() {
				@Override
				public void doneClose(IToken token, FileSystemException error) {
					IStatus status = error == null ? Status.OK_STATUS : new Status(IStatus.ERROR, CorePlugin.getUniqueIdentifier(), getErrorMessage(error), error);
					callback.done(this, status);
				}
			});
		}
	}
}

Back to the top