/*******************************************************************************
* Copyright (c) 2007 IBM Corporation 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:
* IBM Corporation - initial API and implementation
* Red Hat - update to Mylyn 3.0 API
*******************************************************************************/
package org.eclipse.ecf.internal.mylyn.ui;
import java.io.File;
import java.io.FileOutputStream;
import org.eclipse.core.runtime.*;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.ContainerManagerTracker;
import org.eclipse.ecf.datashare.*;
import org.eclipse.ecf.datashare.events.IChannelEvent;
import org.eclipse.ecf.datashare.events.IChannelMessageEvent;
import org.eclipse.ecf.internal.mylyn.ui.repository.RepositoryManager;
import org.eclipse.ecf.presence.service.IPresenceService;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.mylyn.context.core.IInteractionContext;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.ui.actions.ImportAction;
import org.eclipse.mylyn.internal.tasks.ui.actions.ImportAction.ImportStatus;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.progress.UIJob;
import org.osgi.framework.*;
public class Activator extends AbstractUIPlugin implements IChannelListener, ServiceListener {
static final String PLUGIN_ID = "org.eclipse.ecf.mylyn.ui"; //$NON-NLS-1$
private static Activator plugin;
private BundleContext context;
private ContainerManagerTracker tracker;
private RepositoryManager manager;
public void start(final BundleContext context) throws Exception {
super.start(context);
plugin = this;
this.context = context;
context.addServiceListener(this);
tracker = new ContainerManagerTracker(context);
}
protected void initializeImageRegistry(ImageRegistry reg) {
reg.put("IMG_SHARED_TASK", imageDescriptorFromPlugin(PLUGIN_ID, "icons/full/etool16/shared_task.gif").createImage());
}
public void stop(BundleContext context) throws Exception {
if (manager != null) {
manager.write();
}
plugin = null;
context.removeServiceListener(this);
tracker.close();
super.stop(context);
}
private void registerChannel(IChannelContainerAdapter channelAdapter) {
try {
ID channelID = channelAdapter.getChannelNamespace().createInstance(new Object[] {Activator.PLUGIN_ID});
IChannel channel = channelAdapter.getChannel(channelID);
if (channel == null) {
channel = channelAdapter.createChannel(channelID, this, null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void unregisterChannel(IChannelContainerAdapter channelAdapter) {
try {
ID channelID = channelAdapter.getChannelNamespace().createInstance(new Object[] {Activator.PLUGIN_ID});
channelAdapter.removeChannel(channelID);
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void handleChannelEvent(IChannelEvent e) {
if (e instanceof IChannelMessageEvent) {
IChannelMessageEvent msgEvent = (IChannelMessageEvent) e;
byte[] data = msgEvent.getData();
File file = new File(getStateLocation().toFile(), "incoming.xml.zip");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
ImportAction action = new ImportAction();
ImportStatus result = action.importElements(file);
final AbstractTask task = result.getTaskList().getAllTasks().iterator().next();
IInteractionContext context = ContextCorePlugin.getContextStore().loadContext(task.getHandleIdentifier());
CompoundContextActivationContributionItem.enqueue(task, context);
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
Shell aShell = null;
for (int i = 0; i < windows.length; i++) {
aShell = windows[i].getShell();
if (aShell != null) {
break;
}
}
if (aShell == null) {
return;
}
final Shell shell = aShell;
UIJob job = new UIJob("Notify of incoming shared task") {
public IStatus runInUIThread(IProgressMonitor monitor) {
final IncomingSharedTaskNotificationPopup popup = new IncomingSharedTaskNotificationPopup(shell);
popup.setTask(task);
popup.open();
new UIJob(shell.getDisplay(), "Close Popup Job") { //$NON-NLS-1$
public IStatus runInUIThread(IProgressMonitor monitor) {
Shell shell = popup.getShell();
if (shell != null && !shell.isDisposed()) {
popup.close();
}
monitor.done();
return Status.OK_STATUS;
}
}.schedule(5000);
return Status.OK_STATUS;
}
};
job.schedule();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
file.delete();
}
}
}
public void serviceChanged(ServiceEvent event) {
Object service = context.getService(event.getServiceReference());
if (service instanceof IAdaptable) {
service = ((IAdaptable) service).getAdapter(IPresenceService.class);
}
if (service instanceof IPresenceService) {
IPresenceService presenceService = (IPresenceService) service;
IChannelContainerAdapter channelAdapter = (IChannelContainerAdapter) presenceService.getAdapter(IChannelContainerAdapter.class);
if (channelAdapter != null) {
switch (event.getType()) {
case ServiceEvent.REGISTERED :
registerChannel(channelAdapter);
break;
case ServiceEvent.UNREGISTERING :
unregisterChannel(channelAdapter);
break;
}
}
}
}
public static Activator getDefault() {
return plugin;
}
public IContainerManager getContainerManager() {
return tracker.getContainerManager();
}
public synchronized RepositoryManager getRepositoryManager() {
if (manager == null) {
manager = new RepositoryManager(getRepostioriesPath().toFile());
}
return manager;
}
private IPath getRepostioriesPath() {
IPath stateLocation = Platform.getStateLocation(getBundle());
IPath cacheFile = stateLocation.append("repositories.bin"); //$NON-NLS-1$
return cacheFile;
}
}