/*
 * Copyright 2000-2015 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.util.diff.impl;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.util.diff.DiffRequestPanel;
import com.intellij.openapi.util.diff.requests.DiffRequest;
import com.intellij.openapi.util.diff.requests.NoDiffRequest;
import com.intellij.openapi.util.diff.util.DiffUserDataKeys;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class DiffRequestPanelImpl implements DiffRequestPanel {
  @NotNull private final JPanel myPanel;
  @NotNull private final MyCacheDiffRequestChainProcessor myProcessor;

  public DiffRequestPanelImpl(@Nullable Project project, @Nullable Window window) {
    myProcessor = new MyCacheDiffRequestChainProcessor(project, window);
    myProcessor.init();

    myPanel = new JPanel(new BorderLayout()) {
      @Override
      public void addNotify() {
        super.addNotify();
        myProcessor.updateRequest();
      }
    };
    myPanel.add(myProcessor.getComponent());
  }

  @Override
  public void setRequest(@Nullable DiffRequest request) {
    myProcessor.setRequest(request);
    myProcessor.updateRequest();
  }

  @Override
  public <T> void putContextHints(@NotNull Key<T> key, @Nullable T value) {
    myProcessor.putContextUserData(key, value);
  }

  @NotNull
  @Override
  public JComponent getComponent() {
    return myPanel;
  }

  @Nullable
  @Override
  public JComponent getPreferredFocusedComponent() {
    return myProcessor.getPreferredFocusedComponent();
  }

  @Override
  public void dispose() {
    Disposer.dispose(myProcessor);
  }

  private static class MyCacheDiffRequestChainProcessor extends DiffRequestProcessor {
    @Nullable private final Window myWindow;

    @NotNull private DiffRequest myRequest = new NoDiffRequest();

    public MyCacheDiffRequestChainProcessor(@Nullable Project project, @Nullable Window window) {
      super(project);
      myWindow = window;
    }

    public void setRequest(@Nullable DiffRequest request) {
      myRequest = request != null ? request : new NoDiffRequest();
    }

    @Override
    public void updateRequest(boolean force, @Nullable DiffUserDataKeys.ScrollToPolicy scrollToChangePolicy) {
      applyRequest(myRequest, force, scrollToChangePolicy);
    }

    @Override
    protected void setWindowTitle(@NotNull String title) {
      if (myWindow == null) return;
      if (myWindow instanceof JDialog) ((JDialog)myWindow).setTitle(title);
      if (myWindow instanceof JFrame) ((JFrame)myWindow).setTitle(title);
    }
  }
}

@@ -770,110 +770,8 @@
 ey;%0A
-import com.intellij.openapi.util.UserDataHolder;%0Aimport com.intellij.openapi.util.UserDataHolderBase;%0A
 impo
@@ -1005,16 +1005,52 @@
 taKeys;%0A
+import com.intellij.util.ui.UIUtil;%0A
 import o
@@ -1855,24 +1855,118 @@
 t(request);%0A
+    UIUtil.invokeLaterIfNeeded(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A    
     myProces
@@ -1982,24 +1982,40 @@
 eRequest();%0A
+      %7D%0A    %7D);%0A
   %7D%0A%0A  @Over

allow to call setRequest() from any thread