/*
 * Copyright 2000-2011 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.diff.actions;

import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.diff.DirDiffSettings;
import com.intellij.ide.diff.JarFileDiffElement;
import com.intellij.ide.diff.VirtualFileDiffElement;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.diff.DirDiffManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;

/**
 * @author Konstantin Bulenkov
 */
public class CompareJarsAction extends AnAction {
  //
  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = getEventProject(e);
    final VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
    FeatureUsageTracker.getInstance().triggerFeatureUsed("jar.diff");
    if (project != null && files != null) {
      VirtualFileDiffElement src = null;
      VirtualFileDiffElement trg = null;
      if (files.length == 2 && isJarOrZip(files[0]) && isJarOrZip(files[1])) {
        src = new JarFileDiffElement(files[0]);
        trg = new JarFileDiffElement(files[1]);
      } else if (files.length == 1 && isJarOrZip(files[0])) {
        src = new JarFileDiffElement(files[0]);
        final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, true, false, false) {
          @Override
          public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
            return file.isDirectory()
                   || (!file.isDirectory() && isJarOrZip(file));
          }
        };
        final VirtualFile[] result = FileChooserFactory.getInstance()
          .createFileChooser(descriptor, project).choose(project.getBaseDir(), project);
        if (result.length == 1 && result[0] != null && isJarOrZip(result[0])) {
          trg = new JarFileDiffElement(result[0]);
        }
      }
      final DirDiffManager mgr = DirDiffManager.getInstance(project);
      if (src != null && trg != null && mgr.canShow(src, trg)) {
        mgr.showDiff(src, trg, new DirDiffSettings(), null);
      }
    }
  }

  @Override
  public void update(AnActionEvent e) {
    final VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
    if (files != null && files.length > 0 && files.length < 3) {
      if (isJarOrZip(files[0]) && (files.length == 1 || isJarOrZip(files[1]))) {
        e.getPresentation().setEnabled(true);
        e.getPresentation().setVisible(true);
        e.getPresentation().setText(files.length == 1 ? "Compare Archive File with..." : "Compare Archives");
        return;
      }
    }

    e.getPresentation().setEnabled(false);
    e.getPresentation().setVisible(false);
  }

  private static boolean isJarOrZip(VirtualFile file) {
    final String ext = file.getExtension();
    return JarFileSystem.PROTOCOL.equalsIgnoreCase(ext) || "zip".equalsIgnoreCase(ext);
  }
}

@@ -837,32 +837,85 @@
 ileDiffElement;%0A
+import com.intellij.ide.highlighter.ArchiveFileType;%0A
 import com.intel
@@ -949,16 +949,16 @@
 Action;%0A
-
 import c
@@ -1285,55 +1285,8 @@
 ct;%0A
-import com.intellij.openapi.vfs.JarFileSystem;%0A
 impo
@@ -1835,32 +1835,31 @@
 h == 2 && is
-JarOrZip
+Archive
 (files%5B0%5D) &
@@ -1858,32 +1858,31 @@
 es%5B0%5D) && is
-JarOrZip
+Archive
 (files%5B1%5D)) 
@@ -2015,32 +2015,31 @@
 h == 1 && is
-JarOrZip
+Archive
 (files%5B0%5D)) 
@@ -2394,24 +2394,23 @@
 () && is
-JarOrZip
+Archive
 (file));
@@ -2649,24 +2649,23 @@
 ll && is
-JarOrZip
+Archive
 (result%5B
@@ -3164,24 +3164,23 @@
   if (is
-JarOrZip
+Archive
 (files%5B0
@@ -3209,24 +3209,23 @@
  1 %7C%7C is
-JarOrZip
+Archive
 (files%5B1
@@ -3586,16 +3586,15 @@
 n is
-JarOrZip
+Archive
 (Vir
@@ -3618,134 +3618,60 @@
     
-final String ext = file.getExtension();%0A    return JarFileSystem.PROTOCOL.equalsIgnoreCase(ext) %7C%7C %22zip%22.equalsIgnoreCase(ext)
+return file.getFileType() instanceof ArchiveFileType
 ;%0A  

support all archives