1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 **********************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.commands
;
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
;
41 * Open file handler, used to open files (not directories)
43 * @author Matthew Khouzam
45 public class OpenFileHandler
extends AbstractHandler
{
48 public Object
execute(ExecutionEvent event
) {
50 ISelection currentSelection
= HandlerUtil
.getCurrentSelection(event
);
51 // Menu Selection is not null for context-sensitive menu
52 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
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());
66 String filePath
= fd
.open();
67 if (filePath
== null) {
73 TmfTraceFolder destinationFolder
;
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
);
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();
87 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, filePath
, shell
);
88 } catch (CoreException e
) {
89 Activator
.getDefault().logError(e
.getMessage(), e
);
92 InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
).put(ITmfUIPreferences
.PREF_SAVED_OPEN_FILE_LOCATION
, filePath
);