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