use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class MoveInnerDialog method getTargetContainer.
@Nullable
private PsiElement getTargetContainer() {
if (myTargetContainer instanceof PsiDirectory) {
final PsiDirectory psiDirectory = (PsiDirectory) myTargetContainer;
PsiPackage oldPackage = getTargetPackage();
String name = oldPackage == null ? "" : oldPackage.getQualifiedName();
final String targetName = myPackageNameField.getText();
if (!Comparing.equal(name, targetName)) {
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(myProject);
final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(myProject), targetName);
final VirtualFile targetSourceRoot;
if (contentSourceRoots.size() > 1) {
PsiPackage targetPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetName);
PsiDirectory initialDir = null;
if (targetPackage != null) {
final PsiDirectory[] directories = targetPackage.getDirectories();
final VirtualFile root = projectRootManager.getFileIndex().getSourceRootForFile(psiDirectory.getVirtualFile());
for (PsiDirectory dir : directories) {
if (Comparing.equal(projectRootManager.getFileIndex().getSourceRootForFile(dir.getVirtualFile()), root)) {
initialDir = dir;
break;
}
}
}
final VirtualFile sourceRoot = MoveClassesOrPackagesUtil.chooseSourceRoot(newPackage, contentSourceRoots, initialDir);
if (sourceRoot == null)
return null;
targetSourceRoot = sourceRoot;
} else {
targetSourceRoot = contentSourceRoots.get(0);
}
PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
if (dir == null) {
dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {
public PsiDirectory compute() {
try {
return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
} catch (IncorrectOperationException e) {
return null;
}
}
});
}
return dir;
}
}
return myTargetContainer;
}
use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class DestinationFolderComboBox method setData.
public void setData(final Project project, final PsiDirectory initialTargetDirectory, final Pass<String> errorMessageUpdater, final EditorComboBox editorComboBox) {
myInitialTargetDirectory = initialTargetDirectory;
mySourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
new ComboboxSpeedSearch(getComboBox()) {
@Override
protected String getElementText(Object element) {
if (element == NULL_WRAPPER)
return LEAVE_IN_SAME_SOURCE_ROOT;
if (element instanceof DirectoryChooser.ItemWrapper) {
final VirtualFile virtualFile = ((DirectoryChooser.ItemWrapper) element).getDirectory().getVirtualFile();
final Module module = ModuleUtil.findModuleForFile(virtualFile, project);
if (module != null) {
return module.getName();
}
}
return super.getElementText(element);
}
};
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
getComboBox().setRenderer(new ListCellRendererWrapper<DirectoryChooser.ItemWrapper>() {
@Override
public void customize(JList list, DirectoryChooser.ItemWrapper itemWrapper, int index, boolean selected, boolean hasFocus) {
if (itemWrapper != NULL_WRAPPER && itemWrapper != null) {
setIcon(itemWrapper.getIcon(fileIndex));
setText(itemWrapper.getRelativeToProjectPath());
} else {
setText(LEAVE_IN_SAME_SOURCE_ROOT);
}
}
});
final VirtualFile initialSourceRoot = initialTargetDirectory != null ? fileIndex.getSourceRootForFile(initialTargetDirectory.getVirtualFile()) : null;
final VirtualFile[] selection = new VirtualFile[] { initialSourceRoot };
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
VirtualFile root = MoveClassesOrPackagesUtil.chooseSourceRoot(new PackageWrapper(PsiManager.getInstance(project), getTargetPackage()), mySourceRoots, initialTargetDirectory);
if (root == null)
return;
final ComboBoxModel model = getComboBox().getModel();
for (int i = 0; i < model.getSize(); i++) {
DirectoryChooser.ItemWrapper item = (DirectoryChooser.ItemWrapper) model.getElementAt(i);
if (item != NULL_WRAPPER && Comparing.equal(fileIndex.getSourceRootForFile(item.getDirectory().getVirtualFile()), root)) {
getComboBox().setSelectedItem(item);
getComboBox().repaint();
return;
}
}
setComboboxModel(getComboBox(), root, root, fileIndex, mySourceRoots, project, true, errorMessageUpdater);
}
});
editorComboBox.addDocumentListener(new DocumentAdapter() {
@Override
public void documentChanged(DocumentEvent e) {
JComboBox comboBox = getComboBox();
DirectoryChooser.ItemWrapper selectedItem = (DirectoryChooser.ItemWrapper) comboBox.getSelectedItem();
setComboboxModel(comboBox, selectedItem != null && selectedItem != NULL_WRAPPER ? fileIndex.getSourceRootForFile(selectedItem.getDirectory().getVirtualFile()) : initialSourceRoot, selection[0], fileIndex, mySourceRoots, project, false, errorMessageUpdater);
}
});
setComboboxModel(getComboBox(), initialSourceRoot, selection[0], fileIndex, mySourceRoots, project, false, errorMessageUpdater);
getComboBox().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object selectedItem = getComboBox().getSelectedItem();
updateErrorMessage(errorMessageUpdater, fileIndex, selectedItem);
if (selectedItem instanceof DirectoryChooser.ItemWrapper && selectedItem != NULL_WRAPPER) {
PsiDirectory directory = ((DirectoryChooser.ItemWrapper) selectedItem).getDirectory();
if (directory != null) {
selection[0] = fileIndex.getSourceRootForFile(directory.getVirtualFile());
}
}
}
});
}
use of com.intellij.refactoring.PackageWrapper in project intellij-plugins by JetBrains.
the class CreateValidationXmlIntention method createValidationXml.
private static void createValidationXml(final Project project, final PsiClass actionClass, @Nullable final String path) {
final PsiManager manager = PsiManager.getInstance(project);
final String actionClassQualifiedName = actionClass.getQualifiedName();
assert actionClassQualifiedName != null;
final PackageWrapper targetPackage = new PackageWrapper(manager, StringUtil.getPackageName(actionClassQualifiedName));
final Module module = ModuleUtilCore.findModuleForPsiElement(actionClass);
assert module != null;
final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.PRODUCTION);
final VirtualFile sourceRoot = sourceRoots.size() == 1 ? sourceRoots.get(0) : MoveClassesOrPackagesUtil.chooseSourceRoot(targetPackage, sourceRoots, manager.findDirectory(sourceRoots.get(0)));
if (sourceRoot == null) {
return;
}
final PsiDirectory directory = manager.findDirectory(sourceRoot);
assert directory != null : sourceRoot.getPresentableUrl();
final FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
final FileTemplate validationTemplate = templateManager.getJ2eeTemplate(StrutsFileTemplateGroupDescriptorFactory.VALIDATION_XML);
final PsiDirectory packageDirectoryInSourceRoot = RefactoringUtil.createPackageDirectoryInSourceRoot(targetPackage, sourceRoot);
try {
final String filename = path == null ? actionClass.getName() + "-validation.xml" : actionClass.getName() + "-" + path + "-validation.xml";
final PsiElement psiElement = FileTemplateUtil.createFromTemplate(validationTemplate, filename, null, packageDirectoryInSourceRoot);
NavigationUtil.activateFileWithPsiElement(psiElement, true);
} catch (Exception e) {
throw new IncorrectOperationException("error creating validation.xml", (Throwable) e);
}
}
use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class WrapReturnValueDialog method doAction.
protected void doAction() {
final boolean useExistingClass = useExistingClassButton.isSelected();
final boolean createInnerClass = myCreateInnerClassButton.isSelected();
final String existingClassName = existingClassField.getText().trim();
final String className;
final String packageName;
if (useExistingClass) {
className = StringUtil.getShortName(existingClassName);
packageName = StringUtil.getPackageName(existingClassName);
} else if (createInnerClass) {
className = getInnerClassName();
packageName = "";
} else {
className = getClassName();
packageName = getPackageName();
}
invokeRefactoring(new WrapReturnValueProcessor(className, packageName, ((DestinationFolderComboBox) myDestinationCb).selectDirectory(new PackageWrapper(sourceMethod.getManager(), packageName), false), sourceMethod, useExistingClass, createInnerClass, (PsiField) myFieldsCombo.getSelectedItem()));
}
use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class CreateClassDialog method doOKAction.
@Override
protected void doOKAction() {
RecentsManager.getInstance(myProject).registerRecentEntry(RECENTS_KEY, myPackageComponent.getText());
final String packageName = getPackageName();
final String[] errorString = new String[1];
CommandProcessor.getInstance().executeCommand(myProject, () -> {
try {
final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(myProject), packageName);
final MoveDestination destination = myDestinationCB.selectDirectory(targetPackage, false);
if (destination == null)
return;
myTargetDirectory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
PsiDirectory baseDir = getBaseDir(packageName);
if (baseDir == null && destination instanceof MultipleRootsMoveDestination) {
errorString[0] = "Destination not found for package '" + packageName + "'";
return null;
}
return destination.getTargetDirectory(baseDir);
}
});
if (myTargetDirectory == null) {
return;
}
errorString[0] = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getClassName());
} catch (IncorrectOperationException e) {
errorString[0] = e.getMessage();
}
}, CodeInsightBundle.message("create.directory.command"), null);
if (errorString[0] != null) {
if (errorString[0].length() > 0) {
Messages.showMessageDialog(myProject, errorString[0], CommonBundle.getErrorTitle(), Messages.getErrorIcon());
}
return;
}
super.doOKAction();
}
Aggregations