protected void performRefactoring(
      Project project,
      PsiDirectory directory,
      PsiPackage aPackage,
      boolean searchInComments,
      boolean searchForTextOccurences) {
    final VirtualFile sourceRoot =
        ProjectRootManager.getInstance(project)
            .getFileIndex()
            .getSourceRootForFile(directory.getVirtualFile());
    if (sourceRoot == null) {
      Messages.showErrorDialog(
          project,
          RefactoringBundle.message("destination.directory.does.not.correspond.to.any.package"),
          RefactoringBundle.message("cannot.move"));
      return;
    }
    final JavaRefactoringFactory factory = JavaRefactoringFactory.getInstance(project);
    final MoveDestination destination =
        myPreserveSourceRoot.isSelected() && myPreserveSourceRoot.isVisible()
            ? factory.createSourceFolderPreservingMoveDestination(aPackage.getQualifiedName())
            : factory.createSourceRootMoveDestination(aPackage.getQualifiedName(), sourceRoot);

    MoveClassesOrPackagesProcessor processor =
        new MoveClassesOrPackagesProcessor(
            myDirectory.getProject(),
            myElementsToMove,
            destination,
            searchInComments,
            searchForTextOccurences,
            myMoveCallback);
    if (processor.verifyValidPackageName()) {
      processor.run();
    }
  }
  public MoveClassesOrPackagesToNewDirectoryDialog(
      @NotNull final PsiDirectory directory,
      PsiElement[] elementsToMove,
      boolean canShowPreserveSourceRoots,
      final MoveCallback moveCallback) {
    super(false);
    setTitle(MoveHandler.REFACTORING_NAME);
    myDirectory = directory;
    myElementsToMove = elementsToMove;
    myMoveCallback = moveCallback;
    myDestDirectoryField.setText(
        FileUtil.toSystemDependentName(directory.getVirtualFile().getPath()));
    final FileChooserDescriptor descriptor =
        new FileChooserDescriptor(false, true, false, false, false, false);
    myDestDirectoryField
        .getButton()
        .addActionListener(
            new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                final VirtualFile[] files =
                    FileChooser.chooseFiles(
                        myDirectory.getProject(), descriptor, directory.getVirtualFile());
                if (files.length == 1) {
                  myDestDirectoryField.setText(FileUtil.toSystemDependentName(files[0].getPath()));
                }
              }
            });
    if (elementsToMove.length == 1) {
      PsiElement firstElement = elementsToMove[0];
      myNameLabel.setText(
          RefactoringBundle.message(
              "move.single.class.or.package.name.label",
              UsageViewUtil.getType(firstElement),
              UsageViewUtil.getLongName(firstElement)));
    } else if (elementsToMove.length > 1) {
      myNameLabel.setText(
          elementsToMove[0] instanceof PsiClass
              ? RefactoringBundle.message("move.specified.classes")
              : RefactoringBundle.message("move.specified.packages"));
    }
    final JavaRefactoringSettings refactoringSettings = JavaRefactoringSettings.getInstance();
    mySearchInCommentsAndStringsCheckBox.setSelected(refactoringSettings.MOVE_SEARCH_IN_COMMENTS);
    mySearchForTextOccurrencesCheckBox.setSelected(refactoringSettings.MOVE_SEARCH_FOR_TEXT);

    myDestDirectoryField
        .getTextField()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              public void textChanged(DocumentEvent event) {
                setOKActionEnabled(myDestDirectoryField.getText().length() > 0);
              }
            });

    if (canShowPreserveSourceRoots) {
      final Set<VirtualFile> sourceRoots = new HashSet<VirtualFile>();
      final ProjectFileIndex fileIndex =
          ProjectRootManager.getInstance(directory.getProject()).getFileIndex();
      for (PsiElement element : elementsToMove) {
        if (element instanceof PsiPackage) {
          for (PsiDirectory psiDirectory : ((PsiPackage) element).getDirectories()) {
            sourceRoots.add(fileIndex.getSourceRootForFile(psiDirectory.getVirtualFile()));
          }
        } else if (element instanceof PsiClass) {
          final PsiDirectory psiDirectory = element.getContainingFile().getContainingDirectory();
          LOG.assertTrue(psiDirectory != null);
          if (psiDirectory != null) {
            sourceRoots.add(psiDirectory.getVirtualFile());
          }
        }
      }
      myPreserveSourceRoot.setVisible(sourceRoots.size() > 1);
    }
    init();
  }