Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2012-05-03 10:20:26 +0000
committerUwe Stieber2012-05-03 10:21:42 +0000
commit23b78ff68f8835172a86f7cfa2b93bdf5794229d (patch)
treee6b159d2681de27226f58236ebc28518957a62ca /target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler
parent3c010ba2d91f0576a7459a761c15687f9a23b1ab (diff)
downloadorg.eclipse.tcf-23b78ff68f8835172a86f7cfa2b93bdf5794229d.tar.gz
org.eclipse.tcf-23b78ff68f8835172a86f7cfa2b93bdf5794229d.tar.xz
org.eclipse.tcf-23b78ff68f8835172a86f7cfa2b93bdf5794229d.zip
Target Explorer: Added "Always Available Offline" command + handler
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/AlwaysAvailableOfflineCommandHandler.java101
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/OfflineCommandHandler.java221
2 files changed, 221 insertions, 101 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/AlwaysAvailableOfflineCommandHandler.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/AlwaysAvailableOfflineCommandHandler.java
deleted file mode 100644
index edb5d0ae0..000000000
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/AlwaysAvailableOfflineCommandHandler.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2012 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.Map;
-
-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.Status;
-import org.eclipse.osgi.util.NLS;
-import org.eclipse.tcf.core.TransientPeer;
-import org.eclipse.tcf.protocol.IPeer;
-import org.eclipse.tcf.protocol.Protocol;
-import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
-import org.eclipse.tcf.te.runtime.services.ServiceManager;
-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.nls.Messages;
-
-/**
- *
- */
-public class AlwaysAvailableOfflineCommandHandler extends AbstractHandler {
-
- /* (non-Javadoc)
- * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
- */
- @Override
- public Object execute(ExecutionEvent event) throws ExecutionException {
- return null;
- }
-
- /**
- * Adds the given peer node to the "My Targets" list by creating
- * a static copy of the given peer.
- *
- * @param node The peer node. Must be not <code>null</code>.
- */
- public void addToMyTargets(final IPeerModel node) {
- Assert.isNotNull(node);
-
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- // If the peer node is a static node already, there is nothing to do here
- String value = node.getPeer().getAttributes().get("static.transient"); //$NON-NLS-1$
- boolean isStatic = value != null && Boolean.parseBoolean(value.trim());
- if (isStatic) {
- return;
- }
-
- // Not a static node yet, 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 uRIPersistenceService = ServiceManager.getInstance().getService(IURIPersistenceService.class);
- if (uRIPersistenceService == null) {
- throw new IOException("Persistence service instance unavailable."); //$NON-NLS-1$
- }
- uRIPersistenceService.write(new TransientPeer(attrs), null);
-
- Model.getModel().getService(ILocatorModelRefreshService.class).refresh();
- } catch (IOException e) {
- IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
- NLS.bind(Messages.CategoryManager_dnd_failed, e.getLocalizedMessage()), e);
- UIPlugin.getDefault().getLog().log(status);
- }
- }
- };
-
- if (Protocol.isDispatchThread()) {
- runnable.run();
- }
- else {
- Protocol.invokeAndWait(runnable);
- }
- }
-}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/OfflineCommandHandler.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/OfflineCommandHandler.java
new file mode 100644
index 000000000..93e2789fe
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui/src/org/eclipse/tcf/te/tcf/ui/handler/OfflineCommandHandler.java
@@ -0,0 +1,221 @@
+/*******************************************************************************
+ * Copyright (c) 2012 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.Status;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.tcf.core.TransientPeer;
+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.interfaces.properties.IPropertiesContainer;
+import org.eclipse.tcf.te.runtime.persistence.interfaces.IURIPersistenceService;
+import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
+import org.eclipse.tcf.te.runtime.services.ServiceManager;
+import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerManager;
+import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandler;
+import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandlerConstants;
+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.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.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) {
+ // Fill in the status handler custom data
+ IPropertiesContainer data = new PropertiesContainer();
+ data.setProperty(IStatusHandlerConstants.PROPERTY_TITLE, Messages.OfflineCommandHandler_error_title);
+ data.setProperty(IStatusHandlerConstants.PROPERTY_CONTEXT_HELP_ID, IContextHelpIds.MESSAGE_MAKEOFFLINE_FAILED);
+ data.setProperty(IStatusHandlerConstants.PROPERTY_CALLER, this);
+
+ // Get the status handler
+ IStatusHandler[] handler = StatusHandlerManager.getInstance().getHandler(selection);
+ if (handler.length > 0) {
+ handler[0].handleStatus(status, data, null);
+ }
+ }
+
+ // Get the locator model
+ final ILocatorModel model = Model.getModel();
+ if (model != null) {
+ // Trigger a refresh of the model
+ final ILocatorModelRefreshService service = model.getService(ILocatorModelRefreshService.class);
+ if (service != null) {
+ Protocol.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ // Refresh the model now (must be executed within the TCF dispatch thread)
+ service.refresh();
+ }
+ });
+ }
+ }
+ }
+ });
+
+ 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() {
+ String value = node.getPeer().getAttributes().get("static.transient"); //$NON-NLS-1$
+ isStatic.set(value != null && Boolean.parseBoolean(value.trim()));
+ }
+ };
+
+ 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 TransientPeer(attrs), null);
+
+ Model.getModel().getService(ILocatorModelRefreshService.class).refresh();
+ } 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