@Nullable private MoveDestination selectDestination() { final String packageName = getTargetPackage().trim(); if (packageName.length() > 0 && !JavaPsiFacade.getInstance(myManager.getProject()) .getNameHelper() .isQualifiedName(packageName)) { Messages.showErrorDialog( myProject, RefactoringBundle.message("please.enter.a.valid.target.package.name"), RefactoringBundle.message("move.title")); return null; } RecentsManager.getInstance(myProject).registerRecentEntry(RECENTS_KEY, packageName); PackageWrapper targetPackage = new PackageWrapper(myManager, packageName); if (!targetPackage.exists()) { final int ret = Messages.showYesNoDialog( myProject, RefactoringBundle.message("package.does.not.exist", packageName), RefactoringBundle.message("move.title"), Messages.getQuestionIcon()); if (ret != 0) return null; } return ((DestinationFolderComboBox) myDestinationFolderCB).selectDirectory(targetPackage, true); }
public MoveClassesOrPackagesDialog( Project project, boolean searchTextOccurences, PsiElement[] elementsToMove, final PsiElement initialTargetElement, MoveCallback moveCallback) { super(project, true); myElementsToMove = elementsToMove; myMoveCallback = moveCallback; myManager = PsiManager.getInstance(myProject); setTitle(MoveHandler.REFACTORING_NAME); mySearchTextOccurencesEnabled = searchTextOccurences; selectInitialCard(); init(); if (initialTargetElement instanceof PsiClass) { myMakeInnerClassOfRadioButton.setSelected(true); myInnerClassChooser.setText(((PsiClass) initialTargetElement).getQualifiedName()); ApplicationManager.getApplication() .invokeLater( new Runnable() { public void run() { myInnerClassChooser.requestFocus(); } }, ModalityState.stateForComponent(myMainPanel)); } else if (initialTargetElement instanceof PsiPackage) { myClassPackageChooser.setText(((PsiPackage) initialTargetElement).getQualifiedName()); } updateControlsEnabled(); myToPackageRadioButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { updateControlsEnabled(); myClassPackageChooser.requestFocus(); } }); myMakeInnerClassOfRadioButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { updateControlsEnabled(); myInnerClassChooser.requestFocus(); } }); }
@Override protected void canRun() throws ConfigurationException { if (isMoveToPackage()) { String name = getTargetPackage().trim(); if (name.length() != 0 && !JavaPsiFacade.getInstance(myManager.getProject()) .getNameHelper() .isQualifiedName(name)) { throw new ConfigurationException("\'" + name + "\' is invalid destination package name"); } } else { if (findTargetClass() == null) throw new ConfigurationException("Destination class not found"); final String validationError = getValidationError(); if (validationError != null) throw new ConfigurationException(validationError); } }
private void createUIComponents() { myMainPanel = new JPanel(); myWithBrowseButtonReference = createPackageChooser(); myClassPackageChooser = createPackageChooser(); myInnerClassChooser = new ClassNameReferenceEditor( PsiManager.getInstance(myProject), null, ProjectScope.getProjectScope(myProject)); myInnerClassChooser.addDocumentListener( new DocumentAdapter() { public void documentChanged(DocumentEvent e) { validateButtons(); } }); // override CardLayout sizing behavior myCardPanel = new JPanel() { public Dimension getMinimumSize() { return myHavePackages ? myMovePackagePanel.getMinimumSize() : myMoveClassPanel.getMinimumSize(); } public Dimension getPreferredSize() { return myHavePackages ? myMovePackagePanel.getPreferredSize() : myMoveClassPanel.getPreferredSize(); } }; myDestinationFolderCB = new DestinationFolderComboBox() { @Override public String getTargetPackage() { return MoveClassesOrPackagesDialog.this.getTargetPackage(); } }; }
@Nullable private PsiClass findTargetClass() { String name = myInnerClassChooser.getText().trim(); return JavaPsiFacade.getInstance(myManager.getProject()) .findClass(name, ProjectScope.getProjectScope(myProject)); }
@Override protected String validateAndCommitData() { PsiManager manager = PsiManager.getInstance(myProject); PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); String name = getMethodName(); if (!JavaPsiFacade.getInstance(manager.getProject()).getNameHelper().isIdentifier(name)) { return RefactoringMessageUtil.getIncorrectIdentifierMessage(name); } if (myMethod.canChangeReturnType() == MethodDescriptor.ReadWriteOption.ReadWrite) { try { ((PsiTypeCodeFragment) myReturnTypeCodeFragment).getType(); } catch (PsiTypeCodeFragment.TypeSyntaxException e) { myReturnTypeField.requestFocus(); return RefactoringBundle.message( "changeSignature.wrong.return.type", myReturnTypeCodeFragment.getText()); } catch (PsiTypeCodeFragment.NoTypeException e) { myReturnTypeField.requestFocus(); return RefactoringBundle.message("changeSignature.no.return.type"); } } List<ParameterTableModelItemBase<ParameterInfoImpl>> parameterInfos = myParametersTableModel.getItems(); final int newParametersNumber = parameterInfos.size(); for (int i = 0; i < newParametersNumber; i++) { final ParameterTableModelItemBase<ParameterInfoImpl> item = parameterInfos.get(i); if (!JavaPsiFacade.getInstance(manager.getProject()) .getNameHelper() .isIdentifier(item.parameter.getName())) { return RefactoringMessageUtil.getIncorrectIdentifierMessage(item.parameter.getName()); } final PsiType type; try { type = ((PsiTypeCodeFragment) parameterInfos.get(i).typeCodeFragment).getType(); } catch (PsiTypeCodeFragment.TypeSyntaxException e) { return RefactoringBundle.message( "changeSignature.wrong.type.for.parameter", item.typeCodeFragment.getText(), item.parameter.getName()); } catch (PsiTypeCodeFragment.NoTypeException e) { return RefactoringBundle.message( "changeSignature.no.type.for.parameter", item.parameter.getName()); } item.parameter.setType(type); if (type instanceof PsiEllipsisType && i != newParametersNumber - 1) { return RefactoringBundle.message("changeSignature.vararg.not.last"); } if (item.parameter.oldParameterIndex < 0) { item.parameter.defaultValue = ApplicationManager.getApplication() .runWriteAction( new Computable<String>() { @Override public String compute() { return JavaCodeStyleManager.getInstance(myProject) .qualifyClassReferences(item.defaultValueCodeFragment) .getText(); } }); String def = item.parameter.defaultValue; def = def.trim(); if (!(type instanceof PsiEllipsisType)) { try { if (!StringUtil.isEmpty(def)) { factory.createExpressionFromText(def, null); } } catch (IncorrectOperationException e) { return e.getMessage(); } } } } ThrownExceptionInfo[] exceptionInfos = myExceptionsModel.getThrownExceptions(); PsiTypeCodeFragment[] typeCodeFragments = myExceptionsModel.getTypeCodeFragments(); for (int i = 0; i < exceptionInfos.length; i++) { ThrownExceptionInfo exceptionInfo = exceptionInfos[i]; PsiTypeCodeFragment typeCodeFragment = typeCodeFragments[i]; try { PsiType type = typeCodeFragment.getType(); if (!(type instanceof PsiClassType)) { return RefactoringBundle.message( "changeSignature.wrong.type.for.exception", typeCodeFragment.getText()); } PsiClassType throwable = JavaPsiFacade.getInstance(myProject) .getElementFactory() .createTypeByFQClassName("java.lang.Throwable", type.getResolveScope()); if (!throwable.isAssignableFrom(type)) { return RefactoringBundle.message( "changeSignature.not.throwable.type", typeCodeFragment.getText()); } exceptionInfo.setType((PsiClassType) type); } catch (PsiTypeCodeFragment.TypeSyntaxException e) { return RefactoringBundle.message( "changeSignature.wrong.type.for.exception", typeCodeFragment.getText()); } catch (PsiTypeCodeFragment.NoTypeException e) { return RefactoringBundle.message("changeSignature.no.type.for.exception"); } } // warnings try { if (myMethod.canChangeReturnType() == MethodDescriptor.ReadWriteOption.ReadWrite) { if (!RefactoringUtil.isResolvableType( ((PsiTypeCodeFragment) myReturnTypeCodeFragment).getType())) { if (Messages.showOkCancelDialog( myProject, RefactoringBundle.message( "changeSignature.cannot.resolve.return.type", myReturnTypeCodeFragment.getText()), RefactoringBundle.message("changeSignature.refactoring.name"), Messages.getWarningIcon()) != 0) { return EXIT_SILENTLY; } } } for (ParameterTableModelItemBase<ParameterInfoImpl> item : parameterInfos) { if (!RefactoringUtil.isResolvableType( ((PsiTypeCodeFragment) item.typeCodeFragment).getType())) { if (Messages.showOkCancelDialog( myProject, RefactoringBundle.message( "changeSignature.cannot.resolve.parameter.type", item.typeCodeFragment.getText(), item.parameter.getName()), RefactoringBundle.message("changeSignature.refactoring.name"), Messages.getWarningIcon()) != 0) { return EXIT_SILENTLY; } } } } catch (PsiTypeCodeFragment.IncorrectTypeException ignored) { } return null; }