package com.intellij.compiler.artifacts.ui;

import com.intellij.compiler.artifacts.ArtifactsTestUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorEx;
import com.intellij.openapi.roots.ui.configuration.artifacts.actions.ExtractArtifactAction;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TestInputDialog;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.packaging.artifacts.Artifact;

/**
 * @author nik
 */
public class ExtractArtifactActionTest extends ArtifactEditorActionTestCase {

  public void testSimple() throws Exception {
    createEditor(addArtifact(root().file(createFile("a.txt"))));
    assertDisabled();

    selectNode("a.txt");
    extract("new");
    assertLayout("<root>\n" +
                 " artifact:new");

    applyChanges();
    assertLayout(ArtifactsTestUtil.findArtifact(myProject, "new"), "<root>\n" +
                                                                   " file:" + getProjectBasePath() + "/a.txt");
  }

  public void testDisabledForJarFromLib() throws Exception {
    final VirtualFile jar = getJDomJar();
    createEditor(addArtifact(root().lib(addProjectLibrary(null, "dom", jar))), true);

    selectNode("jdom.jar");
    assertWillNotBePerformed();
  }

  public void testDirectoryFromIncludedArtifact() throws Exception {
    final Artifact included = addArtifact("included", root().dir("dir").file(createFile("a.txt")));
    createEditor(addArtifact(root()
                              .artifact(included)
                              .dir("dir")
                                .file(createFile("b.txt"))), true);
    selectNode("dir");
    assertDisabled();

    selectNode("dir/a.txt");
    assertWillNotBePerformed();

    selectNode("dir/b.txt");
    extract("new");
    assertLayout("<root>\n" +
                 " artifact:included\n" +
                 " dir/\n" +
                 "  artifact:new");
    applyChanges();
    assertLayout(ArtifactsTestUtil.findArtifact(myProject, "new"), "<root>\n" +
                                                                   " file:" + getProjectBasePath() + "/b.txt");
  }

  private void extract(final String name) {
    final Ref<Boolean> shown = Ref.create(false);
    final TestInputDialog old = Messages.setTestInputDialog(new TestInputDialog() {
      public String show(String message) {
        shown.set(true);
        return name;
      }
    });
    try {
      perform();
    }
    finally {
      Messages.setTestInputDialog(old);
    }
    assertTrue(shown.get());
  }

  @Override
  protected AnAction createAction(final ArtifactEditorEx artifactEditor) {
    return new ExtractArtifactAction(artifactEditor);
  }
}

@@ -291,78 +291,212 @@
 cts.
-actions.ExtractArtifactAction;%0Aimport com.intellij.openapi.ui.Messages
+LayoutTreeComponent;%0Aimport com.intellij.openapi.roots.ui.configuration.artifacts.actions.ExtractArtifactAction;%0Aimport com.intellij.openapi.roots.ui.configuration.artifacts.actions.IExtractArtifactDialog
 ;%0Aim
@@ -525,26 +525,23 @@
 api.
-ui.TestInputDialog
+vfs.VirtualFile
 ;%0Aim
@@ -562,68 +562,89 @@
 lij.
-openapi.util.Ref;%0Aimport com.intellij.openapi.vfs.VirtualFil
+packaging.artifacts.Artifact;%0Aimport com.intellij.packaging.artifacts.ArtifactTyp
 e;%0Ai
@@ -664,32 +664,37 @@
 ellij.packaging.
+impl.
 artifacts.Artifa
@@ -687,24 +687,29 @@
 tifacts.
+Plain
 Artifact
 ;%0A%0A/**%0A 
@@ -700,16 +700,20 @@
 Artifact
+Type
 ;%0A%0A/**%0A 
@@ -968,37 +968,32 @@
 .txt%22);%0A    
-extract(%22new%22
+perform(
 );%0A    asser
@@ -1037,27 +1037,29 @@
  %22 artifact:
-new
+a_txt
 %22);%0A%0A    app
@@ -1123,35 +1123,37 @@
 act(myProject, %22
-new
+a_txt
 %22), %22%3Croot%3E%5Cn%22 +
@@ -2035,21 +2035,16 @@
     
-extract(%22new%22
+perform(
 );%0A 
@@ -2172,19 +2172,21 @@
 rtifact:
-new
+b_txt
 %22);%0A    
@@ -2261,19 +2261,21 @@
 oject, %22
-new
+b_txt
 %22), %22%3Cro
@@ -2406,172 +2406,311 @@
 %0A%0A  
-private void extract(final String name) %7B%0A    final Ref%3CBoolean%3E shown = Ref.create(false);%0A    final TestInputDialog old = Messages.setTestInputDialog(new TestInpu
+@Override%0A  protected AnAction createAction(final ArtifactEditorEx artifactEditor) %7B%0A    return new ExtractArtifactAction(artifactEditor) %7B%0A      @Override%0A      protected IExtractArtifactDialog showDialog(LayoutTreeComponent treeComponent, final String initialName) %7B%0A        return new IExtractArtifac
 tDia
@@ -2723,16 +2723,20 @@
 %7B%0A      
+  
+  
 public S
@@ -2745,56 +2745,32 @@
 ing 
-show(String message) %7B%0A        shown.set(true);%0A
+getArtifactName() %7B%0A    
     
@@ -2780,17 +2780,24 @@
  return 
-n
+initialN
 ame;%0A   
@@ -2803,284 +2803,153 @@
     
-%7D%0A
     %7D
-);
+%0A
 %0A    
-try %7B%0A      perform();%0A    %7D%0A    finally %7B%0A      Messages.setTestInputDialog(old
+      public ArtifactType getArtifactType() %7B%0A            return PlainArtifactType.getInstance(
 );%0A    
-%7D%0A
     
-assertTrue(shown.get());%0A
   %7D%0A
-%0A
   
-@Override%0A  protected AnAction createAction(final ArtifactEditorEx artifactEditor) %7B%0A    return new ExtractArtifactAction(artifactEditor)
+      %7D;%0A      %7D%0A    %7D
 ;%0A  

test fixed