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.io.OutputStream; |
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.Status; |
24 |
import org.eclipse.core.runtime.jobs.Job; |
25 |
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil; |
26 |
import org.eclipse.mylyn.tasks.core.ITaskAttachment; |
27 |
import org.eclipse.swt.widgets.Display; |
28 |
import org.eclipse.ui.IWorkbenchPage; |
29 |
import org.eclipse.ui.PartInitException; |
30 |
|
31 |
class DownloadAndOpenTaskAttachmentJob extends Job { |
32 |
private final ITaskAttachment attachment; |
33 |
|
34 |
private final IWorkbenchPage page; |
35 |
|
36 |
private final String editorID; |
37 |
|
38 |
DownloadAndOpenTaskAttachmentJob(String jobName, ITaskAttachment attachment, IWorkbenchPage page, String editorID) { |
39 |
super(jobName); |
40 |
|
41 |
this.attachment = attachment; |
42 |
this.page = page; |
43 |
this.editorID = editorID; |
44 |
} |
45 |
|
46 |
@Override |
47 |
protected IStatus run(IProgressMonitor monitor) { |
48 |
final String attachmentFilename = AttachmentUtil.getAttachmentFilename(attachment); |
49 |
|
50 |
File file = null; |
51 |
try { |
52 |
file = File.createTempFile("attach-", "-" + attachmentFilename); |
53 |
} catch (IOException e) { |
54 |
return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Failed to download attachment", e); |
55 |
} |
56 |
file.deleteOnExit(); |
57 |
|
58 |
FileOutputStream fos = null; |
59 |
try { |
60 |
fos = new FileOutputStream(file); |
61 |
AttachmentUtil.downloadAttachment(attachment, fos, monitor); |
62 |
fos.close(); |
63 |
fos = null; |
64 |
} catch (IOException e) { |
65 |
closeAndDelete(fos, file); |
66 |
fos = null; |
67 |
return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Failed to download attachment", e); |
68 |
} catch (CoreException e) { |
69 |
closeAndDelete(fos, file); |
70 |
fos = null; |
71 |
return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Failed to download attachment", e); |
72 |
} finally { |
73 |
if (fos != null) { |
74 |
try { |
75 |
fos.close(); |
76 |
} catch (IOException e) { |
77 |
// ignore ... something bad is happening here |
78 |
} |
79 |
} |
80 |
} |
81 |
|
82 |
Display disp = page.getWorkbenchWindow().getWorkbench().getDisplay(); |
83 |
if (disp.isDisposed()) { |
84 |
return new Status(IStatus.WARNING, TasksUiPlugin.ID_PLUGIN, "Display is disposed"); |
85 |
} |
86 |
|
87 |
if (disp.getThread() == Thread.currentThread()) { |
88 |
try { |
89 |
page.openEditor(new FileEditorInput(file, attachmentFilename, attachment.getUrl()), editorID); |
90 |
} catch (PartInitException e) { |
91 |
return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Cannot open editor", e); |
92 |
} |
93 |
|
94 |
return new Status(IStatus.OK, TasksUiPlugin.ID_PLUGIN, "OK"); |
95 |
} else { |
96 |
final AtomicReference<IStatus> status = new AtomicReference<IStatus>(); |
97 |
final File tmpFile = file; |
98 |
|
99 |
disp.syncExec(new Runnable() { |
100 |
public void run() { |
101 |
try { |
102 |
page.openEditor(new FileEditorInput(tmpFile, attachmentFilename, attachment.getUrl()), editorID); |
103 |
status.set(new Status(IStatus.OK, TasksUiPlugin.ID_PLUGIN, "OK")); |
104 |
} catch (PartInitException e) { |
105 |
status.set(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Cannot open editor", e)); |
106 |
} |
107 |
}; |
108 |
}); |
109 |
|
110 |
if (status.get() == null) { |
111 |
return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Cannot open editor"); |
112 |
} |
113 |
|
114 |
return status.get(); |
115 |
} |
116 |
} |
117 |
|
118 |
void closeAndDelete(OutputStream os, File file) { |
119 |
if (os != null) { |
120 |
try { |
121 |
os.close(); |
122 |
} catch (IOException ex) { |
123 |
// ignore ... we are going to delete the file anyway |
124 |
} |
125 |
} |
126 |
|
127 |
if (file == null) { |
128 |
return; |
129 |
} |
130 |
|
131 |
file.delete(); |
132 |
} |
133 |
} |