Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3baf14819225055a5b98f245452fef4dfd1413a6 (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
205
206
207
208
209
/*******************************************************************************
 * 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.handler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerUtil;
import org.eclipse.tcf.te.tcf.core.peers.Peer;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelRefreshService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.tcf.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.ui.help.IContextHelpIds;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.views.Managers;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.tcf.te.ui.views.interfaces.categories.ICategorizable;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * "Always available offline" command handler implementation.
 */
public class OfflineCommandHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		// Get the current selection
		final ISelection selection = HandlerUtil.getCurrentSelection(event);
		// Make the selection available offline
		if (selection != null) makeAvailableOffline(selection, new Callback() {
			@Override
			protected void internalDone(Object caller, IStatus status) {
				if (status.getSeverity() == IStatus.ERROR) {
					StatusHandlerUtil.handleStatus(status, selection, null,
													Messages.OfflineCommandHandler_error_title, IContextHelpIds.MESSAGE_MAKEOFFLINE_FAILED, OfflineCommandHandler.this, null);
				}

				// Trigger a refresh of the model
				Protocol.invokeLater(new Runnable() {
					@Override
					public void run() {
						ILocatorModelRefreshService service = Model.getModel().getService(ILocatorModelRefreshService.class);
						// Refresh the model now (must be executed within the TCF dispatch thread)
						if (service != null) service.refresh(null);
					}
				});
			}
		});

		return null;
	}

	/**
	 * Tests if the elements of the given selection can be made
	 * available offline.
	 *
	 * @param selection The selection. Must not be <code>null</code>.
	 * @return <code>True</code> if the elements can be made available offline, <code>false</code> otherwise.
	 */
	public boolean canMakeAvailableOffline(ISelection selection) {
		Assert.isNotNull(selection);

		boolean enabled = false;

		// The selection must be a structured selection and must not be empty
		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			// Assume the selection to be deletable
			enabled = true;
			// Iterate the selection. All elements must be of type IPeerModel
			Iterator<?> iterator = ((IStructuredSelection)selection).iterator();
			while (iterator.hasNext()) {
				Object element = iterator.next();
				if (!(element instanceof IPeerModel)) {
					enabled = false;
					break;
				}

				// Determine if the selected peer model is static
				boolean isStatic = isStatic((IPeerModel)element);
				if (isStatic) enabled = false;

				if (!enabled) break;
			}
		}

		return enabled;
	}

	/**
	 * Determines if the given peer model node is a static node.
	 *
	 * @param node The peer model node. Must not be <code>null</code>.
	 * @return <code>True</code> if the node is static, <code>false</code> otherwise.
	 */
	private boolean isStatic(final IPeerModel node) {
		Assert.isNotNull(node);

		final AtomicBoolean isStatic = new AtomicBoolean();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				isStatic.set(node.isStatic());
			}
		};

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

		return isStatic.get();
	}

	/**
	 * Creates an offline copy of the peer attributes.
	 *
	 * @param selection The selection. Must not be <code>null</code>.
	 * @param callback The callback. Must not be <code>null</code>.
	 */
	public void makeAvailableOffline(final ISelection selection, final ICallback callback) {
		Assert.isNotNull(selection);
		Assert.isNotNull(callback);

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// The status to report back
				IStatus status = Status.OK_STATUS;
				// The selection must be a structured selection and must not be empty
				if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
					// Iterate the selection. All elements must be of type IPeerModel
					Iterator<?> iterator = ((IStructuredSelection)selection).iterator();
					while (iterator.hasNext()) {
						Object element = iterator.next();
						Assert.isTrue(element instanceof IPeerModel);
						IPeerModel node = (IPeerModel)element;

						// Copy the peer attributes
						Map<String, String> attrs = new HashMap<String, String>();
						attrs.putAll(node.getPeer().getAttributes());

						// Remove attributes filled in by the discovery
						attrs.remove(IPeer.ATTR_AGENT_ID);
						attrs.remove(IPeer.ATTR_SERVICE_MANGER_ID);
						attrs.remove("ServerManagerID"); //$NON-NLS-1$
						attrs.remove(IPeer.ATTR_USER_NAME);
						attrs.remove(IPeer.ATTR_OS_NAME);

						// Persist the attributes
						try {
							IURIPersistenceService service = ServiceManager.getInstance().getService(IURIPersistenceService.class);
							if (service == null) {
								throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
							}
							service.write(new Peer(attrs), null);

							// Remove the node from the "Neighborhood" category
			        	    ICategorizable categorizable = (ICategorizable)node.getAdapter(ICategorizable.class);
			            	if (categorizable == null) categorizable = (ICategorizable)Platform.getAdapterManager().getAdapter(node, ICategorizable.class);
			            	Assert.isNotNull(categorizable);

							Managers.getCategoryManager().remove(IUIConstants.ID_CAT_NEIGHBORHOOD, categorizable.getId());
						} catch (IOException e) {
							status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
											   NLS.bind(Messages.OfflineCommandHandler_error_makeOffline_failed, node.getName(), e.getLocalizedMessage()), e);
						}

						if (status != null) break;
					}
				}

				// Invoke the callback
				callback.done(OfflineCommandHandler.this, status);
			}
		};

		Protocol.invokeLater(runnable);
	}
}

Back to the top