Skip to main content
summaryrefslogtreecommitdiffstats
blob: 83b3c50ce2ad1d9c0425e7a5c4d486b86d83349e (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
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial implementation
 ******************************************************************************/
package org.eclipse.team.internal.ui.target;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;
import org.eclipse.team.core.target.IRemoteTargetResource;
import org.eclipse.team.internal.ui.Policy;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Used to show IRemoteTargetResource instances in the UI. In addition these elements
 * support caching of values returned from IRemoteTargetResource methods, as such these
 * instances aren't as much proxies as the underlying remote handles.
 * <p>
 * Implementation in progress: support for configuring these elements with a custom
 * progress monitor that can be the target of long running operations. This will
 * allow showing progress in wizards and in the SiteExplorerView when navigating
 * remote servers.</p>
 */
public class RemoteResourceElement implements IWorkbenchAdapter, IAdaptable {
	
	// remote resource this element represents
	private IRemoteTargetResource remote;
	
	// cache for the remote values
	private IRemoteResource[] children = null;
	private int size = 0;
	private String lastModified = null;

	// embeded progress monitoring support
	private Shell shell;
	private IProgressMonitor monitor;
	
	public RemoteResourceElement(IRemoteTargetResource remote) {
		this.remote = remote;
	}
	
	public IRemoteTargetResource getRemoteResource() {
		return remote;
	}
	
	public Object getAdapter(Class adapter) {
		if (adapter == IWorkbenchAdapter.class) return this;
		return null;
	}

	public Object[] getChildren(Object o) {
		final Object[][] result = new Object[1][];
		try {	
			IRunnableWithProgress runnable = new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
						monitor.beginTask(null, 100);
						if(children == null) {
							setCachedChildren(remote.members(Policy.subMonitorFor(monitor, 50)));
						}
						List remoteElements = new ArrayList();
						for (int i = 0; i < children.length; i++) {
							IRemoteTargetResource child = (IRemoteTargetResource)children[i];
							RemoteResourceElement element = new RemoteResourceElement(child);
							
							// setup progress monitors
							element.setShell(shell);
							element.setProgressMonitor(monitor);
							
							// decide which children to return based on filter settings
							element.setLastModified(child.getLastModified(Policy.subMonitorFor(monitor, 25)));
							// cache size and last modified
							element.setSize(child.getSize(Policy.subMonitorFor(monitor, 25)));								
							remoteElements.add(element);
						}
						result[0] = (RemoteResourceElement[])remoteElements.toArray(new RemoteResourceElement[remoteElements.size()]);							
					} catch (TeamException e) {
						throw new InvocationTargetException(e);
					} finally {
						monitor.done();
					}
				}
			};
			
			TeamUIPlugin.runWithProgress(null, true /*cancelable*/, runnable);
		} catch (InterruptedException e) {
			return new Object[0];
		} catch (InvocationTargetException e) {
			TeamUIPlugin.handle(e.getTargetException());
			return new Object[0];
		}
		return result[0];
	}
	
	public ImageDescriptor getImageDescriptor(Object object) {
		if(remote.isContainer()) {
			return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER);
		} else {
			return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(remote.getName());
		}
	}
	
	public String getLabel(Object o) {
		// would be nice to display more than just the name (e.g. timestamp, size...)
		return remote.getName();
	}
	
	public Object getParent(Object o) {
		return null;
	}
	
	public int hashCode() {
		return getRemoteResource().hashCode();
	}
	
	public boolean equals(Object obj) {
		if(this == obj)
			return true;
		if(!(obj instanceof RemoteResourceElement))
			return false;
		return ((RemoteResourceElement)obj).getRemoteResource().equals(getRemoteResource());
	}
	
	public void clearChildren() {
		children = null;
	}
	
	public IRemoteResource[] getCachedChildren() {
		return children;
	}
	
	public void setCachedChildren(IRemoteResource[] children) {
		this.children = children;
	}
	
	protected void setRemoteResource(IRemoteTargetResource remote) {
		this.remote = remote;
	}
	
	public void setShell(Shell shell) {
		this.shell = shell;
	}
	
	public void setProgressMonitor(IProgressMonitor monitor) {
		this.monitor = monitor;
	}
	
	public Shell getShell() {
		return shell;
	}

	public IProgressMonitor getProgressMonitor() {
		return monitor;
	}
	
	public String getLastModified() {
		return lastModified;
	}

	public int getSize() {
		return size;
	}

	public void setLastModified(String lastModified) {
		this.lastModified = lastModified;
	}

	public void setSize(int size) {
		this.size = size;
	}
	
	public String getName() {
		return remote.getName();
	}
}

Back to the top