Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d300f4a0f9ff03fe34ea6f42223473329e7a5db3 (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
/*******************************************************************************
 * 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.tcf.te.tcf.filesystem.ui.internal.testers;

import java.util.List;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.FsClipboard;
/**
 * Provide a tester to test if the paste operation is enabled.
 */
public class ClipboardPropertyTester extends PropertyTester {

	/**
	 * Create an instance.
	 */
	public ClipboardPropertyTester() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
	 */
	@Override
	public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
		Assert.isTrue(receiver instanceof IStructuredSelection);
		if (property.equals("canPaste")) { //$NON-NLS-1$
			FsClipboard cb = UIPlugin.getClipboard();
			Clipboard clipboard = cb.getSystemClipboard();
			Object contents = clipboard.getContents(FileTransfer.getInstance());
			if(contents != null) {
				List<FSTreeNode> selection = ((IStructuredSelection) receiver).toList();
				FSTreeNode hovered = null;
				Assert.isTrue(!selection.isEmpty());
				if (selection.size() == 1) {
					FSTreeNode node = selection.get(0);
					if (node.isFile()) {
						hovered = node.parent;
					}
					else {
						hovered = node;
					}
				}
				else {
					for (FSTreeNode node : selection) {
						if (hovered == null) hovered = node.parent;
						else if (hovered != node.parent) return false;
					}
				}
				if (hovered.isDirectory() && hovered.isWritable()) {
					return true;
				}
				return false;
			} 
			else if (!cb.isEmpty()) {
				List<FSTreeNode> nodes = cb.getFiles();
				boolean moving = cb.isCutOp();
				boolean copying = cb.isCopyOp();
				List<FSTreeNode> selection = ((IStructuredSelection) receiver).toList();
				FSTreeNode hovered = null;
				Assert.isTrue(!selection.isEmpty());
				if (selection.size() == 1) {
					FSTreeNode node = selection.get(0);
					if (node.isDirectory() && moving) {
						hovered = node;
					}
					else if (node.isRoot()) {
						hovered = node;
					}
					else {
						hovered = node.parent;
					}
				}
				else {
					for (FSTreeNode node : selection) {
						if (hovered == null) hovered = node.parent;
						else if (hovered != node.parent) return false;
					}
				}
				if (hovered.isDirectory() && hovered.isWritable() && (moving || copying)) {
					FSTreeNode head = nodes.get(0);
					String hid = head.peerNode.getPeerId();
					String tid = hovered.peerNode.getPeerId();
					if (hid.equals(tid)) {
						for (FSTreeNode node : nodes) {
							if (moving && node.parent == hovered || node.isAncestorOf(hovered)) {
								return false;
							}
						}
						return true;
					}
				}
			}
		}
		return false;
	}
}

Back to the top