gdbtrace: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / handlers / ImportHandler.java
index 970baa7fa67d100ac23942197198bf413fc4dec4..99ea37c732b6c65d10f7613ee9f3d3bf21cfff80 100644 (file)
@@ -23,49 +23,61 @@ import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.MultiStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.MessageDialogWithToggle;
+import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.window.Window;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.linuxtools.internal.lttng2.control.core.model.TraceSessionState;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
+import org.eclipse.linuxtools.internal.lttng2.control.ui.relayd.LttngRelaydConnectionInfo;
+import org.eclipse.linuxtools.internal.lttng2.control.ui.relayd.LttngRelaydConnectionManager;
+import org.eclipse.linuxtools.internal.lttng2.control.ui.relayd.LttngRelaydConsumer;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.ControlView;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.IImportDialog;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.ImportFileInfo;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
+import org.eclipse.linuxtools.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard;
 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException;
 import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfConstants;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
+import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils;
 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTracesFolder;
 import org.eclipse.linuxtools.tmf.ui.project.model.TraceUtils;
-import org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace.BatchImportTraceWizard;
 import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
 import org.eclipse.rse.services.files.IFileService;
 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
 import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFileSubSystem;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 
 /**
  * <p>
- * Command handler implementation to import traces from a (remote) session to a tracing project.
+ * Command handler implementation to import traces from a (remote) session to a
+ * tracing project.
  * </p>
  *
  * @author Bernd Hufmann
@@ -78,6 +90,9 @@ public class ImportHandler extends BaseControlViewHandler {
     /** Name of default project to import traces to */
     public static final String DEFAULT_REMOTE_PROJECT_NAME = "Remote"; //$NON-NLS-1$
 
+    /** The preference key to remeber whether or not the user wants the notification shown next time **/
+    private static final String NOTIFY_IMPORT_STREAMED_PREF_KEY = "NOTIFY_IMPORT_STREAMED"; //$NON-NLS-1$
+
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
@@ -107,12 +122,22 @@ public class ImportHandler extends BaseControlViewHandler {
             // create default project
             IProject project = TmfProjectRegistry.createProject(DEFAULT_REMOTE_PROJECT_NAME, null, null);
 
-            if (param.getSession().isStreamedTrace()) {
+            if (param.getSession().isLiveTrace()) {
+                importLiveTrace(new LttngRelaydConnectionInfo(param.getSession().getLiveUrl(), param.getSession().getLivePort(), param.getSession().getName()), project);
+                return null;
+            } else if (param.getSession().isStreamedTrace()) {
+
+                IPreferenceStore store = Activator.getDefault().getPreferenceStore();
+                String notify = store.getString(NOTIFY_IMPORT_STREAMED_PREF_KEY);
+                if (!MessageDialogWithToggle.ALWAYS.equals(notify)) {
+                    MessageDialogWithToggle.openInformation(window.getShell(), null, Messages.TraceControl_ImportDialogStreamedTraceNotification, Messages.TraceControl_ImportDialogStreamedTraceNotificationToggle, false, store, NOTIFY_IMPORT_STREAMED_PREF_KEY);
+                }
+
                 // Streamed trace
                 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
                 TmfTraceFolder traceFolder = projectElement.getTracesFolder();
 
-                BatchImportTraceWizard wizard = new BatchImportTraceWizard();
+                ImportTraceWizard wizard = new ImportTraceWizard();
                 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(traceFolder));
                 WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
                 dialog.open();
@@ -156,7 +181,7 @@ public class ImportHandler extends BaseControlViewHandler {
                                 TraceTypeHelper helper = null;
 
                                 try {
-                                    helper = TmfTraceTypeUIUtils.selectTraceType(file.getLocationURI().getPath(), null, null);
+                                    helper = TmfTraceTypeUIUtils.selectTraceType(file.getLocation().toOSString(), null, null);
                                 } catch (TmfTraceImportException e) {
                                     // the trace did not match any trace type
                                 }
@@ -212,9 +237,10 @@ public class ImportHandler extends BaseControlViewHandler {
             for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
                 Object element = iterator.next();
                 if (element instanceof TraceSessionComponent) {
-                    // Add only TraceSessionComponents that are inactive and not destroyed
+                    // Add only TraceSessionComponents that are inactive and not
+                    // destroyed
                     TraceSessionComponent tmpSession = (TraceSessionComponent) element;
-                    if (((tmpSession.isSnapshotSession()) || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
+                    if ((tmpSession.isSnapshotSession() || tmpSession.isLiveTrace() || (tmpSession.getSessionState() == TraceSessionState.INACTIVE)) && (!tmpSession.isDestroyed())) {
                         session = tmpSession;
                     }
                 }
@@ -256,7 +282,7 @@ public class ImportHandler extends BaseControlViewHandler {
 
             IFolder traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
             if (!traceFolder.exists()) {
-                throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTracesFolder.TRACES_FOLDER_NAME + ")");  //$NON-NLS-1$//$NON-NLS-2$
+                throw new ExecutionException(Messages.TraceControl_ImportDialogInvalidTracingProject + " (" + TmfTracesFolder.TRACES_FOLDER_NAME + ")"); //$NON-NLS-1$//$NON-NLS-2$
             }
 
             IFolder destinationFolder = trace.getDestinationFolder();
@@ -265,7 +291,7 @@ public class ImportHandler extends BaseControlViewHandler {
             String traceName = trace.getLocalTraceName();
             IFolder folder = destinationFolder.getFolder(traceName);
             if (folder.exists()) {
-                if(!trace.isOverwrite()) {
+                if (!trace.isOverwrite()) {
                     throw new ExecutionException(Messages.TraceControl_ImportDialogTraceAlreadyExistError + ": " + traceName); //$NON-NLS-1$
                 }
             } else {
@@ -281,8 +307,8 @@ public class ImportHandler extends BaseControlViewHandler {
                     monitor.setCanceled(true);
                     return;
                 }
-                String destination = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toString();
-                subMonitor.setTaskName(Messages.TraceControl_DownloadTask + ' '  + traceName + '/' +sources[i].getName());
+                String destination = folder.getLocation().addTrailingSeparator().append(sources[i].getName()).toOSString();
+                subMonitor.setTaskName(Messages.TraceControl_DownloadTask + ' ' + traceName + '/' + sources[i].getName());
                 fsss.download(sources[i], destination, null, subMonitor.newChild(1));
             }
         } catch (SystemMessageException e) {
@@ -291,4 +317,74 @@ public class ImportHandler extends BaseControlViewHandler {
             throw new ExecutionException(e.toString(), e);
         }
     }
+
+    private static void importLiveTrace(final LttngRelaydConnectionInfo connectionInfo, final IProject project) {
+        Job job = new Job(Messages.TraceControl_ImportJob) {
+
+            @Override
+            protected IStatus run(final IProgressMonitor monitor) {
+                try {
+                    // We initiate the connection first so that we can retrieve the trace path
+                    LttngRelaydConsumer lttngRelaydConsumer = LttngRelaydConnectionManager.getInstance().getConsumer(connectionInfo);
+                    try {
+                        lttngRelaydConsumer.connect();
+                    } catch (CoreException e) {
+                        new Status(IStatus.ERROR, Activator.PLUGIN_ID, org.eclipse.linuxtools.internal.lttng2.control.ui.relayd.Messages.LttngRelaydConnectionManager_ConnectionError, e);
+                    }
+                    initializeTraceResource(connectionInfo, lttngRelaydConsumer.getTracePath(), project);
+                    return Status.OK_STATUS;
+                } catch (CoreException | TmfTraceImportException e) {
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportHandler_LiveTraceInitError, e);
+                }
+            }
+
+        };
+        job.setSystem(true);
+        job.schedule();
+    }
+
+
+    private static void initializeTraceResource(final LttngRelaydConnectionInfo connectionInfo, final String tracePath, final IProject project) throws CoreException, TmfTraceImportException {
+        IFolder folder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
+        IFolder traceFolder = folder.getFolder(connectionInfo.getSessionName());
+        Path location = new Path(tracePath);
+        IStatus result = ResourcesPlugin.getWorkspace().validateLinkLocation(folder, location);
+        if (result.isOK()) {
+            traceFolder.createLink(location, IResource.REPLACE, new NullProgressMonitor());
+        } else {
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, result.getMessage()));
+        }
+
+        TraceTypeHelper selectedTraceType = TmfTraceTypeUIUtils.selectTraceType(location.toOSString(), null, null);
+        // No trace type was determined.
+        TmfTraceTypeUIUtils.setTraceType(traceFolder, selectedTraceType);
+
+        final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
+        final TmfTraceFolder tracesFolder = projectElement.getTracesFolder();
+        final List<TmfTraceElement> traces = tracesFolder.getTraces();
+        TmfTraceElement found = null;
+        for (TmfTraceElement candidate : traces) {
+            if (candidate.getName().equals(connectionInfo.getSessionName())) {
+                found = candidate;
+            }
+        }
+
+        if (found == null) {
+            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportHandler_LiveTraceElementError));
+        }
+
+        // Properties used to be able to reopen a trace in live mode
+        traceFolder.setPersistentProperty(CtfConstants.LIVE_HOST, connectionInfo.getHost());
+        traceFolder.setPersistentProperty(CtfConstants.LIVE_PORT, Integer.toString(connectionInfo.getPort()));
+        traceFolder.setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, connectionInfo.getSessionName());
+
+        final TmfTraceElement finalTrace = found;
+        Display.getDefault().syncExec(new Runnable() {
+
+            @Override
+            public void run() {
+                TmfOpenTraceHelper.openTraceFromElement(finalTrace);
+            }
+        });
+    }
 }
This page took 0.032532 seconds and 5 git commands to generate.