Skip to content

Commit

Permalink
Introduce-method refactoring should suggest a method name
Browse files Browse the repository at this point in the history
if the test field is left empty, it creates the following problem:

 - cancel button has focus, ok button is disabled until validation
 - method name validation is delayed by 200ms which can cause
   users to cancel the hint after filling out the method name and
   pressing enter quickly, giving the wrong impression of a not
   functioning refactoring action

this can be avoided by initializing the panel with a method name

 - added btnCancel.setDefaultCapable(false) just in case
  • Loading branch information
mbien committed Jul 19, 2024
1 parent cf4a54d commit 001eb85
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public String toString() {
public ChangeInfo implement() throws Exception {
JButton btnOk = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Ok"));
JButton btnCancel = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Cancel"));
IntroduceMethodPanel panel = new IntroduceMethodPanel("", duplicatesCount, targets, targetIsInterface); //NOI18N
btnCancel.setDefaultCapable(false);
IntroduceMethodPanel panel = new IntroduceMethodPanel("method", duplicatesCount, targets, targetIsInterface); //NOI18N
String caption = NbBundle.getMessage(IntroduceHint.class, "CAP_IntroduceMethod");
DialogDescriptor dd = new DialogDescriptor(panel, caption, true, new Object[]{btnOk, btnCancel}, btnOk, DialogDescriptor.DEFAULT_ALIGN, null, null);
NotificationLineSupport notifier = dd.createNotificationLineSupport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public ChangeInfo implement() throws IOException, BadLocationException, ParseExc
JButton btnOk = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Ok"));
btnOk.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(IntroduceHint.class, "AD_IntrHint_OK"));
JButton btnCancel = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Cancel"));
btnCancel.setDefaultCapable(false);
btnCancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(IntroduceHint.class, "AD_IntrHint_Cancel"));
IntroduceFieldPanel panel = createPanel(btnOk);
FieldValidator fv = new FieldValidator(source, null, this.handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ public String toDebugString(CompilationInfo info) {
public ChangeInfo implement() throws Exception {
JButton btnOk = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Ok"));
JButton btnCancel = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Cancel"));
IntroduceMethodPanel panel = new IntroduceMethodPanel("", duplicatesCount, targets, targetIsInterface); //NOI18N
btnCancel.setDefaultCapable(false);
IntroduceMethodPanel panel = new IntroduceMethodPanel("method", duplicatesCount, targets, targetIsInterface); //NOI18N
String caption = NbBundle.getMessage(IntroduceHint.class, "CAP_IntroduceMethod");
DialogDescriptor dd = new DialogDescriptor(panel, caption, true, new Object[]{btnOk, btnCancel}, btnOk, DialogDescriptor.DEFAULT_ALIGN, null, null);
NotificationLineSupport notifier = dd.createNotificationLineSupport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.prefs.Preferences;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -73,15 +72,17 @@ public IntroduceMethodPanel(String name, int duplicatesCount, Collection<TargetD
initComponents();

this.targetInterface = targetInterface;
this.name.setText(name);

this.changeSupport = new MethodNameSupport(this.name, true);
this.changeSupport.setChangeListener(this);

this.name.setText(name); // triggers validation task
if ( name != null && name.trim().length() > 0 ) {
this.name.setCaretPosition(name.length());
this.name.setSelectionStart(0);
this.name.setSelectionEnd(name.length());
}
this.changeSupport = new MethodNameSupport(this.name);
this.changeSupport.setChangeListener(this);


Preferences pref = getPreferences();

if (!targetInterface) {
Expand Down Expand Up @@ -127,8 +128,9 @@ public void setNotifier(NotificationLineSupport notifier) {
}

private class MethodNameSupport extends NameChangeSupport {
public MethodNameSupport(JTextField control) {
super(control);

public MethodNameSupport(JTextField control, boolean initAsValid) {
super(control, initAsValid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public String getText() {
public ChangeInfo implement() throws IOException, BadLocationException, ParseException {
JButton btnOk = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Ok"));
JButton btnCancel = new JButton(NbBundle.getMessage(IntroduceHint.class, "LBL_Cancel"));
btnCancel.setDefaultCapable(false);
IntroduceFieldPanel panel = new IntroduceFieldPanel(guessedName, null, duplicatesCount,
true, handle.getKind() == Tree.Kind.VARIABLE,
IntroduceFieldPanel.VARIABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.lang.reflect.InvocationTargetException;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.api.java.source.TreePathHandle;
import org.openide.NotificationLineSupport;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.Utilities;
Expand All @@ -56,13 +50,18 @@ class NameChangeSupport extends FocusAdapter implements DocumentListener, Runnab
private Modifier minAccess;
private boolean valid;
private String validateName;

public NameChangeSupport(JTextField control) {
this(control, false);
}

public NameChangeSupport(JTextField control, boolean initAsValid) {
this.control = control;
this.valid = initAsValid;
control.getDocument().addDocumentListener(this);
control.addFocusListener(this);
}

public synchronized void setChangeListener(ChangeListener l) {
assert listener == null;
this.listener = l;
Expand Down

0 comments on commit 001eb85

Please sign in to comment.