mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
copy refactoring can copy a mix of files and directories
This commit is contained in:
+11
-22
@@ -8,32 +8,21 @@ import com.intellij.codeInsight.daemon.impl.CollectHighlightsUtil;
|
||||
* @author yole
|
||||
*/
|
||||
public class JavaCopyFilesOrDirectoriesHandler extends CopyFilesOrDirectoriesHandler {
|
||||
protected boolean canCopyFiles(final PsiElement[] elements) {
|
||||
for (PsiElement element : elements) {
|
||||
if (!(element instanceof PsiFile) ||
|
||||
element instanceof PsiClassOwner &&
|
||||
PsiUtilBase.getTemplateLanguageFile(element) != element &&
|
||||
!CollectHighlightsUtil.isOutsideSourceRoot((PsiFile) element)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return super.canCopyFiles(elements);
|
||||
}
|
||||
|
||||
protected boolean canCopyDirectories(final PsiElement[] elements) {
|
||||
if (!super.canCopyDirectories(elements)) return false;
|
||||
|
||||
for (PsiElement element1 : elements) {
|
||||
PsiDirectory directory = (PsiDirectory)element1;
|
||||
|
||||
if (hasPackages(directory)) {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
protected boolean canCopyFile(final PsiFile element) {
|
||||
if (element instanceof PsiClassOwner &&
|
||||
PsiUtilBase.getTemplateLanguageFile(element) != element &&
|
||||
!CollectHighlightsUtil.isOutsideSourceRoot(element)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean canCopyDirectory(PsiDirectory element) {
|
||||
return !hasPackages(element);
|
||||
}
|
||||
|
||||
public static boolean hasPackages(PsiDirectory directory) {
|
||||
if (JavaDirectoryService.getInstance().getPackage(directory) != null) {
|
||||
return true;
|
||||
|
||||
+23
-3
@@ -68,9 +68,7 @@ class CopyFilesOrDirectoriesDialog extends DialogWrapper{
|
||||
myInformationLabel.setText(text);
|
||||
}
|
||||
else {
|
||||
myInformationLabel.setText((elements[0] instanceof PsiFile)?
|
||||
RefactoringBundle.message("copy.files.copy.specified.files.label") :
|
||||
RefactoringBundle.message("copy.files.copy.specified.directories.label"));
|
||||
setMultipleElementCopyLabel(elements);
|
||||
}
|
||||
|
||||
if (myShowDirectoryField) {
|
||||
@@ -79,6 +77,28 @@ class CopyFilesOrDirectoriesDialog extends DialogWrapper{
|
||||
validateOKButton();
|
||||
}
|
||||
|
||||
private void setMultipleElementCopyLabel(PsiElement[] elements) {
|
||||
boolean allFiles = true;
|
||||
boolean allDirectories = true;
|
||||
for (PsiElement element : elements) {
|
||||
if (element instanceof PsiDirectory) {
|
||||
allFiles = false;
|
||||
}
|
||||
else {
|
||||
allDirectories = false;
|
||||
}
|
||||
}
|
||||
if (allFiles) {
|
||||
myInformationLabel.setText(RefactoringBundle.message("copy.files.copy.specified.files.label"));
|
||||
}
|
||||
else if (allDirectories) {
|
||||
myInformationLabel.setText(RefactoringBundle.message("copy.files.copy.specified.directories.label"));
|
||||
}
|
||||
else {
|
||||
myInformationLabel.setText(RefactoringBundle.message("copy.files.copy.specified.mixed.label"));
|
||||
}
|
||||
}
|
||||
|
||||
protected Action[] createActions(){
|
||||
return new Action[]{getOKAction(),getCancelAction(),getHelpAction()};
|
||||
}
|
||||
|
||||
+14
-18
@@ -21,38 +21,34 @@ import java.util.HashSet;
|
||||
|
||||
public class CopyFilesOrDirectoriesHandler implements CopyHandlerDelegate {
|
||||
public boolean canCopy(final PsiElement[] elements) {
|
||||
return canCopyFiles(elements) || canCopyDirectories(elements);
|
||||
}
|
||||
|
||||
protected boolean canCopyFiles(PsiElement[] elements) {
|
||||
// the second 'for' statement is for effectivity - to prevent creation of the 'names' array
|
||||
HashSet<String> names = new HashSet<String>();
|
||||
for (PsiElement element : elements) {
|
||||
if (!(element instanceof PsiFile)) {
|
||||
return false;
|
||||
}
|
||||
PsiFile file = (PsiFile)element;
|
||||
String name = file.getName();
|
||||
if (!(element instanceof PsiFileSystemItem)) return false;
|
||||
|
||||
String name = ((PsiFileSystemItem) element).getName();
|
||||
if (names.contains(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
names.add(name);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean canCopyDirectories(PsiElement[] elements) {
|
||||
for (PsiElement element : elements) {
|
||||
if (!(element instanceof PsiDirectory)) {
|
||||
if (element instanceof PsiFile && !canCopyFile((PsiFile) element)) {
|
||||
return false;
|
||||
}
|
||||
else if (element instanceof PsiDirectory && !canCopyDirectory((PsiDirectory)element)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
PsiElement[] filteredElements = PsiTreeUtil.filterAncestors(elements);
|
||||
return filteredElements.length == elements.length;
|
||||
}
|
||||
|
||||
protected boolean canCopyFile(PsiFile element) {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean canCopyDirectory(PsiDirectory element) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void doCopy(final PsiElement[] elements, PsiDirectory defaultTargetDirectory) {
|
||||
|
||||
@@ -300,6 +300,7 @@ copy.files.new.name.label=New name:
|
||||
no.new.name.specified=No new name specified
|
||||
copy.files.copy.specified.files.label=Copy specified files
|
||||
copy.files.copy.specified.directories.label=Copy specified directories
|
||||
copy.files.copy.specified.mixed.label=Copy specified files and directories
|
||||
copy.files.to.directory.label=To directory:
|
||||
the.file.will.be.copied.to.this.directory=The file will be copied to this directory
|
||||
copy.files.clone.title=Clone
|
||||
|
||||
Reference in New Issue
Block a user