Skip to main content
summaryrefslogtreecommitdiffstats
blob: eb0f00dcc6fe3d817054eab945fa7471a25e2ad1 (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
/*******************************************************************************
 * Copyright (c) 2009 Remy Chi Jian Suen 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:
 *    Remy Chi Jian Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.internal.sync.ui.resources;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.datashare.IChannelContainerAdapter;
import org.eclipse.ecf.docshare2.DocShare;
import org.eclipse.ecf.internal.sync.resources.core.ResourcesShare;
import org.eclipse.ecf.internal.sync.resources.core.SyncResourcesCore;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.viewers.IPostSelectionProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.ui.texteditor.ITextEditor;
import org.osgi.framework.BundleContext;

public class SyncResourcesUI extends AbstractUIPlugin {

	public static final String PLUGIN_ID = "org.eclipse.ecf.sync.ui.resources"; //$NON-NLS-1$

	private static SyncResourcesUI instance;

	private IWindowListener windowListener = new IWindowListener() {

		public void windowOpened(IWorkbenchWindow window) {
			window.addPageListener(pageListener);
		}

		public void windowDeactivated(IWorkbenchWindow window) {
			// nothing to do
		}

		public void windowClosed(IWorkbenchWindow window) {
			window.removePageListener(pageListener);
		}

		public void windowActivated(IWorkbenchWindow window) {
			// nothing to do
		}
	};

	private IPageListener pageListener = new IPageListener() {

		public void pageOpened(IWorkbenchPage page) {
			page.addPartListener(partListener);
		}

		public void pageClosed(IWorkbenchPage page) {
			page.removePartListener(partListener);
		}

		public void pageActivated(IWorkbenchPage page) {
			// nothing to do
		}
	};

	private IPartListener partListener = new IPartListener() {

		public void partOpened(IWorkbenchPart part) {
			ITextEditor editor = (ITextEditor) part
					.getAdapter(ITextEditor.class);
			if (editor != null) {
				IFile file = (IFile) editor.getEditorInput().getAdapter(
						IFile.class);
				if (file != null) {
					startSharing(editor, file);
				}
			}
		}

		public void partDeactivated(IWorkbenchPart part) {
			// nothing to do
		}

		public void partClosed(IWorkbenchPart part) {
			ITextEditor editor = (ITextEditor) part
					.getAdapter(ITextEditor.class);
			if (editor != null) {
				IFile file = (IFile) editor.getEditorInput().getAdapter(
						IFile.class);
				if (file != null) {
					stopSharing(editor, file);
				}
			}
		}

		public void partBroughtToTop(IWorkbenchPart part) {
			// nothing to do
		}

		public void partActivated(IWorkbenchPart part) {
			// nothing to do
		}
	};

	private Map sharedFiles = new HashMap();

	private Map sharedEditors = new HashMap();

	private boolean share(IFile file) {
		IPath path = file.getFullPath();
		Integer integer = (Integer) sharedFiles.get(path);
		if (integer == null) {
			sharedFiles.put(path, new Integer(1));
			return true;
		}
		sharedFiles.put(path, new Integer(integer.intValue() + 1));
		return false;
	}

	private boolean unshare(IFile file) {
		IPath path = file.getFullPath();
		Integer integer = (Integer) sharedFiles.remove(path);
		if (integer == null) {
			// if this file isn't being shared, will be null, return false
			return false;
		}

		if (integer.intValue() == 1) {
			return true;
		}

		sharedFiles.put(path, new Integer(integer.intValue() - 1));
		return false;
	}

	private void startSharing(ITextEditor editor, IFile file) {
		String projectName = file.getProject().getName();
		for (Iterator it = SyncResourcesCore.getResourceShares().iterator(); it
				.hasNext();) {
			ResourcesShare share = (ResourcesShare) it.next();
			if (share.isSharing(projectName) && share(file)) {
				DocShare docShare = getDocShare(share.getContainerID());
				try {
					IAnnotationModel annotationModel = editor
							.getDocumentProvider().getAnnotationModel(
									editor.getEditorInput());
					docShare.startSharing(share.getLocalID(), share
							.getReceiverID(), file.getFullPath().toString(),
							annotationModel);
					ISelectionProvider provider = editor.getSelectionProvider();
					if (provider instanceof IPostSelectionProvider) {
						ISelectionChangedListener listener = new SelectionChangedListener(
								share.getReceiverID(), file.getFullPath()
										.toString(), docShare);
						((IPostSelectionProvider) provider)
								.addPostSelectionChangedListener(listener);
						sharedEditors.put(editor, listener);
					}
				} catch (ECFException e) {
					IStatus status = new Status(IStatus.ERROR, PLUGIN_ID,
							"Could not send initiation request to " //$NON-NLS-1$
									+ share.getReceiverID(), e);
					log(status);
					StatusManager.getManager().handle(status,
							StatusManager.SHOW);
				} catch (CoreException e) {
					IStatus status = new Status(IStatus.ERROR, PLUGIN_ID,
							"Could not connect to the file buffer of " //$NON-NLS-1$
									+ file.getFullPath(), e);
					log(status);
					StatusManager.getManager().handle(status,
							StatusManager.SHOW);
				}
			}
		}
	}

	private void stopSharing(ITextEditor editor, IFile file) {
		String projectName = file.getProject().getName();
		for (Iterator it = SyncResourcesCore.getResourceShares().iterator(); it
				.hasNext();) {
			ResourcesShare share = (ResourcesShare) it.next();
			if (share.isSharing(projectName) && unshare(file)) {
				DocShare docShare = getDocShare(share.getContainerID());
				stopSharing(docShare, share.getReceiverID(), editor, file);
			}
		}
	}

	private void stopSharing(DocShare share, ID id, ITextEditor editor,
			IFile file) {
		try {
			share.stopSharing(id, file.getFullPath().toString());
		} catch (ECFException e) {
			log(new Status(IStatus.ERROR, PLUGIN_ID,
					"Could not send stop message to " + id, e)); //$NON-NLS-1$
		}

		ISelectionChangedListener listener = (ISelectionChangedListener) sharedEditors
				.remove(editor);
		if (listener != null) {
			ISelectionProvider provider = editor.getSelectionProvider();
			if (provider instanceof IPostSelectionProvider) {
				((IPostSelectionProvider) provider)
						.removePostSelectionChangedListener(listener);
			}
		}
	}

	public SyncResourcesUI() {
		instance = this;
	}

	public void start(BundleContext context) throws Exception {
		super.start(context);

		getWorkbench().addWindowListener(windowListener);

		IWorkbenchWindow[] workbenchWindows = getWorkbench()
				.getWorkbenchWindows();
		for (int i = 0; i < workbenchWindows.length; i++) {
			workbenchWindows[i].addPageListener(pageListener);
			IWorkbenchPage[] pages = workbenchWindows[i].getPages();
			for (int j = 0; j < pages.length; j++) {
				pages[j].addPartListener(partListener);
			}
		}
	}
	
	public static SyncResourcesUI getDefault() {
		return instance;
	}
	
	public Display getDisplay() {
		return getWorkbench().getDisplay();
	}

	public static void log(IStatus status) {
		instance.getLog().log(status);
	}

	public void stop(BundleContext context) throws Exception {
		getWorkbench().removeWindowListener(windowListener);

		IWorkbenchWindow[] workbenchWindows = getWorkbench()
				.getWorkbenchWindows();
		for (int i = 0; i < workbenchWindows.length; i++) {
			workbenchWindows[i].removePageListener(pageListener);
			IWorkbenchPage[] pages = workbenchWindows[i].getPages();
			for (int j = 0; j < pages.length; j++) {
				pages[j].removePartListener(partListener);
			}
		}

		super.stop(context);
	}

	private static Map docshareChannels = new HashMap();

	public static DocShare getDocShare(ID containerID) {
		return (DocShare) docshareChannels.get(containerID);
	}

	public static DocShare addDocShare(ID containerID,
			IChannelContainerAdapter channelAdapter) throws ECFException {
		DocShare docShare = (DocShare) docshareChannels.get(containerID);
		if (docShare == null) {
			return (DocShare) docshareChannels.put(containerID,
					new WorkbenchAwareDocShare(channelAdapter));
		}
		return docShare;
	}

	public static DocShare removeDocShare(ID containerID) {
		return (DocShare) docshareChannels.remove(containerID);
	}

	public static ResourcesShare getResourceShare(ID containerID) {
		return SyncResourcesCore.getResourcesShare(containerID);
	}

	public static ResourcesShare addResourceShare(ID containerID,
			IChannelContainerAdapter channelAdapter) throws ECFException {
		ResourcesShare resourcesShare = SyncResourcesCore
				.getResourcesShare(containerID);
		if (resourcesShare == null) {
			resourcesShare = new WorkbenchAwareResourcesShare(containerID,
					channelAdapter);
			SyncResourcesCore.addResourcesShare(containerID, resourcesShare);
		}
		return resourcesShare;
	}

	public static ResourcesShare removeResourceShare(ID containerID) {
		return SyncResourcesCore.removeResourcesShare(containerID);
	}

	private class SelectionChangedListener implements ISelectionChangedListener {

		private ID targetID;
		private String path;
		private DocShare share;

		public SelectionChangedListener(ID targetID, String path, DocShare share) {
			this.targetID = targetID;
			this.path = path;
			this.share = share;
		}

		public void selectionChanged(SelectionChangedEvent e) {
			ISelection selection = e.getSelection();
			if (selection instanceof ITextSelection) {
				ITextSelection textSelection = (ITextSelection) selection;
				try {
					share.sendSelection(targetID, path, textSelection
							.getOffset(), textSelection.getLength());
				} catch (ECFException exception) {
					log(new Status(IStatus.ERROR, PLUGIN_ID,
							"Could not send selection message to " + targetID, //$NON-NLS-1$
							exception));
				}
			}
		}

	}

}

Back to the top