Commit | Line | Data |
---|---|---|
e8d771d5 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | * | |
12 | *******************************************************************************/ | |
13 | package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions; | |
14 | ||
15 | import java.io.File; | |
16 | import java.lang.reflect.InvocationTargetException; | |
17 | import java.util.ArrayList; | |
18 | import java.util.Iterator; | |
19 | import java.util.List; | |
20 | ||
21 | import org.eclipse.core.resources.IFolder; | |
22 | import org.eclipse.core.resources.IProject; | |
23 | import org.eclipse.core.resources.IResource; | |
24 | import org.eclipse.core.runtime.CoreException; | |
25 | import org.eclipse.core.runtime.IPath; | |
26 | import org.eclipse.core.runtime.NullProgressMonitor; | |
27 | import org.eclipse.jface.action.IAction; | |
28 | import org.eclipse.jface.dialogs.Dialog; | |
29 | import org.eclipse.jface.dialogs.MessageDialog; | |
30 | import org.eclipse.jface.viewers.ISelection; | |
31 | import org.eclipse.jface.viewers.IStructuredSelection; | |
6c13869b | 32 | import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource; |
e8d771d5 BH |
33 | import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages; |
34 | import org.eclipse.linuxtools.lttng.ui.tracecontrol.dialogs.ImportTraceDialog; | |
35 | import org.eclipse.rse.core.RSECorePlugin; | |
36 | import org.eclipse.rse.core.model.IHost; | |
37 | import org.eclipse.rse.core.subsystems.ISubSystem; | |
38 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; | |
39 | import org.eclipse.rse.services.files.IFileService; | |
40 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.FileServiceSubSystem; | |
41 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; | |
42 | import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; | |
43 | import org.eclipse.rse.ui.SystemBasePlugin; | |
44 | import org.eclipse.swt.widgets.Shell; | |
45 | import org.eclipse.ui.IObjectActionDelegate; | |
46 | import org.eclipse.ui.IViewActionDelegate; | |
47 | import org.eclipse.ui.IViewPart; | |
48 | import org.eclipse.ui.IWorkbenchPart; | |
49 | import org.eclipse.ui.IWorkbenchWindow; | |
50 | import org.eclipse.ui.IWorkbenchWindowActionDelegate; | |
51 | import org.eclipse.ui.dialogs.IOverwriteQuery; | |
52 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; | |
53 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
54 | ||
55 | /** | |
56 | * <b><u>ImportToProject</u></b> | |
57 | * <p> | |
58 | * Action implementation to import a trace to a LTTng project. | |
59 | * </p> | |
60 | */ | |
61 | public class ImportToProject implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate { | |
62 | ||
63 | public static final String TRACE_FOLDER_NAME = "Traces"; //$NON-NLS-1$ | |
64 | ||
65 | // ------------------------------------------------------------------------ | |
66 | // Attributes | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | private List<TraceResource> fSelectedTraces; | |
70 | ||
71 | // ------------------------------------------------------------------------ | |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | public ImportToProject() { | |
76 | fSelectedTraces = new ArrayList<TraceResource>(); | |
77 | } | |
78 | ||
79 | // ------------------------------------------------------------------------ | |
80 | // Operations | |
81 | // ------------------------------------------------------------------------ | |
82 | ||
83 | /* | |
84 | * (non-Javadoc) | |
85 | * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart) | |
86 | */ | |
87 | @Override | |
88 | public void setActivePart(IAction arg0, IWorkbenchPart arg1) { | |
89 | } | |
90 | ||
91 | /** | |
92 | * Gets the first selected trace. | |
93 | * @return first selected trace | |
94 | */ | |
95 | protected TraceResource getFirstSelectedTrace() { | |
96 | if (fSelectedTraces.size() > 0) { | |
97 | return (TraceResource) fSelectedTraces.get(0); | |
98 | } | |
99 | return null; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Gets the trace SubSystem for the selected trace. | |
104 | * | |
105 | * @return trace SubSystem | |
106 | */ | |
107 | protected ISubSystem getSubSystem() { | |
108 | return getFirstSelectedTrace().getSubSystem(); | |
109 | } | |
110 | ||
111 | /* | |
112 | * (non-Javadoc) | |
113 | * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) | |
114 | */ | |
115 | @Override | |
116 | public void run(IAction arg0) { | |
117 | for (int i = 0; i < fSelectedTraces.size(); i++) { | |
118 | ||
119 | final TraceResource trace = (TraceResource) fSelectedTraces.get(i); | |
120 | ImportTraceDialog dialog = new ImportTraceDialog(getShell(), trace); | |
121 | int result = dialog.open(); | |
122 | if (result == Dialog.OK) { | |
123 | IProject project = dialog.getProject(); | |
124 | String traceName = dialog.getTraceName(); | |
125 | if (trace.getTraceConfig().isNetworkTrace()) { | |
126 | if (dialog.getLinkOnly()) { | |
a79913eb | 127 | linkTrace(getShell(), trace, project, traceName); |
e8d771d5 BH |
128 | } else { |
129 | copyTrace(trace, project, traceName); | |
130 | } | |
131 | } else { | |
132 | downloadTrace(trace, project, traceName); | |
133 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | /* | |
139 | * method to download a trace from the remote system. | |
140 | */ | |
141 | private void downloadTrace(TraceResource trace, IProject project, String traceName) { | |
142 | try { | |
143 | IHost host = trace.getSubSystem().getHost(); | |
144 | ISubSystem[] sss = RSECorePlugin.getTheSystemRegistry().getSubsystems(host, IFileServiceSubSystem.class); | |
145 | if (sss.length == 0 || !(sss[0] instanceof FileServiceSubSystem)) { | |
146 | MessageDialog.openWarning(getShell(), | |
147 | Messages.ImportToProject_ImportFailed, | |
148 | Messages.ImportToProject_NoFileServiceSubsystem); | |
149 | return; | |
150 | } | |
151 | FileServiceSubSystem fsss = (FileServiceSubSystem) sss[0]; | |
152 | ||
153 | IFolder traceFolder = project.getFolder(TRACE_FOLDER_NAME); | |
154 | if (!traceFolder.exists()) { | |
155 | MessageDialog.openWarning(getShell(), | |
156 | Messages.ImportToProject_ImportFailed, | |
157 | Messages.ImportToProject_NoProjectTraceFolder); | |
158 | return; | |
159 | } | |
160 | ||
161 | IRemoteFile remoteFolder = fsss.getRemoteFileObject(trace.getTraceConfig().getTracePath(), new NullProgressMonitor()); | |
162 | if (remoteFolder == null || !remoteFolder.exists()) { | |
163 | MessageDialog.openWarning(getShell(), | |
164 | Messages.ImportToProject_ImportFailed, | |
165 | Messages.ImportToProject_NoRemoteTraceFolder); | |
166 | return; | |
167 | } | |
168 | ||
169 | IFolder folder = traceFolder.getFolder(traceName); | |
170 | if (folder.exists()) { | |
171 | MessageDialog.openWarning(getShell(), | |
172 | Messages.ImportToProject_ImportFailed, | |
173 | Messages.ImportToProject_AlreadyExists); | |
174 | return; | |
175 | } else { | |
176 | folder.create(true, true, null); | |
177 | } | |
178 | ||
179 | IRemoteFile[] sources = fsss.list(remoteFolder, IFileService.FILE_TYPE_FILES, new NullProgressMonitor()); | |
180 | ||
181 | String[] destinations = new String[sources.length]; | |
182 | String[] encodings = new String[sources.length]; | |
183 | for (int i = 0; i < sources.length; i++) { | |
184 | destinations[i] = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString(); | |
185 | encodings[i] = null; | |
186 | } | |
187 | ||
188 | fsss.downloadMultiple(sources, destinations, encodings, new NullProgressMonitor()); | |
189 | ||
190 | } catch (SystemMessageException e) { | |
191 | MessageDialog.openWarning(getShell(), | |
192 | Messages.ImportToProject_ImportFailed, | |
193 | e.getMessage()); | |
194 | } catch (CoreException e) { | |
195 | MessageDialog.openWarning(getShell(), | |
196 | Messages.ImportToProject_ImportFailed, | |
197 | e.getMessage()); | |
198 | } | |
199 | } | |
200 | ||
201 | /* | |
202 | * Method to copy a trace residing on the local host. | |
203 | */ | |
204 | private void copyTrace(TraceResource trace, IProject project, String traceName) { | |
205 | IFolder traceFolder = project.getFolder(TRACE_FOLDER_NAME); | |
206 | if (!traceFolder.exists()) { | |
207 | MessageDialog.openWarning(getShell(), | |
208 | Messages.ImportToProject_ImportFailed, | |
209 | Messages.ImportToProject_NoProjectTraceFolder); | |
210 | return; | |
211 | } | |
212 | ||
213 | IPath containerPath = traceFolder.getFullPath().addTrailingSeparator().append(traceName); | |
214 | ||
215 | File sourceFolder = new File(trace.getTraceConfig().getTracePath()); | |
216 | ||
217 | IOverwriteQuery overriteImplementor = new IOverwriteQuery(){ | |
218 | @Override | |
219 | public String queryOverwrite(String pathString) { | |
220 | MessageDialog.openWarning(getShell(), | |
221 | Messages.ImportToProject_ImportFailed, | |
222 | Messages.ImportToProject_AlreadyExists); | |
223 | return IOverwriteQuery.NO_ALL; | |
224 | }}; | |
225 | ||
226 | ImportOperation operation = new ImportOperation( | |
227 | containerPath, | |
228 | sourceFolder, | |
229 | FileSystemStructureProvider.INSTANCE, | |
230 | overriteImplementor); | |
231 | operation.setCreateContainerStructure(false); | |
232 | ||
233 | try { | |
234 | operation.run(new NullProgressMonitor()); | |
235 | } catch (InvocationTargetException e) { | |
236 | MessageDialog.openWarning(getShell(), | |
237 | Messages.ImportToProject_ImportFailed, | |
238 | e.getMessage()); | |
239 | } catch (InterruptedException e) { | |
240 | MessageDialog.openWarning(getShell(), | |
241 | Messages.ImportToProject_ImportFailed, | |
242 | e.getMessage()); | |
243 | } | |
244 | } | |
245 | ||
246 | /* | |
247 | * Method to create a symbolic link to a trace residing on the local host. | |
248 | */ | |
a79913eb | 249 | public static void linkTrace(Shell shell, TraceResource trace, IProject project, String traceName) { |
e8d771d5 BH |
250 | IFolder traceFolder = project.getFolder(TRACE_FOLDER_NAME); |
251 | if (!traceFolder.exists()) { | |
a79913eb | 252 | MessageDialog.openWarning(shell, |
e8d771d5 BH |
253 | Messages.ImportToProject_ImportFailed, |
254 | Messages.ImportToProject_NoProjectTraceFolder); | |
255 | return; | |
256 | } | |
257 | ||
258 | IFolder folder = traceFolder.getFolder(traceName); | |
259 | if (folder.exists()) { | |
a79913eb | 260 | MessageDialog.openWarning(shell, |
e8d771d5 BH |
261 | Messages.ImportToProject_ImportFailed, |
262 | Messages.ImportToProject_AlreadyExists); | |
263 | return; | |
264 | } | |
265 | ||
266 | File sourceFolder = new File(trace.getTraceConfig().getTracePath()); | |
267 | ||
268 | try { | |
269 | folder.createLink(sourceFolder.toURI(), IResource.REPLACE, null); | |
270 | } catch (CoreException e) { | |
a79913eb | 271 | MessageDialog.openWarning(shell, |
e8d771d5 BH |
272 | Messages.ImportToProject_ImportFailed, |
273 | e.getMessage()); | |
274 | } | |
275 | } | |
276 | ||
277 | /* | |
278 | * (non-Javadoc) | |
279 | * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) | |
280 | */ | |
281 | @SuppressWarnings("unchecked") | |
282 | @Override | |
283 | public void selectionChanged(IAction action, ISelection selection) { | |
284 | if (selection instanceof IStructuredSelection) { | |
285 | fSelectedTraces.clear(); | |
286 | // store the selected targets to be used when running | |
287 | Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator(); | |
288 | while (theSet.hasNext()) { | |
289 | Object obj = theSet.next(); | |
290 | if (obj instanceof TraceResource) { | |
291 | fSelectedTraces.add((TraceResource)obj); | |
292 | } | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | /** | |
298 | * Set selected traces | |
299 | * @param traces | |
300 | */ | |
301 | public void setSelectedTraces(List<TraceResource> traces) { | |
302 | fSelectedTraces = traces; | |
303 | } | |
304 | ||
305 | ||
306 | /** | |
307 | * Returns the active workbench shell of this plug-in. | |
308 | * | |
309 | * @return active workbench shell. | |
310 | */ | |
311 | protected Shell getShell() { | |
312 | return SystemBasePlugin.getActiveWorkbenchShell(); | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * (non-Javadoc) | |
318 | * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) | |
319 | */ | |
320 | @Override | |
321 | public void init(IWorkbenchWindow window) { | |
322 | } | |
323 | ||
324 | /* | |
325 | * (non-Javadoc) | |
326 | * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() | |
327 | */ | |
328 | @Override | |
329 | public void dispose() { | |
330 | } | |
331 | ||
332 | /* | |
333 | * (non-Javadoc) | |
334 | * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart) | |
335 | */ | |
336 | @Override | |
337 | public void init(IViewPart view) { | |
338 | } | |
339 | } |