Commit | Line | Data |
---|---|---|
291cbdbf BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 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 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers; | |
13 | ||
14 | import java.util.Iterator; | |
15 | import java.util.List; | |
16 | ||
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.ui.Activator; | |
31 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; | |
32 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages; | |
291cbdbf | 33 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IImportDialog; |
f455db37 | 34 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.ImportFileInfo; |
291cbdbf BH |
35 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.TraceControlDialogFactory; |
36 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceSessionState; | |
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; | |
46 | ||
47 | /** | |
48 | * <b><u>ImportHandler</u></b> | |
49 | * <p> | |
50 | * Command handler implementation to import traces from a (remote) session to a tracing project. | |
51 | * </p> | |
52 | */ | |
53 | public class ImportHandler extends BaseControlViewHandler { | |
54 | ||
55 | // ------------------------------------------------------------------------ | |
56 | // Attributes | |
57 | // ------------------------------------------------------------------------ | |
58 | protected CommandParameter fParam; | |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Accessors | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Operations | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
68 | /* | |
69 | * (non-Javadoc) | |
70 | * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) | |
71 | */ | |
72 | @Override | |
73 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
74 | ||
75 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
76 | ||
77 | if (window == null) { | |
78 | return false; | |
79 | } | |
80 | ||
81 | fLock.lock(); | |
82 | try { | |
83 | final CommandParameter param = fParam.clone(); | |
84 | ||
85 | final IImportDialog dialog = TraceControlDialogFactory.getInstance().getImportDialog(); | |
86 | dialog.setSession(param.getSession()); | |
87 | ||
88 | if (dialog.open() != Window.OK) { | |
89 | return null; | |
90 | } | |
91 | ||
92 | Job job = new Job(Messages.TraceControl_ImportJob) { | |
93 | @Override | |
94 | protected IStatus run(IProgressMonitor monitor) { | |
95 | try { | |
96 | List<ImportFileInfo> traces = dialog.getTracePathes(); | |
97 | IProject project = dialog.getProject(); | |
98 | ||
99 | for (Iterator<ImportFileInfo> iterator = traces.iterator(); iterator.hasNext();) { | |
100 | ImportFileInfo remoteFile = (ImportFileInfo) iterator.next(); | |
101 | downloadTrace(remoteFile, project); | |
102 | } | |
103 | ||
104 | } catch (ExecutionException e) { | |
f455db37 | 105 | return new Status(Status.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ImportFailure, e); |
291cbdbf BH |
106 | } |
107 | return Status.OK_STATUS; | |
108 | } | |
109 | }; | |
110 | job.setUser(true); | |
111 | job.schedule(); | |
112 | } finally { | |
113 | fLock.unlock(); | |
114 | } | |
115 | return null; | |
116 | } | |
117 | ||
118 | /* | |
119 | * (non-Javadoc) | |
120 | * @see org.eclipse.core.commands.AbstractHandler#isEnabled() | |
121 | */ | |
122 | @Override | |
123 | public boolean isEnabled() { | |
124 | // Get workbench page for the Control View | |
125 | IWorkbenchPage page = getWorkbenchPage(); | |
126 | if (page == null) { | |
127 | return false; | |
128 | } | |
129 | ||
130 | // Check if one or more session are selected | |
131 | ISelection selection = page.getSelection(ControlView.ID); | |
132 | TraceSessionComponent session = null; | |
133 | if (selection instanceof StructuredSelection) { | |
134 | StructuredSelection structered = ((StructuredSelection) selection); | |
135 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { | |
136 | Object element = (Object) iterator.next(); | |
137 | if (element instanceof TraceSessionComponent) { | |
138 | // Add only TraceSessionComponents that are inactive and not destroyed | |
139 | TraceSessionComponent tmpSession = (TraceSessionComponent) element; | |
140 | if ((tmpSession.getSessionState() == TraceSessionState.INACTIVE) && (!tmpSession.isDestroyed())) { | |
141 | session = tmpSession; | |
142 | } | |
143 | } | |
144 | } | |
145 | } | |
146 | boolean isEnabled = session != null; | |
147 | ||
148 | fLock.lock(); | |
149 | try { | |
150 | fParam = null; | |
151 | if (isEnabled) { | |
152 | fParam = new CommandParameter(session); | |
153 | } | |
154 | } finally { | |
155 | fLock.unlock(); | |
156 | } | |
157 | return isEnabled; | |
158 | } | |
159 | ||
160 | // ------------------------------------------------------------------------ | |
161 | // Helper methods | |
162 | // ------------------------------------------------------------------------ | |
163 | /** | |
164 | * Downloads a trace from the remote host to the given project. | |
165 | * @param trace - trace information of trace to import | |
166 | * @param project - project to import to | |
167 | * @throws ExecutionException | |
168 | */ | |
169 | private void downloadTrace(ImportFileInfo trace, IProject project) throws ExecutionException { | |
170 | try { | |
171 | IRemoteFileSubSystem fsss = trace.getImportFile().getParentRemoteFileSubSystem(); | |
172 | ||
173 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
174 | if (!traceFolder.exists()) { | |
175 | throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTraceFolder.TRACE_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$ | |
176 | } | |
177 | ||
178 | String traceName = trace.getLocalTraceName(); | |
179 | IFolder folder = traceFolder.getFolder(traceName); | |
180 | if (folder.exists()) { | |
181 | if(!trace.isOverwrite()) { | |
182 | throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$ | |
183 | } | |
184 | } else { | |
185 | folder.create(true, true, null); | |
186 | } | |
187 | ||
188 | IRemoteFile[] sources = fsss.list(trace.getImportFile(), IFileService.FILE_TYPE_FILES, new NullProgressMonitor()); | |
189 | ||
190 | String[] destinations = new String[sources.length]; | |
191 | String[] encodings = new String[sources.length]; | |
192 | for (int i = 0; i < sources.length; i++) { | |
193 | destinations[i] = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString(); | |
194 | encodings[i] = null; | |
195 | } | |
196 | ||
197 | fsss.downloadMultiple(sources, destinations, encodings, new NullProgressMonitor()); | |
198 | ||
199 | } catch (SystemMessageException e) { | |
200 | throw new ExecutionException(e.fillInStackTrace().toString()); | |
201 | } catch (CoreException e) { | |
202 | throw new ExecutionException(e.fillInStackTrace().toString()); | |
203 | } | |
204 | } | |
205 | } |