Java Code Examples for org.eclipse.ui.handlers.HandlerUtil#getActiveMenuSelection()

The following examples show how to use org.eclipse.ui.handlers.HandlerUtil#getActiveMenuSelection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenFileHandler.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) {

    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    // Menu Selection is not null for context-sensitive menu
    ISelection menuSelection  = HandlerUtil.getActiveMenuSelection(event);

    // Get trace path
    final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    FileDialog fd = TmfFileDialogFactory.create(shell);
    fd.setText(Messages.OpenFileHandler_SelectTraceFile);
    IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
    String lastLocation = defaultPreferences.get(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, null);
    if (lastLocation != null && !lastLocation.isEmpty()) {
        File parentFile = new File(lastLocation).getParentFile();
        if (parentFile != null && parentFile.exists()) {
            fd.setFilterPath(parentFile.toString());
        }
    }
    String filePath = fd.open();
    if (filePath == null) {
        return null;
    }

    try {

        TmfTraceFolder destinationFolder;

        if ((menuSelection != null) && (currentSelection instanceof IStructuredSelection)) {
            // If handler is called from the context sensitive menu of a tracing project import to
            // the traces folder from this project
            destinationFolder = TmfHandlerUtil.getTraceFolderFromSelection(currentSelection);
        } else {
            // If handler is called from file menu import into default tracing project
            IProject project = TmfProjectRegistry.createProject(
                    TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME, null, new NullProgressMonitor());
            TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
            destinationFolder = projectElement.getTracesFolder();
        }

        TmfOpenTraceHelper.openTraceFromPath(destinationFolder, filePath, shell);
    } catch (CoreException e) {
        Activator.getDefault().logError(e.getMessage(), e);
    }

    InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).put(ITmfUIPreferences.PREF_SAVED_OPEN_FILE_LOCATION, filePath);
    return null;
}