Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 237471
Collapse All | Expand All

(-)src/org/eclipse/ecf/docshare/menu/DocShareRosterMenuHandler.java (-28 / +16 lines)
Lines 1-5 Link Here
1
/****************************************************************************
1
/****************************************************************************
2
 * Copyright (c) 2007 Composent, Inc. and others.
2
 * Copyright (c) 2007, 2008 Composent, Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-31 Link Here
11
11
12
package org.eclipse.ecf.docshare.menu;
12
package org.eclipse.ecf.docshare.menu;
13
13
14
import org.eclipse.ecf.docshare.DocShare;
15
16
import org.eclipse.core.commands.ExecutionEvent;
14
import org.eclipse.core.commands.ExecutionEvent;
17
import org.eclipse.core.commands.ExecutionException;
15
import org.eclipse.core.commands.ExecutionException;
16
import org.eclipse.core.resources.IFile;
18
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
18
import org.eclipse.core.runtime.Status;
20
import org.eclipse.ecf.core.IContainer;
19
import org.eclipse.ecf.core.IContainer;
21
import org.eclipse.ecf.core.user.IUser;
20
import org.eclipse.ecf.core.user.IUser;
21
import org.eclipse.ecf.docshare.DocShare;
22
import org.eclipse.ecf.internal.docshare.Activator;
22
import org.eclipse.ecf.internal.docshare.Activator;
23
import org.eclipse.ecf.internal.docshare.Messages;
23
import org.eclipse.ecf.internal.docshare.Messages;
24
import org.eclipse.ecf.presence.roster.IRoster;
24
import org.eclipse.ecf.presence.roster.IRoster;
25
import org.eclipse.ecf.presence.roster.IRosterEntry;
25
import org.eclipse.ecf.presence.roster.IRosterEntry;
26
import org.eclipse.ecf.presence.ui.menu.AbstractRosterMenuHandler;
26
import org.eclipse.ecf.presence.ui.menu.AbstractRosterMenuHandler;
27
import org.eclipse.jface.dialogs.ErrorDialog;
27
import org.eclipse.jface.dialogs.ErrorDialog;
28
import org.eclipse.ui.*;
28
import org.eclipse.ui.IEditorInput;
29
import org.eclipse.ui.IEditorPart;
30
import org.eclipse.ui.handlers.HandlerUtil;
29
import org.eclipse.ui.texteditor.ITextEditor;
31
import org.eclipse.ui.texteditor.ITextEditor;
30
32
31
/**
33
/**
Lines 40-68 Link Here
40
		super(entry);
42
		super(entry);
41
	}
43
	}
42
44
43
	protected ITextEditor getTextEditor() {
45
	protected ITextEditor getTextEditor(ExecutionEvent event) {
44
		final IWorkbench workbench = PlatformUI.getWorkbench();
46
		IEditorPart part = HandlerUtil.getActiveEditor(event);
45
		if (workbench == null)
47
		return part instanceof ITextEditor ? (ITextEditor) part : null;
46
			return null;
47
		final IWorkbenchWindow ww = workbench.getActiveWorkbenchWindow();
48
		if (ww == null)
49
			return null;
50
		final IWorkbenchPage wp = ww.getActivePage();
51
		if (wp == null)
52
			return null;
53
		final IEditorPart ep = wp.getActiveEditor();
54
		if (ep instanceof ITextEditor)
55
			return (ITextEditor) ep;
56
		return null;
57
	}
48
	}
58
49
59
	private String getFileName(IEditorPart editorPart) {
50
	private String getFileName(IEditorPart editorPart) {
60
		final IEditorInput input = editorPart.getEditorInput();
51
		final IEditorInput input = editorPart.getEditorInput();
61
		if (input instanceof IFileEditorInput) {
52
		IFile file = (IFile) input.getAdapter(IFile.class);
62
			final IFileEditorInput fei = (IFileEditorInput) input;
53
		return file == null ? null : file.getFullPath().toString();
63
			return fei.getName();
64
		}
65
		return null;
66
	}
54
	}
67
55
68
	private void showErrorMessage(String errorMessage) {
56
	private void showErrorMessage(String errorMessage) {
Lines 72-78 Link Here
72
	/**
60
	/**
73
	 * @throws ExecutionException  
61
	 * @throws ExecutionException  
74
	 */
62
	 */
75
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
63
	public Object execute(ExecutionEvent event) throws ExecutionException {
76
		IRosterEntry rosterEntry = getRosterEntry();
64
		IRosterEntry rosterEntry = getRosterEntry();
77
		if (rosterEntry != null) {
65
		if (rosterEntry != null) {
78
			IRoster roster = rosterEntry.getRoster();
66
			IRoster roster = rosterEntry.getRoster();
Lines 84-97 Link Here
84
				showErrorMessage(Messages.DocShareRosterMenuHandler_ERROR_NO_SENDER);
72
				showErrorMessage(Messages.DocShareRosterMenuHandler_ERROR_NO_SENDER);
85
			if (sender.isSharing())
73
			if (sender.isSharing())
86
				showErrorMessage(Messages.DocShareRosterMenuHandler_ERROR_EDITOR_ALREADY_SHARING);
74
				showErrorMessage(Messages.DocShareRosterMenuHandler_ERROR_EDITOR_ALREADY_SHARING);
87
			final ITextEditor textEditor = getTextEditor();
75
			final ITextEditor textEditor = getTextEditor(event);
88
			if (textEditor == null)
76
			if (textEditor == null)
89
				showErrorMessage(Messages.DocShareRosterMenuHandler_EXCEPTION_EDITOR_NOT_TEXT);
77
				showErrorMessage(Messages.DocShareRosterMenuHandler_EXCEPTION_EDITOR_NOT_TEXT);
90
			final String fileName = getFileName(textEditor);
78
			final String filePath = getFileName(textEditor);
91
			if (fileName == null)
79
			if (filePath == null)
92
				showErrorMessage(Messages.DocShareRosterMenuHandler_NO_FILENAME_WITH_CONTENT);
80
				showErrorMessage(Messages.DocShareRosterMenuHandler_NO_FILENAME_WITH_CONTENT);
93
			final IUser user = roster.getUser();
81
			final IUser user = roster.getUser();
94
			sender.startShare(user.getID(), user.getName(), rosterEntry.getUser().getID(), fileName, textEditor);
82
			sender.startShare(user.getID(), user.getName(), rosterEntry.getUser().getID(), filePath, textEditor);
95
		}
83
		}
96
		return null;
84
		return null;
97
	}
85
	}
(-)src/org/eclipse/ecf/internal/docshare/messages.properties (+1 lines)
Lines 10-15 Link Here
10
################################################################################
10
################################################################################
11
DocShare_EDITOR_SHARE_POPUP_MESSAGE=Editor share request from {0} for {1}.\n\nDo you want to accept and open editor?
11
DocShare_EDITOR_SHARE_POPUP_MESSAGE=Editor share request from {0} for {1}.\n\nDo you want to accept and open editor?
12
DocShare_EDITOR_SHARE_POPUP_TITLE=Editor Share
12
DocShare_EDITOR_SHARE_POPUP_TITLE=Editor Share
13
DocShare_EDITOR_SHARE_FILE_EXISTS_POPUP_MESSAGE=The file {0} already exists in your workspace and its contents may differ from the remote peer's. Are you sure you wish to proceed? Proceeding will cause the file's content to be overridden by the remote copy.\n\nThe original copy will be backed up under a similar name.
13
DocShare_ERROR_STARTING_EDITOR_MESSAGE=Error Starting Editor Sharing {0}
14
DocShare_ERROR_STARTING_EDITOR_MESSAGE=Error Starting Editor Sharing {0}
14
DocShare_ERROR_STARTING_EDITOR_TITLE=Error
15
DocShare_ERROR_STARTING_EDITOR_TITLE=Error
15
DocShare_EXCEPTION_DESERIALIZING_MESSAGE0=Exception deserializing DocumentChangeMessage
16
DocShare_EXCEPTION_DESERIALIZING_MESSAGE0=Exception deserializing DocumentChangeMessage
(-)src/org/eclipse/ecf/internal/docshare/Messages.java (-1 / +2 lines)
Lines 1-5 Link Here
1
/****************************************************************************
1
/****************************************************************************
2
 * Copyright (c) 2007 Composent, Inc. and others.
2
 * Copyright (c) 2007, 2008 Composent, Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 20-25 Link Here
20
	private static final String BUNDLE_NAME = "org.eclipse.ecf.internal.docshare.messages"; //$NON-NLS-1$
20
	private static final String BUNDLE_NAME = "org.eclipse.ecf.internal.docshare.messages"; //$NON-NLS-1$
21
	public static String DocShare_EDITOR_SHARE_POPUP_MESSAGE;
21
	public static String DocShare_EDITOR_SHARE_POPUP_MESSAGE;
22
	public static String DocShare_EDITOR_SHARE_POPUP_TITLE;
22
	public static String DocShare_EDITOR_SHARE_POPUP_TITLE;
23
	public static String DocShare_EDITOR_SHARE_FILE_EXISTS_POPUP_MESSAGE;
23
	public static String DocShare_ERROR_STARTING_EDITOR_MESSAGE;
24
	public static String DocShare_ERROR_STARTING_EDITOR_MESSAGE;
24
	public static String DocShare_ERROR_STARTING_EDITOR_TITLE;
25
	public static String DocShare_ERROR_STARTING_EDITOR_TITLE;
25
	public static String DocShare_EXCEPTION_DESERIALIZING_MESSAGE0;
26
	public static String DocShare_EXCEPTION_DESERIALIZING_MESSAGE0;
(-)src/org/eclipse/ecf/docshare/DocShare.java (-13 / +62 lines)
Lines 16-21 Link Here
16
import java.io.*;
16
import java.io.*;
17
import org.eclipse.core.filesystem.EFS;
17
import org.eclipse.core.filesystem.EFS;
18
import org.eclipse.core.filesystem.IFileStore;
18
import org.eclipse.core.filesystem.IFileStore;
19
import org.eclipse.core.resources.IFile;
20
import org.eclipse.core.resources.ResourcesPlugin;
19
import org.eclipse.core.runtime.*;
21
import org.eclipse.core.runtime.*;
20
import org.eclipse.core.runtime.Assert;
22
import org.eclipse.core.runtime.Assert;
21
import org.eclipse.ecf.core.IContainer;
23
import org.eclipse.ecf.core.IContainer;
Lines 39-44 Link Here
39
import org.eclipse.swt.widgets.*;
41
import org.eclipse.swt.widgets.*;
40
import org.eclipse.ui.*;
42
import org.eclipse.ui.*;
41
import org.eclipse.ui.editors.text.EditorsUI;
43
import org.eclipse.ui.editors.text.EditorsUI;
44
import org.eclipse.ui.ide.IDE;
45
import org.eclipse.ui.ide.ResourceUtil;
42
import org.eclipse.ui.texteditor.IDocumentProvider;
46
import org.eclipse.ui.texteditor.IDocumentProvider;
43
import org.eclipse.ui.texteditor.ITextEditor;
47
import org.eclipse.ui.texteditor.ITextEditor;
44
48
Lines 220-234 Link Here
220
	 *            a name to present to the receiver. If
224
	 *            a name to present to the receiver. If
221
	 *            <code>null, our.getName() will be used.
225
	 *            <code>null, our.getName() will be used.
222
	 * @param toID the ID of the intended receiver.  Must not be <code>null</code>.
226
	 * @param toID the ID of the intended receiver.  Must not be <code>null</code>.
223
	 * @param fileName the file name of the file to be shared (with suffix type extension).  Must not be <code>null</code>.
227
	 * @param filePath the workspace-relative path of the file to be shared (with suffix type extension).  Must not be <code>null</code>.
224
	 * @param editorPart the text editor currently showing the contents of this editor.  Must not be <code>null</code>.
228
	 * @param editorPart the text editor currently showing the contents of this editor.  Must not be <code>null</code>.
225
	 */
229
	 */
226
	public void startShare(final ID our, final String fromName, final ID toID, final String fileName, final ITextEditor editorPart) {
230
	public void startShare(final ID our, final String fromName, final ID toID, final String filePath, final ITextEditor editorPart) {
227
		Trace.entering(Activator.PLUGIN_ID, DocshareDebugOptions.METHODS_ENTERING, DocShare.class, "startShare", new Object[] {our, fromName, toID, fileName, editorPart}); //$NON-NLS-1$
231
		Trace.entering(Activator.PLUGIN_ID, DocshareDebugOptions.METHODS_ENTERING, DocShare.class, "startShare", new Object[] {our, fromName, toID, filePath, editorPart}); //$NON-NLS-1$
228
		Assert.isNotNull(our);
232
		Assert.isNotNull(our);
229
		final String fName = (fromName == null) ? our.getName() : fromName;
233
		final String fName = (fromName == null) ? our.getName() : fromName;
230
		Assert.isNotNull(toID);
234
		Assert.isNotNull(toID);
231
		Assert.isNotNull(fName);
235
		Assert.isNotNull(fName);
236
		Assert.isNotNull(filePath);
232
		Assert.isNotNull(editorPart);
237
		Assert.isNotNull(editorPart);
233
		Display.getDefault().syncExec(new Runnable() {
238
		Display.getDefault().syncExec(new Runnable() {
234
			public void run() {
239
			public void run() {
Lines 240-246 Link Here
240
					// Get content from local document
245
					// Get content from local document
241
					final String content = editorPart.getDocumentProvider().getDocument(editorPart.getEditorInput()).get();
246
					final String content = editorPart.getDocumentProvider().getDocument(editorPart.getEditorInput()).get();
242
					// Send start message with current content
247
					// Send start message with current content
243
					sendMessage(toID, new StartMessage(our, fName, toID, content, fileName).serialize());
248
					sendMessage(toID, new StartMessage(our, fName, toID, content, filePath).serialize());
244
					// Set local sharing start (to setup doc listener)
249
					// Set local sharing start (to setup doc listener)
245
					localStartShare(getLocalRosterManager(), our, our, toID, editorPart);
250
					localStartShare(getLocalRosterManager(), our, our, toID, editorPart);
246
				} catch (final Exception e) {
251
				} catch (final Exception e) {
Lines 308-315 Link Here
308
		Assert.isNotNull(senderUsername);
313
		Assert.isNotNull(senderUsername);
309
		final ID our = message.getReceiverID();
314
		final ID our = message.getReceiverID();
310
		Assert.isNotNull(our);
315
		Assert.isNotNull(our);
311
		final String filename = message.getFilename();
316
		final String filePath = message.getFilename();
312
		Assert.isNotNull(filename);
317
		Assert.isNotNull(filePath);
313
		final String documentContent = message.getDocumentContent();
318
		final String documentContent = message.getDocumentContent();
314
		Assert.isNotNull(documentContent);
319
		Assert.isNotNull(documentContent);
315
320
Lines 334-346 Link Here
334
			public void run() {
339
			public void run() {
335
				try {
340
				try {
336
					// First, ask user if they want to receive the doc
341
					// First, ask user if they want to receive the doc
337
					if (openReceiverDialog(senderID, senderUsername, filename)) {
342
					if (openReceiverDialog(senderID, senderUsername, filePath)) {
338
						// If so, then we create a new DocShareEditorInput
343
						IPath path = new Path(filePath);
339
						final DocShareEditorInput dsei = new DocShareEditorInput(getTempFileStore(senderUsername, filename, startContent), senderUsername, filename);
344
						String filename = path.lastSegment();
340
						// Then open up text editor
345
						IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
341
						final ITextEditor ep = (ITextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(dsei, getEditorIdForFileName(filename));
346
						IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
342
						// Then change our local state
347
						// check if the file exists
343
						localStartShare(getLocalRosterManager(), our, senderID, our, ep);
348
						if (file.exists()) {
349
							// if it does, prompt for overwrite
350
							if (openOverwritePrompt(filePath)) {
351
								int counter = 0;
352
								// Test.java -> Test.java.0
353
								IFile backup = file.getParent().getFile(new Path(filename + '.' + counter));
354
								while (backup.exists()) {
355
									// if the file exists, keep trying
356
									counter++;
357
									backup = file.getParent().getFile(new Path(filename + '.' + counter));
358
								}
359
360
								// set the backup's contents to match the original's
361
								backup.create(file.getContents(), true, null);
362
363
								// find an existing editor for the backed up file
364
								ITextEditor textEditor = (ITextEditor) ResourceUtil.findEditor(page, file);
365
								if (textEditor == null) {
366
									// none found, open one
367
									textEditor = (ITextEditor) IDE.openEditor(page, file, getEditorIdForFileName(filePath));
368
								}
369
								// retrieve the document
370
								IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
371
								// alter its contents to match the remote peer's
372
								document.set(startContent);
373
								// Then change our local state
374
								localStartShare(getLocalRosterManager(), our, senderID, our, textEditor);
375
							} else {
376
								// User doesn't want to overwrite with remote changes, send stop message to initiator
377
								sendStopMessage();
378
								// Then we stop the local share
379
								localStopShare();
380
							}
381
						} else {
382
							// The file doesn't exist, so we create a new DocShareEditorInput
383
							final DocShareEditorInput dsei = new DocShareEditorInput(getTempFileStore(senderUsername, filename, startContent), senderUsername, filename);
384
							// Then open up text editor
385
							final ITextEditor ep = (ITextEditor) page.openEditor(dsei, getEditorIdForFileName(filename));
386
							// Then change our local state
387
							localStartShare(getLocalRosterManager(), our, senderID, our, ep);
388
						}
344
					} else {
389
					} else {
345
						// Send stop message to initiator
390
						// Send stop message to initiator
346
						sendStopMessage();
391
						sendStopMessage();
Lines 473-478 Link Here
473
		return MessageDialog.openQuestion(null, Messages.DocShare_EDITOR_SHARE_POPUP_TITLE, NLS.bind(Messages.DocShare_EDITOR_SHARE_POPUP_MESSAGE, fromUsername, fileName));
518
		return MessageDialog.openQuestion(null, Messages.DocShare_EDITOR_SHARE_POPUP_TITLE, NLS.bind(Messages.DocShare_EDITOR_SHARE_POPUP_MESSAGE, fromUsername, fileName));
474
	}
519
	}
475
520
521
	boolean openOverwritePrompt(String filePath) {
522
		return MessageDialog.openQuestion(null, Messages.DocShare_EDITOR_SHARE_POPUP_TITLE, NLS.bind(Messages.DocShare_EDITOR_SHARE_FILE_EXISTS_POPUP_MESSAGE, filePath));
523
	}
524
476
	protected void handleDisconnectEvent(IChannelDisconnectEvent cde) {
525
	protected void handleDisconnectEvent(IChannelDisconnectEvent cde) {
477
		boolean weDisconnected = (ourID != null && ourID.equals(cde.getTargetID()));
526
		boolean weDisconnected = (ourID != null && ourID.equals(cde.getTargetID()));
478
		Shell shell = null;
527
		Shell shell = null;

Return to bug 237471