diff options
Diffstat (limited to 'incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf')
7 files changed, 708 insertions, 0 deletions
diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/ActivateReceivedContextHandler.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/ActivateReceivedContextHandler.java new file mode 100755 index 000000000..d26d76315 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/ActivateReceivedContextHandler.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import org.eclipse.core.commands.*; +import org.eclipse.ecf.internal.mylyn.ui.CompoundContextActivationContributionItem.ActivateTaskAction; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; + +public class ActivateReceivedContextHandler extends AbstractHandler { + + public Object execute(ExecutionEvent event) throws ExecutionException { + Shell shell = HandlerUtil.getActiveShell(event); + if (shell != null) { + open(shell); + } + return null; + } + + static void open(Shell shell) { + SelectTaskDialog dialog = new SelectTaskDialog(shell); + dialog.setInput(CompoundContextActivationContributionItem.tasks); + if (Window.OK == dialog.open()) { + ActivateTaskAction action = new CompoundContextActivationContributionItem.ActivateTaskAction(); + action.setTask(dialog.getTask()); + action.run(); + } + } +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/Activator.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/Activator.java new file mode 100755 index 000000000..b179a33c2 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/Activator.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.List; +import java.util.Set; +import org.eclipse.core.runtime.*; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.datashare.*; +import org.eclipse.ecf.datashare.events.IChannelEvent; +import org.eclipse.ecf.datashare.events.IChannelMessageEvent; +import org.eclipse.ecf.presence.service.IPresenceService; +import org.eclipse.jface.resource.ImageRegistry; +import org.eclipse.mylyn.context.core.ContextCorePlugin; +import org.eclipse.mylyn.internal.context.core.InteractionContext; +import org.eclipse.mylyn.tasks.core.AbstractTask; +import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; +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; + + public void start(final BundleContext context) throws Exception { + super.start(context); + plugin = this; + this.context = context; + context.addServiceListener(this); + } + + 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 { + plugin = null; + context.removeServiceListener(this); + 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); + List tasks = TasksUiPlugin.getTaskListManager().getTaskListWriter().readTasks(file); + final AbstractTask task = (AbstractTask) tasks.get(0); + Set repositories = TasksUiPlugin.getTaskListManager().getTaskListWriter().readRepositories(file); + TasksUiPlugin.getRepositoryManager().insertRepositories(repositories, TasksUiPlugin.getDefault().getRepositoriesFilePath()); + InteractionContext context = ContextCorePlugin.getContextManager().loadContext(task.getHandleIdentifier(), file); + 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; + } +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/CompoundContextActivationContributionItem.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/CompoundContextActivationContributionItem.java new file mode 100755 index 000000000..9a3bb9f15 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/CompoundContextActivationContributionItem.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import java.util.*; +import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.action.*; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.mylyn.context.core.ContextCorePlugin; +import org.eclipse.mylyn.internal.context.core.InteractionContext; +import org.eclipse.mylyn.internal.tasks.ui.ITasksUiConstants; +import org.eclipse.mylyn.tasks.core.AbstractTask; +import org.eclipse.mylyn.tasks.core.TaskList; +import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.actions.CompoundContributionItem; +import org.eclipse.ui.progress.UIJob; + +public class CompoundContextActivationContributionItem extends CompoundContributionItem { + + static LinkedList tasks = new LinkedList(); + + static Map contexts = new HashMap(); + + private static ActivateTaskAction[] actions = new ActivateTaskAction[5]; + + private static Shell shell; + + static { + for (int i = 0; i < 5; i++) { + actions[i] = new ActivateTaskAction(); + } + } + + public void fill(Menu menu, int index) { + super.fill(menu, index); + shell = menu.getShell(); + } + + protected IContributionItem[] getContributionItems() { + int count = 0; + for (Iterator it = tasks.iterator(); it.hasNext() && count != 5;) { + AbstractTask task = (AbstractTask) it.next(); + actions[count].setTask(task); + count++; + } + + IContributionItem[] array = null; + + if (count == 5 && tasks.size() != 5) { + array = new IContributionItem[7]; + array[5] = new Separator(); + array[6] = new ActionContributionItem(new Action("Activate received task...") { + public void run() { + ActivateReceivedContextHandler.open(shell); + } + }); + } else { + array = new IContributionItem[count]; + } + + for (int i = 0; i < count; i++) { + array[i] = new ActionContributionItem(actions[i]); + } + + return array; + } + + static void enqueue(AbstractTask task, InteractionContext context) { + tasks.add(task); + contexts.put(task, context); + } + + static class ActivateTaskAction extends Action { + private AbstractTask task; + + void setTask(AbstractTask task) { + this.task = task; + setText(task.getSummary()); + } + + public void run() { + final InteractionContext context = (InteractionContext) contexts.get(task); + + final TaskList taskList = TasksUiPlugin.getTaskListManager().getTaskList(); + if (taskList.getTask(task.getHandleIdentifier()) != null) { + boolean confirmed = MessageDialog.openConfirm(shell, ITasksUiConstants.TITLE_DIALOG, "The task '" + task.getSummary() + "' already exists. Do you want to override its context with the source?"); + if (confirmed) { + Job job = new Job("Import context") { + protected IStatus run(IProgressMonitor monitor) { + ContextCorePlugin.getContextManager().importContext(context); + scheduleTaskActivationJob(); + return Status.OK_STATUS; + } + }; + job.schedule(); + } else { + return; + } + } else { + Job job = new Job("Import task") { + protected IStatus run(IProgressMonitor monitor) { + ContextCorePlugin.getContextManager().importContext(context); + taskList.insertTask(task, null, null); + scheduleTaskActivationJob(); + return Status.OK_STATUS; + } + }; + job.schedule(); + } + tasks.remove(task); + contexts.remove(task); + } + + private void scheduleTaskActivationJob() { + UIJob job = new UIJob(shell.getDisplay(), "Activate imported task") { + public IStatus runInUIThread(IProgressMonitor monitor) { + TasksUiPlugin.getTaskListManager().activateTask(task); + return Status.OK_STATUS; + } + }; + job.schedule(); + } + } + +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/IncomingSharedTaskNotificationPopup.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/IncomingSharedTaskNotificationPopup.java new file mode 100755 index 000000000..afe8100f9 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/IncomingSharedTaskNotificationPopup.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 University Of British Columbia 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: + * University Of British Columbia - initial API and implementation + * Rob Elves - creator of the original TaskListNotificationPopup class + *******************************************************************************/ + +package org.eclipse.ecf.internal.mylyn.ui; + +import org.eclipse.ecf.internal.mylyn.ui.CompoundContextActivationContributionItem.ActivateTaskAction; +import org.eclipse.jface.dialogs.PopupDialog; +import org.eclipse.mylyn.tasks.core.AbstractTask; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.*; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.forms.events.HyperlinkAdapter; +import org.eclipse.ui.forms.events.HyperlinkEvent; +import org.eclipse.ui.forms.widgets.*; + +class IncomingSharedTaskNotificationPopup extends PopupDialog { + + private FormToolkit toolkit; + + private Form form; + + private Composite sectionClient; + + private AbstractTask task; + + IncomingSharedTaskNotificationPopup(Shell parent) { + super(parent, PopupDialog.INFOPOPUP_SHELLSTYLE | SWT.ON_TOP, false, false, false, false, null, null); + } + + public boolean close() { + toolkit.dispose(); + return super.close(); + } + + void setTask(AbstractTask task) { + this.task = task; + } + + protected Control createContents(Composite parent) { + getShell().setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_GRAY)); + toolkit = new FormToolkit(parent.getDisplay()); + form = toolkit.createForm(parent); + form.getBody().setLayout(new FillLayout()); + + Section section = toolkit.createSection(form.getBody(), ExpandableComposite.TITLE_BAR); + section.setText("Incoming Shared Task"); + section.setLayout(new FillLayout()); + + sectionClient = toolkit.createComposite(section); + sectionClient.setLayout(new GridLayout()); + + Hyperlink link = toolkit.createHyperlink(sectionClient, task.getTaskId(), SWT.NONE); + link.addHyperlinkListener(new HyperlinkAdapter() { + public void linkActivated(HyperlinkEvent e) { + ActivateTaskAction action = new CompoundContextActivationContributionItem.ActivateTaskAction(); + action.setTask(task); + action.run(); + } + }); + + toolkit.createLabel(sectionClient, task.getSummary(), SWT.WRAP); + + section.setClient(sectionClient); + + ImageHyperlink hyperlink = toolkit.createImageHyperlink(section, SWT.NONE); + hyperlink.setBackground(null); + hyperlink.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE)); + hyperlink.addHyperlinkListener(new HyperlinkAdapter() { + public void linkActivated(HyperlinkEvent e) { + close(); + } + }); + + section.setTextClient(hyperlink); + + form.pack(); + return parent; + } + + /** + * Initialize the shell's bounds. + */ + public void initializeBounds() { + getShell().setBounds(restoreBounds()); + } + + private Rectangle restoreBounds() { + Rectangle bounds = form.getBounds(); + Rectangle maxBounds = getShell().getMonitor().getClientArea(); + + if (bounds.width > -1 && bounds.height > -1) { + if (maxBounds != null) { + bounds.width = Math.min(bounds.width, maxBounds.width); + bounds.height = Math.min(bounds.height, maxBounds.height); + } + // Enforce an absolute minimal size + bounds.width = Math.max(bounds.width, 30); + bounds.height = Math.max(bounds.height, 30); + } + + if (bounds.x > -1 && bounds.y > -1 && maxBounds != null) { + if (bounds.width > -1 && bounds.height > -1) { + bounds.x = maxBounds.x + maxBounds.width - bounds.width; + bounds.y = maxBounds.y + maxBounds.height - bounds.height; + } + } + + return bounds; + } +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/NoopECFStart.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/NoopECFStart.java new file mode 100755 index 000000000..47b81e946 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/NoopECFStart.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import org.eclipse.core.runtime.*; +import org.eclipse.ecf.core.start.IECFStart; + +public class NoopECFStart implements IECFStart { + + public IStatus run(IProgressMonitor monitor) { + return Status.OK_STATUS; + } + +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SelectTaskDialog.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SelectTaskDialog.java new file mode 100755 index 000000000..f0ff2629f --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SelectTaskDialog.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.viewers.*; +import org.eclipse.mylyn.tasks.core.AbstractTask; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.*; + +class SelectTaskDialog extends Dialog { + + private ListViewer viewer; + + private AbstractTask task; + + private Object input; + + SelectTaskDialog(Shell shell) { + super(shell); + } + + protected void configureShell(Shell newShell) { + super.configureShell(newShell); + newShell.setText("Task Selection"); + } + + public void create() { + super.create(); + getButton(IDialogConstants.OK_ID).setEnabled(false); + } + + public Control createDialogArea(Composite parent) { + parent = (Composite) super.createDialogArea(parent); + parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + parent.setLayout(new GridLayout(1, true)); + + viewer = new ListViewer(parent); + viewer.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + viewer.setContentProvider(new ArrayContentProvider()); + viewer.setLabelProvider(new LabelProvider() { + public String getText(Object element) { + String summary = ((AbstractTask) element).getSummary(); + if (summary.length() > 30) { + return summary.substring(0, 30) + "..."; //$NON-NLS-1$ + } else { + return summary; + } + } + }); + viewer.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + getButton(IDialogConstants.OK_ID).setEnabled(true); + } + }); + viewer.addOpenListener(new IOpenListener() { + public void open(OpenEvent e) { + okPressed(); + } + }); + viewer.setInput(input); + + return parent; + } + + protected Point getInitialSize() { + Point point = super.getInitialSize(); + return new Point(point.x, 400); + } + + protected void okPressed() { + task = (AbstractTask) ((IStructuredSelection) viewer.getSelection()).getFirstElement(); + super.okPressed(); + } + + void setInput(Object input) { + this.input = input; + } + + AbstractTask getTask() { + return task; + } + +} diff --git a/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SendContextContributionItem.java b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SendContextContributionItem.java new file mode 100755 index 000000000..e6b6bbb62 --- /dev/null +++ b/incubation/bundles/org.eclipse.ecf.mylyn.ui/src/org/eclipse/ecf/internal/mylyn/ui/SendContextContributionItem.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.ecf.internal.mylyn.ui; + +import java.io.*; +import java.net.URLEncoder; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.identity.IDCreateException; +import org.eclipse.ecf.datashare.IChannel; +import org.eclipse.ecf.datashare.IChannelContainerAdapter; +import org.eclipse.ecf.presence.IPresenceContainerAdapter; +import org.eclipse.ecf.presence.roster.IRosterEntry; +import org.eclipse.ecf.presence.ui.menu.AbstractRosterMenuContributionItem; +import org.eclipse.ecf.presence.ui.menu.AbstractRosterMenuHandler; +import org.eclipse.jface.action.IContributionItem; +import org.eclipse.jface.viewers.*; +import org.eclipse.mylyn.internal.tasks.ui.ITasksUiConstants; +import org.eclipse.mylyn.tasks.core.AbstractTask; +import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchSite; +import org.eclipse.ui.handlers.HandlerUtil; + +public class SendContextContributionItem extends AbstractRosterMenuContributionItem { + + public SendContextContributionItem() { + setTopMenuName("Send Context"); + setTopMenuImageDescriptor(Activator.getDefault().getImageRegistry().getDescriptor("IMG_SHARED_TASK")); + } + + private String encodeName(AbstractTask task) { + try { + return URLEncoder.encode(task.getHandleIdentifier(), ITasksUiConstants.FILENAME_ENCODING); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + protected IContributionItem[] createContributionItemsForPresenceContainer(IPresenceContainerAdapter presenceContainerAdapter) { + // if this IPCA doesn't support the datashare APIs, we should not create any contribution items + IChannelContainerAdapter channelAdapter = (IChannelContainerAdapter) presenceContainerAdapter.getAdapter(IChannelContainerAdapter.class); + if (channelAdapter == null) { + return new IContributionItem[0]; + } + return super.createContributionItemsForPresenceContainer(presenceContainerAdapter); + } + + protected AbstractRosterMenuHandler createRosterEntryHandler(final IRosterEntry rosterEntry) { + return new AbstractRosterMenuHandler(rosterEntry) { + public Object execute(ExecutionEvent event) throws ExecutionException { + IWorkbenchPart part = HandlerUtil.getActivePart(event); + if (part == null) { + return null; + } + IWorkbenchSite site = part.getSite(); + if (site == null) { + return null; + } + ISelectionProvider provider = site.getSelectionProvider(); + if (provider == null) { + return null; + } + ISelection selection = provider.getSelection(); + if (selection instanceof IStructuredSelection) { + IChannelContainerAdapter icca = (IChannelContainerAdapter) rosterEntry.getRoster().getPresenceContainerAdapter().getAdapter(IChannelContainerAdapter.class); + ID channelID; + try { + channelID = icca.getChannelNamespace().createInstance(new Object[] {Activator.PLUGIN_ID}); + } catch (IDCreateException e1) { + return null; + } + final IChannel channel = icca.getChannel(channelID); + if (channel == null) { + return null; + } + Object element = ((IStructuredSelection) selection).getFirstElement(); + if (element instanceof AbstractTask) { + final AbstractTask task = (AbstractTask) element; + Job job = new Job("Send Task") { + protected IStatus run(IProgressMonitor monitor) { + monitor.beginTask("Sending task...", 100); + File tmpDir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$ + File outputFile = new File(tmpDir, encodeName(task)); + TasksUiPlugin.getTaskListManager().getTaskListWriter().writeTask(task, outputFile); + monitor.worked(30); + FileInputStream stream = null; + try { + byte[] data = new byte[(int) outputFile.length()]; + stream = new FileInputStream(outputFile); + stream.read(data); + monitor.worked(30); + channel.sendMessage(getRosterEntry().getUser().getID(), data); + monitor.worked(40); + } catch (Exception e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "An error occurred while sending the task.", e); + } finally { + try { + if (stream != null) { + stream.close(); + } + } catch (Exception e) { + // ignored + } + outputFile.delete(); + monitor.done(); + } + return Status.OK_STATUS; + } + }; + job.schedule(); + } + } + return null; + } + + }; + } +} |