package com.intellij.ide.actions;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.DataConstantsEx;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.impl.EditorWindow;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.ContentManagerUtil;

public class PinActiveTabAction extends ToggleAction {
  /**
   * @return selected editor or <code>null</code>
   */
  private VirtualFile getFile(final DataContext context){
    Project project = (Project)context.getData(DataConstants.PROJECT);
    if(project == null){
      return null;
    }

    // To provide file from editor manager, editor component should be active
    if(!ToolWindowManager.getInstance(project).isEditorComponentActive()){
      return null;
    }

    return (VirtualFile)context.getData(DataConstants.VIRTUAL_FILE);
  }

  /**
   * @return selected content or <code>null</code>
   */
  private Content getContent(final DataContext context){
    ContentManager contentManager = ContentManagerUtil.getContentManagerFromContext(context, true);
    if (contentManager == null){
      return null;
    }
    return contentManager.getSelectedContent();
  }

  public boolean isSelected(AnActionEvent e) {
    DataContext context = e.getDataContext();
    VirtualFile file = getFile(context);
    if(file != null){
      // 1. Check editor
      final Project project = (Project)context.getData(DataConstants.PROJECT);
      final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
      EditorWindow editorWindow = (EditorWindow)context.getData(DataConstantsEx.EDITOR_WINDOW);
      if (editorWindow == null){
        editorWindow = fileEditorManager.getCurrentWindow();
      }
      return editorWindow.isFilePinned(file);
    }
    else{
      // 2. Check content
      final Content content = getContent(context);
      if(content != null){
        return content.isPinned();
      }
      else{
        return false;
      }
    }
  }

  public void setSelected(AnActionEvent e, boolean state) {
    DataContext context = e.getDataContext();
    VirtualFile file = getFile(context);
    if(file != null){
      // 1. Check editor
      Project project = (Project)context.getData(DataConstants.PROJECT);
      final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);
      EditorWindow editorWindow = (EditorWindow)context.getData(DataConstantsEx.EDITOR_WINDOW);
      if (editorWindow == null){
        editorWindow = fileEditorManager.getCurrentWindow();
      }
      editorWindow.setFilePinned(file, state);
    }
    else{
      Content content = getContent(context); // at this point content cannot be null
      content.setPinned(state);
    }
  }

  public void update(AnActionEvent e){
    super.update(e);
    Presentation presentation = e.getPresentation();
    DataContext context = e.getDataContext();
    presentation.setEnabled(getFile(context) != null || getContent(context) != null);
    if (ActionPlaces.EDITOR_TAB_POPUP.equals(e.getPlace())) {
      presentation.setText(isSelected(e) ? "_Unpin Tab" : "_Pin Tab");
    } else {
      presentation.setText(isSelected(e) ? "Unpin _Active Tab" : "_Pin Active Tab");
    }
  }
}

@@ -1599,183 +1599,8 @@
 tor%0A
-      final Project project = (Project)context.getData(DataConstants.PROJECT);%0A      final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);%0A
     
@@ -1621,33 +1621,35 @@
  editorWindow = 
-(
+get
 EditorWindow)con
@@ -1648,54 +1648,16 @@
 ndow
-)
+(
 context
-.getData(DataConstantsEx.EDITOR_WINDOW
 );%0A 
@@ -1657,32 +1657,33 @@
 ext);%0A      if (
+!
 editorWindow == 
@@ -1674,33 +1674,43 @@
 editorWindow
- == null)
+.isFileOpen(file)) 
 %7B%0A        ed
@@ -1698,32 +1698,39 @@
 file)) %7B%0A       
+ file =
  editorWindow = 
@@ -1730,47 +1730,66 @@
 ndow
- = fileEditorManager.g
+.getSelectedFile();%0A        if (file == null) r
 et
-C
 ur
-rentWindow()
+n false
 ;%0A  
@@ -1790,24 +1790,25 @@
 se;%0A      %7D%0A
+%0A
       return
@@ -2255,177 +2255,8 @@
 tor%0A
-      Project project = (Project)context.getData(DataConstants.PROJECT);%0A      final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);%0A
     
@@ -2285,17 +2285,19 @@
 indow = 
-(
+get
 EditorWi
@@ -2304,54 +2304,16 @@
 ndow
-)
+(
 context
-.getData(DataConstantsEx.EDITOR_WINDOW
 );%0A 
@@ -2321,16 +2321,17 @@
     if (
+!
 editorWi
@@ -2334,25 +2334,35 @@
 orWindow
- == null)
+.isFileOpen(file)) 
 %7B%0A      
@@ -2358,24 +2358,31 @@
 )) %7B%0A       
+ file =
  editorWindo
@@ -2386,47 +2386,60 @@
 ndow
- = fileEditorManager.g
+.getSelectedFile();%0A        if (file == null) r
 et
-C
 ur
-rentWindow()
+n
 ;%0A  
@@ -2440,24 +2440,25 @@
 rn;%0A      %7D%0A
+%0A
       editor
@@ -2617,32 +2617,32 @@
 tPinned(state);%0A
-
     %7D%0A  %7D%0A%0A  pub
@@ -2628,32 +2628,488 @@
 te);%0A    %7D%0A  %7D%0A%0A
+  private EditorWindow getEditorWindow(DataContext context) %7B%0A    final Project project = (Project) context.getData(DataConstants.PROJECT);%0A    final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project);%0A    EditorWindow editorWindow = (EditorWindow) context.getData(DataConstantsEx.EDITOR_WINDOW);%0A    if (editorWindow == null) %7B%0A      editorWindow = fileEditorManager.getCurrentWindow();%0A    %7D%0A    return editorWindow;%0A  %7D%0A%0A
   public void up

Fix for Fabrique - File in the context and opened file are not the same.