Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@
import consulo.language.psi.PsiElement;
import consulo.localize.LocalizeValue;
import consulo.project.Project;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.awt.DialogWrapper;
import consulo.ui.ex.awt.UIUtil;
import consulo.util.collection.MultiMap;
import consulo.util.lang.StringUtil;
import org.jetbrains.annotations.NonNls;

import jakarta.annotation.Nonnull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.function.Function;

public class ExtractMethodObjectDialog extends DialogWrapper implements AbstractExtractDialog {
private final Project myProject;
Expand Down Expand Up @@ -122,7 +119,6 @@ public ExtractMethodObjectDialog(

// Create UI components


myCbMakeVarargs.setVisible(canBeVarargs);
myCbMakeVarargsAnonymous.setVisible(canBeVarargs);

Expand All @@ -131,6 +127,7 @@ public ExtractMethodObjectDialog(

}

@Override
public boolean isMakeStatic() {
if (myStaticFlag) {
return true;
Expand All @@ -141,6 +138,7 @@ public boolean isMakeStatic() {
return myCbMakeStatic.isSelected();
}

@Override
public boolean isChainedConstructor() {
return false;
}
Expand All @@ -151,6 +149,7 @@ public PsiType getReturnType() {
}

@Nonnull
@Override
protected Action[] createActions() {
return new Action[]{
getOKAction(),
Expand All @@ -159,22 +158,30 @@ protected Action[] createActions() {
};
}

@Override
public String getChosenMethodName() {
return myCreateInnerClassRb.isSelected() ? myInnerClassName.getText() : myMethodName.getText();
}

@Override
public VariableData[] getChosenParameters() {
return myInputVariables;
}

@Override
@RequiredUIAccess
public JComponent getPreferredFocusedComponent() {
return myInnerClassName;
}

@Override
@RequiredUIAccess
protected void doHelpAction() {
HelpManager.getInstance().invokeHelp(HelpID.EXTRACT_METHOD_OBJECT);
}

@Override
@RequiredUIAccess
protected void doOKAction() {
MultiMap<PsiElement, LocalizeValue> conflicts = new MultiMap<>();
if (myCreateInnerClassRb.isSelected()) {
Expand All @@ -201,8 +208,8 @@ protected void doOKAction() {
JCheckBox makeVarargsCb = myCreateInnerClassRb.isSelected() ? myCbMakeVarargs : myCbMakeVarargsAnonymous;
if (makeVarargsCb != null && makeVarargsCb.isSelected()) {
VariableData data = myInputVariables[myInputVariables.length - 1];
if (data.type instanceof PsiArrayType) {
data.type = new PsiEllipsisType(((PsiArrayType) data.type).getComponentType());
if (data.type instanceof PsiArrayType arrayType) {
data.type = new PsiEllipsisType(arrayType.getComponentType());
}
}
super.doOKAction();
Expand All @@ -222,11 +229,13 @@ private void update() {
myCbMakeStatic.setEnabled(myCreateInnerClassRb.isSelected() && myCanBeStatic && !myStaticFlag);
updateSignature();
PsiNameHelper helper = PsiNameHelper.getInstance(myProject);
setOKActionEnabled((myCreateInnerClassRb.isSelected() && helper.isIdentifier(myInnerClassName.getText())) || (!myCreateInnerClassRb.isSelected() && helper.isIdentifier(
myMethodName.getText()
)));
setOKActionEnabled(
(myCreateInnerClassRb.isSelected() && helper.isIdentifier(myInnerClassName.getText()))
|| (!myCreateInnerClassRb.isSelected() && helper.isIdentifier(myMethodName.getText()))
);
}

@Override
public String getVisibility() {
if (myPublicRadioButton.isSelected()) {
return PsiModifier.PUBLIC;
Expand All @@ -243,16 +252,12 @@ public String getVisibility() {
return null;
}


@Override
protected JComponent createCenterPanel() {
mySignatureArea.setEditable(false);
myCreateInnerClassRb.setSelected(true);

ActionListener enableDisableListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
enable(myCreateInnerClassRb.isSelected());
}
};
ActionListener enableDisableListener = e -> enable(myCreateInnerClassRb.isSelected());
myCreateInnerClassRb.addActionListener(enableDisableListener);
myCreateAnonymousClassWrapperRb.addActionListener(enableDisableListener);
myCreateAnonymousClassWrapperRb.setEnabled(!myMultipleExitPoints);
Expand All @@ -261,33 +266,29 @@ public void actionPerformed(ActionEvent e) {
myFoldCb.setVisible(myVariableData.isFoldable());
myVariableData.setFoldingAvailable(myFoldCb.isSelected());
myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
myFoldCb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myVariableData.setFoldingAvailable(myFoldCb.isSelected());
myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
myParametersTableContainer.removeAll();
myParametersTableContainer.add(createParametersPanel(), BorderLayout.CENTER);
myParametersTableContainer.revalidate();
updateSignature();
updateVarargsEnabled();
}
myFoldCb.addActionListener(e -> {
myVariableData.setFoldingAvailable(myFoldCb.isSelected());
myInputVariables = myVariableData.getInputVariables().toArray(new VariableData[myVariableData.getInputVariables().size()]);
myParametersTableContainer.removeAll();
myParametersTableContainer.add(createParametersPanel(), BorderLayout.CENTER);
myParametersTableContainer.revalidate();
updateSignature();
updateVarargsEnabled();
});
myParametersTableContainer.add(createParametersPanel(), BorderLayout.CENTER);

ActionListener updateSugnatureListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateSignature();
ApplicationIdeFocusManager.getInstance()
.getInstanceForProject(myProject)
.requestFocus(myCreateInnerClassRb.isSelected() ? myInnerClassName : myMethodName, false);
}
ActionListener updateSignatureListener = e -> {
updateSignature();
ApplicationIdeFocusManager.getInstance()
.getInstanceForProject(myProject)
.requestFocus(myCreateInnerClassRb.isSelected() ? myInnerClassName : myMethodName, false);
};

if (myStaticFlag || myCanBeStatic) {
myCbMakeStatic.setEnabled(!myStaticFlag);
myCbMakeStatic.setSelected(myStaticFlag);

myCbMakeStatic.addActionListener(updateSugnatureListener);
myCbMakeStatic.addActionListener(updateSignatureListener);
}
else {
myCbMakeStatic.setSelected(false);
Expand All @@ -297,10 +298,10 @@ public void actionPerformed(ActionEvent e) {
updateVarargsEnabled();

myCbMakeVarargs.setSelected(myWasStatic);
myCbMakeVarargs.addActionListener(updateSugnatureListener);
myCbMakeVarargs.addActionListener(updateSignatureListener);

myCbMakeVarargsAnonymous.setSelected(myWasStatic);
myCbMakeVarargsAnonymous.addActionListener(updateSugnatureListener);
myCbMakeVarargsAnonymous.addActionListener(updateSignatureListener);

DocumentAdapter nameListener = new DocumentAdapter() {
@Override
Expand All @@ -313,12 +314,12 @@ public void documentChanged(DocumentEvent e) {

myPrivateRadioButton.setSelected(true);

myCreateInnerClassRb.addActionListener(updateSugnatureListener);
myCreateAnonymousClassWrapperRb.addActionListener(updateSugnatureListener);
myCreateInnerClassRb.addActionListener(updateSignatureListener);
myCreateAnonymousClassWrapperRb.addActionListener(updateSignatureListener);

Enumeration<AbstractButton> visibilities = myVisibilityGroup.getElements();
while (visibilities.hasMoreElements()) {
visibilities.nextElement().addActionListener(updateSugnatureListener);
visibilities.nextElement().addActionListener(updateSignatureListener);
}

enable(true);
Expand All @@ -333,15 +334,18 @@ private void enable(boolean innerClassSelected) {

private JComponent createParametersPanel() {
return new ParameterTablePanel(myProject, myInputVariables, myElementsToExtract) {
@Override
protected void updateSignature() {
updateVarargsEnabled();
ExtractMethodObjectDialog.this.updateSignature();
}

@Override
protected void doEnterAction() {
clickDefaultButton();
}

@Override
protected void doCancelAction() {
ExtractMethodObjectDialog.this.doCancelAction();
}
Expand All @@ -361,13 +365,13 @@ protected void updateSignature() {
if (mySignatureArea == null) {
return;
}
@NonNls StringBuffer buffer = getSignature();
StringBuilder buffer = getSignature();
mySignatureArea.setText(buffer.toString());
}

protected StringBuffer getSignature() {
String INDENT = " ";
@NonNls StringBuffer buffer = new StringBuffer();
protected StringBuilder getSignature() {
String indent = " ";
StringBuilder buffer = new StringBuilder();
String visibilityString = VisibilityUtil.getVisibilityString(getVisibility());
if (myCreateInnerClassRb.isSelected()) {
buffer.append(visibilityString);
Expand All @@ -384,67 +388,63 @@ protected StringBuffer getSignature() {
buffer.append(" ");
}
buffer.append("{\n");
buffer.append(INDENT);
buffer.append(indent);
buffer.append("public ");
buffer.append(myInnerClassName.getText());
methodSignature(INDENT, buffer);
methodSignature(indent, buffer);
buffer.append("\n}");
}
else {
buffer.append("new Object(){\n");
buffer.append(INDENT);
buffer.append(indent);
buffer.append("private ");
buffer.append(PsiFormatUtil.formatType(myReturnType, 0, PsiSubstitutor.EMPTY));
buffer.append(" ");
buffer.append(myMethodName.getText());
methodSignature(INDENT, buffer);
methodSignature(indent, buffer);
buffer.append("\n}.");
buffer.append(myMethodName.getText());
buffer.append("(");
buffer.append(StringUtil.join(myInputVariables, new Function<VariableData, String>() {
public String apply(VariableData variableData) {
return variableData.name;
}
}, ", "));
buffer.append(StringUtil.join(myInputVariables, variableData -> variableData.name, ", "));
buffer.append(")");
}

return buffer;
}

private void methodSignature(String INDENT, StringBuffer buffer) {
private void methodSignature(String indent, StringBuilder buffer) {
buffer.append("(");
int count = 0;
String indent = " ";
for (int i = 0; i < myInputVariables.length; i++) {
VariableData data = myInputVariables[i];
if (data.passAsParameter) {
//String typeAndModifiers = PsiFormatUtil.formatVariable(data.variable,
// PsiFormatUtil.SHOW_MODIFIERS | PsiFormatUtil.SHOW_TYPE);
PsiType type = data.type;
if (i == myInputVariables.length - 1 && type instanceof PsiArrayType && ((myCreateInnerClassRb.isSelected() && myCbMakeVarargs.isSelected()) || (myCreateAnonymousClassWrapperRb
.isSelected() && myCbMakeVarargsAnonymous.isSelected()))) {
type = new PsiEllipsisType(((PsiArrayType) type).getComponentType());
}
if (!data.passAsParameter) {
continue;
}

String typeText = type.getPresentableText();
if (count > 0) {
buffer.append(", ");
}
buffer.append("\n");
buffer.append(indent);
buffer.append(typeText);
buffer.append(" ");
buffer.append(data.name);
count++;
PsiType type = data.type;
if (i == myInputVariables.length - 1 && type instanceof PsiArrayType arrayType
&& ((myCreateInnerClassRb.isSelected() && myCbMakeVarargs.isSelected())
|| (myCreateAnonymousClassWrapperRb.isSelected() && myCbMakeVarargsAnonymous.isSelected()))) {
type = new PsiEllipsisType(arrayType.getComponentType());
}

String typeText = type.getPresentableText();
if (count > 0) {
buffer.append(", ");
}
buffer.append("\n");
buffer.append(indent);
buffer.append(typeText);
buffer.append(" ");
buffer.append(data.name);
count++;
}
buffer.append(")");
if (myExceptions.length > 0) {
buffer.append("\n");
buffer.append("throws\n");
for (PsiType exception : myExceptions) {
buffer.append(INDENT);
buffer.append(indent);
buffer.append(PsiFormatUtil.formatType(exception, 0, PsiSubstitutor.EMPTY));
buffer.append("\n");
}
Expand Down
Loading
Loading