Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b22e38a35ba62be06227b97ec1b4d32d70f499e (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
/*******************************************************************************
 * Copyright (c) 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.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.events.ChangeEvent;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.persistence.history.HistoryManager;
import org.eclipse.tcf.te.runtime.services.AbstractService;
import org.eclipse.tcf.te.tcf.core.interfaces.IPeerType;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelLookupService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Default context service implementation.
 */
public class DefaultContextService extends AbstractService implements IDefaultContextService {

	/**
	 * Part id: System Management view
	 */
	private static final String PART_ID_TE_VIEW = "org.eclipse.tcf.te.ui.views.View"; //$NON-NLS-1$

	/**
	 * Constructor.
	 */
	public DefaultContextService() {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService#getCandidates(java.lang.Object, org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService.IContextFilter)
	 */
	@Override
	public IPeerModel[] getCandidates(Object currentSelection, IContextFilter filter) {
		List<IPeerModel> candidates = new ArrayList<IPeerModel>();

		// add given selection first
		if (currentSelection instanceof IStructuredSelection) {
			addCandidates((IStructuredSelection)currentSelection, filter, candidates);
		}

		// add default selection
		addCandidates(getDefaultSelections(filter), filter, candidates);

		// add active editor
		addCandidates(getEditorSelection(), filter, candidates);

		// add system management selection
		addCandidates(getPartSelection(PART_ID_TE_VIEW), filter, candidates);

		return candidates.toArray(new IPeerModel[candidates.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService#setDefaultContext(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel)
	 */
	@Override
	public void setDefaultContext(final IPeerModel peerModel) {
		if (peerModel != null) {
			HistoryManager.getInstance().add(getClass().getName(), peerModel.getPeerId());
			EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_ADDED, peerModel, peerModel));

			final AtomicReference<String> type = new AtomicReference<String>();
			Protocol.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					type.set(peerModel.getPeer().getAttributes().get((IPeerModelProperties.PROP_TYPE)));
				}
			});
			HistoryManager.getInstance().add(type.get() != null ? type.get() : IPeerType.TYPE_GENERIC, peerModel.getPeerId());
		}
		else {
			HistoryManager.getInstance().clear(getClass().getName());
			EventManager.getInstance().fireEvent(new ChangeEvent(this, ChangeEvent.ID_REMOVED, null, null));
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService#getDefaultContext(org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService.IContextFilter)
	 */
	@Override
	public IPeerModel getDefaultContext(IContextFilter filter) {
		for (String peerId : HistoryManager.getInstance().getHistory(getClass().getName())) {
			IPeerModel peerModel = addCandidate(getPeerModel(peerId), filter, null);
			if (peerModel != null) {
				return peerModel;
			}
		}

		return null;
	}


	private IPeerModel addCandidate(IPeerModel peerModel, IContextFilter filter, List<IPeerModel> candidates) {
		if (peerModel != null && (filter == null || filter.select(peerModel))) {
			if (candidates != null && !candidates.contains(peerModel)) {
				candidates.add(peerModel);
			}
			return peerModel;
		}

		return null;
	}

	private void addCandidates(IStructuredSelection selection, IContextFilter filter, List<IPeerModel> candidates) {
		if (selection != null) {
			Iterator<Object> it = selection.iterator();
			while (it.hasNext()) {
				addCandidate((IPeerModel)Platform.getAdapterManager().getAdapter(it.next(), IPeerModel.class), filter, candidates);
			}
		}
	}

	private void addCandidates(IPeerModel[] peerModels, IContextFilter filter, List<IPeerModel> candidates) {
		for (IPeerModel peerModel : peerModels) {
			addCandidate(peerModel, filter, candidates);
		}
	}

	private IPeerModel[] getDefaultSelections(IContextFilter filter) {
		List<IPeerModel> candidates = new ArrayList<IPeerModel>();

		for (String peerId : HistoryManager.getInstance().getHistory(getClass().getName())) {
			addCandidate(getPeerModel(peerId), filter, candidates);
		}

		return candidates.toArray(new IPeerModel[candidates.size()]);
	}

	private IPeerModel getPeerModel(final String peerId) {
		if (peerId != null) {
			final AtomicReference<IPeerModel> peerModel = new AtomicReference<IPeerModel>();

			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					ILocatorModel model = Model.getModel();
					Assert.isNotNull(model);
					peerModel.set(model.getService(ILocatorModelLookupService.class).lkupPeerModelById(peerId));
				}
			};

			if (Protocol.isDispatchThread()) {
				runnable.run();
			}
			else {
				Protocol.invokeAndWait(runnable);
			}

			return peerModel.get();
		}

		return null;
	}

	private IStructuredSelection getEditorSelection() {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null && window.getActivePage() != null && window.getActivePage().getActiveEditor() != null) {
			return new StructuredSelection(window.getActivePage().getActiveEditor().getEditorInput());
		}
		return null;
	}

	private IStructuredSelection getPartSelection(String partId) {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window != null && window.getActivePage() != null) {
			ISelection sel = null;
			if (partId != null) {
				sel = window.getActivePage().getSelection(partId);
			}
			else {
				sel = window.getActivePage().getSelection();
			}

			if (sel instanceof IStructuredSelection) {
				return (IStructuredSelection)sel;
			}
		}
		return null;
	}
}

Back to the top