package com.codeaffine.extras.ide.internal.openwith;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.ide.ResourceUtil;

public class OpenWithQuickMenuHandler extends AbstractHandler {

  public static final String COMMAND_ID
    = "com.codeaffine.extras.ide.internal.OpenWithQuickMenu";

  @Override
  public Object execute( ExecutionEvent event ) {
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow( event );
    IFile file = extractFileFromSelection( HandlerUtil.getCurrentSelection( event ) );
    showMenu( workbenchWindow.getActivePage(), file );
    return null;
  }

  @Override
  public void setEnabled( Object evaluationContext ) {
    if( evaluationContext instanceof IEvaluationContext ) {
      setEnabled( ( IEvaluationContext )evaluationContext );
    } else {
      setBaseEnabled( false );
    }
  }

  protected void showMenu( IWorkbenchPage workbenchPage, IFile file ) {
    new OpenWithQuickMenu( workbenchPage, file ).show();
  }

  private void setEnabled( IEvaluationContext evaluationContext ) {
    ISelection selection = getSelection( evaluationContext );
    setBaseEnabled( isEnabled( selection ) );
  }

  private static boolean isEnabled( ISelection selection ) {
    return extractFileFromSelection( selection ) != null;
  }

  private static ISelection getSelection( IEvaluationContext evaluationContext ) {
    ISelection result = StructuredSelection.EMPTY;
    Object variable = evaluationContext.getVariable( ISources.ACTIVE_CURRENT_SELECTION_NAME );
    if( variable instanceof ISelection ) {
      result = ( ISelection )variable;
    }
    return result;
  }

  private static IFile extractFileFromSelection( ISelection selection ) {
    IFile result = null;
    if( selection instanceof IStructuredSelection ) {
      IStructuredSelection structuredSelection = ( IStructuredSelection )selection;
      if( structuredSelection.size() == 1 ) {
        IFile file = ResourceUtil.getFile( structuredSelection.getFirstElement() );
        result = file;
      }
    }
    return result;
  }
}

@@ -955,30 +955,21 @@
 on( 
-HandlerUtil.getCurrent
+getStructured
 Sele
@@ -1506,32 +1506,42 @@
 ntext ) %7B%0A    IS
+tructuredS
 election selecti
@@ -1665,24 +1665,34 @@
 sEnabled( IS
+tructuredS
 election sel
@@ -1784,16 +1784,26 @@
 tatic IS
+tructuredS
 election
@@ -1864,16 +1864,26 @@
 %7B%0A    IS
+tructuredS
 election
@@ -2044,16 +2044,26 @@
 nceof IS
+tructuredS
 election
@@ -2082,24 +2082,34 @@
 esult = ( IS
+tructuredS
 election )va
@@ -2168,28 +2168,41 @@
 ic I
-File extractFileFrom
+StructuredSelection getStructured
 Sele
@@ -2212,56 +2212,161 @@
 on( 
-ISelection selection ) %7B%0A    IFile result = null
+ExecutionEvent event ) %7B%0A    IStructuredSelection result = StructuredSelection.EMPTY;%0A    ISelection selection = HandlerUtil.getCurrentSelection( event )
 ;%0A  
@@ -2422,16 +2422,27 @@
  %7B%0A     
+ result = (
  IStruct
@@ -2455,26 +2455,96 @@
 lection 
-structured
+)selection;%0A    %7D%0A    return result;%0A  %7D%0A%0A  private static IFile extractFileFrom
 Selectio
@@ -2544,19 +2544,16 @@
 election
- = 
 ( IStruc
@@ -2559,33 +2559,32 @@
 cturedSelection 
-)
 selection;%0A     
@@ -2580,31 +2580,47 @@
 tion
-;
+ ) %7B
 %0A    
-  if( structuredS
+IFile result = null;%0A    if( s
 elec
@@ -2646,28 +2646,22 @@
 %7B%0A      
-  IFile file
+result
  = Resou
@@ -2678,26 +2678,16 @@
 tFile( s
-tructuredS
 election
@@ -2712,39 +2712,8 @@
  );%0A
-        result = file;%0A      %7D%0A
     

Simplify extracting selected file(s)