/*
 * Copyright 2000-2017 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.openapi.vcs.changes.committed;

import com.intellij.icons.AllIcons;
import com.intellij.ide.actions.EditSourceAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.pom.Navigatable;

import javax.swing.*;
import java.util.stream.Stream;

import static com.intellij.openapi.vcs.changes.ChangesUtil.getFiles;
import static com.intellij.openapi.vcs.changes.ChangesUtil.getNavigatableArray;

class EditSourceFromChangesBrowserAction extends EditSourceAction {
  private final Icon myEditSourceIcon;

  public EditSourceFromChangesBrowserAction() {
    myEditSourceIcon = AllIcons.Actions.EditSource;
  }

  public void update(final AnActionEvent event) {
    super.update(event);
    event.getPresentation().setIcon(myEditSourceIcon);
    event.getPresentation().setText("Edit Source");
    if ((!ModalityState.NON_MODAL.equals(ModalityState.current())) ||
        CommittedChangesBrowserUseCase.IN_AIR.equals(CommittedChangesBrowserUseCase.DATA_KEY.getData(event.getDataContext()))) {
      event.getPresentation().setEnabled(false);
    }
  }

  protected Navigatable[] getNavigatables(DataContext dataContext) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    Change[] changes = VcsDataKeys.SELECTED_CHANGES.getData(dataContext);
    return changes != null && project != null ? getNavigatableArray(project, getFiles(Stream.of(changes))) : null;
  }
}

@@ -1093,16 +1093,79 @@
 Change;%0A
+import com.intellij.openapi.vcs.changes.ui.ChangesBrowserBase;%0A
 import c
@@ -1801,16 +1801,148 @@
 %0A    if 
+(event.getData(ChangesBrowserBase.DATA_KEY) == null) %7B%0A      event.getPresentation().setEnabledAndVisible(false);%0A    %7D%0A    else if 
 ((!Modal
@@ -1996,16 +1996,21 @@
 ())) %7C%7C%0A
+     
         

edit source from changes should only be active in changes browser