Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d35287e12c59bf6c927fddf3ddaa1de0704a00e6 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*******************************************************************************
 * Copyright (c) 2011 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.tm.te.tcf.filesystem.controls;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IPeer;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.IFileSystem;
import org.eclipse.tm.tcf.services.IFileSystem.DirEntry;
import org.eclipse.tm.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tm.tcf.services.IFileSystem.IFileHandle;
import org.eclipse.tm.te.tcf.core.Tcf;
import org.eclipse.tm.te.tcf.core.interfaces.IChannelManager;
import org.eclipse.tm.te.tcf.filesystem.model.FSModel;
import org.eclipse.tm.te.tcf.filesystem.model.FSTreeNode;
import org.eclipse.tm.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tm.te.tcf.locator.interfaces.nodes.IPeerModelProperties;
import org.eclipse.tm.te.ui.nls.Messages;
import org.eclipse.ui.PlatformUI;


/**
 * Target Explorer: File system tree content provider implementation.
 */
public class FSTreeContentProvider implements ITreeContentProvider {
	/**
	 * Static reference to the return value representing no elements.
	 */
	protected final static Object[] NO_ELEMENTS = new Object[0];

	/**
	 * The file system model instance associated with this file system
	 * tree content provider instance. Each content provider has it's own
	 * file system mode instance.
	 */
	/* default*/ final FSModel model = new FSModel();

	/* default */ Viewer viewer = null;

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		this.viewer = viewer;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	@Override
	public void dispose() {
		model.dispose();
	}

	/**
	 * Close the open communication channel.
	 */
	protected void closeOpenChannel(final IChannel channel) {
		if (channel != null) {
			if (Protocol.isDispatchThread()) {
				channel.close();
			} else {
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						channel.close();
					}
				});
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
	 */
	@Override
	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
	 */
	@Override
	public Object getParent(Object element) {
		if (element instanceof FSTreeNode) {
			FSTreeNode parent = ((FSTreeNode)element).parent;
			// If the parent is a root node, return the associated peer node
			if (parent != null && parent.type != null && parent.type.endsWith("RootNode")) { //$NON-NLS-1$
				return parent.peerNode;
			}
			return parent;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
	 */
	@Override
	public Object[] getChildren(Object parentElement) {
		Assert.isNotNull(parentElement);

		Object[] children = NO_ELEMENTS;

		// For the file system, we need the peer node
		if (parentElement instanceof IPeerModel) {
			final IPeerModel peerNode = (IPeerModel)parentElement;
			final String peerId = peerNode.getPeer().getID();

			// Get the file system model root node, if already stored
			final FSTreeNode[] root = new FSTreeNode[1];
			if (Protocol.isDispatchThread()) {
				root[0] = model.getRoot(peerId);
			} else {
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						root[0] = model.getRoot(peerId);
					}
				});
			}

			// If the file system model root node hasn't been created, create
			// and initialize the root node now.
			if (root[0] == null) {
				IPeer peer = peerNode.getPeer();
				final int[] state = new int[1];
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						state[0] = peerNode.getIntProperty(IPeerModelProperties.PROP_STATE);
					}
				});
				if (peer != null && IPeerModelProperties.STATE_ERROR != state[0] && IPeerModelProperties.STATE_NOT_REACHABLE != state[0]) {
					final List<FSTreeNode> candidates = new ArrayList<FSTreeNode>();
					// Create the root node and the initial pending node.
					// This must happen in the TCF dispatch thread.
					Protocol.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							// The root node
							FSTreeNode rootNode = new FSTreeNode();
							rootNode.type = "FSRootNode"; //$NON-NLS-1$
							rootNode.peerNode = peerNode;
							rootNode.childrenQueried = false;
							rootNode.childrenQueryRunning = true;
							model.putRoot(peerId, rootNode);

							// Add a special "Pending..." node
							FSTreeNode pendingNode = new FSTreeNode();
							pendingNode.name = Messages.PendingOperation_label;
							pendingNode.type ="FSPendingNode"; //$NON-NLS-1$
							pendingNode.parent = rootNode;
							pendingNode.peerNode = rootNode.peerNode;
							rootNode.getChildren().add(pendingNode);

							candidates.addAll(rootNode.getChildren());
						}
					});

					children = candidates.toArray();

					Tcf.getChannelManager().openChannel(peer, new IChannelManager.DoneOpenChannel() {
						@Override
						public void doneOpenChannel(final Throwable error, final IChannel channel) {
							Assert.isTrue(Protocol.isDispatchThread());

							if (channel != null) {
								final IFileSystem service = channel.getRemoteService(IFileSystem.class);
								if (service != null) {

									Protocol.invokeLater(new Runnable() {
										@Override
										public void run() {
											service.roots(new IFileSystem.DoneRoots() {
												@Override
												public void doneRoots(IToken token, FileSystemException error, DirEntry[] entries) {
													// Close the channel, not needed anymore
													closeOpenChannel(channel);

													FSTreeNode rootNode = model.getRoot(peerId);
													if (rootNode != null && error == null) {

														for (DirEntry entry : entries) {
															FSTreeNode node = createNodeFromDirEntry(entry, true);
															if (node != null) {
																node.parent = rootNode;
																node.peerNode = rootNode.peerNode;
																rootNode.getChildren().add(node);
															}
														}

														// Find the pending node and remove it from the child list
														Iterator<FSTreeNode> iterator = rootNode.getChildren().iterator();
														while (iterator.hasNext()) {
															FSTreeNode candidate = iterator.next();
															if (Messages.PendingOperation_label.equals(candidate.name)) {
																iterator.remove();
																break;
															}
														}

														// Reset the children query markers
														rootNode.childrenQueryRunning = false;
														rootNode.childrenQueried = true;
													}

													PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
														@Override
														public void run() {
															if (viewer != null) viewer.refresh();
														}
													});
												}
											});
										}
									});

									PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
										@Override
										public void run() {
											if (viewer != null) viewer.refresh();
										}
									});
								} else {
									// The file system service is not available for this peer.
									// --> Close the just opened channel
									closeOpenChannel(channel);
								}
							}
						}
					});
				}
			} else {
				// Get possible children
				// This must happen in the TCF dispatch thread.
				final List<FSTreeNode> candidates = new ArrayList<FSTreeNode>();
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						candidates.addAll(root[0].getChildren());
					}
				});
				children = candidates.toArray();
			}
		} else if (parentElement instanceof FSTreeNode) {
			final FSTreeNode node = (FSTreeNode)parentElement;

			// Get possible children
			// This must happen in the TCF dispatch thread.
			final List<FSTreeNode> candidates = new ArrayList<FSTreeNode>();
			Protocol.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					candidates.addAll(node.getChildren());
				}
			});
			children = candidates.toArray();
			// No children -> check for "childrenQueried" property. If false, trigger the query.
			if (children.length == 0 && !node.childrenQueried && node.type.endsWith("DirNode")) { //$NON-NLS-1$
				candidates.clear();
				// Add a special "Pending..." node
				// This must happen in the TCF dispatch thread.
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						FSTreeNode pendingNode = new FSTreeNode();
						pendingNode.name = Messages.PendingOperation_label;
						pendingNode.type ="FSPendingNode"; //$NON-NLS-1$
						pendingNode.parent = node;
						pendingNode.peerNode = node.peerNode;
						node.getChildren().add(pendingNode);

						candidates.addAll(node.getChildren());
					}
				});
				children = candidates.toArray();

				if (!node.childrenQueryRunning && node.peerNode != null) {
					node.childrenQueryRunning = true;
					final String absName = getEntryAbsoluteName(node);

					if (absName != null) {
						// Open a channel to the peer and query the children
						Tcf.getChannelManager().openChannel(node.peerNode.getPeer(), new IChannelManager.DoneOpenChannel() {
							@Override
							public void doneOpenChannel(final Throwable error, final IChannel channel) {
								Assert.isTrue(Protocol.isDispatchThread());

								if (channel != null && channel.getState() == IChannel.STATE_OPEN) {
									final IFileSystem service = channel.getRemoteService(IFileSystem.class);
									if (service != null) {

										Protocol.invokeLater(new Runnable() {
											@Override
											public void run() {
												service.opendir(absName, new IFileSystem.DoneOpen() {
													@Override
													public void doneOpen(IToken token, FileSystemException error, final IFileHandle handle) {
														if (error == null) {
															// Read the directory content until finished
															readdir(channel, service, handle, node);
														} else {
															// In case of an error, we are done here
															node.childrenQueryRunning = false;
															node.childrenQueried = true;
														}
													}
												});
											}
										});
									} else {
										// No file system service available
										node.childrenQueryRunning = false;
										node.childrenQueried = true;
									}
								} else {
									// Channel failed to open
									node.childrenQueryRunning = false;
									node.childrenQueried = true;
								}
							}
						});
					} else {
						// No absolute name
						node.childrenQueryRunning = false;
						node.childrenQueried = true;
					}
				}
			}
		}

		return children;
	}

	/**
	 * Reads the content of a directory until the file system service signals EOF.
	 *
	 * @param channel The open channel. Must not be <code>null</code>.
	 * @param service The file system service. Must not be <code>null</code>.
	 * @param handle The directory handle. Must not be <code>null</code>.
	 * @param parentNode The parent node receiving the entries. Must not be <code>null</code>.
	 * @param mode The notification mode to set to the parent node once done.
	 */
	protected void readdir(final IChannel channel, final IFileSystem service, final IFileHandle handle, final FSTreeNode parentNode) {
		Assert.isNotNull(channel);
		Assert.isNotNull(service);
		Assert.isNotNull(handle);
		Assert.isNotNull(parentNode);

		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				service.readdir(handle, new IFileSystem.DoneReadDir() {

					@Override
					public void doneReadDir(IToken token, FileSystemException error, DirEntry[] entries, boolean eof) {
						// Close the handle and channel if EOF is signaled or an error occurred.
						if (eof) {
							service.close(handle, new IFileSystem.DoneClose() {
								@Override
								public void doneClose(IToken token, FileSystemException error) {
									closeOpenChannel(channel);
								}
							});
						}

						// Process the returned data
						if (error == null && entries != null && entries.length > 0) {
							for (DirEntry entry : entries) {
								FSTreeNode node = createNodeFromDirEntry(entry, false);
								if (node != null) {
									node.parent = parentNode;
									node.peerNode = parentNode.peerNode;
									parentNode.getChildren().add(node);
								}
							}
						}

						if (eof) {
							// Find the pending node and remove it from the child list
							Iterator<FSTreeNode> iterator = parentNode.getChildren().iterator();
							while (iterator.hasNext()) {
								FSTreeNode candidate = iterator.next();
								if (Messages.PendingOperation_label.equals(candidate.name)) {
									iterator.remove();
									break;
								}
							}

							// Reset the children query markers
							parentNode.childrenQueryRunning = false;
							parentNode.childrenQueried = true;
						} else {
							// And invoke ourself again
							readdir(channel, service, handle, parentNode);
						}

						PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
							@Override
							public void run() {
								if (viewer instanceof StructuredViewer) ((StructuredViewer)viewer).refresh(parentNode);
							}
						});
					}
				});
			}
		});
	}


	/**
	 * Creates a tree node from the given directory entry.
	 *
	 * @param entry The directory entry. Must not be <code>null</code>.
	 *
	 * @return The tree node.
	 */
	protected FSTreeNode createNodeFromDirEntry(DirEntry entry, boolean entryIsRootNode) {
		Assert.isNotNull(entry);

		FSTreeNode node = null;

		IFileSystem.FileAttrs attrs = entry.attrs;

		if (attrs == null || attrs.isDirectory()) {
			node = new FSTreeNode();
			node.childrenQueried = false;
			node.childrenQueryRunning = false;
			node.attr = attrs;
			node.name = entry.filename;
			node.type = entryIsRootNode ? "FSRootDirNode" : "FSDirNode"; //$NON-NLS-1$ //$NON-NLS-2$
		} else if (attrs.isFile()) {
			node = new FSTreeNode();
			node.childrenQueried = false;
			node.childrenQueryRunning = false;
			node.attr = attrs;
			node.name = entry.filename;
			node.type = "FSFileNode"; //$NON-NLS-1$
		}

		return node;
	}

	/**
	 * Returns the absolute name for the given node.
	 *
	 * @param node The node. Must not be <code>null</code>.
	 * @return The absolute name.
	 */
	public static String getEntryAbsoluteName(FSTreeNode node) {
		Assert.isNotNull(node);

		StringBuilder path = new StringBuilder();

		// We have to walk upwards the hierarchy until the root node is found
		FSTreeNode parent = node.parent;
		while (parent != null && parent.type != null && parent.type.startsWith("FS")) { //$NON-NLS-1$
			if ("FSRootNode".equals(parent.type)) { //$NON-NLS-1$
				// We are done if reaching the root node
				break;
			}

			if (path.length() == 0) path.append(parent.name.replaceAll("\\\\", "/")); //$NON-NLS-1$ //$NON-NLS-2$
			else {
				String name = parent.name.replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$
				if (!name.endsWith("/")) name = name + "/"; //$NON-NLS-1$ //$NON-NLS-2$
				path.insert(0, name);
			}

			parent = parent.parent;
		}

		if (path.length() > 0 && path.charAt(path.length() - 1) != '/') {
			path.append("/"); //$NON-NLS-1$
		}
		path.append(node.name);

		return path.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
	 */
	@Override
	public boolean hasChildren(final Object element) {
		Assert.isNotNull(element);

		boolean hasChildren = false;

		if (element instanceof FSTreeNode) {
			final FSTreeNode node = (FSTreeNode)element;
			if (node.type != null && (node.type.endsWith("DirNode") || node.type.endsWith("RootNode"))) { //$NON-NLS-1$ //$NON-NLS-2$
				if (!node.childrenQueried || node.childrenQueryRunning) {
					hasChildren = true;
				} else if (node.childrenQueried) {
					final boolean[] result = new boolean[1];
					Protocol.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							result[0] = node.getChildren().size() > 0;
						}
					});
					hasChildren = result[0];
				}
			}
		}
		else if (element instanceof IPeerModel) {
			final String[] peerId = new String[1];
			if (Protocol.isDispatchThread()) {
				peerId[0] = ((IPeerModel)element).getPeer().getID();
			} else {
				Protocol.invokeAndWait(new Runnable() {
					@Override
					public void run() {
						peerId[0] = ((IPeerModel)element).getPeer().getID();
					}
				});
			}

			// Get the root node for this peer model object.
			// If null, true is returned as it means that the file system
			// model hasn't been created yet and have to treat is as children
			// not queried yet.
			FSTreeNode root = peerId[0] != null ? model.getRoot(peerId[0]): null;
			hasChildren = root != null ? hasChildren(root) : true;
		}

		return hasChildren;
	}
}

Back to the top