/*
 * 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.usages.impl;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.usageView.UsageInfo;
import com.intellij.usages.UsageContextPanel;
import com.intellij.usages.UsageViewPresentation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author cdr
 */
public abstract class UsageContextPanelBase extends JPanel implements UsageContextPanel {
  protected final Project myProject;
  @NotNull protected final UsageViewPresentation myPresentation;
  protected volatile boolean isDisposed;

  public UsageContextPanelBase(@NotNull Project project, @NotNull UsageViewPresentation presentation) {
    myProject = project;
    myPresentation = presentation;
    setLayout(new BorderLayout());
    setBorder(IdeBorderFactory.createEmptyBorder());
  }

  @NotNull
  @Override
  public final JComponent createComponent() {
    isDisposed = false;
    return this;
  }

  @Override
  public void dispose() {
    isDisposed = true;
  }

  @Override
  public final void updateLayout(@Nullable final List<UsageInfo> infos) {
    PsiDocumentManager.getInstance(myProject).performLaterWhenAllCommitted(() -> {
      if (isDisposed || myProject.isDisposed()) return;
      updateLayoutLater(infos);
    });
  }

  protected abstract void updateLayoutLater(@Nullable List<UsageInfo> infos);
}

@@ -1855,31 +1855,153 @@
 ager
-.getInstance(myProject)
+ pdm = PsiDocumentManager.getInstance(myProject);%0A    if (!pdm.hasUncommitedDocuments()) %7B%0A      updateLayoutLater(infos);%0A    %7D else %7B%0A      pdm
 .per
@@ -2036,24 +2036,26 @@
  -%3E %7B%0A      
+ 
+ 
 if (isDispos
@@ -2094,24 +2094,26 @@
 turn;%0A      
+  
 updateLayout
@@ -2134,12 +2134,21 @@
     
+ 
+ 
 %7D);%0A
+    %7D%0A%0A
   %7D%0A

update usage preview immediately if everything is committed (IDEA-CR-11111, EA-83054 - assert: PsiDocumentManagerBase.commitAllDocuments)