diff --git a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesDialog.java b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesDialog.java index 70e5826f9abe..1a0882add180 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesDialog.java +++ b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesDialog.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.refactoring.copy; import com.intellij.ide.util.DirectoryUtil; @@ -28,9 +27,9 @@ import com.intellij.openapi.help.HelpManager; import com.intellij.openapi.keymap.KeymapUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.DialogWrapperPeer; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.TextComponentAccessor; -import com.intellij.openapi.ui.impl.DialogWrapperPeerImpl; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; @@ -43,7 +42,6 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.util.PathUtil; import com.intellij.util.ui.FormBuilder; import com.intellij.util.ui.UIUtil; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import javax.swing.*; @@ -55,29 +53,33 @@ import java.util.List; public class CopyFilesOrDirectoriesDialog extends DialogWrapper { public static final int MAX_PATH_LENGTH = 70; - private static final String COPY_OPEN_IN_EDITOR = "Copy.OpenInEditor"; - private JLabel myInformationLabel; - private TextFieldWithHistoryWithBrowseButton myTargetDirectoryField; - private JCheckBox myOpenFilesInEditor = createOpenInEditorCB(); + private static final String COPY_OPEN_IN_EDITOR = "Copy.OpenInEditor"; + private static final String RECENT_KEYS = "CopyFile.RECENT_KEYS"; + + public static String shortenPath(VirtualFile file) { + return StringUtil.shortenPathWithEllipsis(file.getPresentableUrl(), MAX_PATH_LENGTH); + } public static JCheckBox createOpenInEditorCB() { - final JCheckBox checkBox = new JCheckBox("Open copy in editor", PropertiesComponent.getInstance().getBoolean(COPY_OPEN_IN_EDITOR, true)); + JCheckBox checkBox = new JCheckBox("Open copy in editor", PropertiesComponent.getInstance().getBoolean(COPY_OPEN_IN_EDITOR, true)); checkBox.setMnemonic('o'); return checkBox; } - + public static void saveOpenInEditorState(boolean selected) { PropertiesComponent.getInstance().setValue(COPY_OPEN_IN_EDITOR, String.valueOf(selected)); } + private JLabel myInformationLabel; + private TextFieldWithHistoryWithBrowseButton myTargetDirectoryField; + private JCheckBox myOpenFilesInEditor = createOpenInEditorCB(); private JTextField myNewNameField; private final Project myProject; private final boolean myShowDirectoryField; private final boolean myShowNewNameField; private PsiDirectory myTargetDirectory; - @NonNls private static final String RECENT_KEYS = "CopyFile.RECENT_KEYS"; private boolean myFileCopy = false; public CopyFilesOrDirectoriesDialog(PsiElement[] elements, PsiDirectory defaultTargetDirectory, Project project, boolean doClone) { @@ -90,9 +92,7 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { throw new IllegalArgumentException("wrong number of elements to clone: " + elements.length); } - setTitle(doClone ? - RefactoringBundle.message("copy.files.clone.title") : - RefactoringBundle.message("copy.files.copy.title")); + setTitle(RefactoringBundle.message(doClone ? "copy.files.clone.title" : "copy.files.copy.title")); init(); if (elements.length == 1) { @@ -100,24 +100,20 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { if (elements[0] instanceof PsiFile) { PsiFile file = (PsiFile)elements[0]; String url = shortenPath(file.getVirtualFile()); - text = doClone ? - RefactoringBundle.message("copy.files.clone.file.0", url) : - RefactoringBundle.message("copy.files.copy.file.0", url); + text = RefactoringBundle.message(doClone ? "copy.files.clone.file.0" : "copy.files.copy.file.0", url); final String fileName = file.getName(); myNewNameField.setText(fileName); final int dotIdx = fileName.lastIndexOf("."); if (dotIdx > -1) { myNewNameField.select(0, dotIdx); - myNewNameField.putClientProperty(DialogWrapperPeerImpl.HAVE_INITIAL_SELECTION, true); + myNewNameField.putClientProperty(DialogWrapperPeer.HAVE_INITIAL_SELECTION, true); } myFileCopy = true; } else { PsiDirectory directory = (PsiDirectory)elements[0]; String url = shortenPath(directory.getVirtualFile()); - text = doClone ? - RefactoringBundle.message("copy.files.clone.directory.0", url) : - RefactoringBundle.message("copy.files.copy.directory.0", url); + text = RefactoringBundle.message(doClone ? "copy.files.clone.directory.0" : "copy.files.copy.directory.0", url); myNewNameField.setText(directory.getName()); } myInformationLabel.setText(text); @@ -137,16 +133,12 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { myOpenFilesInEditor.setVisible(false); } if (myShowDirectoryField) { - myTargetDirectoryField.getChildComponent() - .setText(defaultTargetDirectory == null ? "" : defaultTargetDirectory.getVirtualFile().getPresentableUrl()); + String targetPath = defaultTargetDirectory == null ? "" : defaultTargetDirectory.getVirtualFile().getPresentableUrl(); + myTargetDirectoryField.getChildComponent().setText(targetPath); } validateOKButton(); } - public static String shortenPath(VirtualFile file) { - return StringUtil.shortenPathWithEllipsis(file.getPresentableUrl(), MAX_PATH_LENGTH); - } - private void setMultipleElementCopyLabel(PsiElement[] elements) { boolean allFiles = true; boolean allDirectories = true; @@ -252,11 +244,10 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { String newName = getNewName(); if (newName.length() == 0) { - Messages.showMessageDialog(myProject, RefactoringBundle.message("no.new.name.specified"), RefactoringBundle.message("error.title"), - Messages.getErrorIcon()); + Messages.showErrorDialog(myProject, RefactoringBundle.message("no.new.name.specified"), RefactoringBundle.message("error.title")); return; } - + if (myFileCopy && !PathUtil.isValidFileName(newName)) { Messages.showErrorDialog(myNewNameField, "Name is not a valid file name"); return; @@ -268,8 +259,8 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { final String targetDirectoryName = myTargetDirectoryField.getChildComponent().getText(); if (targetDirectoryName.length() == 0) { - Messages.showMessageDialog(myProject, RefactoringBundle.message("no.target.directory.specified"), - RefactoringBundle.message("error.title"), Messages.getErrorIcon()); + Messages.showErrorDialog(myProject, RefactoringBundle.message("no.target.directory.specified"), + RefactoringBundle.message("error.title")); return; } @@ -285,17 +276,14 @@ public class CopyFilesOrDirectoriesDialog extends DialogWrapper { myTargetDirectory = DirectoryUtil.mkdirs(PsiManager.getInstance(myProject), targetDirectoryName.replace(File.separatorChar, '/')); } - catch (IncorrectOperationException e) { - } + catch (IncorrectOperationException ignored) { } } }); } }, RefactoringBundle.message("create.directory"), null); if (myTargetDirectory == null) { - Messages - .showMessageDialog(myProject, RefactoringBundle.message("cannot.create.directory"), RefactoringBundle.message("error.title"), - Messages.getErrorIcon()); + Messages.showErrorDialog(myProject, RefactoringBundle.message("cannot.create.directory"), RefactoringBundle.message("error.title")); return; } } diff --git a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java index fcce0761bef4..eb2aa59b0a70 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java +++ b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.refactoring.copy; import com.intellij.CommonBundle; @@ -39,11 +38,12 @@ import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.util.HashSet; +import java.util.Set; public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { @Override public boolean canCopy(PsiElement[] elements, boolean fromUpdate) { - HashSet names = new HashSet(); + Set names = new HashSet(); for (PsiElement element : elements) { if (!(element instanceof PsiFileSystemItem)) return false; if (!element.isValid()) return false; @@ -105,6 +105,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { CommonRefactoringUtil.showErrorHint(project, null, e.getMessage(), CommonBundle.getErrorTitle(), null); return; } + copyImpl(elements, newName, targetDirectory, false, openInEditor); } } @@ -118,6 +119,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { PsiDirectory targetDirectory; if (element instanceof PsiDirectory) { targetDirectory = ((PsiDirectory)element).getParentDirectory(); + assert targetDirectory != null : element; } else { targetDirectory = ((PsiFile)element).getContainingDirectory(); @@ -166,7 +168,6 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { } /** - * * @param elements * @param newName can be not null only if elements.length == 1 * @param targetDirectory @@ -175,7 +176,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { private static void copyImpl(@NotNull final PsiElement[] elements, @Nullable final String newName, @NotNull final PsiDirectory targetDirectory, - final boolean doClone, + final boolean doClone, final boolean openInEditor) { if (doClone && elements.length != 1) { throw new IllegalArgumentException("invalid number of elements to clone:" + elements.length); @@ -207,11 +208,11 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { if (!(firstFile instanceof PsiBinaryFile) && openInEditor){ EditorHelper.openInEditor(firstFile); ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - ToolWindowManager.getInstance(project).activateEditorComponent(); - } - }); + @Override + public void run() { + ToolWindowManager.getInstance(project).activateEditorComponent(); + } + }); } } } @@ -219,7 +220,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - Messages.showMessageDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()); + Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title")); } }); } @@ -227,7 +228,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - Messages.showMessageDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()); + Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title")); } }); } @@ -236,36 +237,34 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { ApplicationManager.getApplication().runWriteAction(action); } }; - CommandProcessor.getInstance().executeCommand(project, command, doClone ? - RefactoringBundle.message("copy,handler.clone.files.directories") : - RefactoringBundle.message("copy.handler.copy.files.directories"), null); + + String title = RefactoringBundle.message(doClone ? "copy,handler.clone.files.directories" : "copy.handler.copy.files.directories"); + CommandProcessor.getInstance().executeCommand(project, command, title, null); } /** * @param elementToCopy PsiFile or PsiDirectory * @param newName can be not null only if elements.length == 1 - * @return first copied PsiFile (recursivly); null if no PsiFiles copied + * @return first copied PsiFile (recursively); null if no PsiFiles copied */ @Nullable public static PsiFile copyToDirectory(@NotNull PsiFileSystemItem elementToCopy, @Nullable String newName, - @NotNull PsiDirectory targetDirectory) - throws IncorrectOperationException, IOException { + @NotNull PsiDirectory targetDirectory) throws IncorrectOperationException, IOException { return copyToDirectory(elementToCopy, newName, targetDirectory, null); } /** - * * @param elementToCopy PsiFile or PsiDirectory * @param newName can be not null only if elements.length == 1 - * @param choice - * @return first copied PsiFile (recursivly); null if no PsiFiles copied + * @param choice a horrible way to pass/keep user preference + * @return first copied PsiFile (recursively); null if no PsiFiles copied */ @Nullable public static PsiFile copyToDirectory(@NotNull PsiFileSystemItem elementToCopy, @Nullable String newName, - @NotNull PsiDirectory targetDirectory, int[] choice) - throws IncorrectOperationException, IOException { + @NotNull PsiDirectory targetDirectory, + @Nullable int[] choice) throws IncorrectOperationException, IOException { if (elementToCopy instanceof PsiFile) { PsiFile file = (PsiFile)elementToCopy; String name = newName == null ? file.getName() : newName; @@ -303,23 +302,33 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { } } - public static boolean checkFileExist(PsiDirectory targetDirectory, int[] choice, PsiFile file, String name, final String title) { + public static boolean checkFileExist(PsiDirectory targetDirectory, int[] choice, PsiFile file, String name, String title) { final PsiFile existing = targetDirectory.findFile(name); if (existing != null && !existing.equals(file)) { - int selection = choice == null || choice[0] == -1 ? Messages.showDialog( - String.format("File '%s' already exists in directory '%s'", name, targetDirectory.getVirtualFile().getPath()), - title, - choice == null ? new String[]{"Overwrite", "Skip"} - : new String[]{"Overwrite", "Skip", "Overwrite for all", "Skip for all"}, 0, Messages.getQuestionIcon()) - : choice[0]; + int selection; + if (choice == null || choice[0] == -1) { + String message = String.format("File '%s' already exists in directory '%s'", name, targetDirectory.getVirtualFile().getPath()); + String[] options = choice == null ? new String[]{"Overwrite", "Skip"} + : new String[]{"Overwrite", "Skip", "Overwrite for all", "Skip for all"}; + selection = Messages.showDialog(message, title, options, 0, Messages.getQuestionIcon()); + } + else { + selection = choice[0]; + } + if (choice != null && selection > 1) { choice[0] = selection % 2; selection = choice[0]; } + if (selection == 0 && file != existing) { existing.delete(); - } else return true; + } + else { + return true; + } } + return false; }