use of com.intellij.refactoring.PackageWrapper in project intellij-community by JetBrains.
the class CreateClassToBindFix method run.
public void run() {
final Project project = myEditor.getProject();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myEditor.getFile());
if (sourceRoot == null) {
Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class.not.in.source.root"), CommonBundle.getErrorTitle());
return;
}
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
// 1. Create all necessary packages
final int indexOfLastDot = myClassName.lastIndexOf('.');
final String packageName = myClassName.substring(0, indexOfLastDot != -1 ? indexOfLastDot : 0);
final PsiDirectory psiDirectory;
if (packageName.length() > 0) {
final PackageWrapper packageWrapper = new PackageWrapper(PsiManager.getInstance(project), packageName);
try {
psiDirectory = RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, sourceRoot);
LOG.assertTrue(psiDirectory != null);
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.package", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
return;
}
} else {
psiDirectory = PsiManager.getInstance(project).findDirectory(sourceRoot);
LOG.assertTrue(psiDirectory != null);
}
// 2. Create class in the package
try {
final String name = myClassName.substring(indexOfLastDot != -1 ? indexOfLastDot + 1 : 0);
final PsiClass aClass = JavaDirectoryService.getInstance().createClass(psiDirectory, name);
createBoundFields(aClass);
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(myEditor, UIDesignerBundle.message("error.cannot.create.class", packageName, e.getMessage()), CommonBundle.getErrorTitle()));
}
}, getName(), null));
}
use of com.intellij.refactoring.PackageWrapper in project smali by JesusFreke.
the class ClassMoveTest method doMove.
private void doMove(String oldQualifiedName, final String newPackage) throws Exception {
final PsiClass testClass = myJavaFacade.findClass(oldQualifiedName, GlobalSearchScope.allScope(getProject()));
final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(getProject());
new MoveClassesOrPackagesProcessor(getProject(), new PsiClass[] { testClass }, new AutocreatingSingleSourceRootMoveDestination(new PackageWrapper(getPsiManager(), newPackage), contentSourceRoots.get(0)), false, false, null).run();
}
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-community by JetBrains.
the class ConfigurationRefactoringsTest method move.
private void move(final PsiElement psiElement, String packageName) {
VirtualFile pkgFile = mySource.createPackageDir(packageName);
final PsiDirectory toDir = PsiManager.getInstance(myProject).findDirectory(pkgFile);
assertNotNull(toDir);
PackageWrapper wrapper = PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(toDir));
new MoveClassesOrPackagesProcessor(myProject, new PsiElement[] { psiElement }, new SingleSourceRootMoveDestination(wrapper, toDir), false, false, null).run();
}
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;
}
Aggregations