1 /**********************************************************************
2 * Copyright (c) 2012 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
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.handlers
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.commands
.ExecutionEvent
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.resources
.IFolder
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.core
.runtime
.jobs
.Job
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
29 import org
.eclipse
.jface
.window
.Window
;
30 import org
.eclipse
.linuxtools
.internal
.lttng2
.core
.control
.model
.TraceSessionState
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.ControlView
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.IImportDialog
;
34 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.ImportFileInfo
;
35 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
.TraceControlDialogFactory
;
36 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
37 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.impl
.TraceSessionComponent
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
39 import org
.eclipse
.rse
.services
.clientserver
.messages
.SystemMessageException
;
40 import org
.eclipse
.rse
.services
.files
.IFileService
;
41 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFile
;
42 import org
.eclipse
.rse
.subsystems
.files
.core
.subsystems
.IRemoteFileSubSystem
;
43 import org
.eclipse
.ui
.IWorkbenchPage
;
44 import org
.eclipse
.ui
.IWorkbenchWindow
;
45 import org
.eclipse
.ui
.PlatformUI
;
49 * Command handler implementation to import traces from a (remote) session to a tracing project.
52 * @author Bernd Hufmann
54 public class ImportHandler
extends BaseControlViewHandler
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
59 protected CommandParameter fParam
;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
71 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
74 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
76 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
84 final CommandParameter param
= fParam
.clone();
86 final IImportDialog dialog
= TraceControlDialogFactory
.getInstance().getImportDialog();
87 dialog
.setSession(param
.getSession());
89 if (dialog
.open() != Window
.OK
) {
93 Job job
= new Job(Messages
.TraceControl_ImportJob
) {
95 protected IStatus
run(IProgressMonitor monitor
) {
97 List
<ImportFileInfo
> traces
= dialog
.getTracePathes();
98 IProject project
= dialog
.getProject();
100 for (Iterator
<ImportFileInfo
> iterator
= traces
.iterator(); iterator
.hasNext();) {
101 ImportFileInfo remoteFile
= (ImportFileInfo
) iterator
.next();
102 downloadTrace(remoteFile
, project
);
105 } catch (ExecutionException e
) {
106 return new Status(Status
.ERROR
, Activator
.PLUGIN_ID
, Messages
.TraceControl_ImportFailure
, e
);
108 return Status
.OK_STATUS
;
121 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
124 public boolean isEnabled() {
125 // Get workbench page for the Control View
126 IWorkbenchPage page
= getWorkbenchPage();
131 // Check if one or more session are selected
132 ISelection selection
= page
.getSelection(ControlView
.ID
);
133 TraceSessionComponent session
= null;
134 if (selection
instanceof StructuredSelection
) {
135 StructuredSelection structered
= ((StructuredSelection
) selection
);
136 for (Iterator
<?
> iterator
= structered
.iterator(); iterator
.hasNext();) {
137 Object element
= (Object
) iterator
.next();
138 if (element
instanceof TraceSessionComponent
) {
139 // Add only TraceSessionComponents that are inactive and not destroyed
140 TraceSessionComponent tmpSession
= (TraceSessionComponent
) element
;
141 if ((tmpSession
.getSessionState() == TraceSessionState
.INACTIVE
) && (!tmpSession
.isDestroyed())) {
142 session
= tmpSession
;
147 boolean isEnabled
= session
!= null;
153 fParam
= new CommandParameter(session
);
161 // ------------------------------------------------------------------------
163 // ------------------------------------------------------------------------
165 * Downloads a trace from the remote host to the given project.
166 * @param trace - trace information of trace to import
167 * @param project - project to import to
168 * @throws ExecutionException
170 private void downloadTrace(ImportFileInfo trace
, IProject project
) throws ExecutionException
{
172 IRemoteFileSubSystem fsss
= trace
.getImportFile().getParentRemoteFileSubSystem();
174 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
175 if (!traceFolder
.exists()) {
176 throw new ExecutionException(Messages
.TraceControl_ImportDialogInvalidTracingProject
+ " (" + TmfTraceFolder
.TRACE_FOLDER_NAME
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
179 String traceName
= trace
.getLocalTraceName();
180 IFolder folder
= traceFolder
.getFolder(traceName
);
181 if (folder
.exists()) {
182 if(!trace
.isOverwrite()) {
183 throw new ExecutionException(Messages
.TraceControl_ImportDialogTraceAlreadyExistError
+ ": " + traceName
); //$NON-NLS-1$
186 folder
.create(true, true, null);
189 IRemoteFile
[] sources
= fsss
.list(trace
.getImportFile(), IFileService
.FILE_TYPE_FILES
, new NullProgressMonitor());
191 String
[] destinations
= new String
[sources
.length
];
192 String
[] encodings
= new String
[sources
.length
];
193 for (int i
= 0; i
< sources
.length
; i
++) {
194 destinations
[i
] = folder
.getLocation().addTrailingSeparator().append(sources
[i
].getName()).toString();
198 fsss
.downloadMultiple(sources
, destinations
, encodings
, new NullProgressMonitor());
200 } catch (SystemMessageException e
) {
201 throw new ExecutionException(e
.toString(), e
);
202 } catch (CoreException e
) {
203 throw new ExecutionException(e
.toString(), e
);