Skip to main content
summaryrefslogtreecommitdiffstats
blob: 579785910330a7d877ca30c00c98cc561291969b (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2009 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.action;

import java.util.List;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylyn.internal.bugzilla.ui.editor.BugzillaTaskEditorPage;
import org.eclipse.mylyn.internal.bugzilla.ui.editor.FlagAttributeEditor;
import org.eclipse.mylyn.internal.bugzilla.ui.wizard.BugzillaAttachmentWizard;
import org.eclipse.mylyn.internal.tasks.core.TaskTask;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.wizards.NewAttachmentWizardDialog;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataModel;
import org.eclipse.mylyn.tasks.core.data.TaskDataModelEvent;
import org.eclipse.mylyn.tasks.core.data.TaskDataModelListener;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.AttributeEditorFactory;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
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
 */
@SuppressWarnings("restriction")
public class BugzillaAttachmentUpdateAction extends BaseSelectionListenerAction implements IViewActionDelegate {

	private ISelection currentSelection;

	public BugzillaAttachmentUpdateAction() {
		super("BugzillaAttachmentDetailAction"); //$NON-NLS-1$
	}

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

	public void run(IAction action) {
		IStructuredSelection selection = null;
		if (currentSelection instanceof IStructuredSelection) {
			selection = (IStructuredSelection) currentSelection;
		}
		if (selection == null || selection.isEmpty() || selection.size() != 1) {
			return;
		}
		ITaskAttachment attachment = (ITaskAttachment) selection.getFirstElement();
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		IWorkbenchPage page = window.getActivePage();
		IEditorPart activeEditor = page.getActiveEditor();
		IWorkbenchPartSite site = activeEditor.getSite();
		Shell shell = site.getShell();
		if (activeEditor instanceof TaskEditor) {
			final TaskEditor taskEditor = (TaskEditor) activeEditor;
			IFormPage taskEditorPage = taskEditor.findPage("id"); //$NON-NLS-1$
			if (taskEditorPage instanceof BugzillaTaskEditorPage) {
				BugzillaTaskEditorPage bugzillaTaskEditorPage = (BugzillaTaskEditorPage) taskEditorPage;

				ITask attachmentTask = attachment.getTask();
				ITask nTask = new TaskTask(attachmentTask.getConnectorKind(), attachmentTask.getRepositoryUrl(),
						attachmentTask.getTaskId() + "attachment"); //$NON-NLS-1$

				TaskData editTaskData = new TaskData(attachment.getTaskAttribute().getTaskData().getAttributeMapper(),
						attachment.getTaskAttribute().getTaskData().getConnectorKind(), attachment.getTaskAttribute()
								.getTaskData()
								.getRepositoryUrl(), attachment.getTaskAttribute().getTaskData().getTaskId());
				editTaskData.setVersion(attachment.getTaskAttribute().getTaskData().getVersion());
				TaskAttribute target0 = editTaskData.getRoot();
				TaskAttribute temp = attachment.getTaskAttribute();
				target0.setValues(temp.getValues());
				for (TaskAttribute child : temp.getAttributes().values()) {
					target0.deepAddCopy(child);
				}

				TaskAttribute comment = target0.createAttribute("comment"); //$NON-NLS-1$
				TaskAttributeMetaData commentMeta = comment.getMetaData();
				commentMeta.setType(TaskAttribute.TYPE_LONG_RICH_TEXT);
				commentMeta.setLabel(Messages.BugzillaAttachmentUpdateAction_Comment);

				ITaskDataWorkingCopy workingCopy = TasksUi.getTaskDataManager().createWorkingCopy(nTask, editTaskData);
				TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(
						attachment.getTaskAttribute().getTaskData().getRepositoryUrl());
				final TaskDataModel model = new TaskDataModel(repository, nTask, workingCopy);
				AttributeEditorFactory factory = new AttributeEditorFactory(model, repository,
						bugzillaTaskEditorPage.getEditorSite()) {
					@Override
					public AbstractAttributeEditor createEditor(String type, final TaskAttribute taskAttribute) {
						AbstractAttributeEditor editor;
						if (IBugzillaConstants.EDITOR_TYPE_FLAG.equals(type)) {
							editor = new FlagAttributeEditor(model, taskAttribute, 350);
						} else {
							editor = super.createEditor(type, taskAttribute);
							if (TaskAttribute.TYPE_BOOLEAN.equals(type)) {
								editor.setDecorationEnabled(false);
							}
						}
						return editor;
					}
				};

				TaskAttribute target = workingCopy.getLocalData().getRoot();
				target.setValue(target0.getValue());

				final BugzillaAttachmentWizard attachmentWizard = new BugzillaAttachmentWizard(shell, factory, target,
						taskEditor, attachment);
				final NewAttachmentWizardDialog dialog = new NewAttachmentWizardDialog(shell, attachmentWizard, false);
				model.addModelListener(new TaskDataModelListener() {

					@Override
					public void attributeChanged(TaskDataModelEvent event) {
						attachmentWizard.setChanged(true);
						dialog.updateButtons();
					}
				});

				dialog.setBlockOnOpen(false);
				dialog.create();
				dialog.open();
			}
		}
	}

	@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();
		if (attachmentList != null && attachmentList.size() == 1) {
			action.setEnabled(true);
		} else {
			action.setEnabled(false);
		}
	}
}

Back to the top