--- /dev/null
+/*
+ * Copyright 2000-2015 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.refactoring.move;
+
+import com.intellij.openapi.fileChooser.FileChooserDescriptor;
+import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.ProjectRootManager;
+import com.intellij.openapi.ui.TextComponentAccessor;
+import com.intellij.openapi.ui.TextFieldWithBrowseButton;
+import com.intellij.refactoring.ui.RefactoringDialog;
+import com.intellij.ui.components.JBLabel;
+import com.intellij.util.ui.update.UiNotifyConnector;
+import com.jetbrains.python.PyBundle;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import javax.swing.*;
+import java.io.File;
+
+/**
+ * @author Mikhail Golubev
+ */
+public abstract class PyBaseMoveDialog extends RefactoringDialog {
+ protected JPanel myCenterPanel;
+ protected JPanel myExtraPanel;
+ protected TextFieldWithBrowseButton myBrowseFieldWithButton;
+ protected JBLabel myDescription;
+ protected JTextField mySourcePathField;
+
+ public PyBaseMoveDialog(Project project, @NotNull String sourcePath, @NotNull String destinationPath) {
+ super(project, true);
+ mySourcePathField.setText(sourcePath);
+ myBrowseFieldWithButton.setText(destinationPath);
+ final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor();
+ descriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots());
+ descriptor.withTreeRootVisible(true);
+ myBrowseFieldWithButton.addBrowseFolderListener(PyBundle.message("refactoring.move.choose.destination.file.title"),
+ null,
+ project,
+ descriptor,
+ TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
+ }
+
+ @Override
+ protected void init() {
+ UiNotifyConnector.doWhenFirstShown(myCenterPanel, new Runnable() {
+ @Override
+ public void run() {
+ doWhenFirstShown();
+ }
+ });
+ super.init();
+ }
+
+ protected void doWhenFirstShown() {
+ preselectLastPathComponent(myBrowseFieldWithButton.getTextField());
+ }
+
+ protected static void preselectLastPathComponent(@NotNull JTextField field) {
+ final String text = field.getText();
+ final int start = text.lastIndexOf(File.separatorChar);
+ final int lastDotIndex = text.lastIndexOf('.');
+ final int end = lastDotIndex < 0 ? text.length() : lastDotIndex;
+ if (start + 1 < end) {
+ field.select(start + 1, end);
+ }
+ }
+
+ @Override
+ protected abstract String getHelpId();
+
+ @Nullable
+ @Override
+ protected abstract String getDimensionServiceKey();
+
+ @Nullable
+ @Override
+ protected JComponent createCenterPanel() {
+ return myCenterPanel;
+ }
+
+ @Override
+ protected void doAction() {
+ close(OK_EXIT_CODE);
+ }
+
+ @Override
+ public JComponent getPreferredFocusedComponent() {
+ return myBrowseFieldWithButton.getTextField();
+ }
+
+ @NotNull
+ public String getTargetPath() {
+ return myBrowseFieldWithButton.getText();
+ }
+}
package com.jetbrains.python.refactoring.move;
import com.intellij.ide.util.PropertiesComponent;
-import com.intellij.openapi.fileChooser.FileChooserDescriptor;
-import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.DialogWrapperPeer;
-import com.intellij.openapi.ui.TextComponentAccessor;
-import com.intellij.openapi.ui.TextFieldWithBrowseButton;
-import com.intellij.openapi.util.io.FileUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.refactoring.classMembers.MemberInfoChange;
import com.intellij.refactoring.classMembers.MemberInfoModel;
import com.intellij.refactoring.ui.AbstractMemberSelectionTable;
-import com.intellij.refactoring.ui.RefactoringDialog;
import com.intellij.ui.HideableDecorator;
import com.intellij.ui.RowIcon;
-import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
-import com.intellij.util.ui.update.UiNotifyConnector;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyElement;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*;
-import java.io.File;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
/**
* @author Mikhail Golubev
*/
-public class PyMoveModuleMembersDialog extends RefactoringDialog {
+public class PyMoveModuleMembersDialog extends PyBaseMoveDialog {
@NonNls private final static String BULK_MOVE_TABLE_VISIBLE = "python.move.module.members.dialog.show.table";
/**
private final TopLevelSymbolsSelectionTable myMemberSelectionTable;
private final PyModuleMemberInfoModel myModuleMemberModel;
private final boolean mySeveralElementsSelected;
- private JPanel myCenterPanel;
- private JPanel myTablePanel;
- private TextFieldWithBrowseButton myBrowseFieldWithButton;
- private JBLabel myDescription;
- private JTextField mySourcePathField;
/**
* Either creates new dialog or return singleton instance initialized with {@link #setInstanceToReplace)}.
*
* @param project dialog project
* @param elements elements to move
- * @param destination destination where elements have to be moved
- * @return dialog
+ * @param source
+ *@param destination destination where elements have to be moved @return dialog
*/
- public static PyMoveModuleMembersDialog getInstance(@NotNull final Project project,
- @NotNull final List<PsiNamedElement> elements,
- @Nullable final String destination) {
- return ourInstanceToReplace != null ? ourInstanceToReplace : new PyMoveModuleMembersDialog(project, elements, destination);
+ public static PyMoveModuleMembersDialog getInstance(@NotNull Project project,
+ @NotNull List<PsiNamedElement> elements,
+ @NotNull String source,
+ @NotNull String destination) {
+ return ourInstanceToReplace != null ? ourInstanceToReplace : new PyMoveModuleMembersDialog(project, elements, source, destination);
}
/**
/**
* @param project dialog project
* @param elements elements to move
+ * @param source
* @param destination destination where elements have to be moved
*/
- protected PyMoveModuleMembersDialog(@NotNull Project project, @NotNull final List<PsiNamedElement> elements, @Nullable String destination) {
- super(project, true);
+ protected PyMoveModuleMembersDialog(@NotNull Project project,
+ @NotNull List<PsiNamedElement> elements,
+ @NotNull String source,
+ @NotNull String destination) {
+ super(project, source, destination);
assert !elements.isEmpty();
final PsiNamedElement firstElement = elements.get(0);
setTitle(PyBundle.message("refactoring.move.module.members.dialog.title"));
- final String sourceFilePath = getContainingFileName(firstElement);
- mySourcePathField.setText(sourceFilePath);
- if (destination == null) {
- destination = sourceFilePath;
- }
- myBrowseFieldWithButton.setText(destination);
- final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor();
- descriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots());
- descriptor.withTreeRootVisible(true);
- myBrowseFieldWithButton.addBrowseFolderListener(PyBundle.message("refactoring.move.module.members.dialog.choose.destination.file.title"),
- null,
- project,
- descriptor,
- TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
-
final PyFile pyFile = (PyFile)firstElement.getContainingFile();
myModuleMemberModel = new PyModuleMemberInfoModel(pyFile);
description = PyBundle.message("refactoring.move.module.members.dialog.description.selection");
}
myDescription.setText(description);
- final HideableDecorator decorator = new HideableDecorator(myTablePanel, PyBundle.message("refactoring.move.module.members.dialog.table.title"), true) {
+ final HideableDecorator decorator = new HideableDecorator(myExtraPanel, PyBundle.message("refactoring.move.module.members.dialog.table.title"), true) {
@Override
protected void on() {
super.on();
}
});
- UiNotifyConnector.doWhenFirstShown(myCenterPanel, () -> {
- enlargeDialogHeightIfNecessary();
- preselectLastPathComponent(myBrowseFieldWithButton.getTextField());
- });
init();
}
+ @Override
+ protected void doWhenFirstShown() {
+ super.doWhenFirstShown();
+ enlargeDialogHeightIfNecessary();
+ }
+
private void enlargeDialogHeightIfNecessary() {
if (mySeveralElementsSelected && !PropertiesComponent.getInstance(getProject()).getBoolean(BULK_MOVE_TABLE_VISIBLE)) {
final DialogWrapperPeer peer = getPeer();
}
}
- private static void preselectLastPathComponent(@NotNull JTextField field) {
- final String text = field.getText();
- final int start = text.lastIndexOf(File.separatorChar);
- final int lastDotIndex = text.lastIndexOf('.');
- final int end = lastDotIndex < 0 ? text.length() : lastDotIndex;
- if (start + 1 < end) {
- field.select(start + 1, end);
- }
- }
-
@Nullable
@Override
protected String getDimensionServiceKey() {
return "#com.jetbrains.python.refactoring.move.PyMoveModuleMembersDialog";
}
- @Nullable
- @Override
- protected JComponent createCenterPanel() {
- return myCenterPanel;
- }
-
- @Override
- protected void doAction() {
- close(OK_EXIT_CODE);
- }
-
@Override
protected String getHelpId() {
return "python.reference.moveModuleMembers";
}
- @Override
- public JComponent getPreferredFocusedComponent() {
- return myBrowseFieldWithButton.getTextField();
- }
-
@Override
protected boolean areButtonsValid() {
return !myMemberSelectionTable.getSelectedMemberInfos().isEmpty();
}
- @NotNull
- public String getTargetPath() {
- return myBrowseFieldWithButton.getText();
- }
-
/**
* @return selected elements in the same order as they are declared in the original file
*/
return ContainerUtil.mapNotNull(moduleMembers, element -> new PyModuleMemberInfo(element));
}
- @NotNull
- private static String getContainingFileName(@NotNull PsiElement element) {
- final VirtualFile file = element.getContainingFile().getVirtualFile();
- if (file != null) {
- return FileUtil.toSystemDependentName(file.getPath());
- }
- else {
- return "";
- }
- }
-
static class TopLevelSymbolsSelectionTable extends AbstractMemberSelectionTable<PyElement, PyModuleMemberInfo> {
public TopLevelSymbolsSelectionTable(Collection<PyModuleMemberInfo> memberInfos,
@Nullable MemberInfoModel<PyElement, PyModuleMemberInfo> memberInfoModel) {