Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cff764f81983c0691c48954b9bb43257a701a698 (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
package org.eclipse.ecf.internal.examples.webinar.util.rosterentry;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.datashare.IChannel;
import org.eclipse.ecf.datashare.IChannelContainerAdapter;
import org.eclipse.ecf.datashare.IChannelListener;
import org.eclipse.ecf.datashare.events.IChannelEvent;
import org.eclipse.ecf.datashare.events.IChannelMessageEvent;
import org.eclipse.ecf.presence.roster.IRosterEntry;
import org.eclipse.ecf.presence.ui.roster.AbstractRosterEntryContributionItem;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;

public class RosterEntryContributionItem2 extends
		AbstractRosterEntryContributionItem {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.presence.ui.roster.AbstractPresenceContributionItem#makeActions()
	 */
	protected IAction[] makeActions() {
		IAction action = null;
		final IRosterEntry rosterEntry = getSelectedRosterEntry();
		if (rosterEntry != null) {
			if (initializeChannelFor(getContainerForRosterEntry(rosterEntry)) != null) {
				action = new Action() {
					public void run() {
						sendDataToChannel(rosterEntry);
					}

				};
				action.setText("Send Message and URL");
				action.setImageDescriptor(PlatformUI.getWorkbench()
						.getSharedImages().getImageDescriptor(
								ISharedImages.IMG_DEF_VIEW));
				return new IAction[] { action };
			}
		}
		return null;
	}

	private void sendDataToChannel(IRosterEntry rosterEntry) {
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(new String[] { "Hi " + rosterEntry.getName(),
					"http://www.eclipse.org/ecf" });
			IChannel channel = initializeChannelFor(getContainerForRosterEntry(rosterEntry));
			channel.sendMessage(rosterEntry.getUser().getID(), bos
					.toByteArray());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void receiveDataFromChannel(byte[] data) {
		try {
			ByteArrayInputStream bins = new ByteArrayInputStream(data);
			ObjectInputStream oos = new ObjectInputStream(bins);
			String[] received = (String[]) oos.readObject();
			showMessageAndURL(received[0], received[1]);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static Map channels = new HashMap();

	private IChannel initializeChannelFor(IContainer container) {
		if (container == null)
			return null;
		ID containerID = container.getID();
		IChannel chan = (IChannel) channels.get(containerID);
		if (chan == null) {
			IChannelContainerAdapter adapter = (IChannelContainerAdapter) container
					.getAdapter(IChannelContainerAdapter.class);
			if (adapter != null) {
				chan = createChannel(adapter);
				channels.put(containerID, chan);
			}
		}
		return chan;
	}

	public void dispose() {
		super.dispose();
		for (Iterator i = channels.keySet().iterator(); i.hasNext();) {
			IChannel chan = (IChannel) channels.get(i.next());
			chan.dispose();
		}
		channels.clear();
	}

	private IChannel createChannel(IChannelContainerAdapter adapter) {
		try {
			return adapter.createChannel(IDFactory.getDefault().createStringID(
					"mychannel"), new IChannelListener() {
				public void handleChannelEvent(IChannelEvent event) {
					if (event instanceof IChannelMessageEvent) {
						receiveDataFromChannel(((IChannelMessageEvent) event)
								.getData());
					}
				}

			}, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private void showMessageAndURL(final String string, final String url) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				if (MessageDialog.openConfirm(null, "Received message", string
						+ ".  Show URL?"))
					showURL(url);
			}
		});
	}

	private void showURL(String string) {
		IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
				.getBrowserSupport();
		IWebBrowser browser;
		try {
			browser = support.createBrowser(null);
			browser.openURL(new URL(string));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Back to the top