tmf: Bug 452415: Remember last location for Open Trace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / commands / OpenFileHandler.java
CommitLineData
76fccfb0 1/**********************************************************************
977ca87f 2 * Copyright (c) 2013, 2014 Ericsson
76fccfb0
MK
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
977ca87f 11 * Patrick Tasse - Add support for folder elements
76fccfb0
MK
12 **********************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.internal.tmf.ui.commands;
76fccfb0 15
9bec1d5e
MAL
16import java.io.File;
17
76fccfb0
MK
18import org.eclipse.core.commands.AbstractHandler;
19import org.eclipse.core.commands.ExecutionEvent;
1aec2e92 20import org.eclipse.core.resources.IProject;
76fccfb0 21import org.eclipse.core.runtime.CoreException;
1aec2e92 22import org.eclipse.core.runtime.NullProgressMonitor;
9bec1d5e
MAL
23import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24import org.eclipse.core.runtime.preferences.InstanceScope;
1aec2e92
BH
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.IStructuredSelection;
76fccfb0
MK
27import org.eclipse.swt.widgets.FileDialog;
28import org.eclipse.swt.widgets.Shell;
2bdf0193 29import org.eclipse.tracecompass.internal.tmf.ui.Activator;
9bec1d5e 30import org.eclipse.tracecompass.internal.tmf.ui.ITmfUIPreferences;
2bdf0193
AM
31import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
32import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
33import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
34import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
35import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
76fccfb0 36import org.eclipse.ui.PlatformUI;
c0c7c656 37import org.eclipse.ui.handlers.HandlerUtil;
76fccfb0
MK
38
39/**
40 * Open file handler, used to open files (not directories)
41 *
42 * @author Matthew Khouzam
43 */
44public class OpenFileHandler extends AbstractHandler {
45
46 @Override
47 public Object execute(ExecutionEvent event) {
1aec2e92
BH
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
76fccfb0
MK
54 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
55 FileDialog fd = new FileDialog(shell);
56 fd.setText(Messages.OpenFileHandler_SelectTraceFile);
9bec1d5e
MAL
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 }
76fccfb0
MK
65 String filePath = fd.open();
66 if (filePath == null) {
67 return null;
68 }
c0c7c656 69
76fccfb0 70 try {
1aec2e92
BH
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
977ca87f 86 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, filePath, shell);
76fccfb0 87 } catch (CoreException e) {
c0c7c656 88 Activator.getDefault().logError(e.getMessage(), e);
76fccfb0 89 }
9bec1d5e
MAL
90
91 InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).put(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, filePath);
76fccfb0
MK
92 return null;
93 }
94}
This page took 0.067301 seconds and 5 git commands to generate.