Fixable Hint

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.

Comments:

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

Posted by guest on February 19, 2008 at 11:41 PM PST #

Editor Library 2 is the API it comes from.

Posted by Geertjan on February 20, 2008 at 06:38 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today