1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
12 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng
.ui
.tracecontrol
.actions
;
16 import java
.lang
.reflect
.InvocationTargetException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
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
;
32 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.tracecontrol
.model
.TraceResource
;
33 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.Activator
;
34 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.tracecontrol
.Messages
;
35 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.tracecontrol
.dialogs
.ImportTraceDialog
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
37 import org
.eclipse
.rse
.core
.RSECorePlugin
;
38 import org
.eclipse
.rse
.core
.model
.IHost
;
39 import org
.eclipse
.rse
.core
.subsystems
.ISubSystem
;
40 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
41 import org
.eclipse
.rse
.services
.files
.IFileService
;
42 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.FileServiceSubSystem
;
43 import org
.eclipse
.rse
.subsystems
.files
.core
.servicesubsystem
.IFileServiceSubSystem
;
44 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
45 import org
.eclipse
.rse
.ui
.SystemBasePlugin
;
46 import org
.eclipse
.swt
.widgets
.Shell
;
47 import org
.eclipse
.ui
.IObjectActionDelegate
;
48 import org
.eclipse
.ui
.IViewActionDelegate
;
49 import org
.eclipse
.ui
.IViewPart
;
50 import org
.eclipse
.ui
.IWorkbenchPart
;
51 import org
.eclipse
.ui
.IWorkbenchWindow
;
52 import org
.eclipse
.ui
.IWorkbenchWindowActionDelegate
;
53 import org
.eclipse
.ui
.dialogs
.IOverwriteQuery
;
54 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
55 import org
.eclipse
.ui
.wizards
.datatransfer
.ImportOperation
;
58 * <b><u>ImportToProject</u></b>
60 * Action implementation to import a trace to a LTTng project.
63 public class ImportToProject
implements IObjectActionDelegate
, IWorkbenchWindowActionDelegate
, IViewActionDelegate
{
65 public static final String TRACE_FOLDER_NAME
= "Traces"; //$NON-NLS-1$
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 private List
<TraceResource
> fSelectedTraces
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
77 public ImportToProject() {
78 fSelectedTraces
= new ArrayList
<TraceResource
>();
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
87 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
90 public void setActivePart(IAction arg0
, IWorkbenchPart arg1
) {
94 * Gets the first selected trace.
95 * @return first selected trace
97 protected TraceResource
getFirstSelectedTrace() {
98 if (fSelectedTraces
.size() > 0) {
99 return (TraceResource
) fSelectedTraces
.get(0);
105 * Gets the trace SubSystem for the selected trace.
107 * @return trace SubSystem
109 protected ISubSystem
getSubSystem() {
110 return getFirstSelectedTrace().getSubSystem();
115 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
118 public void run(IAction arg0
) {
119 for (int i
= 0; i
< fSelectedTraces
.size(); i
++) {
121 final TraceResource trace
= (TraceResource
) fSelectedTraces
.get(i
);
122 ImportTraceDialog dialog
= new ImportTraceDialog(getShell(), trace
);
123 int result
= dialog
.open();
124 if (result
== Dialog
.OK
) {
125 IProject project
= dialog
.getProject();
126 String traceName
= dialog
.getTraceName();
127 if (trace
.getTraceConfig().isNetworkTrace()) {
128 if (dialog
.getLinkOnly()) {
129 linkTrace(getShell(), trace
, project
, traceName
);
131 copyTrace(trace
, project
, traceName
);
134 downloadTrace(trace
, project
, traceName
);
141 * method to download a trace from the remote system.
143 private void downloadTrace(TraceResource trace
, IProject project
, String traceName
) {
145 IHost host
= trace
.getSubSystem().getHost();
146 ISubSystem
[] sss
= RSECorePlugin
.getTheSystemRegistry().getSubsystems(host
, IFileServiceSubSystem
.class);
147 if (sss
.length
== 0 || !(sss
[0] instanceof FileServiceSubSystem
)) {
148 MessageDialog
.openWarning(getShell(),
149 Messages
.ImportToProject_ImportFailed
,
150 Messages
.ImportToProject_NoFileServiceSubsystem
);
153 FileServiceSubSystem fsss
= (FileServiceSubSystem
) sss
[0];
155 IFolder traceFolder
= project
.getFolder(TRACE_FOLDER_NAME
);
156 if (!traceFolder
.exists()) {
157 MessageDialog
.openWarning(getShell(),
158 Messages
.ImportToProject_ImportFailed
,
159 Messages
.ImportToProject_NoProjectTraceFolder
);
163 IRemoteFile remoteFolder
= fsss
.getRemoteFileObject(trace
.getTraceConfig().getTracePath(), new NullProgressMonitor());
164 if (remoteFolder
== null || !remoteFolder
.exists()) {
165 MessageDialog
.openWarning(getShell(),
166 Messages
.ImportToProject_ImportFailed
,
167 Messages
.ImportToProject_NoRemoteTraceFolder
);
171 IFolder folder
= traceFolder
.getFolder(traceName
);
172 if (folder
.exists()) {
173 MessageDialog
.openWarning(getShell(),
174 Messages
.ImportToProject_ImportFailed
,
175 Messages
.ImportToProject_AlreadyExists
);
178 folder
.create(true, true, null);
181 IRemoteFile
[] sources
= fsss
.list(remoteFolder
, IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
183 String
[] destinations
= new String
[sources
.length
];
184 String
[] encodings
= new String
[sources
.length
];
185 for (int i
= 0; i
< sources
.length
; i
++) {
186 destinations
[i
] = folder
.getLocation().addTrailingSeparator().append(sources
[i
].getName()).toString();
190 fsss
.downloadMultiple(sources
, destinations
, encodings
, new NullProgressMonitor());
192 } catch (SystemMessageException e
) {
193 MessageDialog
.openWarning(getShell(),
194 Messages
.ImportToProject_ImportFailed
,
196 } catch (CoreException e
) {
197 MessageDialog
.openWarning(getShell(),
198 Messages
.ImportToProject_ImportFailed
,
204 * Method to copy a trace residing on the local host.
206 private void copyTrace(TraceResource trace
, IProject project
, String traceName
) {
207 IFolder traceFolder
= project
.getFolder(TRACE_FOLDER_NAME
);
208 if (!traceFolder
.exists()) {
209 MessageDialog
.openWarning(getShell(),
210 Messages
.ImportToProject_ImportFailed
,
211 Messages
.ImportToProject_NoProjectTraceFolder
);
215 IPath containerPath
= traceFolder
.getFullPath().addTrailingSeparator().append(traceName
);
217 File sourceFolder
= new File(trace
.getTraceConfig().getTracePath());
219 IOverwriteQuery overriteImplementor
= new IOverwriteQuery(){
221 public String
queryOverwrite(String pathString
) {
222 MessageDialog
.openWarning(getShell(),
223 Messages
.ImportToProject_ImportFailed
,
224 Messages
.ImportToProject_AlreadyExists
);
225 return IOverwriteQuery
.NO_ALL
;
228 ImportOperation operation
= new ImportOperation(
231 FileSystemStructureProvider
.INSTANCE
,
232 overriteImplementor
);
233 operation
.setCreateContainerStructure(false);
236 operation
.run(new NullProgressMonitor());
237 } catch (InvocationTargetException e
) {
238 MessageDialog
.openWarning(getShell(),
239 Messages
.ImportToProject_ImportFailed
,
241 } catch (InterruptedException e
) {
242 MessageDialog
.openWarning(getShell(),
243 Messages
.ImportToProject_ImportFailed
,
249 * Method to create a symbolic link to a trace residing on the local host.
251 public static void linkTrace(Shell shell
, TraceResource trace
, IProject project
, String traceName
) {
252 IFolder traceFolder
= project
.getFolder(TRACE_FOLDER_NAME
);
253 if (!traceFolder
.exists()) {
254 MessageDialog
.openWarning(shell
,
255 Messages
.ImportToProject_ImportFailed
,
256 Messages
.ImportToProject_NoProjectTraceFolder
);
260 IFolder folder
= traceFolder
.getFolder(traceName
);
261 if (folder
.exists()) {
262 MessageDialog
.openWarning(shell
,
263 Messages
.ImportToProject_ImportFailed
,
264 Messages
.ImportToProject_AlreadyExists
);
268 File sourceFolder
= new File(trace
.getTraceConfig().getTracePath());
271 folder
.createLink(sourceFolder
.toURI(), IResource
.REPLACE
, null);
272 // Set the trace properties for this resource
273 // FIXME: update from extension point properties
274 folder
.setPersistentProperty(TmfTraceElement
.TRACEBUNDLE
, Activator
.PLUGIN_ID
);
275 folder
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, "org.eclipse.linuxtools.lttng.tracetype.kernel"); //$NON-NLS-1$
276 folder
.setPersistentProperty(TmfTraceElement
.TRACEICON
, "icons/obj16/tux2.png"); //$NON-NLS-1$
277 } catch (CoreException e
) {
278 MessageDialog
.openWarning(shell
,
279 Messages
.ImportToProject_ImportFailed
,
286 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
288 @SuppressWarnings("unchecked")
290 public void selectionChanged(IAction action
, ISelection selection
) {
291 if (selection
instanceof IStructuredSelection
) {
292 fSelectedTraces
.clear();
293 // store the selected targets to be used when running
294 Iterator
<IStructuredSelection
> theSet
= ((IStructuredSelection
) selection
).iterator();
295 while (theSet
.hasNext()) {
296 Object obj
= theSet
.next();
297 if (obj
instanceof TraceResource
) {
298 fSelectedTraces
.add((TraceResource
)obj
);
305 * Set selected traces
308 public void setSelectedTraces(List
<TraceResource
> traces
) {
309 fSelectedTraces
= traces
;
314 * Returns the active workbench shell of this plug-in.
316 * @return active workbench shell.
318 protected Shell
getShell() {
319 return SystemBasePlugin
.getActiveWorkbenchShell();
325 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
328 public void init(IWorkbenchWindow window
) {
333 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
336 public void dispose() {
341 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
344 public void init(IViewPart view
) {