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
.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
;
40 * Open file handler, used to open files (not directories)
42 * @author Matthew Khouzam
44 public class OpenFileHandler
extends AbstractHandler
{
47 public Object
execute(ExecutionEvent event
) {
49 ISelection currentSelection
= HandlerUtil
.getCurrentSelection(event
);
50 // Menu Selection is not null for context-sensitive menu
51 ISelection menuSelection
= HandlerUtil
.getActiveMenuSelection(event
);
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());
65 String filePath
= fd
.open();
66 if (filePath
== null) {
72 TmfTraceFolder destinationFolder
;
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
);
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();
86 TmfOpenTraceHelper
.openTraceFromPath(destinationFolder
, filePath
, shell
);
87 } catch (CoreException e
) {
88 Activator
.getDefault().logError(e
.getMessage(), e
);
91 InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
).put(ITmfUIPreferences
.PREF_SAVED_OPEN_FILE_LOCATION
, filePath
);