package jetbrains.mps.ide.java.platform.refactorings;
/*Generated by MPS */
import jetbrains.mps.ide.platform.refactoring.RefactoringDialog;
import jetbrains.mps.openapi.editor.EditorContext;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nullable;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import jetbrains.mps.baseLanguage.util.plugin.refactorings.IntroduceVariableRefactoring;
import jetbrains.mps.editor.runtime.commands.EditorComputable;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.baseLanguage.util.plugin.refactorings.MoveRefactoringUtils;
import java.awt.Insets;
import javax.swing.JComponent;
public abstract class IntroduceVariableDialog extends RefactoringDialog {
protected final EditorContext myEditorContext;
protected JPanel myPanel;
protected JCheckBox myReplaceAll = null;
protected JCheckBox myIsFinal = null;
protected VisibilityPanel myVisibilityPanel = null;
private JComboBox myName;
public IntroduceVariableDialog(Project project, String name, EditorContext editorContext) {
super(project, true);
setTitle(name);
myEditorContext = editorContext;
}
@Nullable
@Override
protected String getHelpId() {
return "refactoring.introduceVariable1";
}
private JPanel createNamePanel() {
JPanel result = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
result.add(new JLabel("Name: "), c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
List<String> expectedNames = getRefactoring().getExpectedNames();
myName = new JComboBox(ListSequence.fromList(expectedNames).toGenericArray(String.class));
myName.setEditable(true);
result.add(myName, c);
myName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent p0) {
getRefactoring().setName(((String) myName.getSelectedItem()));
}
});
myName.setSelectedItem(ListSequence.fromList(expectedNames).first());
JTextField textField = ((JTextField) myName.getEditor().getEditorComponent());
textField.setSelectionStart(0);
textField.setSelectionEnd(ListSequence.fromList(expectedNames).first().length());
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent p0) {
if (p0.getKeyCode() == KeyEvent.VK_ENTER && !(myName.isPopupVisible())) {
doRefactoringAction();
}
}
});
return result;
}
public void addReplacingAll(int gridy) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = gridy;
this.myReplaceAll = new JCheckBox("replace duplicates (" + ListSequence.fromList(getRefactoring().getDuplicates()).count() + " found)");
this.myPanel.add(this.myReplaceAll, c);
}
public void addIsFinal(GridBagConstraints c) {
myIsFinal = new JCheckBox("declare final");
myPanel.add(myIsFinal, c);
}
public void addVisibilityPanel(GridBagConstraints c) {
myVisibilityPanel = new VisibilityPanel();
myPanel.add(myVisibilityPanel, c);
}
public abstract IntroduceVariableRefactoring getRefactoring();
protected void doRefactoring() {
EditorComputable<SNode> command = new EditorComputable<SNode>(myEditorContext) {
@Override
protected SNode doCompute() {
SNode result = getRefactoring().doRefactoring();
MoveRefactoringUtils.fixImportsFromNode(result);
return result;
}
};
myEditorContext.getRepository().getModelAccess().executeCommand(command);
myEditorContext.select(command.getResult());
}
protected void initPanel() {
myPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(3, 3, 3, 3);
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 0;
myPanel.add(createNamePanel(), c);
}
/**
* This method will be called on pressing "Refactor" button in dialog.
*
*/
@Override
protected void doRefactoringAction() {
String name = (String) myName.getEditor().getItem();
IntroduceVariableDialog.this.getRefactoring().setName(name);
if (myVisibilityPanel != null) {
getRefactoring().setVisibilityLevel(myVisibilityPanel.getResult());
}
if (myReplaceAll != null) {
getRefactoring().setReplacingAll(myReplaceAll.isSelected());
}
if (myIsFinal != null) {
getRefactoring().setIsFinal(myIsFinal.isSelected());
}
super.doRefactoringAction();
this.doRefactoring();
}
@Nullable
@Override
public JComponent getPreferredFocusedComponent() {
JComponent candidate = super.getPreferredFocusedComponent();
return (candidate == null ? this.myName : candidate);
}
}