package com.intellij.refactoring.lang;

import com.intellij.ide.util.DirectoryUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.lang.jsp.extractInclude.ExtractJspIncludeFileHandler;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.util.IncorrectOperationException;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;

/**
 * @author ven
 */
public class ExtractIncludeDialog extends DialogWrapper {
  private TextFieldWithBrowseButton myTargetDirectoryField;
  private JTextField myNameField;
  private final PsiDirectory myCurrentDirectory;
  private final LanguageFileType myFileType;
  private static final String REFACTORING_NAME = RefactoringBundle.message("extractIncludeFile.name");

  public PsiDirectory getTargetDirectory() {
    return myTargetDirectory;
  }

  private PsiDirectory myTargetDirectory;

  public String getTargetFileName () {
    return myNameField.getText().trim() + "." + myFileType.getDefaultExtension();
  }

  public ExtractIncludeDialog(final PsiDirectory currentDirectory, final LanguageFileType fileType) {
    super(false);
    myCurrentDirectory = currentDirectory;
    myFileType = fileType;
    setTitle(REFACTORING_NAME);
    init();
  }

  public JComponent getPreferredFocusedComponent() {
    return myNameField;
  }

  protected JComponent createCenterPanel() {
    JPanel panel = new JPanel(new VerticalFlowLayout());
    panel.setBorder(IdeBorderFactory.createBorder());

    JLabel nameLabel = new JLabel();
    panel.add(nameLabel);

    myNameField = new JTextField();
    nameLabel.setLabelFor(myNameField);
    myNameField.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        validateOKButton();
      }
    });
    panel.add(myNameField);
    nameLabel.setText(RefactoringBundle.message("name.for.extracted.include.file"));

    JLabel targetDirLabel = new JLabel();
    panel.add(targetDirLabel);

    myTargetDirectoryField = new TextFieldWithBrowseButton();
    myTargetDirectoryField.setText(myCurrentDirectory.getVirtualFile().getPresentableUrl());
    myTargetDirectoryField.addBrowseFolderListener(RefactoringBundle.message("select.target.directory"),
                                                   RefactoringBundle.message("select.target.directory.description"),
                                                   null, FileChooserDescriptorFactory.createSingleFolderDescriptor());

    targetDirLabel.setText(RefactoringBundle.message("extract.to.directory"));
    panel.add(myTargetDirectoryField);

    myTargetDirectoryField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
      public void textChanged(DocumentEvent event) {
        validateOKButton();
      }
    });

    validateOKButton();

    return panel;
  }

  private void validateOKButton() {
    final String fileName = myNameField.getText().trim();
    setOKActionEnabled(myTargetDirectoryField.getText().trim().length() > 0 &&
                       fileName.length() > 0 && fileName.indexOf(File.separatorChar) < 0);
  }

  protected void doOKAction() {
    final Project project = myCurrentDirectory.getProject();
    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
      public void run() {
        final Runnable action = new Runnable() {
          public void run() {
            String directoryName = myTargetDirectoryField.getText().replace(File.separatorChar, '/');
            try {
              PsiDirectory targetDirectory = DirectoryUtil.mkdirs(PsiManager.getInstance(project), directoryName);
              String targetFileName = getTargetFileName();
              targetDirectory.checkCreateFile(targetFileName);
              final String webPath = ExtractJspIncludeFileHandler.getWebPath(targetDirectory);
              myTargetDirectory = webPath == null ? null : targetDirectory;
            }
            catch (IncorrectOperationException e) {
              CommonRefactoringUtil.showErrorMessage(REFACTORING_NAME, e.getMessage(), null, project);
            }
          }
        };
        ApplicationManager.getApplication().runWriteAction(action);
      }
    }, RefactoringBundle.message("create.directory"), null);
    if (myTargetDirectory == null) return;
    super.doOKAction();
  }
}

@@ -253,32 +253,88 @@
 criptorFactory;%0A
+import com.intellij.openapi.fileTypes.LanguageFileType;%0A
 import com.intel
@@ -521,64 +521,8 @@
 ut;%0A
-import com.intellij.openapi.fileTypes.LanguageFileType;%0A
 impo
@@ -627,19 +627,8 @@
 ing.
-util.Common
 Refa
@@ -634,20 +634,22 @@
 actoring
-Util
+Bundle
 ;%0Aimport
@@ -752,32 +752,43 @@
 lij.refactoring.
+util.Common
 RefactoringBundl
@@ -774,38 +774,36 @@
 ommonRefactoring
-Bundle
+Util
 ;%0Aimport com.int
@@ -990,74 +990,8 @@
 nt;%0A
-import java.awt.event.KeyAdapter;%0Aimport java.awt.event.KeyEvent;%0A
 impo
@@ -2268,14 +2268,33 @@
 eld.
-addKey
+getDocument().addDocument
 List
@@ -2302,19 +2302,24 @@
 ner(new 
-Key
+Document
 Adapter(
@@ -2333,33 +2333,42 @@
    p
-ublic void keyPressed(Key
+rotected void textChanged(Document
 Even

change listener