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 |
} |