tmf.ui: Introduce TmfFileDialogFactory
[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.dialog.TmfFileDialogFactory;
33 import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
34 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
35 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
36 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.handlers.HandlerUtil;
39
40 /**
41 * Open file handler, used to open files (not directories)
42 *
43 * @author Matthew Khouzam
44 */
45 public class OpenFileHandler extends AbstractHandler {
46
47 @Override
48 public Object execute(ExecutionEvent event) {
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
55 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
56 FileDialog fd = TmfFileDialogFactory.create(shell);
57 fd.setText(Messages.OpenFileHandler_SelectTraceFile);
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 }
66 String filePath = fd.open();
67 if (filePath == null) {
68 return null;
69 }
70
71 try {
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
87 TmfOpenTraceHelper.openTraceFromPath(destinationFolder, filePath, shell);
88 } catch (CoreException e) {
89 Activator.getDefault().logError(e.getMessage(), e);
90 }
91
92 InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).put(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, filePath);
93 return null;
94 }
95 }
This page took 0.032154 seconds and 5 git commands to generate.