Can't get onChange event handler to run

Asked by Terence Cooper

    static void FindTXDevMenuRegion()
    throws FindFailed{
        // sub menus of the main menu all appear within a certain region
        // we'll monitor this region for changes
        subMenusRegion = ideMainMenuR.below(500);
        subMenusRegion.highlight(4);

        // register change observer to be triggered when the TXDev menu appears...
        subMenusRegion.onChange(new SikuliEventObserver(){
            public void targetChanged(ChangeEvent evnt) {
                scrpt.popup("targetChanged called");
                for (Match m : evnt.changes) {
                    m.highlight(5);
                }
                subMenusRegion.stopObserver();
            }

            public void targetAppeared(AppearEvent e) {
                scrpt.popup("targetAppeared called");
                ;
            }

            public void targetVanished(VanishEvent e) {
                scrpt.popup("targetVanished called");
                ;
            }
        } );

        // start the observation...
        subMenusRegion.observeInBackground(10);

        // click the menu...
        ideMainMenu_SearchM.click(ideMainMenu_TXDevM.getTarget());

    }

    public static void main(String[] args)
            throws InterruptedException,
            java.awt.AWTException,
            org.sikuli.script.FindFailed{

        scrpt = new SikuliScript();
        scrn = new Screen();

blah blah blah...!

        FindTXDevMenuRegion();

blah blah blah...!

When I run it....

Exception in thread "Thread-2" Traceback (most recent call last):
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/sikuli/Region.py", line 287, in observe
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/sikuli/Region.py", line 277, in targetChanged
TypeError: 'TestSikuli$1' object is not callable

Really appreciate your help, thx, Terry.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Terence Cooper (terence-o) said :
#1

(FindTXDevMenuRegion() is a method of class TestSikuli)

Revision history for this message
RaiMan (raimund-hocke) said :
#2

It looks like Java, what you are doing ;-)
and it seems you have set it up, as it should be.

But what is strange: the message comes from Jython:

 File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/sikuli/Region.py", line 277, in targetChanged
TypeError: 'TestSikuli$1' object is not callable.

So it seems, you are mixing some things up:
scrpt = new SikuliScript();

Are you running Jython scripts from within Java?

Is ideMainMenuR a Jython Region or a Java Region object?

It seems that the change is detected on the Jython level and the callback to the Java level makes problems.

Revision history for this message
Terence Cooper (terence-o) said :
#3

Hi RaiMan, thanks as always for a very prompt response.

'But what is strange: the message comes from Jython:'

- Yes I agree! I imagined that by using the java api I was short-circuiting all the Jython stuff, but apparently not(?)

'So it seems, you are mixing some things up:
scrpt = new SikuliScript();'

- My intention was to use java exclusively, and I don't understand why you highlight this fragment(?) In the javadoc
I see:
public SikuliScript()
             throws java.awt.AWTException

having method:
public void popup(java.lang.String message)

I'm also using method input (public java.lang.String input(java.lang.String msg) here and there.

'Are you running Jython scripts from within Java?'

- Not knowingly! (No)

'Is ideMainMenuR a Jython Region or a Java Region object?'

- Its all java...

public class TestSikuli {

    static App txDev;
    static Screen scrn;
    static Region ideR;
    static Region ideMainMenuR = Region.create(0,0,1000,22);
    static SikuliScript scrpt;
       ...

Again, really appreciate your assistance with this, thx Terry.

Revision history for this message
Terence Cooper (terence-o) said :
#4

Just noticed I AM importing org.python.modules.time.Time.sleep - surely not relevant?

Revision history for this message
Terence Cooper (terence-o) said :
#5

Just eliminated the python module, but I still get the originally reported problem.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

--- scrpt = new SikuliScript();
Sorry, my fault. Mixed things up myself.
As long as you only use popup() and input(), this should not be a problem.
But you could use javax.swing.JOptionPane directly (nothing else is done in popup and input).

--- sleep
You can use scr.wait((double) number_of_seconds) (a Region method)

--- going to Jython
there seems to be one critical function:
reg = Region.create(other_region)
where other_region is of class Region

where the code denotes
 //TODO: determine if the caller is Jython

and then it jumps into Jython and comes back with a Region object, that was created in the Jython context. I am not really sure, what is happening there. I will make some tests myself and come back.

Meanwhile you might look for usages like
reg = Region.create(other_region)

Revision history for this message
Terence Cooper (terence-o) said :
#7

OK I will see if I can effect some work-arounds, but most interested in your findings, thx Terry.

Revision history for this message
RaiMan (raimund-hocke) said :
#8

It seems to be a more general Problem with the callback structure of the observe feature.

I get the same error, with just a simple example.

I will try to find out the reason and come back tomorrow.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

The above mentioned TODO

--- going to Jython
there seems to be one critical function:
reg = Region.create(other_region)
where other_region is of class Region

where the code denotes
 //TODO: determine if the caller is Jython

and then it jumps into Jython and comes back with a Region object, that was created in the Jython context. I am not really sure, what is happening there. I will make some tests myself and come back.
--------------------------------------------------------------------

is really the problem. When I switched this of (it just returns the region), everything runs fine (at least in Java).

I will try to suggest a fix for that.

Meanwhile there is a workaround, by creating a sikuli-script.jar, that is only used with Java.
Come back if interested.

Revision history for this message
Terence Cooper (terence-o) said :
#10

Hi RaiMan, thanks for all your efforts.

Yes, I am interested in the sikuli-script.jar for Java only - details please!

Terry.

Revision history for this message
RaiMan (raimund-hocke) said :
#11

Yes I will do it for you ;-)

Today I had other prio's. I'll come back tomorrow (your morning ;)

Revision history for this message
Best RaiMan (raimund-hocke) said :
#12

@ Terence

Was a bit more complicated, than I thought, to setup an useable update for you.
sikuli-script.jar has to be modified using the jar utility from the Java JDK.

Download form here: http://dl.dropbox.com/u/42895525/sikuli-script-update_01.zip

and unzip to a place of your choice.

You will have a folder sikuli-script-update, that contains the update stuff and a readme.txt, that talks about the steps to get it running.

Be aware:
- This contains a temporary workaround fix of Java class Region, to make observe running with Java in the above way
- The modified sikuli-script.jar can only be used with Java (not with Sikuli-IDE or Jython)

Revision history for this message
Terence Cooper (terence-o) said :
#13

I will try it out asap and let you know - thanks V. much. Terry

Revision history for this message
Terence Cooper (terence-o) said :
#14

[RaiMan] observer started!!

That should tell you everything!

Great job, many thanks. Terry.

PS. When will your change become part of an official release - any idea?

Revision history for this message
Terence Cooper (terence-o) said :
#15

Thanks RaiMan, that solved my question.

Revision history for this message
Jacob Dorman (japhezbemeye) said :
#16

Thanks for the fix, RaiMan. I needed this too :)

Revision history for this message
kinisoftware (kinisoftware) said :
#17

Awesome! :D Works for me too :) I think it must be included in next release guys ;)

Thanks a lot RaiMan!