View | Details | Raw Unified | Return to bug 220314 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/tasks/ui/messages.properties (+6 lines)
Lines 19-24 Link Here
19
http://bugs.eclipse.org/bugs/enter_bug.cgi?product=Mylyn\n\
19
http://bugs.eclipse.org/bugs/enter_bug.cgi?product=Mylyn\n\
20
\n\
20
\n\
21
Or via the popup menu in the Error Log view (see Window -> Show View)
21
Or via the popup menu in the Error Log view (see Window -> Show View)
22
DownloadAndOpenTaskAttachmentJob_cannotOpenEditor=Cannot open editor
23
DownloadAndOpenTaskAttachmentJob_editorTooltip=Attachment from {0}, {1}
24
DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment=Failed to download attachment
22
25
23
MoveToCategoryMenuContributor_Move_to=Move to
26
MoveToCategoryMenuContributor_Move_to=Move to
24
27
Lines 37-42 Link Here
37
ScheduleTaskMenuContributor_Not_Scheduled=Not Scheduled
40
ScheduleTaskMenuContributor_Not_Scheduled=Not Scheduled
38
ScheduleTaskMenuContributor_Schedule_for=Schedule for
41
ScheduleTaskMenuContributor_Schedule_for=Schedule for
39
42
43
TaskAttachmentEditorViewer_openingAttachment=Opening attachment {0}
44
TaskAttachmentViewerBrowser_browser=Browser
40
TaskHistoryDropDown_Activate_Task_=Activate Task...
45
TaskHistoryDropDown_Activate_Task_=Activate Task...
41
TaskHistoryDropDown_Deactivate_Task=Deactivate Task
46
TaskHistoryDropDown_Deactivate_Task=Deactivate Task
42
47
Lines 82-84 Link Here
82
TaskHyperlink_Open_Task_X_in_X=Open Task {0} in {1}
87
TaskHyperlink_Open_Task_X_in_X=Open Task {0} in {1}
83
88
84
AbstractRetrieveTitleFromUrlJob_Retrieving_summary_from_URL=Retrieving summary from URL
89
AbstractRetrieveTitleFromUrlJob_Retrieving_summary_from_URL=Retrieving summary from URL
90
FileStorage_unableToReadAttachmentFile=Unable to read attachment file
(-)src/org/eclipse/mylyn/internal/tasks/ui/ITasksUiPreferenceConstants.java (+2 lines)
Lines 83-86 Link Here
83
	public static final String PREF_DATA_DIR = "org.eclipse.mylyn.data.dir"; //$NON-NLS-1$
83
	public static final String PREF_DATA_DIR = "org.eclipse.mylyn.data.dir"; //$NON-NLS-1$
84
84
85
	public static final String DEFAULT_ATTACHMENTS_DIRECTORY = "org.eclipse.mylyn.tasks.ui.attachments.defaultDirectory"; //$NON-NLS-1$
85
	public static final String DEFAULT_ATTACHMENTS_DIRECTORY = "org.eclipse.mylyn.tasks.ui.attachments.defaultDirectory"; //$NON-NLS-1$
86
87
	public static final String PREFERRED_TASK_ATTACHMENT_VIEWER_ID = "org.eclipse.mylyn.tasks.ui.attachments.preferredViewerID"; //$NON-NLS-1$
86
}
88
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/Messages.java (+12 lines)
Lines 35-40 Link Here
35
35
36
	public static String DialogErrorReporter_Please_report_the_following_error_at;
36
	public static String DialogErrorReporter_Please_report_the_following_error_at;
37
37
38
	public static String DownloadAndOpenTaskAttachmentJob_cannotOpenEditor;
39
40
	public static String DownloadAndOpenTaskAttachmentJob_editorTooltip;
41
42
	public static String DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment;
43
38
	public static String MoveToCategoryMenuContributor_Move_to;
44
	public static String MoveToCategoryMenuContributor_Move_to;
39
45
40
	public static String OpenRepositoryTaskJob_Could_not_find_repository_configuration_for_X;
46
	public static String OpenRepositoryTaskJob_Could_not_find_repository_configuration_for_X;
Lines 61-66 Link Here
61
67
62
	public static String ScheduleTaskMenuContributor_Schedule_for;
68
	public static String ScheduleTaskMenuContributor_Schedule_for;
63
69
70
	public static String TaskAttachmentEditorViewer_openingAttachment;
71
72
	public static String TaskAttachmentViewerBrowser_browser;
73
64
	public static String TaskHistoryDropDown_Activate_Task_;
74
	public static String TaskHistoryDropDown_Activate_Task_;
65
75
66
	public static String TaskHistoryDropDown_Deactivate_Task;
76
	public static String TaskHistoryDropDown_Deactivate_Task;
Lines 127-130 Link Here
127
137
128
	public static String AbstractRetrieveTitleFromUrlJob_Retrieving_summary_from_URL;
138
	public static String AbstractRetrieveTitleFromUrlJob_Retrieving_summary_from_URL;
129
139
140
	public static String FileStorage_unableToReadAttachmentFile;
141
130
}
142
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/actions/SaveAttachmentsAction.java (-32 / +2 lines)
Lines 35-50 Link Here
35
 */
35
 */
36
public class SaveAttachmentsAction extends Action {
36
public class SaveAttachmentsAction extends Action {
37
37
38
	private static final String ATTACHMENT_DEFAULT_NAME = "attachment"; //$NON-NLS-1$
39
40
	private static final String CTYPE_ZIP = "zip"; //$NON-NLS-1$
41
42
	private static final String CTYPE_OCTET_STREAM = "octet-stream"; //$NON-NLS-1$
43
44
	private static final String CTYPE_TEXT = "text"; //$NON-NLS-1$
45
46
	private static final String CTYPE_HTML = "html"; //$NON-NLS-1$
47
48
	public SaveAttachmentsAction(String text) {
38
	public SaveAttachmentsAction(String text) {
49
		super(text);
39
		super(text);
50
	}
40
	}
Lines 66-72 Link Here
66
	 */
56
	 */
67
	private void saveSingleAttachment(ITaskAttachment attachment) {
57
	private void saveSingleAttachment(ITaskAttachment attachment) {
68
		FileDialog fileChooser = new FileDialog(WorkbenchUtil.getShell(), SWT.SAVE);
58
		FileDialog fileChooser = new FileDialog(WorkbenchUtil.getShell(), SWT.SAVE);
69
		fileChooser.setFileName(getAttachmentFilename(attachment));
59
		fileChooser.setFileName(AttachmentUtil.getAttachmentFilename(attachment));
70
60
71
		File initDirectory = getInitialDirectory();
61
		File initDirectory = getInitialDirectory();
72
		if (initDirectory != null) {
62
		if (initDirectory != null) {
Lines 123-129 Link Here
123
		}
113
		}
124
114
125
		for (ITaskAttachment attachment : attachments) {
115
		for (ITaskAttachment attachment : attachments) {
126
			String filename = getAttachmentFilename(attachment);
116
			String filename = AttachmentUtil.getAttachmentFilename(attachment);
127
			File file = getTargetFile(WorkbenchUtil.getShell(), directory, filename);
117
			File file = getTargetFile(WorkbenchUtil.getShell(), directory, filename);
128
			if (file != null) {
118
			if (file != null) {
129
				DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file);
119
				DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file);
Lines 160-185 Link Here
160
		}
150
		}
161
	}
151
	}
162
152
163
	private String getAttachmentFilename(ITaskAttachment attachment) {
164
		String fname = attachment.getFileName();
165
		// default name if none is found
166
		if (fname.equals("")) { //$NON-NLS-1$
167
			String ctype = attachment.getContentType();
168
			if (ctype.endsWith(CTYPE_HTML)) {
169
				fname = ATTACHMENT_DEFAULT_NAME + ".html"; //$NON-NLS-1$
170
			} else if (ctype.startsWith(CTYPE_TEXT)) {
171
				fname = ATTACHMENT_DEFAULT_NAME + ".txt"; //$NON-NLS-1$
172
			} else if (ctype.endsWith(CTYPE_OCTET_STREAM)) {
173
				fname = ATTACHMENT_DEFAULT_NAME;
174
			} else if (ctype.endsWith(CTYPE_ZIP)) {
175
				fname = ATTACHMENT_DEFAULT_NAME + "." + CTYPE_ZIP; //$NON-NLS-1$
176
			} else {
177
				fname = ATTACHMENT_DEFAULT_NAME + "." + ctype.substring(ctype.indexOf("/") + 1); //$NON-NLS-1$ //$NON-NLS-2$
178
			}
179
		}
180
		return fname;
181
	}
182
183
	private File getInitialDirectory() {
153
	private File getInitialDirectory() {
184
		String dirName = TasksUiPlugin.getDefault().getPreferenceStore().getString(
154
		String dirName = TasksUiPlugin.getDefault().getPreferenceStore().getString(
185
				ITasksUiPreferenceConstants.DEFAULT_ATTACHMENTS_DIRECTORY);
155
				ITasksUiPreferenceConstants.DEFAULT_ATTACHMENTS_DIRECTORY);
(-)src/org/eclipse/mylyn/internal/tasks/ui/util/AttachmentUtil.java (+32 lines)
Lines 70-75 Link Here
70
70
71
	private static final String CONTEXT_CONTENT_TYPE = "application/octet-stream"; //$NON-NLS-1$
71
	private static final String CONTEXT_CONTENT_TYPE = "application/octet-stream"; //$NON-NLS-1$
72
72
73
	private static final String ATTACHMENT_DEFAULT_NAME = "attachment"; //$NON-NLS-1$
74
75
	private static final String CTYPE_ZIP = "zip"; //$NON-NLS-1$
76
77
	private static final String CTYPE_OCTET_STREAM = "octet-stream"; //$NON-NLS-1$
78
79
	private static final String CTYPE_TEXT = "text"; //$NON-NLS-1$
80
81
	private static final String CTYPE_HTML = "html"; //$NON-NLS-1$
82
73
	public static boolean postContext(AbstractRepositoryConnector connector, TaskRepository repository, ITask task,
83
	public static boolean postContext(AbstractRepositoryConnector connector, TaskRepository repository, ITask task,
74
			String comment, TaskAttribute attribute, IProgressMonitor monitor) throws CoreException {
84
			String comment, TaskAttribute attribute, IProgressMonitor monitor) throws CoreException {
75
		AbstractTaskAttachmentHandler attachmentHandler = connector.getTaskAttachmentHandler();
85
		AbstractTaskAttachmentHandler attachmentHandler = connector.getTaskAttachmentHandler();
Lines 307-310 Link Here
307
		}
317
		}
308
		return Collections.emptyList();
318
		return Collections.emptyList();
309
	}
319
	}
320
321
	public static String getAttachmentFilename(ITaskAttachment attachment) {
322
		String name = attachment.getFileName();
323
		// if no filename is set, make one up with the proper extension so
324
		// we can support opening in that filetype's default editor
325
		if (name == null || "".equals(name)) { //$NON-NLS-1$
326
			String ctype = attachment.getContentType();
327
			if (ctype.endsWith(CTYPE_HTML)) {
328
				name = ATTACHMENT_DEFAULT_NAME + ".html"; //$NON-NLS-1$
329
			} else if (ctype.startsWith(CTYPE_TEXT)) {
330
				name = ATTACHMENT_DEFAULT_NAME + ".txt"; //$NON-NLS-1$
331
			} else if (ctype.endsWith(CTYPE_OCTET_STREAM)) {
332
				name = ATTACHMENT_DEFAULT_NAME;
333
			} else if (ctype.endsWith(CTYPE_ZIP)) {
334
				name = ATTACHMENT_DEFAULT_NAME + "." + CTYPE_ZIP; //$NON-NLS-1$
335
			} else {
336
				name = ATTACHMENT_DEFAULT_NAME + "." + ctype.substring(ctype.indexOf("/") + 1); //$NON-NLS-1$ //$NON-NLS-2$
337
			}
338
		}
339
340
		return name;
341
	}
310
}
342
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/commands/Messages.java (+4 lines)
Lines 29-34 Link Here
29
29
30
	public static String NewLocalTaskHandler_Could_not_create_local_task;
30
	public static String NewLocalTaskHandler_Could_not_create_local_task;
31
31
32
	public static String OpenTaskAttachmentHandler_failedToOpenViewer;
33
34
	public static String OpenTaskAttachmentHandler_noAttachmentViewerFound;
35
32
	public static String OpenTaskAttachmentInDefaultEditorHandler_Failed_to_open_editor;
36
	public static String OpenTaskAttachmentInDefaultEditorHandler_Failed_to_open_editor;
33
37
34
	public static String OpenTaskAttachmentInDefaultEditorHandler_No_default_editor_for_X_found;
38
	public static String OpenTaskAttachmentInDefaultEditorHandler_No_default_editor_for_X_found;
(-)src/org/eclipse/mylyn/internal/tasks/ui/commands/messages.properties (+2 lines)
Lines 12-17 Link Here
12
12
13
NewLocalTaskHandler_Could_not_create_local_task=Could not create local task
13
NewLocalTaskHandler_Could_not_create_local_task=Could not create local task
14
14
15
OpenTaskAttachmentHandler_failedToOpenViewer=Failed to open attachment viewer
16
OpenTaskAttachmentHandler_noAttachmentViewerFound=No suitable attachment viewer found
15
OpenTaskAttachmentInDefaultEditorHandler_Failed_to_open_editor=Failed to open editor
17
OpenTaskAttachmentInDefaultEditorHandler_Failed_to_open_editor=Failed to open editor
16
OpenTaskAttachmentInDefaultEditorHandler_No_default_editor_for_X_found=No default editor for "{0}" found
18
OpenTaskAttachmentInDefaultEditorHandler_No_default_editor_for_X_found=No default editor for "{0}" found
17
OpenTaskAttachmentInDefaultEditorHandler_Open_Attachment_Failed=Open Attachment Failed
19
OpenTaskAttachmentInDefaultEditorHandler_Open_Attachment_Failed=Open Attachment Failed
(-)src/org/eclipse/mylyn/internal/tasks/ui/commands/OpenTaskAttachmentInDefaultEditorHandler.java (-78 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui.commands;
13
14
import java.text.MessageFormat;
15
import java.util.List;
16
17
import org.eclipse.core.commands.AbstractHandler;
18
import org.eclipse.core.commands.ExecutionEvent;
19
import org.eclipse.core.commands.ExecutionException;
20
import org.eclipse.core.runtime.IStatus;
21
import org.eclipse.core.runtime.Status;
22
import org.eclipse.jface.viewers.ISelection;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
25
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskAttachmentEditorInput;
26
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
27
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
28
import org.eclipse.ui.IEditorDescriptor;
29
import org.eclipse.ui.IWorkbenchPage;
30
import org.eclipse.ui.IWorkbenchWindow;
31
import org.eclipse.ui.PartInitException;
32
import org.eclipse.ui.PlatformUI;
33
import org.eclipse.ui.handlers.HandlerUtil;
34
35
/**
36
 * @author Steffen Pingel
37
 */
38
public class OpenTaskAttachmentInDefaultEditorHandler extends AbstractHandler {
39
40
	public Object execute(ExecutionEvent event) throws ExecutionException {
41
		IWorkbenchPage page = null;
42
		ISelection selection = HandlerUtil.getCurrentSelection(event);
43
		if (selection instanceof IStructuredSelection) {
44
			List<?> items = ((IStructuredSelection) selection).toList();
45
			for (Object item : items) {
46
				if (item instanceof ITaskAttachment) {
47
					if (page == null) {
48
						IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
49
						page = window.getActivePage();
50
						if (page == null) {
51
							throw new ExecutionException("No active workbench page"); //$NON-NLS-1$
52
						}
53
					}
54
					openAttachment(page, (ITaskAttachment) item);
55
				}
56
			}
57
		}
58
		return null;
59
	}
60
61
	private void openAttachment(IWorkbenchPage page, ITaskAttachment attachment) throws ExecutionException {
62
		TaskAttachmentEditorInput input = new TaskAttachmentEditorInput(attachment);
63
		IEditorDescriptor description = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(input.getName());
64
		if (description == null) {
65
			TasksUiInternal.displayStatus(Messages.OpenTaskAttachmentInDefaultEditorHandler_Open_Attachment_Failed,
66
					new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN, MessageFormat.format(
67
							Messages.OpenTaskAttachmentInDefaultEditorHandler_No_default_editor_for_X_found,
68
							input.getName())));
69
		} else {
70
			try {
71
				page.openEditor(input, description.getId());
72
			} catch (PartInitException e) {
73
				throw new ExecutionException(Messages.OpenTaskAttachmentInDefaultEditorHandler_Failed_to_open_editor, e);
74
			}
75
		}
76
	}
77
78
}
(-)plugin.xml (-22 / +20 lines)
Lines 1281-1288 Link Here
1281
      </command>
1281
      </command>
1282
      <command
1282
      <command
1283
            categoryId="org.eclipse.mylyn.tasks.ui.category.editor"
1283
            categoryId="org.eclipse.mylyn.tasks.ui.category.editor"
1284
            id="org.eclipse.mylyn.tasks.ui.command.attachment.openInBrowser"
1284
            id="org.eclipse.mylyn.tasks.ui.command.attachment.open"
1285
            name="%command.attachment.openInBrowser.name">
1285
            name="%command.attachment.open.name">
1286
      </command>
1286
      </command>
1287
      <category
1287
      <category
1288
            id="org.eclipse.mylyn.tasks.ui.category.editor"
1288
            id="org.eclipse.mylyn.tasks.ui.category.editor"
Lines 1290-1300 Link Here
1290
      </category>
1290
      </category>
1291
      <command
1291
      <command
1292
            categoryId="org.eclipse.mylyn.tasks.ui.category.editor"
1292
            categoryId="org.eclipse.mylyn.tasks.ui.category.editor"
1293
            id="org.eclipse.mylyn.tasks.ui.command.attachment.openInDefaultEditor"
1294
            name="%command.attachment.openInDefaultEditor.name">
1295
      </command>
1296
      <command
1297
            categoryId="org.eclipse.mylyn.tasks.ui.category.editor"
1298
            defaultHandler="org.eclipse.mylyn.internal.tasks.ui.commands.SubmitTaskHandler"
1293
            defaultHandler="org.eclipse.mylyn.internal.tasks.ui.commands.SubmitTaskHandler"
1299
            description="%command.submitTask.description"
1294
            description="%command.submitTask.description"
1300
            id="org.eclipse.mylyn.tasks.ui.command.submitTask"
1295
            id="org.eclipse.mylyn.tasks.ui.command.submitTask"
Lines 1530-1541 Link Here
1530
         </enabledWhen>
1525
         </enabledWhen>
1531
      </handler>
1526
      </handler>
1532
      <handler
1527
      <handler
1533
            class="org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentInBrowserHandler"
1528
            class="org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentHandler"
1534
            commandId="org.eclipse.mylyn.tasks.ui.command.attachment.openInBrowser">
1529
            commandId="org.eclipse.mylyn.tasks.ui.command.attachment.open">
1535
      </handler>
1536
      <handler
1537
            class="org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentInDefaultEditorHandler"
1538
            commandId="org.eclipse.mylyn.tasks.ui.command.attachment.openInDefaultEditor">
1539
      </handler>
1530
      </handler>
1540
      <handler
1531
      <handler
1541
            class="org.eclipse.mylyn.internal.tasks.ui.commands.MarkTaskHandler$MarkTaskCompleteHandler"
1532
            class="org.eclipse.mylyn.internal.tasks.ui.commands.MarkTaskHandler$MarkTaskCompleteHandler"
Lines 1638-1657 Link Here
1638
      <menuContribution
1629
      <menuContribution
1639
            locationURI="popup:org.eclipse.mylyn.tasks.ui.editor.menu.attachments?after=group.open">
1630
            locationURI="popup:org.eclipse.mylyn.tasks.ui.editor.menu.attachments?after=group.open">
1640
         <command
1631
         <command
1641
               commandId="org.eclipse.mylyn.tasks.ui.command.attachment.openInBrowser"
1632
               commandId="org.eclipse.mylyn.tasks.ui.command.attachment.open"
1642
               label="%command.attachment.openInBrowser.label"
1633
               label="%command.attachment.open.label"
1643
               mnemonic="O"
1634
               mnemonic="O"
1644
               style="push">
1635
               style="push">
1645
         </command>
1636
         </command>
1646
         <menu
1637
         <menu
1647
               label="%command.attachment.openInDefaultEditor.menu.label"
1638
               label="%command.attachment.openWith.menu.label"
1648
               mnemonic="H">
1639
               mnemonic="H">
1649
            <command
1640
            <dynamic
1650
                  commandId="org.eclipse.mylyn.tasks.ui.command.attachment.openInDefaultEditor"
1641
                  class="org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentWithMenu"
1651
                  label="%command.attachment.openInDefaultEditor.label"
1642
                  id="org.eclipse.mylyn.tasks.ui.command.attachment.openTaskAttachmentWithMenu">
1652
                  mnemonic="D"
1643
                  <visibleWhen
1653
                  style="push">
1644
                        checkEnabled="false">
1654
            </command>
1645
                     <with
1646
                           variable="selection">
1647
                        <count
1648
                              value="1">
1649
                        </count>
1650
                     </with>
1651
                  </visibleWhen>
1652
            </dynamic>
1655
         </menu>
1653
         </menu>
1656
      </menuContribution>
1654
      </menuContribution>
1657
      <menuContribution
1655
      <menuContribution
(-)plugin.properties (-5 / +4 lines)
Lines 171-180 Link Here
171
171
172
commands.category.name = Task
172
commands.category.name = Task
173
command.openSelectedTask.name = Open Selected Task
173
command.openSelectedTask.name = Open Selected Task
174
command.attachment.openInBrowser.name = Open Attachment in Browser
175
174
176
commands.category.editor.name = Task Editor
175
commands.category.editor.name = Task Editor
177
command.attachment.openInDefaultEditor.name = Open Attachment in Default Editor
178
command.submitTask.name = Submit Task
176
command.submitTask.name = Submit Task
179
command.submitTask.description = Submits the currently open task
177
command.submitTask.description = Submits the currently open task
180
178
Lines 182-190 Link Here
182
filters.tasks.name = Task List Elements (Recommended)
180
filters.tasks.name = Task List Elements (Recommended)
183
filters.tasks.description = Hides Task List Elements from Common Navigators
181
filters.tasks.description = Hides Task List Elements from Common Navigators
184
182
185
command.attachment.openInBrowser.label = Open With Browser
183
command.attachment.openWith.menu.label = Open With
186
command.attachment.openInDefaultEditor.menu.label = Open With
187
command.attachment.openInDefaultEditor.label = Default Editor
188
184
189
menu.task.mark.label = &Mark as
185
menu.task.mark.label = &Mark as
190
command.markTaskRead.label = Read
186
command.markTaskRead.label = Read
Lines 202-204 Link Here
202
commands.ViewSourceHandler.name = View Unformatted Text
198
commands.ViewSourceHandler.name = View Unformatted Text
203
199
204
context.editors.task.name = In Tasks Editor
200
context.editors.task.name = In Tasks Editor
201
202
command.attachment.open.label = Open
203
command.attachment.open.name = Open Attachment
(-)src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskEditorAttachmentPart.java (-6 / +23 lines)
Lines 34-44 Link Here
34
import org.eclipse.jface.window.ToolTip;
34
import org.eclipse.jface.window.ToolTip;
35
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
35
import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
36
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
36
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
37
import org.eclipse.mylyn.internal.tasks.ui.commands.OpenTaskAttachmentHandler;
37
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiMenus;
38
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiMenus;
38
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskAttachmentWizard.Mode;
39
import org.eclipse.mylyn.internal.tasks.ui.wizards.TaskAttachmentWizard.Mode;
39
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
40
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
40
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
41
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
41
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
42
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPart;
42
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPart;
43
import org.eclipse.swt.SWT;
43
import org.eclipse.swt.SWT;
44
import org.eclipse.swt.events.SelectionAdapter;
44
import org.eclipse.swt.events.SelectionAdapter;
Lines 51-56 Link Here
51
import org.eclipse.swt.widgets.Menu;
51
import org.eclipse.swt.widgets.Menu;
52
import org.eclipse.swt.widgets.Table;
52
import org.eclipse.swt.widgets.Table;
53
import org.eclipse.swt.widgets.TableColumn;
53
import org.eclipse.swt.widgets.TableColumn;
54
import org.eclipse.ui.IWorkbenchPage;
54
import org.eclipse.ui.forms.events.ExpansionAdapter;
55
import org.eclipse.ui.forms.events.ExpansionAdapter;
55
import org.eclipse.ui.forms.events.ExpansionEvent;
56
import org.eclipse.ui.forms.events.ExpansionEvent;
56
import org.eclipse.ui.forms.widgets.FormToolkit;
57
import org.eclipse.ui.forms.widgets.FormToolkit;
Lines 136-146 Link Here
136
				getTaskEditorPage().getAttributeEditorToolkit()));
137
				getTaskEditorPage().getAttributeEditorToolkit()));
137
		attachmentsViewer.addOpenListener(new IOpenListener() {
138
		attachmentsViewer.addOpenListener(new IOpenListener() {
138
			public void open(OpenEvent event) {
139
			public void open(OpenEvent event) {
139
				if (!event.getSelection().isEmpty()) {
140
				openAttachments(event);
140
					StructuredSelection selection = (StructuredSelection) event.getSelection();
141
					ITaskAttachment attachment = (ITaskAttachment) selection.getFirstElement();
142
					TasksUiUtil.openUrl(attachment.getUrl());
143
				}
144
			}
141
			}
145
		});
142
		});
146
		attachmentsViewer.addSelectionChangedListener(getTaskEditorPage());
143
		attachmentsViewer.addSelectionChangedListener(getTaskEditorPage());
Lines 260-263 Link Here
260
		toolBarManager.add(attachFileAction);
257
		toolBarManager.add(attachFileAction);
261
	}
258
	}
262
259
260
	protected void openAttachments(OpenEvent event) {
261
		List<ITaskAttachment> attachments = new ArrayList<ITaskAttachment>();
262
263
		StructuredSelection selection = (StructuredSelection) event.getSelection();
264
265
		List<?> items = selection.toList();
266
		for (Object item : items) {
267
			if (item instanceof ITaskAttachment) {
268
				attachments.add((ITaskAttachment) item);
269
			}
270
		}
271
272
		if (attachments.isEmpty()) {
273
			return;
274
		}
275
276
		IWorkbenchPage page = getTaskEditorPage().getSite().getWorkbenchWindow().getActivePage();
277
278
		OpenTaskAttachmentHandler.openAttachments(page, attachments);
279
	}
263
}
280
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskAttachmentStorage.java (-122 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 Jeff Pound and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Jeff Pound - initial API and implementation
10
 *     Tasktop Technologies - improvements
11
 *******************************************************************************/
12
13
package org.eclipse.mylyn.internal.tasks.ui.editors;
14
15
import java.io.InputStream;
16
17
import org.eclipse.core.resources.IStorage;
18
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IPath;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.NullProgressMonitor;
23
import org.eclipse.core.runtime.PlatformObject;
24
import org.eclipse.core.runtime.Status;
25
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
26
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
27
import org.eclipse.mylyn.tasks.core.ITask;
28
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
29
import org.eclipse.mylyn.tasks.core.TaskRepository;
30
import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler;
31
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
32
import org.eclipse.mylyn.tasks.ui.TasksUi;
33
34
/**
35
 * @author Jeff Pound
36
 * @author Steffen Pingel
37
 */
38
public class TaskAttachmentStorage extends PlatformObject implements IStorage {
39
40
	private static final String ATTACHMENT_DEFAULT_NAME = "attachment"; //$NON-NLS-1$
41
42
	private static final String CTYPE_ZIP = "zip"; //$NON-NLS-1$
43
44
	private static final String CTYPE_OCTET_STREAM = "octet-stream"; //$NON-NLS-1$
45
46
	private static final String CTYPE_TEXT = "text"; //$NON-NLS-1$
47
48
	private static final String CTYPE_HTML = "html"; //$NON-NLS-1$
49
50
	private final TaskRepository taskRepository;
51
52
	private final ITask task;
53
54
	private final TaskAttribute attachmentAttribute;
55
56
	private final String name;
57
58
	public TaskAttachmentStorage(TaskRepository taskRepository, ITask task, TaskAttribute attachmentAttribute,
59
			String name) {
60
		this.taskRepository = taskRepository;
61
		this.task = task;
62
		this.attachmentAttribute = attachmentAttribute;
63
		this.name = name;
64
	}
65
66
	public static IStorage create(ITaskAttachment attachment) throws CoreException {
67
		Assert.isNotNull(attachment);
68
		TaskAttribute taskAttribute = attachment.getTaskAttribute();
69
		if (taskAttribute == null) {
70
			throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Failed to find attachment: " //$NON-NLS-1$
71
					+ attachment.getUrl()));
72
		}
73
		return new TaskAttachmentStorage(attachment.getTaskRepository(), attachment.getTask(), taskAttribute,
74
				getName(attachment));
75
	}
76
77
	private static String getName(ITaskAttachment attachment) {
78
		String name = attachment.getFileName();
79
		// if no filename is set, make one up with the proper extension so
80
		// we can support opening in that filetype's default editor
81
		if (name == null || "".equals(name)) { //$NON-NLS-1$
82
			String ctype = attachment.getContentType();
83
			if (ctype.endsWith(CTYPE_HTML)) {
84
				name = ATTACHMENT_DEFAULT_NAME + ".html"; //$NON-NLS-1$
85
			} else if (ctype.startsWith(CTYPE_TEXT)) {
86
				name = ATTACHMENT_DEFAULT_NAME + ".txt"; //$NON-NLS-1$
87
			} else if (ctype.endsWith(CTYPE_OCTET_STREAM)) {
88
				name = ATTACHMENT_DEFAULT_NAME;
89
			} else if (ctype.endsWith(CTYPE_ZIP)) {
90
				name = ATTACHMENT_DEFAULT_NAME + "." + CTYPE_ZIP; //$NON-NLS-1$
91
			} else {
92
				name = ATTACHMENT_DEFAULT_NAME + "." + ctype.substring(ctype.indexOf("/") + 1); //$NON-NLS-1$ //$NON-NLS-2$
93
			}
94
		}
95
		// treat .patch files as text files
96
		if (name.endsWith(".patch")) { //$NON-NLS-1$
97
			name += ".txt"; //$NON-NLS-1$
98
		}
99
		return name;
100
	}
101
102
	public InputStream getContents() throws CoreException {
103
		AbstractRepositoryConnector connector = TasksUi.getRepositoryManager().getRepositoryConnector(
104
				taskRepository.getConnectorKind());
105
		AbstractTaskAttachmentHandler handler = connector.getTaskAttachmentHandler();
106
		return handler.getContent(taskRepository, task, attachmentAttribute, new NullProgressMonitor());
107
	}
108
109
	public IPath getFullPath() {
110
		// ignore
111
		return null;
112
	}
113
114
	public String getName() {
115
		return name;
116
	}
117
118
	public boolean isReadOnly() {
119
		return true;
120
	}
121
122
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/editors/TaskAttachmentEditorInput.java (-59 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2008 Jeff Pound and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Jeff Pound - initial API and implementation
10
 *     Tasktop Technologies - improvements
11
 *******************************************************************************/
12
13
package org.eclipse.mylyn.internal.tasks.ui.editors;
14
15
import org.eclipse.core.resources.IStorage;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.PlatformObject;
18
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
20
import org.eclipse.ui.IPersistableElement;
21
import org.eclipse.ui.IStorageEditorInput;
22
23
/**
24
 * @author Jeff Pound
25
 * @author Steffen Pingel
26
 */
27
public class TaskAttachmentEditorInput extends PlatformObject implements IStorageEditorInput {
28
29
	private final ITaskAttachment attachment;
30
31
	public TaskAttachmentEditorInput(ITaskAttachment attachment) {
32
		this.attachment = attachment;
33
	}
34
35
	public boolean exists() {
36
		return true;
37
	}
38
39
	public ImageDescriptor getImageDescriptor() {
40
		// ignore
41
		return null;
42
	}
43
44
	public String getName() {
45
		return attachment.getFileName();
46
	}
47
48
	public IPersistableElement getPersistable() {
49
		return null;
50
	}
51
52
	public String getToolTipText() {
53
		return attachment.getUrl();
54
	}
55
56
	public IStorage getStorage() throws CoreException {
57
		return TaskAttachmentStorage.create(attachment);
58
	}
59
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/TaskAttachmentViewerBrowser.java (+19 lines)
Added Link Here
1
package org.eclipse.mylyn.internal.tasks.ui;
2
3
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
4
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
5
import org.eclipse.ui.IWorkbenchPage;
6
7
public class TaskAttachmentViewerBrowser implements ITaskAttachmentViewer {
8
	public String getID() {
9
		return "inBrowserViewer"; //$NON-NLS-1$
10
	}
11
12
	public String getLabel() {
13
		return Messages.TaskAttachmentViewerBrowser_browser;
14
	}
15
16
	public void openAttachment(IWorkbenchPage page, ITaskAttachment attachment) {
17
		TasksUiUtil.openUrl(attachment.getUrl());
18
	}
19
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/commands/OpenTaskAttachmentWithMenu.java (+119 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui.commands;
13
14
import java.util.List;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.jface.action.ContributionItem;
18
import org.eclipse.mylyn.internal.tasks.ui.ITaskAttachmentViewer;
19
import org.eclipse.mylyn.internal.tasks.ui.TaskAttachmentViewersManager;
20
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
21
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
22
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.SelectionAdapter;
25
import org.eclipse.swt.events.SelectionEvent;
26
import org.eclipse.swt.widgets.Menu;
27
import org.eclipse.swt.widgets.MenuItem;
28
import org.eclipse.ui.IWorkbench;
29
import org.eclipse.ui.IWorkbenchPage;
30
import org.eclipse.ui.IWorkbenchWindow;
31
import org.eclipse.ui.PlatformUI;
32
33
public class OpenTaskAttachmentWithMenu extends ContributionItem {
34
	private final TaskAttachmentViewersManager manager = new TaskAttachmentViewersManager();
35
36
	public OpenTaskAttachmentWithMenu() {
37
	}
38
39
	public OpenTaskAttachmentWithMenu(String id) {
40
		super(id);
41
	}
42
43
	@Override
44
	public void fill(Menu menu, int index) {
45
		List<ITaskAttachment> attachments = AttachmentUtil.getSelectedAttachments();
46
		if (attachments.isEmpty() || attachments.size() > 1) {
47
			return;
48
		}
49
50
		// find all interesting editors, and add them into menu
51
		ITaskAttachment at = attachments.get(0);
52
53
		String prefViewerID = manager.getPreferredViewerID(at);
54
55
		int i = 0;
56
57
		List<ITaskAttachmentViewer> viewers = manager.getTaskAttachmentViewers(at);
58
		for (ITaskAttachmentViewer v : viewers) {
59
			MenuItem it = new MenuItem(menu, SWT.RADIO, index + i);
60
61
			it.setText(v.getLabel());
62
			it.addSelectionListener(new RunAssociatedViewer(it, v));
63
			if (prefViewerID != null && prefViewerID.equals(v.getID())) {
64
				it.setSelection(true);
65
			}
66
67
			i++;
68
		}
69
	}
70
71
	private class RunAssociatedViewer extends SelectionAdapter {
72
		private final MenuItem menuItem;
73
74
		private final ITaskAttachmentViewer viewer;
75
76
		RunAssociatedViewer(MenuItem item, ITaskAttachmentViewer handler) {
77
			this.menuItem = item;
78
			this.viewer = handler;
79
		}
80
81
		@Override
82
		public void widgetSelected(SelectionEvent e) {
83
			// this event is fired also for item which gets unselected (i.e. previous 'preferred' viewer)
84
			if (!menuItem.getSelection()) {
85
				return;
86
			}
87
88
			IWorkbench wb = PlatformUI.getWorkbench();
89
90
			IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
91
			if (win == null) {
92
				return;
93
			}
94
95
			IWorkbenchPage page = win.getActivePage();
96
			if (page == null) {
97
				return;
98
			}
99
100
			List<ITaskAttachment> attachments = AttachmentUtil.getSelectedAttachments();
101
			for (ITaskAttachment ta : attachments) {
102
				manager.savePreferredViewerID(ta, viewer.getID());
103
104
				try {
105
					viewer.openAttachment(page, ta);
106
				} catch (CoreException ex) {
107
					TasksUiInternal.logAndDisplayStatus(Messages.OpenTaskAttachmentHandler_failedToOpenViewer,
108
							ex.getStatus());
109
				}
110
			}
111
		}
112
	}
113
114
	@Override
115
	public boolean isDynamic() {
116
		// menu depends on selected attachment(s)
117
		return true;
118
	}
119
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/TaskAttachmentEditorViewer.java (+44 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import java.text.MessageFormat;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
18
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
19
import org.eclipse.ui.IEditorDescriptor;
20
import org.eclipse.ui.IWorkbenchPage;
21
22
public class TaskAttachmentEditorViewer implements ITaskAttachmentViewer {
23
	private final IEditorDescriptor descriptor;
24
25
	TaskAttachmentEditorViewer(IEditorDescriptor descriptor) {
26
		this.descriptor = descriptor;
27
	}
28
29
	public String getID() {
30
		return descriptor.getId();
31
	}
32
33
	public String getLabel() {
34
		return descriptor.getLabel();
35
	}
36
37
	public void openAttachment(IWorkbenchPage page, ITaskAttachment attachment) throws CoreException {
38
		DownloadAndOpenTaskAttachmentJob job = new DownloadAndOpenTaskAttachmentJob(
39
				MessageFormat.format(Messages.TaskAttachmentEditorViewer_openingAttachment,
40
						AttachmentUtil.getAttachmentFilename(attachment)), attachment, page, descriptor.getId());
41
		job.setUser(true);
42
		job.schedule();
43
	}
44
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/DownloadAndOpenTaskAttachmentJob.java (+183 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import java.io.File;
15
import java.io.FileOutputStream;
16
import java.io.IOException;
17
import java.text.MessageFormat;
18
import java.util.concurrent.atomic.AtomicReference;
19
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.OperationCanceledException;
24
import org.eclipse.core.runtime.Status;
25
import org.eclipse.core.runtime.jobs.Job;
26
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
27
import org.eclipse.mylyn.tasks.core.ITask;
28
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
29
import org.eclipse.mylyn.tasks.core.TaskRepository;
30
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
31
import org.eclipse.mylyn.tasks.ui.TasksUi;
32
import org.eclipse.swt.widgets.Display;
33
import org.eclipse.ui.IWorkbenchPage;
34
import org.eclipse.ui.PartInitException;
35
36
class DownloadAndOpenTaskAttachmentJob extends Job {
37
	private final ITaskAttachment attachment;
38
39
	private final IWorkbenchPage page;
40
41
	private final String editorID;
42
43
	DownloadAndOpenTaskAttachmentJob(String jobName, ITaskAttachment attachment, IWorkbenchPage page, String editorID) {
44
		super(jobName);
45
46
		this.attachment = attachment;
47
		this.page = page;
48
		this.editorID = editorID;
49
	}
50
51
	@Override
52
	protected IStatus run(IProgressMonitor monitor) {
53
		final String attachmentFilename = AttachmentUtil.getAttachmentFilename(attachment);
54
55
		File file = null;
56
		try {
57
			// create temporary filename like 'attach-127562364-attachment-name.txt'
58
			// This has correct extension based on attachment filename, resembles attachment name, but
59
			// also indicates that it is temporary file
60
			file = File.createTempFile("tmpattach-", "-" + attachmentFilename); //$NON-NLS-1$ //$NON-NLS-2$
61
		} catch (IOException e) {
62
			return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
63
					Messages.DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment, e);
64
		}
65
		file.deleteOnExit();
66
67
		boolean ok = false;
68
		FileOutputStream fos = null;
69
		try {
70
			fos = new FileOutputStream(file);
71
			AttachmentUtil.downloadAttachment(attachment, fos, monitor);
72
			ok = true;
73
74
		} catch (IOException e) {
75
			return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
76
					Messages.DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment, e);
77
		} catch (CoreException e) {
78
			int s = IStatus.ERROR;
79
			if (e.getStatus() != null && e.getStatus().getCode() == IStatus.CANCEL) {
80
				s = IStatus.CANCEL;
81
			}
82
			return new Status(s, TasksUiPlugin.ID_PLUGIN,
83
					Messages.DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment, e);
84
		} catch (OperationCanceledException e) {
85
			return new Status(IStatus.CANCEL, TasksUiPlugin.ID_PLUGIN,
86
					Messages.DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment, e);
87
		} finally {
88
			// (fos != null) only when there is some problem, in other cases we nulled fos already
89
			if (fos != null) {
90
				try {
91
					fos.close();
92
				} catch (IOException e) {
93
					if (ok) {
94
						file.delete();
95
						return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
96
								Messages.DownloadAndOpenTaskAttachmentJob_failedToDownloadAttachment, e);
97
					}
98
				}
99
			}
100
101
			if (!ok) {
102
				file.delete();
103
			}
104
		}
105
106
		// mark file read-only to warn user that he is working with local copy
107
		file.setReadOnly();
108
109
		Display disp = page.getWorkbenchWindow().getWorkbench().getDisplay();
110
		if (disp.isDisposed()) {
111
			return new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN,
112
					Messages.DownloadAndOpenTaskAttachmentJob_cannotOpenEditor);
113
		}
114
115
		if (disp.getThread() == Thread.currentThread()) {
116
			return openEditor(file, attachmentFilename);
117
		} else {
118
			final AtomicReference<IStatus> status = new AtomicReference<IStatus>();
119
			final File tmpFile = file;
120
121
			disp.syncExec(new Runnable() {
122
				public void run() {
123
					status.set(openEditor(tmpFile, attachmentFilename));
124
				};
125
			});
126
127
			if (status.get() == null) {
128
				return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
129
						Messages.DownloadAndOpenTaskAttachmentJob_cannotOpenEditor);
130
			}
131
132
			return status.get();
133
		}
134
	}
135
136
	IStatus openEditor(File file, String attachmentName) {
137
		try {
138
			String taskLabel = getTaskLabel(attachment.getTask());
139
			String repoLabel = getRepositoryLabel(attachment.getTask());
140
141
			String tooltip = MessageFormat.format(Messages.DownloadAndOpenTaskAttachmentJob_editorTooltip, taskLabel, repoLabel);
142
143
			page.openEditor(new FileEditorInput(file, attachmentName, tooltip), editorID);
144
			return Status.OK_STATUS;
145
		} catch (PartInitException e) {
146
			return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
147
					Messages.DownloadAndOpenTaskAttachmentJob_cannotOpenEditor, e);
148
		}
149
	}
150
151
	private String getTaskLabel(ITask task) {
152
		AbstractRepositoryConnectorUi connectorUi = TasksUiPlugin.getConnectorUi(task.getConnectorKind());
153
		StringBuilder taskLabel = new StringBuilder();
154
		if (connectorUi != null) {
155
			taskLabel.append(connectorUi.getTaskKindLabel(task));
156
		}
157
158
		String key = task.getTaskKey();
159
		if (key != null) {
160
			if (taskLabel.length() > 0) {
161
				taskLabel.append(" "); //$NON-NLS-1$
162
			}
163
			taskLabel.append(key);
164
		}
165
		return taskLabel.toString();
166
	}
167
168
	// copied from TaskListToolTip
169
	private String getRepositoryLabel(ITask task) {
170
		String repositoryKind = task.getConnectorKind();
171
		String repositoryUrl = task.getRepositoryUrl();
172
173
		TaskRepository repository = TasksUi.getRepositoryManager().getRepository(repositoryKind, repositoryUrl);
174
		if (repository != null) {
175
			String label = repository.getRepositoryLabel();
176
			if (label.indexOf("//") != -1) { //$NON-NLS-1$
177
				return label.substring((repository.getRepositoryUrl().indexOf("//") + 2)); //$NON-NLS-1$
178
			}
179
			return label;
180
		}
181
		return ""; //$NON-NLS-1$
182
	}
183
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/FileEditorInput.java (+82 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import java.io.File;
15
16
import org.eclipse.core.resources.IStorage;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.IPath;
19
import org.eclipse.core.runtime.Path;
20
import org.eclipse.core.runtime.PlatformObject;
21
import org.eclipse.jface.resource.ImageDescriptor;
22
import org.eclipse.ui.IPathEditorInput;
23
import org.eclipse.ui.IPersistableElement;
24
import org.eclipse.ui.IStorageEditorInput;
25
26
class FileEditorInput extends PlatformObject implements IPathEditorInput, IStorageEditorInput {
27
	private final File file;
28
29
	private final String name;
30
31
	private final String tooltipText;
32
33
	FileEditorInput(File file, String name, String tooltipText) {
34
		this.file = file;
35
		this.name = name;
36
		this.tooltipText = tooltipText;
37
	}
38
39
	public IPath getPath() {
40
		return Path.fromOSString(file.getAbsolutePath());
41
	}
42
43
	public boolean exists() {
44
		return file.exists();
45
	}
46
47
	public ImageDescriptor getImageDescriptor() {
48
		return null;
49
	}
50
51
	public String getName() {
52
		return name;
53
	}
54
55
	public IPersistableElement getPersistable() {
56
		// ignore
57
		return null;
58
	}
59
60
	public String getToolTipText() {
61
		return tooltipText;
62
	}
63
64
	public IStorage getStorage() throws CoreException {
65
		return new FileStorage(file, name);
66
	}
67
68
	@Override
69
	public int hashCode() {
70
		return file.hashCode() ^ name.hashCode();
71
	}
72
73
	@Override
74
	public boolean equals(Object obj) {
75
		if (!(obj instanceof FileEditorInput)) {
76
			return false;
77
		}
78
79
		FileEditorInput other = (FileEditorInput) obj;
80
		return name.equals(other.name) && file.equals(other.file);
81
	}
82
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/TaskAttachmentViewersManager.java (+120 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import java.util.ArrayList;
15
import java.util.List;
16
17
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
18
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
19
import org.eclipse.ui.IEditorDescriptor;
20
import org.eclipse.ui.IEditorRegistry;
21
import org.eclipse.ui.PlatformUI;
22
import org.eclipse.ui.editors.text.EditorsUI;
23
24
public class TaskAttachmentViewersManager {
25
	public List<ITaskAttachmentViewer> getTaskAttachmentViewers(ITaskAttachment attachment) {
26
		IEditorRegistry er = PlatformUI.getWorkbench().getEditorRegistry();
27
28
		List<ITaskAttachmentViewer> result = new ArrayList<ITaskAttachmentViewer>();
29
		IEditorDescriptor defaultEditor = er.getDefaultEditor(AttachmentUtil.getAttachmentFilename(attachment));
30
		if (defaultEditor != null) {
31
			result.add(new TaskAttachmentEditorViewer(defaultEditor));
32
		}
33
34
		if (attachment.getUrl() != null && attachment.getUrl().trim().length() > 0) {
35
			result.add(new TaskAttachmentViewerBrowser());
36
		}
37
38
		IEditorDescriptor defaultTextEditor = er.findEditor(EditorsUI.DEFAULT_TEXT_EDITOR_ID); // may be null
39
		if (defaultTextEditor != null
40
				&& (defaultEditor == null || !defaultTextEditor.getId().equals(defaultEditor.getId()))) {
41
			result.add(new TaskAttachmentEditorViewer(defaultTextEditor));
42
		}
43
44
		IEditorDescriptor[] descriptors = er.getEditors(AttachmentUtil.getAttachmentFilename(attachment));
45
		for (IEditorDescriptor ied : descriptors) {
46
			if (defaultEditor == null || !ied.getId().equals(defaultEditor.getId())) {
47
				result.add(new TaskAttachmentEditorViewer(ied));
48
			}
49
		}
50
51
		// Don't check whether system external editor is available (IEditorRegistry.isSystemExternalEditorAvailable) ...
52
		// At least Windows can handle even unknown files, and offers user to choose correct program to open file with
53
		IEditorDescriptor extern = er.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
54
		result.add(new TaskAttachmentEditorViewer(extern));
55
56
		if (er.isSystemInPlaceEditorAvailable(AttachmentUtil.getAttachmentFilename(attachment))) {
57
			IEditorDescriptor inplace = er.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID);
58
			result.add(new TaskAttachmentEditorViewer(inplace));
59
		}
60
61
		return result;
62
	}
63
64
	/**
65
	 * @param attachment
66
	 * @return preferred attachment viewers, or null if no suitable viewer can be found
67
	 */
68
	public ITaskAttachmentViewer getPreferredViewer(ITaskAttachment attachment) {
69
		List<ITaskAttachmentViewer> viewers = getTaskAttachmentViewers(attachment);
70
71
		String preferred = getPreferredViewerID(attachment);
72
		if (preferred != null) {
73
			for (int i = 0; i < viewers.size(); i++) {
74
				if (preferred.equals(viewers.get(i).getID())) {
75
					return viewers.get(i);
76
				}
77
			}
78
		}
79
80
		if (viewers.isEmpty()) {
81
			return null;
82
		}
83
84
		return viewers.get(0);
85
	}
86
87
	public String getPreferredViewerID(ITaskAttachment attachment) {
88
		String ext = getExtension(attachment);
89
		if (ext == null) {
90
			return null;
91
		}
92
93
		return TasksUiPlugin.getDefault().getPreferenceStore().getString(
94
				ITasksUiPreferenceConstants.PREFERRED_TASK_ATTACHMENT_VIEWER_ID + "_" + ext); //$NON-NLS-1$
95
	}
96
97
	public void savePreferredViewerID(ITaskAttachment attachment, String handlerID) {
98
		String ext = getExtension(attachment);
99
		if (ext == null) {
100
			return;
101
		}
102
103
		TasksUiPlugin.getDefault().getPreferenceStore().putValue(
104
				ITasksUiPreferenceConstants.PREFERRED_TASK_ATTACHMENT_VIEWER_ID + "_" + ext, handlerID); //$NON-NLS-1$
105
	}
106
107
	private String getExtension(ITaskAttachment attachment) {
108
		if (attachment == null) {
109
			return null;
110
		}
111
112
		String fname = AttachmentUtil.getAttachmentFilename(attachment);
113
		int dot = fname.lastIndexOf('.');
114
		if (dot < 0) {
115
			return null;
116
		}
117
118
		return fname.substring(dot + 1);
119
	}
120
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/ITaskAttachmentViewer.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
16
import org.eclipse.ui.IWorkbenchPage;
17
18
/**
19
 * Simple interface for attachment 'viewers'. Most viewers are based on existing eclipse editors.
20
 */
21
public interface ITaskAttachmentViewer {
22
	/**
23
	 * @return arbitrary string, used to remember preferred viewer
24
	 */
25
	public String getID();
26
27
	/**
28
	 * @return name of the editor, displayed to user
29
	 */
30
	public String getLabel();
31
32
	public void openAttachment(IWorkbenchPage page, ITaskAttachment attachment) throws CoreException;
33
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/FileStorage.java (+57 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui;
13
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.FileNotFoundException;
17
import java.io.InputStream;
18
19
import org.eclipse.core.resources.IStorage;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IPath;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.PlatformObject;
24
import org.eclipse.core.runtime.Status;
25
26
public class FileStorage extends PlatformObject implements IStorage {
27
	private final File file;
28
29
	private final String name;
30
31
	public FileStorage(File file, String name) {
32
		this.file = file;
33
		this.name = name;
34
	}
35
36
	public InputStream getContents() throws CoreException {
37
		try {
38
			return new FileInputStream(file);
39
		} catch (FileNotFoundException e) {
40
			throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
41
					Messages.FileStorage_unableToReadAttachmentFile, e));
42
		}
43
	}
44
45
	public IPath getFullPath() {
46
		// ignore
47
		return null;
48
	}
49
50
	public String getName() {
51
		return name;
52
	}
53
54
	public boolean isReadOnly() {
55
		return true;
56
	}
57
}
(-)src/org/eclipse/mylyn/internal/tasks/ui/commands/OpenTaskAttachmentHandler.java (+77 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Tasktop Technologies and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Tasktop Technologies - initial API and implementation
10
 *******************************************************************************/
11
12
package org.eclipse.mylyn.internal.tasks.ui.commands;
13
14
import java.util.List;
15
16
import org.eclipse.core.commands.AbstractHandler;
17
import org.eclipse.core.commands.ExecutionEvent;
18
import org.eclipse.core.commands.ExecutionException;
19
import org.eclipse.core.commands.IHandler;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
import org.eclipse.mylyn.internal.tasks.ui.ITaskAttachmentViewer;
24
import org.eclipse.mylyn.internal.tasks.ui.TaskAttachmentViewersManager;
25
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
26
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
27
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
28
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
29
import org.eclipse.ui.IWorkbench;
30
import org.eclipse.ui.IWorkbenchPage;
31
import org.eclipse.ui.IWorkbenchWindow;
32
import org.eclipse.ui.PlatformUI;
33
34
public class OpenTaskAttachmentHandler extends AbstractHandler implements IHandler {
35
36
	public Object execute(ExecutionEvent event) throws ExecutionException {
37
		IWorkbench wb = PlatformUI.getWorkbench();
38
39
		IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
40
		if (win == null) {
41
			return null;
42
		}
43
44
		IWorkbenchPage page = win.getActivePage();
45
		if (page == null) {
46
			return null;
47
		}
48
49
		List<ITaskAttachment> attachments = AttachmentUtil.getSelectedAttachments();
50
51
		openAttachments(page, attachments);
52
53
		return null;
54
	}
55
56
	public static void openAttachments(IWorkbenchPage page, List<ITaskAttachment> attachments) {
57
		TaskAttachmentViewersManager manager = new TaskAttachmentViewersManager();
58
59
		for (ITaskAttachment a : attachments) {
60
			ITaskAttachmentViewer v = manager.getPreferredViewer(a);
61
62
			if (v == null) {
63
				TasksUiInternal.logAndDisplayStatus(Messages.OpenTaskAttachmentHandler_failedToOpenViewer, new Status(
64
						IStatus.WARNING, TasksUiPlugin.ID_PLUGIN,
65
						Messages.OpenTaskAttachmentHandler_noAttachmentViewerFound));
66
				continue;
67
			}
68
69
			try {
70
				v.openAttachment(page, a);
71
			} catch (CoreException e) {
72
				TasksUiInternal.logAndDisplayStatus(Messages.OpenTaskAttachmentHandler_failedToOpenViewer,
73
						e.getStatus());
74
			}
75
		}
76
	}
77
}
(-)src/org/eclipse/mylyn/internal/team/ui/actions/Messages.java (+3 lines)
Lines 30-36 Link Here
30
	public static String AddToTaskContextAction_No_resources_to_add;
30
	public static String AddToTaskContextAction_No_resources_to_add;
31
31
32
	public static String ApplyPatchAction_Apply_Patch;
32
	public static String ApplyPatchAction_Apply_Patch;
33
	public static String ApplyPatchAction_cannotApplyPatch;
34
	public static String ApplyPatchAction_downloadingPatch;
33
	public static String ApplyPatchAction_Error_Retrieving_Context;
35
	public static String ApplyPatchAction_Error_Retrieving_Context;
36
	public static String ApplyPatchAction_failedToDownloadPatch;
34
37
35
	public static String OpenCorrespondingTaskAction_Completed;
38
	public static String OpenCorrespondingTaskAction_Completed;
36
	public static String OpenCorrespondingTaskAction_Open_Corresponding_Task;
39
	public static String OpenCorrespondingTaskAction_Open_Corresponding_Task;
(-)src/org/eclipse/mylyn/internal/team/ui/actions/ApplyPatchAction.java (-15 / +115 lines)
Lines 11-30 Link Here
11
11
12
package org.eclipse.mylyn.internal.team.ui.actions;
12
package org.eclipse.mylyn.internal.team.ui.actions;
13
13
14
import java.io.File;
15
import java.io.FileOutputStream;
16
import java.io.IOException;
17
14
import org.eclipse.compare.CompareConfiguration;
18
import org.eclipse.compare.CompareConfiguration;
15
import org.eclipse.compare.patch.ApplyPatchOperation;
19
import org.eclipse.compare.patch.ApplyPatchOperation;
16
import org.eclipse.core.resources.IStorage;
17
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.CoreException;
21
import org.eclipse.core.runtime.IProgressMonitor;
22
import org.eclipse.core.runtime.IStatus;
23
import org.eclipse.core.runtime.OperationCanceledException;
24
import org.eclipse.core.runtime.Status;
25
import org.eclipse.core.runtime.jobs.Job;
18
import org.eclipse.jface.action.IAction;
26
import org.eclipse.jface.action.IAction;
19
import org.eclipse.jface.viewers.ISelection;
27
import org.eclipse.jface.viewers.ISelection;
20
import org.eclipse.jface.viewers.StructuredSelection;
28
import org.eclipse.jface.viewers.StructuredSelection;
21
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskAttachmentStorage;
29
import org.eclipse.mylyn.internal.tasks.ui.FileStorage;
22
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
30
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
31
import org.eclipse.mylyn.internal.team.ui.FocusedTeamUiPlugin;
23
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
32
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
24
import org.eclipse.swt.custom.BusyIndicator;
33
import org.eclipse.swt.custom.BusyIndicator;
25
import org.eclipse.swt.widgets.Display;
34
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.ui.IViewActionDelegate;
35
import org.eclipse.ui.IViewActionDelegate;
27
import org.eclipse.ui.IViewPart;
36
import org.eclipse.ui.IViewPart;
37
import org.eclipse.ui.IWorkbenchPart;
38
import org.eclipse.ui.IWorkbenchPartSite;
28
import org.eclipse.ui.PlatformUI;
39
import org.eclipse.ui.PlatformUI;
29
import org.eclipse.ui.actions.BaseSelectionListenerAction;
40
import org.eclipse.ui.actions.BaseSelectionListenerAction;
30
41
Lines 33-38 Link Here
33
 * @author Steffen Pingel
44
 * @author Steffen Pingel
34
 */
45
 */
35
public class ApplyPatchAction extends BaseSelectionListenerAction implements IViewActionDelegate {
46
public class ApplyPatchAction extends BaseSelectionListenerAction implements IViewActionDelegate {
47
	private IViewPart viewPart;
36
48
37
	public ApplyPatchAction() {
49
	public ApplyPatchAction() {
38
		super(Messages.ApplyPatchAction_Apply_Patch);
50
		super(Messages.ApplyPatchAction_Apply_Patch);
Lines 45-51 Link Here
45
	private ISelection currentSelection;
57
	private ISelection currentSelection;
46
58
47
	public void init(IViewPart view) {
59
	public void init(IViewPart view) {
48
		// ignore
60
		this.viewPart = view;
49
	}
61
	}
50
62
51
	public void run(IAction action) {
63
	public void run(IAction action) {
Lines 53-70 Link Here
53
			Object object = ((StructuredSelection) currentSelection).getFirstElement();
65
			Object object = ((StructuredSelection) currentSelection).getFirstElement();
54
			if (object instanceof ITaskAttachment) {
66
			if (object instanceof ITaskAttachment) {
55
				final ITaskAttachment attachment = (ITaskAttachment) object;
67
				final ITaskAttachment attachment = (ITaskAttachment) object;
56
				IStorage storage;
68
57
				try {
69
				IWorkbenchPart vp = viewPart;
58
					storage = TaskAttachmentStorage.create(attachment);
70
				if (vp == null) {
59
				} catch (CoreException e) {
71
					vp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
60
					TasksUiInternal.displayStatus(Messages.ApplyPatchAction_Error_Retrieving_Context, e.getStatus());
61
					return;
62
				}
72
				}
63
				ApplyPatchOperation op = new ApplyPatchOperation(PlatformUI.getWorkbench()
73
64
						.getActiveWorkbenchWindow()
74
				DownloadAndApplyPatch job = new DownloadAndApplyPatch(Messages.ApplyPatchAction_downloadingPatch,
65
						.getActivePage()
75
						attachment, vp);
66
						.getActivePart(), storage, null, new CompareConfiguration());
76
				job.setUser(true);
67
				BusyIndicator.showWhile(Display.getDefault(), op);
77
				job.schedule();
68
			}
78
			}
69
		}
79
		}
70
	}
80
	}
Lines 72-75 Link Here
72
	public void selectionChanged(IAction action, ISelection selection) {
82
	public void selectionChanged(IAction action, ISelection selection) {
73
		this.currentSelection = selection;
83
		this.currentSelection = selection;
74
	}
84
	}
85
86
	private static class DownloadAndApplyPatch extends Job {
87
		private final ITaskAttachment attachment;
88
89
		private final IWorkbenchPart wbPart;
90
91
		public DownloadAndApplyPatch(String jobName, ITaskAttachment attachment, IWorkbenchPart wbPart) {
92
			super(jobName);
93
94
			this.attachment = attachment;
95
			this.wbPart = wbPart;
96
		}
97
98
		@Override
99
		protected IStatus run(IProgressMonitor monitor) {
100
			String attachmentFilename = AttachmentUtil.getAttachmentFilename(attachment);
101
102
			File file = null;
103
			try {
104
				file = File.createTempFile("patch-", ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
105
			} catch (IOException e) {
106
				return new Status(IStatus.ERROR, FocusedTeamUiPlugin.ID_PLUGIN,
107
						Messages.ApplyPatchAction_failedToDownloadPatch, e);
108
			}
109
			file.deleteOnExit();
110
111
			boolean ok = false;
112
			FileOutputStream fos = null;
113
			try {
114
				fos = new FileOutputStream(file);
115
				AttachmentUtil.downloadAttachment(attachment, fos, monitor);
116
				ok = true;
117
			} catch (IOException e) {
118
				return new Status(IStatus.ERROR, FocusedTeamUiPlugin.ID_PLUGIN,
119
						Messages.ApplyPatchAction_failedToDownloadPatch, e);
120
			} catch (CoreException e) {
121
				int s = IStatus.ERROR;
122
				if (e.getStatus() != null && e.getStatus().getCode() == IStatus.CANCEL) {
123
					s = IStatus.CANCEL;
124
				}
125
				return new Status(s, FocusedTeamUiPlugin.ID_PLUGIN, Messages.ApplyPatchAction_failedToDownloadPatch, e);
126
			} catch (OperationCanceledException e) {
127
				return new Status(IStatus.CANCEL, FocusedTeamUiPlugin.ID_PLUGIN,
128
						Messages.ApplyPatchAction_failedToDownloadPatch, e);
129
			} finally {
130
				if (fos != null) {
131
					try {
132
						fos.close();
133
					} catch (IOException e) {
134
						if (ok) {
135
							// report this exception if there was no other problem until now ... we will not work with file
136
							// which cannot be closed properly, because this indicates some problem
137
							file.delete();
138
							return new Status(IStatus.ERROR, FocusedTeamUiPlugin.ID_PLUGIN,
139
									Messages.ApplyPatchAction_failedToDownloadPatch, e);
140
						}
141
					}
142
				}
143
144
				if (!ok) {
145
					file.delete();
146
				}
147
			}
148
149
			IWorkbenchPartSite site = wbPart.getSite();
150
			if (site == null) {
151
				return new Status(IStatus.WARNING, FocusedTeamUiPlugin.ID_PLUGIN,
152
						Messages.ApplyPatchAction_cannotApplyPatch);
153
			}
154
155
			final Display disp = site.getWorkbenchWindow().getWorkbench().getDisplay();
156
			if (disp.isDisposed()) {
157
				return new Status(IStatus.WARNING, FocusedTeamUiPlugin.ID_PLUGIN,
158
						Messages.ApplyPatchAction_cannotApplyPatch);
159
			}
160
161
			final FileStorage fileStorage = new FileStorage(file, attachmentFilename);
162
163
			disp.asyncExec(new Runnable() {
164
				public void run() {
165
					ApplyPatchOperation op = new ApplyPatchOperation(wbPart, fileStorage, null,
166
							new CompareConfiguration());
167
168
					BusyIndicator.showWhile(disp, op);
169
				}
170
			});
171
172
			return Status.OK_STATUS;
173
		}
174
	}
75
}
175
}
(-)src/org/eclipse/mylyn/internal/team/ui/actions/messages.properties (+3 lines)
Lines 13-19 Link Here
13
AddToTaskContextAction_No_resources_to_add=No resources to add.
13
AddToTaskContextAction_No_resources_to_add=No resources to add.
14
14
15
ApplyPatchAction_Apply_Patch=Apply Patch
15
ApplyPatchAction_Apply_Patch=Apply Patch
16
ApplyPatchAction_cannotApplyPatch=Cannot apply patch
17
ApplyPatchAction_downloadingPatch=Downloading patch
16
ApplyPatchAction_Error_Retrieving_Context=Error Retrieving Context
18
ApplyPatchAction_Error_Retrieving_Context=Error Retrieving Context
19
ApplyPatchAction_failedToDownloadPatch=Failed to download patch
17
20
18
OpenCorrespondingTaskAction_Completed=Completed:
21
OpenCorrespondingTaskAction_Completed=Completed:
19
OpenCorrespondingTaskAction_Open_Corresponding_Task=Open Corresponding Task
22
OpenCorrespondingTaskAction_Open_Corresponding_Task=Open Corresponding Task

Return to bug 220314