tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / commands / OpenFileHandler.java
1 /**********************************************************************
2 * Copyright (c) 2013, 2014 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 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 **********************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.ui.commands;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
24 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
25 import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper;
26 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
27 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
28 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
29 import org.eclipse.swt.widgets.FileDialog;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.handlers.HandlerUtil;
33
34 /**
35 * Open file handler, used to open files (not directories)
36 *
37 * @author Matthew Khouzam
38 */
39 public class OpenFileHandler extends AbstractHandler {
40
41 @Override
42 public Object execute(ExecutionEvent event) {
43
44 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
45 // Menu Selection is not null for context-sensitive menu
46 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
47
48 // Get trace path
49 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
50 FileDialog fd = new FileDialog(shell);
51 fd.setText(Messages.OpenFileHandler_SelectTraceFile);
52 String filePath = fd.open();
53 if (filePath == null) {
54 return null;
55 }
56
57 try {
58
59 TmfTraceFolder destinationFolder;
60
61 if ((menuSelection != null) && (currentSelection instanceof IStructuredSelection)) {
62 // If handler is called from the context sensitive menu of a tracing project import to
63 // the traces folder from this project
64 destinationFolder = TmfHandlerUtil.getTraceFolderFromSelection(currentSelection);
65 } else {
66 // If handler is called from file menu import into default tracing project
67 IProject project = TmfProjectRegistry.createProject(
68 TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor());
69 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
70 destinationFolder = projectElement.getTracesFolder();
71 }
72
73 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, filePath, shell);
74 } catch (CoreException e) {
75 Activator.getDefault().logError(e.getMessage(), e);
76 }
77 return null;
78 }
79 }
This page took 0.033094 seconds and 5 git commands to generate.