Skip to main content
summaryrefslogtreecommitdiffstats
blob: 98e7532612e89cc475344e016af3c873cb01067b (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
/*******************************************************************************
 * Copyright (c) 2010 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylyn.internal.bugzilla.ui.action.ChangeAttachmentJob;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.ui.editors.AttributeEditorFactory;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.swt.widgets.Shell;

public class BugzillaAttachmentWizard extends Wizard {

	private final AttributeEditorFactory factory;

	private final TaskAttribute attachmentAttribute;

	private BugzillaAttachmentWizardPage attachmentWizardPage;

	private final Shell parentShell;

	private boolean changed = false;

	private final TaskEditor taskEditor;

	private final ITaskAttachment attachment;

	public BugzillaAttachmentWizard(Shell parentShell, AttributeEditorFactory factory,
			TaskAttribute attachmentAttribute, TaskEditor taskEditor, ITaskAttachment attachment) {
		super();
		this.factory = factory;
		this.attachmentAttribute = attachmentAttribute;
		this.parentShell = parentShell;
		this.taskEditor = taskEditor;
		this.attachment = attachment;
		setNeedsProgressMonitor(true);
		setWindowTitle(Messages.BugzillaAttachmentWizard_Attachment_Details_Dialog_Title);
	}

	@Override
	public boolean performFinish() {
		TaskAttribute attachmentTaskAttribute = attachment.getTaskAttribute();
		for (TaskAttribute child : attachmentAttribute.getAttributes().values()) {
			attachmentTaskAttribute.deepAddCopy(child);
		}

		final ChangeAttachmentJob job = new ChangeAttachmentJob(attachment, taskEditor);
		job.setUser(true);
		if (attachmentWizardPage.runInBackground()) {
			runInBackground(job);
		} else {
			runInWizard(job);
		}
		return true;
	}

/* currently not needed
	private void handleDone(final ChangeAttachmentJob job, IProgressMonitor monitor) {
		try {
			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);
						}
					});
				}
			} else {
				monitor.setTaskName(Messages.BugzillaAttachmentWizard_Now_synchronize_the_Task);
				IFormPage formPage = taskEditor.getActivePageInstance();
				if (formPage instanceof BugzillaTaskEditorPage) {
					final BugzillaTaskEditorPage bugzillaPage = (BugzillaTaskEditorPage) formPage;
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							bugzillaPage.refreshFormContent();
						}
					});
				}
			}
		} finally {
			monitor.done();
		}
	}
*/

	private boolean runInWizard(final ChangeAttachmentJob job) {
		try {
			getContainer().run(true, true, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					job.run(monitor);
				}
			});
			return true;
		} catch (InvocationTargetException e) {
			StatusHandler.fail(new Status(IStatus.ERROR, BugzillaUiPlugin.ID_PLUGIN, "Unexpected error", e)); //$NON-NLS-1$
			return false;
		} catch (InterruptedException e) {
			// canceled
			return false;
		}
	}

	private void runInBackground(final ChangeAttachmentJob job) {
		job.schedule();
	}

	@Override
	public void addPages() {
		attachmentWizardPage = new BugzillaAttachmentWizardPage(parentShell, factory, attachmentAttribute,
				attachment.getTask().getTaskId());
		addPage(attachmentWizardPage);
	}

	@Override
	public boolean canFinish() {
		return isChanged();
	}

	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}

}

Back to the top