Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6275b764313559199b90917353986a02035e89a4 (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.ui.internal.adapters;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelLookupService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelQueryService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.ui.views.editor.EditorInput;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.internal.part.NullEditorInput;

/**
 * The element factory to create an peer model editor input from a memento which is read
 * from an external persistent storage and holds a peer id.
 */
@SuppressWarnings("restriction")
public class PeerNodeFactory implements IElementFactory {

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
	 */
	@Override
	public IAdaptable createElement(IMemento memento) {
		final AtomicReference<IPeerNode> node = new AtomicReference<IPeerNode>();
		final String peerId = memento.getString("peerId"); //$NON-NLS-1$
		if (peerId != null) {
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					node.set(Model.getModel().getService(IPeerModelLookupService.class).lkupPeerModelById(peerId));
				}
			};

			Assert.isTrue(!Protocol.isDispatchThread());
			Protocol.invokeAndWait(runnable);

			// If the node is null, this might mean that the peer to restore is a dynamically discovered peer.
			// In this case, we have to wait a little bit to give the locator service the chance to sync.
			if (node.get() == null) {
				// Sleep shortly
				try { Thread.sleep(300); } catch (InterruptedException e) {}

				// Refresh and try again to query the node
				Runnable runnable2 = new Runnable() {
					@Override
					public void run() {
						Model.getModel().getService(IPeerModelRefreshService.class).refresh(null);
						node.set(Model.getModel().getService(IPeerModelLookupService.class).lkupPeerModelById(peerId));
					}
				};

				Protocol.invokeAndWait(runnable2);
			}

			if (node.get() != null) {
				IPeerModel model = node.get().getModel();
				IPeerModelQueryService queryService = model.getService(IPeerModelQueryService.class);
				queryService.queryRemoteServices(node.get());
			}
		}

		return node.get() != null ? new EditorInput(node.get()) : new NullEditorInput();
	}
}

Back to the top