mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
(no message)
This commit is contained in:
+13
-110
@@ -1,124 +1,27 @@
|
||||
package com.intellij.refactoring.convertToInstanceMethod;
|
||||
|
||||
import com.intellij.refactoring.move.moveInstanceMethod.MoveInstanceMethodDialog;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiVariable;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.refactoring.ui.BaseRefactoringDialog;
|
||||
import com.intellij.refactoring.ui.VisibilityPanel;
|
||||
import com.intellij.refactoring.RefactoringSettings;
|
||||
import com.intellij.refactoring.util.VisibilityUtil;
|
||||
import com.intellij.ui.ScrollPaneFactory;
|
||||
import com.intellij.ui.IdeBorderFactory;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author dsl
|
||||
* @author ven
|
||||
*/
|
||||
public class ConvertToInstanceMethodDialog extends BaseRefactoringDialog {
|
||||
public class ConvertToInstanceMethodDialog extends MoveInstanceMethodDialog {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.convertToInstanceMethod.ConvertToInstanceMethodDialog");
|
||||
private final PsiMethod myMethod;
|
||||
private final PsiParameter[] myParameters;
|
||||
private JList myList;
|
||||
|
||||
private VisibilityPanel myVisibilityPanel;
|
||||
|
||||
ConvertToInstanceMethodDialog(final PsiMethod method, final PsiParameter[] parameters) {
|
||||
super(method.getProject(), true);
|
||||
myMethod = method;
|
||||
myParameters = parameters;
|
||||
setTitle(ConvertToInstanceMethodHandler.REFACTORING_NAME);
|
||||
init();
|
||||
public ConvertToInstanceMethodDialog(final PsiMethod method, final PsiVariable[] variables, final String refactoringName) {
|
||||
super(method, variables, refactoringName);
|
||||
}
|
||||
|
||||
protected JComponent createCenterPanel() {
|
||||
final Box vBox = Box.createVerticalBox();
|
||||
final Box labelBox = Box.createHorizontalBox();
|
||||
final JLabel jLabel = new JLabel("Select an instance parameter:");
|
||||
jLabel.setDisplayedMnemonic('i');
|
||||
labelBox.add(jLabel);
|
||||
labelBox.add(Box.createHorizontalGlue());
|
||||
vBox.add(labelBox);
|
||||
vBox.add(Box.createVerticalStrut(4));
|
||||
myList = new JList(new MyListModel());
|
||||
myList.setCellRenderer(new MyListCellRenderer());
|
||||
myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
myList.setSelectedIndex(0);
|
||||
myList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
getOKAction().setEnabled(!myList.getSelectionModel().isSelectionEmpty());
|
||||
}
|
||||
});
|
||||
final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myList);
|
||||
final JPanel hBox = new JPanel(new GridBagLayout());
|
||||
final GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||
|
||||
gbConstraints.fill = GridBagConstraints.BOTH;
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.weighty = 0;
|
||||
gbConstraints.gridheight = 1;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 0;
|
||||
gbConstraints.insets = new Insets(0, 0, 0, 0);
|
||||
hBox.add(scrollPane, gbConstraints);
|
||||
hBox.add(Box.createHorizontalStrut(4));
|
||||
gbConstraints.weightx = 0;
|
||||
gbConstraints.fill = GridBagConstraints.VERTICAL;
|
||||
gbConstraints.gridx++;
|
||||
myVisibilityPanel = createVisibilityPanel ();
|
||||
hBox.add (myVisibilityPanel, gbConstraints);
|
||||
|
||||
vBox.add(hBox);
|
||||
return vBox;
|
||||
}
|
||||
|
||||
private VisibilityPanel createVisibilityPanel() {
|
||||
final VisibilityPanel visibilityPanel = new VisibilityPanel (false);
|
||||
visibilityPanel.setVisibility (VisibilityUtil.getVisibilityModifier (myMethod.getModifierList()));
|
||||
return visibilityPanel;
|
||||
}
|
||||
|
||||
protected void doAction() {
|
||||
final PsiParameter targetParameter = (PsiParameter)myList.getSelectedValue();
|
||||
LOG.assertTrue(targetParameter != null);
|
||||
final PsiVariable targetVariable = (PsiVariable)myList.getSelectedValue();
|
||||
LOG.assertTrue(targetVariable instanceof PsiParameter);
|
||||
final ConvertToInstanceMethodProcessor processor = new ConvertToInstanceMethodProcessor(myMethod.getProject(),
|
||||
myMethod, targetParameter, myVisibilityPanel.getVisibility());
|
||||
final PsiClass targetClass = processor.getTargetClass();
|
||||
if (targetClass.isInterface()) {
|
||||
final String message = UsageViewUtil.getDescriptiveName(targetClass) + " is an interface. \n" +
|
||||
"Method implementation will be added to all directly implementing classes.\n Proceed?";
|
||||
|
||||
final int result = Messages.showYesNoDialog(myProject, message, ConvertToInstanceMethodHandler.REFACTORING_NAME,
|
||||
Messages.getQuestionIcon());
|
||||
if (result != 0) return;
|
||||
}
|
||||
myMethod, (PsiParameter)targetVariable,
|
||||
myVisibilityPanel.getVisibility());
|
||||
if (!verifyTargetClass(processor.getTargetClass())) return;
|
||||
invokeRefactoring(processor);
|
||||
}
|
||||
|
||||
private class MyListModel extends AbstractListModel {
|
||||
public int getSize() {
|
||||
return myParameters.length;
|
||||
}
|
||||
|
||||
public Object getElementAt(int index) {
|
||||
return myParameters[index];
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyListCellRenderer extends DefaultListCellRenderer {
|
||||
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||||
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
final PsiParameter psiParameter = (PsiParameter)value;
|
||||
final String text = PsiFormatUtil.formatVariable(psiParameter,
|
||||
PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE,
|
||||
PsiSubstitutor.EMPTY);
|
||||
setText(text);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -90,9 +90,9 @@ public class ConvertToInstanceMethodHandler implements RefactoringActionHandler
|
||||
HelpID.CONVERT_TO_INSTANCE_METHOD, project);
|
||||
return;
|
||||
}
|
||||
final ConvertToInstanceMethodDialog dialog = new ConvertToInstanceMethodDialog(
|
||||
|
||||
new ConvertToInstanceMethodDialog(
|
||||
method,
|
||||
(PsiParameter[])suitableParameters.toArray(new PsiParameter[suitableParameters.size()]));
|
||||
dialog.show();
|
||||
suitableParameters.toArray(new PsiParameter[suitableParameters.size()]), REFACTORING_NAME).show();
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -121,14 +121,14 @@ public class ConvertToInstanceMethodProcessor extends BaseRefactoringProcessor {
|
||||
final Set<PsiMember> methods = Collections.singleton(((PsiMember)myMethod));
|
||||
if (!myTargetClass.isInterface()) {
|
||||
final String original = VisibilityUtil.getVisibilityModifier(myMethod.getModifierList());
|
||||
conflicts.addAll(Arrays.asList(MoveMembersProcessor.analyzeAccessibilityConflicts(methods, myTargetClass, new LinkedHashSet(), original)));
|
||||
conflicts.addAll(Arrays.asList(MoveMembersProcessor.analyzeAccessibilityConflicts(methods, myTargetClass, new LinkedHashSet<String>(), original)));
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < usageList.length; i++) {
|
||||
final UsageInfo usage = usageList[i];
|
||||
if (usage instanceof ImplementingClassUsageInfo) {
|
||||
conflicts.addAll(Arrays.asList(MoveMembersProcessor.analyzeAccessibilityConflicts(
|
||||
methods, ((ImplementingClassUsageInfo)usage).getPsiClass(), new LinkedHashSet(), PsiModifier.PUBLIC)));
|
||||
methods, ((ImplementingClassUsageInfo)usage).getPsiClass(), new LinkedHashSet<String>(), PsiModifier.PUBLIC)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package com.intellij.refactoring.move.moveInstanceMethod;
|
||||
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiSubstitutor;
|
||||
import com.intellij.psi.PsiVariable;
|
||||
import com.intellij.psi.util.PsiFormatUtil;
|
||||
import com.intellij.refactoring.ui.BaseRefactoringDialog;
|
||||
import com.intellij.refactoring.ui.VisibilityPanel;
|
||||
import com.intellij.refactoring.util.VisibilityUtil;
|
||||
import com.intellij.ui.ScrollPaneFactory;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author dsl
|
||||
*/
|
||||
public class MoveInstanceMethodDialog extends BaseRefactoringDialog {
|
||||
protected final PsiMethod myMethod;
|
||||
private final PsiVariable[] myVariables;
|
||||
private final String myRefactoringName;
|
||||
protected JList myList;
|
||||
|
||||
protected VisibilityPanel myVisibilityPanel;
|
||||
|
||||
public MoveInstanceMethodDialog(final PsiMethod method,
|
||||
final PsiVariable[] variables,
|
||||
String refactoringName) {
|
||||
super(method.getProject(), true);
|
||||
myMethod = method;
|
||||
myVariables = variables;
|
||||
myRefactoringName = refactoringName;
|
||||
setTitle(myRefactoringName);
|
||||
init();
|
||||
}
|
||||
|
||||
protected JComponent createCenterPanel() {
|
||||
final Box vBox = Box.createVerticalBox();
|
||||
final Box labelBox = Box.createHorizontalBox();
|
||||
final JLabel jLabel = new JLabel("Select an instance parameter:");
|
||||
jLabel.setDisplayedMnemonic('i');
|
||||
labelBox.add(jLabel);
|
||||
labelBox.add(Box.createHorizontalGlue());
|
||||
vBox.add(labelBox);
|
||||
vBox.add(Box.createVerticalStrut(4));
|
||||
myList = new JList(new MyListModel());
|
||||
myList.setCellRenderer(new MyListCellRenderer());
|
||||
myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
myList.setSelectedIndex(0);
|
||||
myList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
getOKAction().setEnabled(!myList.getSelectionModel().isSelectionEmpty());
|
||||
}
|
||||
});
|
||||
final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myList);
|
||||
final JPanel hBox = new JPanel(new GridBagLayout());
|
||||
final GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||
|
||||
gbConstraints.fill = GridBagConstraints.BOTH;
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.weighty = 0;
|
||||
gbConstraints.gridheight = 1;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 0;
|
||||
gbConstraints.insets = new Insets(0, 0, 0, 0);
|
||||
hBox.add(scrollPane, gbConstraints);
|
||||
hBox.add(Box.createHorizontalStrut(4));
|
||||
gbConstraints.weightx = 0;
|
||||
gbConstraints.fill = GridBagConstraints.VERTICAL;
|
||||
gbConstraints.gridx++;
|
||||
myVisibilityPanel = createVisibilityPanel ();
|
||||
hBox.add (myVisibilityPanel, gbConstraints);
|
||||
|
||||
vBox.add(hBox);
|
||||
return vBox;
|
||||
}
|
||||
|
||||
private VisibilityPanel createVisibilityPanel() {
|
||||
final VisibilityPanel visibilityPanel = new VisibilityPanel (false);
|
||||
visibilityPanel.setVisibility (VisibilityUtil.getVisibilityModifier (myMethod.getModifierList()));
|
||||
return visibilityPanel;
|
||||
}
|
||||
|
||||
protected void doAction() {
|
||||
final PsiVariable targetVariable = (PsiVariable)myList.getSelectedValue();
|
||||
final MoveInstanceMethodProcessor processor = new MoveInstanceMethodProcessor(myMethod.getProject(),
|
||||
myMethod, targetVariable,
|
||||
myVisibilityPanel.getVisibility());
|
||||
if (!verifyTargetClass(processor.getTargetClass())) return;
|
||||
invokeRefactoring(processor);
|
||||
}
|
||||
|
||||
protected boolean verifyTargetClass (PsiClass targetClass) {
|
||||
if (targetClass.isInterface()) {
|
||||
final String message = UsageViewUtil.getDescriptiveName(targetClass) + " is an interface. \n" +
|
||||
"Method implementation will be added to all directly implementing classes.\n Proceed?";
|
||||
|
||||
final int result = Messages.showYesNoDialog(myProject, message, myRefactoringName,
|
||||
Messages.getQuestionIcon());
|
||||
if (result != 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private class MyListModel extends AbstractListModel {
|
||||
public int getSize() {
|
||||
return myVariables.length;
|
||||
}
|
||||
|
||||
public Object getElementAt(int index) {
|
||||
return myVariables[index];
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyListCellRenderer extends DefaultListCellRenderer {
|
||||
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||||
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
final PsiVariable psiVariable = (PsiVariable)value;
|
||||
final String text = PsiFormatUtil.formatVariable(psiVariable,
|
||||
PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE,
|
||||
PsiSubstitutor.EMPTY);
|
||||
setIcon(psiVariable.getIcon(0));
|
||||
setText(text);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.intellij.refactoring.move.moveInstanceMethod;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor;
|
||||
import com.intellij.refactoring.util.VisibilityUtil;
|
||||
import com.intellij.usageView.UsageViewDescriptor;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.usageView.FindUsagesCommand;
|
||||
|
||||
/**
|
||||
* @author ven
|
||||
*/
|
||||
public class MoveInstanceMethodProcessor extends BaseRefactoringProcessor{
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.move.moveInstanceMethod.MoveInstanceMethodProcessor");
|
||||
private PsiMethod myMethod;
|
||||
private PsiVariable myTargetVariable;
|
||||
private PsiClass myTargetClass;
|
||||
private String myOldVisibility;
|
||||
private String myNewVisibility;
|
||||
|
||||
public MoveInstanceMethodProcessor(final Project project,
|
||||
final PsiMethod method,
|
||||
final PsiVariable targetVariable,
|
||||
final String newVisibility) {
|
||||
super(project);
|
||||
myMethod = method;
|
||||
myTargetVariable = targetVariable;
|
||||
LOG.assertTrue(myTargetVariable instanceof PsiParameter || myTargetVariable instanceof PsiField);
|
||||
LOG.assertTrue(myTargetVariable.getType() instanceof PsiClassType);
|
||||
final PsiType type = myTargetVariable.getType();
|
||||
LOG.assertTrue(type instanceof PsiClassType);
|
||||
final PsiClass targetClass = ((PsiClassType) type).resolve();
|
||||
myTargetClass = targetClass;
|
||||
myOldVisibility = VisibilityUtil.getVisibilityModifier(method.getModifierList ());
|
||||
myNewVisibility = newVisibility;
|
||||
}
|
||||
|
||||
protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages, FindUsagesCommand refreshCommand) {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
protected UsageInfo[] findUsages() {
|
||||
return new UsageInfo[0]; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
protected void refreshElements(PsiElement[] elements) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
protected void performRefactoring(UsageInfo[] usages) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
protected String getCommandName() {
|
||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
public PsiClass getTargetClass() {
|
||||
return myTargetClass;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user