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