Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0619b813bd8956922800582b605ef20def805fce (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) 2004, 2011 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
 *     Tasktop Technologies - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.tasks.core.ITaskComment;
import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.TaskMapping;
import org.eclipse.mylyn.tasks.ui.TasksUiImages;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * @author Frank Becker
 * @author Steffen Pingel
 */
public class NewTaskFromSelectionAction extends Action {

	public static final String ID = "org.eclipse.mylyn.tasks.ui.actions.newTaskFromSelection"; //$NON-NLS-1$

	private ITaskMapping taskMapping;

	public NewTaskFromSelectionAction() {
		super(Messages.NewTaskFromSelectionAction_New_Task_from_Selection);
		setId(ID);
		setImageDescriptor(TasksUiImages.TASK_NEW);
	}

	public ITaskMapping getTaskMapping() {
		return taskMapping;
	}

	public void run(IAction action) {
		run();
	}

	@Override
	public void run() {
		if (taskMapping == null) {
			MessageDialog.openError(null, Messages.NewTaskFromSelectionAction_New_Task_from_Selection,
					Messages.NewTaskFromSelectionAction_Nothing_selected_to_create_task_from);
			return;
		}

		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		TasksUiUtil.openNewTaskEditor(shell, taskMapping, null);
	}

	public void selectionChanged(ISelection selection) {
		if (selection instanceof TextSelection) {
			TextSelection textSelection = (TextSelection) selection;
			final String text = textSelection.getText();
			if (text != null && text.length() > 0) {
				taskMapping = new TaskMapping() {
					@Override
					public String getDescription() {
						return text;
					}
				};
			} else {
				taskMapping = null;
			}
//		} else if (selection instanceof RepositoryTaskSelection) {
//			RepositoryTaskSelection repositoryTaskSelection = (RepositoryTaskSelection) selection;
//			IRepositoryManager repositoryManager = TasksUi.getRepositoryManager();
//			AbstractRepositoryConnector connector = repositoryManager.getRepositoryConnector(repositoryTaskSelection.getRepositoryKind());
//
//			TaskComment comment = repositoryTaskSelection.getComment();
//			if (comment != null) {
//				StringBuilder sb = new StringBuilder();
//				sb.append("\n-- Created from Comment --");
//				if (connector != null) {
//					sb.append("\nURL: ");
//					sb.append(connector.getTaskUrl(repositoryTaskSelection.getRepositoryUrl(),
//							repositoryTaskSelection.getId()));
//				}
//				sb.append("\nComment: ");
//				sb.append(comment.getNumber());
//
//				sb.append("\n\n");
//				if (taskSelection != null) {
//					// if text was selected, prefer that 
//					sb.append(taskSelection.getLegacyTaskData().getDescription());
//				} else {
//					sb.append(comment.getText());
//				}
//
//				taskSelection = new TaskSelection("", sb.toString());
//			} else if (taskSelection != null) {
//				StringBuilder sb = new StringBuilder();
//				if (connector != null) {
//					sb.append("\n-- Created from Task --");
//					sb.append("\nURL: ");
//					sb.append(connector.getTaskUrl(repositoryTaskSelection.getRepositoryUrl(),
//							repositoryTaskSelection.getId()));
//				}
//
//				sb.append("\n\n");
//				sb.append(taskSelection.getLegacyTaskData().getDescription());
//
//				taskSelection = new TaskSelection("", sb.toString());
//			}
		} else if (selection instanceof StructuredSelection) {
			Object element = ((StructuredSelection) selection).getFirstElement();
			if (element instanceof ITaskComment) {
				ITaskComment comment = (ITaskComment) element;
				final StringBuilder sb = new StringBuilder();
				sb.append("\n" + Messages.NewTaskFromSelectionAction____Created_from_Comment___); //$NON-NLS-1$
				if (comment.getUrl() == null) {
					sb.append("\n" + Messages.NewTaskFromSelectionAction_URL_); //$NON-NLS-1$
					sb.append(comment.getTask().getUrl());
					sb.append("\n" + Messages.NewTaskFromSelectionAction_Comment_); //$NON-NLS-1$
					sb.append(comment.getNumber());
				} else {
					sb.append("\n" + Messages.NewTaskFromSelectionAction_URL_); //$NON-NLS-1$
					sb.append(comment.getUrl());
				}

				sb.append("\n\n"); //$NON-NLS-1$
				sb.append(comment.getText());
				taskMapping = new TaskMapping() {
					@Override
					public String getDescription() {
						return sb.toString();
					}
				};
			}
		}
		setEnabled(taskMapping != null);
	}

}

Back to the top