X

Geertjan's Blog

  • February 20, 2008

Fixable Hint

Geertjan Wielenga
Product Manager
Yesterday's hint showed up whenever System.out.println or JOptionPane.showMessageDialog are used in the code. Today, let's enhance the hint so that it turns into a lightbulb that the user can click. When clicked, the offending line of code will simply be removed:

Here's the same class as yesterday, supplemented with an implementation of the EnhancedFix class, which is the third argument of our ErrorDescription. The code below produces the result shown above; when the hint is clicked, the line is removed.

public class WrongDebugMethodologies extends AbstractHint {
//private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList();
private static final Set<Tree.Kind> TREE_KINDS =
EnumSet.<Tree.Kind>of(Tree.Kind.METHOD_INVOCATION);
public WrongDebugMethodologies() {
super(true, true, AbstractHint.HintSeverity.WARNING);
}
public Set<Kind> getTreeKinds() {
return TREE_KINDS;
}
public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {
Tree t = treePath.getLeaf();
Element el = info.getTrees().getElement(treePath);
String name = el.getSimpleName().toString();
if (name.equals("showMessageDialog") || name.equals("println")) {JTextComponent editor = EditorRegistry.lastFocusedComponent();
Document doc = editor.getDocument();
SourcePositions sp = info.getTrees().getSourcePositions();
int start = (int) sp.getStartPosition(info.getCompilationUnit(), t);
int end = (int) sp.getEndPosition(info.getCompilationUnit(), t);
String bodyText = info.getText().substring(start, end);
List<Fix> fixes = new ArrayList<Fix>();
fixes.add(new MessagesFix(doc, start, bodyText));

return Collections.<ErrorDescription>singletonList(
ErrorDescriptionFactory.createErrorDescription(
getSeverity().toEditorSeverity(),
getDisplayName(),fixes,//NO_FIXES,
info.getFileObject(),
start,
end));
}
return null;
}
public void cancel() {
// Does nothing
}
public String getId() {
return "Wrong_Debug"; // NOI18N
}
public String getDisplayName() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "LBL_Debug");
}
public String getDescription() {
return NbBundle.getMessage(WrongDebugMethodologies.class, "DSC_Debug");
}class MessagesFix implements EnhancedFix {
Document doc = null;
int start = 0;
String bodyText = null;
public MessagesFix(Document doc, int start, String bodyText) {
this.doc = doc;
this.start = start;
this.bodyText = bodyText;
}
public CharSequence getSortText() {
return "charsequence";
}
public String getText() {
return "Let's remove it...";
}
public ChangeInfo implement() throws Exception {
//Add 1 character, for the semi-colon:
doc.remove(start, bodyText.length() + 1);
//Display message to user in status bar:
StatusDisplayer.getDefault().setStatusText("Removed: " + bodyText);
return null;
}
}

}

Today on NetBeans Zone. A short run through of how to bind a JTable to Swing controls in NetBeans IDE.

Join the discussion

Comments ( 2 )
  • guest Wednesday, February 20, 2008

    Are you sure using EditorRegistry.lastFocusedComponent() is necessary? What kind of API it comes from? Shall there not be a better way?


  • Geertjan Wednesday, February 20, 2008

    Editor Library 2 is the API it comes from.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.