eb94b9a0ae7f6e1effec9f6789a278a31a5dec4e
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / 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.tracecompass.internal.tmf.ui.commands;
15
16 import java.io.File;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.NullProgressMonitor;
23 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24 import org.eclipse.core.runtime.preferences.InstanceScope;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.swt.widgets.FileDialog;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
30 import org.eclipse.tracecompass.internal.tmf.ui.ITmfUIPreferences;
31 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
32 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
33 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
34 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
35 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.handlers.HandlerUtil;
38
39 /**
40 * Open file handler, used to open files (not directories)
41 *
42 * @author Matthew Khouzam
43 */
44 public class OpenFileHandler extends AbstractHandler {
45
46 @Override
47 public Object execute(ExecutionEvent event) {
48
49 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
50 // Menu Selection is not null for context-sensitive menu
51 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
52
53 // Get trace path
54 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
55 FileDialog fd = new FileDialog(shell);
56 fd.setText(Messages.OpenFileHandler_SelectTraceFile);
57 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
58 String lastLocation = defaultPreferences.get(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, null);
59 if (lastLocation != null && !lastLocation.isEmpty()) {
60 File parentFile = new File(lastLocation).getParentFile();
61 if (parentFile != null && parentFile.exists()) {
62 fd.setFilterPath(parentFile.toString());
63 }
64 }
65 String filePath = fd.open();
66 if (filePath == null) {
67 return null;
68 }
69
70 try {
71
72 TmfTraceFolder destinationFolder;
73
74 if ((menuSelection != null) && (currentSelection instanceof IStructuredSelection)) {
75 // If handler is called from the context sensitive menu of a tracing project import to
76 // the traces folder from this project
77 destinationFolder = TmfHandlerUtil.getTraceFolderFromSelection(currentSelection);
78 } else {
79 // If handler is called from file menu import into default tracing project
80 IProject project = TmfProjectRegistry.createProject(
81 TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor());
82 TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
83 destinationFolder = projectElement.getTracesFolder();
84 }
85
86 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, filePath, shell);
87 } catch (CoreException e) {
88 Activator.getDefault().logError(e.getMessage(), e);
89 }
90
91 InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).put(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, filePath);
92 return null;
93 }
94 }
This page took 0.033616 seconds and 4 git commands to generate.