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