Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f3445ebc8a86383b76dc0d3337896cc68cacf79 (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
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.ui.action;

import java.util.List;

import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.bugzilla.ui.editor.BugzillaTaskEditorPage;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.BaseSelectionListenerAction;
import org.eclipse.ui.forms.editor.IFormPage;

/**
 * @author Frank Becker
 */
public class BugzillaUpdateAttachmentAction extends BaseSelectionListenerAction implements IViewActionDelegate {

	private ISelection currentSelection;

	private final boolean obsolete;

	public BugzillaUpdateAttachmentAction(boolean obsolete) {
		super("UpdateAttachmentAction"); //$NON-NLS-1$
		this.obsolete = obsolete;
	}

	public void init(IViewPart view) {
		// ignore
	}

	@SuppressWarnings("unchecked")
	public void run(IAction action) {
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IEditorPart activeEditor = page.getActiveEditor();
		if (activeEditor instanceof TaskEditor) {
			final TaskEditor taskEditor = (TaskEditor) activeEditor;
			IStructuredSelection selection = null;
			if (currentSelection instanceof IStructuredSelection) {
				selection = (IStructuredSelection) currentSelection;
			}
			if (selection == null || selection.isEmpty()) {
				return;
			}
			List<ITaskAttachment> attachment = selection.toList();
			if (attachment != null) {
				final UpdateAttachmentJob job = new UpdateAttachmentJob(attachment, taskEditor, obsolete);
				job.setUser(true);
				job.addJobChangeListener(new JobChangeAdapter() {

					@Override
					public void done(IJobChangeEvent event) {
						if (job.getError() != null) {
							IFormPage formPage = taskEditor.getActivePageInstance();
							if (formPage instanceof BugzillaTaskEditorPage) {
								final BugzillaTaskEditorPage bugzillaPage = (BugzillaTaskEditorPage) formPage;
								PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
									public void run() {
										bugzillaPage.getTaskEditor().setMessage(job.getError().getMessage(),
												IMessageProvider.ERROR);
									}
								});
							}
						}
					}
				});
				job.schedule();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void selectionChanged(IAction action, ISelection selection) {
		this.currentSelection = selection;
		IStructuredSelection sructuredSelection = null;
		if (selection instanceof IStructuredSelection) {
			sructuredSelection = (IStructuredSelection) currentSelection;
		}
		if (sructuredSelection == null || sructuredSelection.isEmpty()) {
			return;
		}
		List<ITaskAttachment> attachmentList = sructuredSelection.toList();
		action.setEnabled(false);
		for (ITaskAttachment taskAttachment : attachmentList) {
			TaskAttribute taskAttribute = taskAttachment.getTaskAttribute();
			TaskAttribute deprecated = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_IS_DEPRECATED);
			if (deprecated != null && deprecated.getValue().equals("1") != obsolete) { //$NON-NLS-1$
				action.setEnabled(true);
				break;
			}
		}
	}
}

Back to the top