/*
 * 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.diff.requests;

import com.intellij.diff.DiffContext;
import com.intellij.diff.DiffContextEx;
import com.intellij.diff.util.DiffUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.fileTypes.ex.FileTypeChooser;
import com.intellij.openapi.vcs.changes.issueLinks.LinkMouseListenerBase;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class UnknownFileTypeDiffRequest extends ComponentDiffRequest {
  @Nullable private final String myFileName;
  @Nullable private final String myTitle;

  public UnknownFileTypeDiffRequest(@NotNull VirtualFile file, @Nullable String title) {
    this(file.getName(), title);
  }

  public UnknownFileTypeDiffRequest(@NotNull String fileName, @Nullable String title) {
    boolean knownFileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName) != UnknownFileType.INSTANCE;
    myFileName = knownFileType ? null : fileName;
    myTitle = title;
  }

  public UnknownFileTypeDiffRequest(@Nullable String title) {
    myFileName = null;
    myTitle = title;
  }

  @NotNull
  @Override
  public JComponent getComponent(@NotNull final DiffContext context) {
    final SimpleColoredComponent label = new SimpleColoredComponent();
    label.append("Can't show diff for unknown file type. ",
                 new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, UIUtil.getInactiveTextColor()));
    if (myFileName != null) {
      label.append("Associate", SimpleTextAttributes.LINK_ATTRIBUTES, new Runnable() {
        @Override
        public void run() {
          FileType type = FileTypeChooser.associateFileType(myFileName);
          if (type != null) onSuccess(context);
        }
      });
      LinkMouseListenerBase.installSingleTagOn(label);
    }
    return DiffUtil.createMessagePanel(label);
  }

  @Nullable
  public String getFileName() {
    return myFileName;
  }

  @Nullable
  @Override
  public String getTitle() {
    return myTitle;
  }

  protected void onSuccess(@NotNull DiffContext context) {
    if (context instanceof DiffContextEx) ((DiffContextEx)context).reloadDiffRequest();
  }
}

@@ -961,24 +961,129 @@
 ypeChooser;%0A
+import com.intellij.openapi.project.DumbModePermission;%0Aimport com.intellij.openapi.project.DumbService;%0A
 import com.i
@@ -1144,16 +1144,16 @@
 erBase;%0A
-
 import c
@@ -1999,119 +1999,8 @@
  %7D%0A%0A
-  public UnknownFileTypeDiffRequest(@Nullable String title) %7B%0A    myFileName = null;%0A    myTitle = title;%0A  %7D%0A%0A
   @N
@@ -2488,24 +2488,190 @@
 oid run() %7B%0A
+          DumbService.allowStartingDumbModeInside(DumbModePermission.MAY_START_BACKGROUND, new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A    
           Fi
@@ -2737,24 +2737,28 @@
 ;%0A          
+    
 if (type != 
@@ -2783,16 +2783,44 @@
 ntext);%0A
+            %7D%0A          %7D);%0A
         

allow bg dumb mode when associating file type from diff (EA-72563 - Throwable: DumbServiceImpl.scheduleCacheUpdate)