/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcs.log.ui.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcs.log.data.VcsLogData;
import com.intellij.vcs.log.impl.VcsLogContentProvider;
import com.intellij.vcs.log.impl.VcsLogManager;
import com.intellij.vcs.log.impl.VcsProjectLog;
import com.intellij.vcs.log.ui.history.FileHistoryUi;
import com.intellij.vcs.log.ui.history.FileHistoryUiFactory;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;

public class ShowHistoryAction extends DumbAwareAction {
  @NotNull
  public static final String TAB_NAME = "History";

  @Override
  public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    assert project != null;
    VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE);

    FilePath path = VcsUtil.getFilePath(file);
    if (!VcsLogContentProvider.findAndSelectContent(project, FileHistoryUi.class, ui -> ui.getPath().equals(path))) {
      VcsLogManager logManager = VcsProjectLog.getInstance(project).getLogManager();
      assert logManager != null;
      VcsLogContentProvider.openLogTab(project, logManager, TAB_NAME, file.getName(), new FileHistoryUiFactory(path));
    }
  }

  @Override
  public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();
    if (!Registry.is("vcs.log.graph.history")) {
      presentation.setEnabledAndVisible(false);
    }
    else {
      VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
      Project project = e.getProject();
      if (file == null || project == null) {
        presentation.setEnabledAndVisible(false);
      }
      else {
        VirtualFile root = ProjectLevelVcsManager.getInstance(project).getVcsRootFor(file);
        VcsLogData dataManager = VcsProjectLog.getInstance(project).getDataManager();
        if (root == null || dataManager == null) {
          presentation.setEnabledAndVisible(false);
        }
        else {
          presentation.setEnabledAndVisible(dataManager.getRoots().contains(root));
        }
      }
    }
  }
}

@@ -2431,23 +2431,25 @@
     
-%7D%0A    else %7B%0A  
+  return;%0A    %7D%0A%0A
     
@@ -2511,18 +2511,16 @@
 E);%0A    
-  
 Project 
@@ -2541,26 +2541,24 @@
 tProject();%0A
-  
     if (file
@@ -2590,26 +2590,24 @@
 ll) %7B%0A      
-  
 presentation
@@ -2646,27 +2646,23 @@
     
-%7D%0A      else %7B%0A    
+return;%0A    %7D%0A%0A
     
@@ -2741,28 +2741,24 @@
 tFor(file);%0A
-    
     VcsLogDa
@@ -2835,31 +2835,95 @@
     
-    if 
+presentation.setEnabledAndVisible
 (root 
-=
+!
 = null 
-%7C%7C
+&&%0A                                     
  dat
@@ -2931,26 +2931,26 @@
 Manager 
-=
+!
 = null
-) %7B
+ &&
 %0A       
@@ -2956,58 +2956,16 @@
     
-presentation.setEnabledAndVisible(false);%0A
         
 %7D%0A  
@@ -2964,18 +2964,16 @@
     
-%7D%0A
         
 else
@@ -2972,15 +2972,8 @@
     
-else %7B%0A
     
@@ -2982,42 +2982,10 @@
     
-presentation.setEnabledAndVisible(
+  
 data
@@ -3024,32 +3024,8 @@
 ));%0A
-        %7D%0A      %7D%0A    %7D%0A
   %7D%0A

simplify update for file history action