package org.yamcs.studio.explorer;

import java.util.Optional;

import org.csstudio.opibuilder.runmode.RunModeService;
import org.csstudio.opibuilder.runmode.RunModeService.DisplayMode;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.ide.DialogUtil;
import org.eclipse.ui.part.FileEditorInput;

@SuppressWarnings("restriction")
public class OpenFileAction extends Action implements IWorkbenchAction {

    private static final String OPI_BUILDER_PERSPECTIVE_ID = "org.csstudio.opibuilder.opieditor";
    private static final String OPI_RUNTIME_PERSPECTIVE_ID = "org.csstudio.opibuilder.OPIRuntime.perspective";

    private IWorkbenchPage page;
    private ISelectionProvider selectionProvider;

    public OpenFileAction(IWorkbenchPage page, ISelectionProvider selectionProvider) {
        super("Open");
        this.page = page;
        this.selectionProvider = selectionProvider;
    }

    @Override
    public void run() {
        IPerspectiveDescriptor activePerspective = page.getPerspective();
        IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
        for (Object o : selection.toList()) {
            if (o instanceof IFile) {
                IFile file = (IFile) o;
                if (activePerspective.getId().equals(OPI_RUNTIME_PERSPECTIVE_ID)) {
                    openFileInRuntimeMode(file);
                } else if (activePerspective.getId().equals(OPI_BUILDER_PERSPECTIVE_ID)) {
                    openFileInBuilderMode(file);
                } else {
                    openFile(file);
                }
            }
        }
    }

    private void openFileInRuntimeMode(IFile file) {
        if (file.getFileExtension().equalsIgnoreCase("opi")) {
            IPath workspacePath = file.getFullPath();
            // IPath workspacePath = LauncherHelper.systemPathToWorkspacePath(path);
            RunModeService.openDisplay(workspacePath, Optional.empty(), DisplayMode.NEW_TAB, Optional.empty());
        } else {
            MessageDialog.openWarning(null, "Problems Opening File", "This file cannot be opened in Display Runner.");
        }
    }

    private void openFileInBuilderMode(IFile file) {
        if (file.getFileExtension().equalsIgnoreCase("opi")) {
            try {
                boolean activate = OpenStrategy.activateOnOpen();
                page.openEditor(new FileEditorInput(file), "org.csstudio.opibuilder.OPIEditor", activate,
                        IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
            } catch (PartInitException e) {
                DialogUtil.openError(page.getWorkbenchWindow().getShell(), "Problems Opening Editor", e.getMessage(),
                        e);
            }
        } else {
            openFile(file);
        }
    }

    private void openFile(IFile file) {
        try {
            boolean activate = OpenStrategy.activateOnOpen();
            IDE.openEditor(page, file, activate);
        } catch (PartInitException e) {
            DialogUtil.openError(page.getWorkbenchWindow().getShell(), "Problems Opening Editor", e.getMessage(), e);
        }
    }

    @Override
    public void dispose() {
    }
}

@@ -2167,32 +2167,83 @@
 e(IFile file) %7B%0A
+        if (file.getFileExtension() != null) %7B%0A    
         if (file
@@ -2281,32 +2281,36 @@
 eCase(%22opi%22)) %7B%0A
+    
             IPat
@@ -2359,16 +2359,20 @@
         
+ 
+   
 // IPath
@@ -2436,16 +2436,20 @@
 (path);%0A
+    
         
@@ -2556,37 +2556,65 @@
 ));%0A        
-%7D else %7B%0A    
+        return;%0A            %7D%0A        %7D%0A%0A
         Mess
@@ -2716,26 +2716,16 @@
 ner.%22);%0A
-        %7D%0A
     %7D%0A%0A 

Fix NPE when no file extension