6

I'm really no debugging expert and lately I came across a problem which hopefully has an easy solution. When I test and debug the Mathematica plugin for IDEA, I write some code, build it and run it in a sandbox IDEA.

For those who are not familiar with writing plugins for IDEA: The main problem is, that all the UI code is already there because it comes with IDEA. My plugin only implements specific interfaces which are required to make IDEA understand the Mathematica language. Therefore, setting a breakpoint or throwing something in an onClickListener like suggested by @Jeroen is not possible, because I virtually haven't written any single line of UI code*.

Now I had the situation that everything works fine, but when I cancel a specific action, something weird happens. I have no idea which specific method (it is none of mine!) is called at the moment when I press Esc to cancel this action. The current point is very likely deep inside the sources of IDEA which I have and which I can navigate with the debugger!.

Question: What is the easiest way in Java to make the debugger break, wherever it might be when I press Esc in the UI-program I currently debug?

*This is not entirely true, but for the sake of a general solution I would like to think of it that way.

halirutan
  • 4,281
  • 18
  • 44
  • You could throw an exception in your `onClickListener` after checking if the pressed key is the Esc key. – Jeroen Vannevel Mar 10 '14 at 01:51
  • @JeroenVannevel Sorry, I forgot to mention: Since the plugin in running in an IDEA sandbox, virtually none of UI code is mine. I only implement the methods of the interfaces which are required to run the plugin and this doesn't contain the basic UI. – halirutan Mar 10 '14 at 01:56

2 Answers2

3

As discussed in this question, you can create your own subclass of EventQueue which inspects events, and create a place in the code where you can set your break point and only catch certain kinds of events. It might look something like this:

    EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    eventQueue.push(new EventQueue(){
        public void postEvent(AWTEvent theEvent) {
            if(theEvent instanceof KeyEvent){
                KeyEvent kEvent = (KeyEvent)theEvent;
                if(kEvent.getKeyCode() == KeyEvent.VK_ESCAPE){
                    System.out.println("escape pressed, set breakpoint here");
                }
            }
            super.postEvent(theEvent);
        }
    });

Then when you hit that breakpoint, you can step into the super.postEvent() call and see where it goes, which component(s) receive it, and when they begin to interact with some part of your code. Be forewarned that it may be a long slog, though -- AWT/Swing event dispatching can get pretty complex.

Community
  • 1
  • 1
Matt McHenry
  • 20,009
  • 8
  • 65
  • 64
  • Hmm, I'm afraid I don't know how to use this because the IDEA sandbox is started automatically; I don't see/have a `main` function, I only have the plugin code which is loaded automatically. But your answer maybe gave me an idea: In the plugin I can add custom key event actions and when I just implement a dummy one, then I can set a breakpoint there and know where I am. +1 – halirutan Mar 27 '14 at 22:51
  • I believe `EventQueue.push()` can be called at any time, it doesn't have to be done in `main` or before the UI is shown or anything. – Matt McHenry Mar 28 '14 at 01:30
  • I couldn't get it to work until now, because it seems to matter from which thread I call this. Anyway, I think this is the right approach. Thanks for your answer. – halirutan Mar 29 '14 at 18:18
1

If you are working in (or can port to) Netbeans you can use their visual debugger which is specifically designed for these GUI-centric issues. It can direct you to specific source code when you interact with a GUI element, so it should be able to point you to the mystery code that is run when you click escape. I believe that other IDEs have similar features, but Netbeans is the only one I know of off the top of my head.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • Unfortunately, for writing an IntelliJ IDEA plugin I'm using IntelliJ IDEA :-) But I can check whether they have something similar, so thanks for the hint. +1 – halirutan Mar 27 '14 at 22:52
  • @halirutan Intellij is big and fancy, more so than Netbeans. I highly suspect they do. You might want to ask on their forums – David says Reinstate Monica Mar 27 '14 at 22:54