@Nullable
  private String verifyInnerClassDestination() {
    PsiClass targetClass = findTargetClass();
    if (targetClass == null) return null;

    for (PsiElement element : myElementsToMove) {
      if (PsiTreeUtil.isAncestor(element, targetClass, false)) {
        return RefactoringBundle.message("move.class.to.inner.move.to.self.error");
      }
      final Language targetClassLanguage = targetClass.getLanguage();
      if (!element.getLanguage().equals(targetClassLanguage)) {
        return RefactoringBundle.message(
            "move.to.different.language",
            UsageViewUtil.getType(element),
            ((PsiClass) element).getQualifiedName(),
            targetClass.getQualifiedName());
      }
      if (element.getLanguage().equals(Language.findLanguageByID("Groovy"))) {
        return RefactoringBundle.message("dont.support.inner.classes", "Groovy");
      }
    }

    while (targetClass != null) {
      if (targetClass.getContainingClass() != null
          && !targetClass.hasModifierProperty(PsiModifier.STATIC)) {
        return RefactoringBundle.message("move.class.to.inner.nonstatic.error");
      }
      targetClass = targetClass.getContainingClass();
    }

    return null;
  }
 @NotNull
 private static UsageViewPresentation createPresentation(
     @NotNull PsiElement psiElement,
     @NotNull FindUsagesOptions findUsagesOptions,
     boolean toOpenInNewTab) {
   UsageViewPresentation presentation = new UsageViewPresentation();
   String scopeString =
       findUsagesOptions.searchScope == null
           ? null
           : findUsagesOptions.searchScope.getDisplayName();
   presentation.setScopeText(scopeString);
   String usagesString = generateUsagesString(findUsagesOptions);
   presentation.setUsagesString(usagesString);
   String title =
       scopeString == null
           ? FindBundle.message(
               "find.usages.of.element.panel.title",
               usagesString,
               UsageViewUtil.getLongName(psiElement))
           : FindBundle.message(
               "find.usages.of.element.in.scope.panel.title",
               usagesString,
               UsageViewUtil.getLongName(psiElement),
               scopeString);
   presentation.setTabText(title);
   presentation.setTabName(
       FindBundle.message(
           "find.usages.of.element.tab.name",
           usagesString,
           UsageViewUtil.getShortName(psiElement)));
   presentation.setTargetsNodeText(StringUtil.capitalize(UsageViewUtil.getType(psiElement)));
   presentation.setOpenInNewTab(toOpenInNewTab);
   return presentation;
 }
예제 #3
0
  private void setNewName(@NotNull String newName) {
    if (myPrimaryElement == null) {
      myCommandName = RefactoringBundle.message("renaming.something");
      return;
    }

    myNewName = newName;
    myAllRenames.put(myPrimaryElement, newName);
    myCommandName =
        RefactoringBundle.message(
            "renaming.0.1.to.2",
            UsageViewUtil.getType(myPrimaryElement),
            UsageViewUtil.getDescriptiveName(myPrimaryElement),
            newName);
  }
  @Override
  protected void performRefactoringRename(final String newName, final StartMarkAction markAction) {
    try {
      final PsiNamedElement variable = getVariable();
      if (variable != null && !newName.equals(myOldName)) {
        if (isIdentifier(newName, variable.getLanguage())) {
          final PsiElement substituted = getSubstituted();
          if (substituted == null) {
            return;
          }

          final String commandName =
              RefactoringBundle.message(
                  "renaming.0.1.to.2",
                  UsageViewUtil.getType(variable),
                  UsageViewUtil.getDescriptiveName(variable),
                  newName);
          CommandProcessor.getInstance()
              .executeCommand(
                  myProject,
                  new Runnable() {
                    @Override
                    public void run() {
                      performRenameInner(substituted, newName);
                      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
                    }
                  },
                  commandName,
                  null);
        }
      }
    } finally {
      try {
        ((EditorImpl) InjectedLanguageUtil.getTopLevelEditor(myEditor)).stopDumbLater();
      } finally {
        FinishMarkAction.finish(myProject, myEditor, markAction);
      }
    }
  }
  public void setData(
      PsiElement[] psiElements,
      String targetPackageName,
      final PsiDirectory initialTargetDirectory,
      boolean isTargetDirectoryFixed,
      boolean searchInComments,
      boolean searchForTextOccurences,
      String helpID) {
    myInitialTargetDirectory = initialTargetDirectory;
    myTargetDirectoryFixed = isTargetDirectoryFixed;
    if (targetPackageName.length() != 0) {
      myWithBrowseButtonReference.prependItem(targetPackageName);
      myClassPackageChooser.prependItem(targetPackageName);
    }

    String nameFromCallback =
        myMoveCallback instanceof MoveClassesOrPackagesCallback
            ? ((MoveClassesOrPackagesCallback) myMoveCallback).getElementsToMoveName()
            : null;
    if (nameFromCallback != null) {
      myNameLabel.setText(nameFromCallback);
    } else if (psiElements.length == 1) {
      PsiElement firstElement = psiElements[0];
      if (firstElement instanceof PsiClass) {
        LOG.assertTrue(!MoveClassesOrPackagesImpl.isClassInnerOrLocal((PsiClass) firstElement));
      } else {
        PsiElement parent = firstElement.getParent();
        LOG.assertTrue(parent != null);
      }
      myNameLabel.setText(
          RefactoringBundle.message(
              "move.single.class.or.package.name.label",
              UsageViewUtil.getType(firstElement),
              UsageViewUtil.getLongName(firstElement)));
    } else if (psiElements.length > 1) {
      myNameLabel.setText(
          psiElements[0] instanceof PsiClass
              ? RefactoringBundle.message("move.specified.classes")
              : RefactoringBundle.message("move.specified.packages"));
    }
    selectInitialCard();

    myCbSearchInComments.setSelected(searchInComments);
    myCbSearchTextOccurences.setSelected(searchForTextOccurences);

    ((DestinationFolderComboBox) myDestinationFolderCB)
        .setData(
            myProject,
            myInitialTargetDirectory,
            new Pass<String>() {
              @Override
              public void pass(String s) {
                setErrorText(s);
              }
            },
            myClassPackageChooser.getChildComponent());
    UIUtil.setEnabled(
        myTargetPanel,
        getSourceRoots().length > 0 && isMoveToPackage() && !isTargetDirectoryFixed,
        true);
    validateButtons();
    myHelpID = helpID;
  }
  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();
  }
  public void setData(
      PsiElement[] psiElements,
      String targetPackageName,
      PsiDirectory initialTargetDirectory,
      boolean isTargetDirectoryFixed,
      boolean suggestToMoveToAnotherRoot,
      boolean searchInComments,
      boolean searchForTextOccurences,
      String helpID) {
    myTargetDirectoryFixed = isTargetDirectoryFixed;
    mySuggestToMoveToAnotherRoot = suggestToMoveToAnotherRoot;
    if (targetPackageName.length() != 0) {
      myWithBrowseButtonReference.prependItem(targetPackageName);
      myClassPackageChooser.prependItem(targetPackageName);
    }

    String nameFromCallback =
        myMoveCallback instanceof MoveClassesOrPackagesCallback
            ? ((MoveClassesOrPackagesCallback) myMoveCallback).getElementsToMoveName()
            : null;
    if (nameFromCallback != null) {
      myNameLabel.setText(nameFromCallback);
    } else if (psiElements.length == 1) {
      PsiElement firstElement = psiElements[0];
      if (firstElement instanceof PsiClass) {
        LOG.assertTrue(!MoveClassesOrPackagesImpl.isClassInnerOrLocal((PsiClass) firstElement));
      } else {
        PsiElement parent = firstElement.getParent();
        LOG.assertTrue(parent != null);
      }
      myNameLabel.setText(
          RefactoringBundle.message(
              "move.single.class.or.package.name.label",
              UsageViewUtil.getType(firstElement),
              UsageViewUtil.getLongName(firstElement)));
    } else if (psiElements.length > 1) {
      myNameLabel.setText(
          psiElements[0] instanceof PsiClass
              ? RefactoringBundle.message("move.specified.classes")
              : RefactoringBundle.message("move.specified.packages"));
    }
    selectInitialCard();

    myCbSearchInComments.setSelected(searchInComments);
    myCbSearchTextOccurences.setSelected(searchForTextOccurences);

    if (initialTargetDirectory != null
        && JavaMoveClassesOrPackagesHandler.packageHasMultipleDirectoriesInModule(
            myProject, initialTargetDirectory)) {
      final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
      final Set<VirtualFile> initialRoots = new HashSet<VirtualFile>();
      collectSourceRoots(psiElements, fileIndex, initialRoots);
      if (initialRoots.size() > 1) {
        initialTargetDirectory = null;
      }
    }
    ((DestinationFolderComboBox) myDestinationFolderCB)
        .setData(
            myProject,
            initialTargetDirectory,
            new Pass<String>() {
              @Override
              public void pass(String s) {
                setErrorText(s);
              }
            },
            myHavePackages
                ? myWithBrowseButtonReference.getChildComponent()
                : myClassPackageChooser.getChildComponent());
    UIUtil.setEnabled(
        myTargetPanel,
        !getSourceRoots().isEmpty() && isMoveToPackage() && !isTargetDirectoryFixed,
        true);
    validateButtons();
    myHelpID = helpID;
  }
 private static String getNoUsagesFoundMessage(PsiElement psiElement) {
   String elementType = UsageViewUtil.getType(psiElement);
   String elementName = UsageViewUtil.getShortName(psiElement);
   return FindBundle.message(
       "find.usages.of.element_type.element_name.not.found.message", elementType, elementName);
 }