Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 613f5f94db929e1f39ee3ce5e6b509084f90a1c5 (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
/*******************************************************************************
 * Copyright (c) 2006 Ken Gilmer 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: Ken Gilmer - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.example.collab.editor.listeners;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.util.ECFException;
import org.eclipse.ecf.datashare.IChannel;
import org.eclipse.ecf.datashare.IChannelContainerAdapter;
import org.eclipse.ecf.datashare.IChannelListener;
import org.eclipse.ecf.example.collab.editor.Activator;
import org.eclipse.ecf.example.collab.editor.message.EditorChangeMessage;
import org.eclipse.ecf.example.collab.editor.message.EditorUpdateRequest;
import org.eclipse.ecf.example.collab.editor.preferences.ClientPreferencePage;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.ui.texteditor.AbstractTextEditor;

public class EditorListener implements IDocumentListener {
	public static final String SESSION_NAME_DELIMITER = "_";

	private IDocument document;

	private AbstractTextEditor editor;

	private IChannel channel;

	private IContainer container = null;

	private IChannelListener channelListener;

	private String sessionID;
	
	private boolean documentOwner;

	public EditorListener(IDocument document, AbstractTextEditor textEditor, boolean owner) {
		this.document = document;
		this.editor = textEditor;
		this.documentOwner = owner;
		
		try {
			intializeEditorSession();
			
			if (Activator.getDefault().getPresenceChannelListener() == null) {
				Activator.getDefault().intializePresenceSession(new PresenceChannelListener());
			}
		} catch (ECFException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, e.getLocalizedMessage(), e));
		} catch (IOException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, e.getLocalizedMessage(), e));		}
	}

	public void documentAboutToBeChanged(DocumentEvent event) {

	}

	public void documentChanged(DocumentEvent event) {
		if (channel == null) {
			// Communication error has occured. Stop listening to
			// document.
			document.removeDocumentListener(this);
			return;
		}
		
		if (!Activator.getDefault().isListenerActive()) {
			//The local editor is being updated by an remote peer, so we do not
			//wish to echo this change.
			return;
		}

		sendDocumentUpdateMessage();
	}
	
	public void sendDocumentUpdateMessage() {
		try {
			
			channel.sendMessage(createMessageFromEvent(document));

		} catch (ECFException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, e.getLocalizedMessage(), e));
		} catch (IOException e) {
			Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, e.getLocalizedMessage(), e));
		}
	}

	private byte[] createMessageFromEvent(IDocument document) throws IOException, ECFException {
		ByteArrayOutputStream bouts = new ByteArrayOutputStream();
		ObjectOutputStream douts = new ObjectOutputStream(bouts);
		douts.writeObject(new EditorChangeMessage(document.get()));
		return bouts.toByteArray();
	}

	public void intializeEditorSession() throws ECFException, IOException {
		container = ContainerFactory.getDefault().createContainer(
				Activator.getDefault().getPreferenceStore().getString(ClientPreferencePage.CONTAINER_TYPE));

		IChannelContainerAdapter channelContainer = (IChannelContainerAdapter) container.getAdapter(IChannelContainerAdapter.class);

		sessionID = Activator.getDefault().getPreferenceStore().getString(ClientPreferencePage.CHANNEL_ID) + SESSION_NAME_DELIMITER + editor.getTitle();

		Activator.getDefault().addSession(sessionID, editor.getTitle());
		
		final ID channelID = IDFactory.getDefault().createID(channelContainer.getChannelNamespace(), sessionID);

		channelListener = new EditChannelListener(document, editor, documentOwner, this);

		channel = channelContainer.createChannel(channelID, channelListener, new HashMap());

		container.connect(IDFactory.getDefault().createID(container.getConnectNamespace(),
				Activator.getDefault().getPreferenceStore().getString(ClientPreferencePage.TARGET_SERVER)), null);
		
		//If we don't own the document, request a fresh copy from the owner.
		if (!documentOwner) {
			sendEditorUpdateRequest();
		}
	}

	

	private void sendEditorUpdateRequest() throws ECFException, IOException {
		if (channel != null) {
			channel.sendMessage((new EditorUpdateRequest()).toByteArray());
		}
	}

	public String getSessionID() {
		return sessionID;
	}
}

Back to the top