I’ve created a bean that provides a tabbed menu layout with a series of tabs for sections (crm, shop) etc and for each of those sections a series of buttons to show each form (crm has a button for contacts, a button for accounts and so on)
I did this because I have so many different sections in the servoy solution and I found it was the simplest solution (only took a couple of hours to make the bean… and I suck at Swing)
The bean is initialised on application startup with:
forms.navigation.elements.menuBean.application = application;
forms.navigation.elements.menuBean.tabString = "TabName1:ButtonName1:FormName1;TabName1:ButtonName2:FormName2;TabName2:ButtonName3:FormName3
The bean parses that string and constructs JTabbedPanes and JButtons
It is passed the JSApplication object, from which I can show the the relevant form on a button click by calling JSApplication.js_showForm(Object)
What I can’t figure out how to do (and have seen other posts saying it is impossible) is call a servoy javascript method instead of show a form - which would give me far more flexibility. Is there any way I could hack a solution? Is there by any chance some control class I can get a reference to or anything?
Or is there a way in which I can mimic this?
Cheers
Willie
The scheduler plugin is open source, you can see the callback method mechanism in there. (java files are in the .jar)
The only requirement is to use SwingUtilities.invokeLater or invokeAndWait for calling a javascript method when in other thread than awt-event thread
Jan,
Could you elaborate a bit as to why the SwingUtilities.invokeLater/invokeAndWait has to be used? (And when to use which one of the two)
Do we always have to use that when launching a JS method from a plugin?
Even if you don’t want the user to be able to do anything from the moment the method is launched untill the method is finished?
Paul
The method must be invoked on the awt-event thread, when in other thread than awt-event thread SwingUtilities methods are the only way to do so.
-SwingUtilities.invokeLater can be used in all occasions and from all threads. (I whould sugest always to use this one)
-SwingUtilities.invokeAndWait can be used from other non awt-thread which than waits until the awt-thread action is completed
I’ve had a look at the source but I still don’t understand. I’m using a bean not a plugin so its a little different:
So in my bean I would import org.mozilla.javascript.Function
then add a property to set the callback function on my bean that I could call on application startup.
onApplicationStart:
forms.navigation.elements.menubean.callbackFunction = globals.someFunction
The trouble with this is when I print out the class name trying to set a function on the bean I get an obviously obfuscated object com.servoy.scripting.l or whatever… I’m a bit stuck really.
Once the above problem is sorted out I would call the function just like in
com/servoy/r2/plugins/scheduler/ExecuteScriptMethodJob.java
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Context context = Context.enter();
try
{
SchedulerProvider.lastRunned = name;
method.call(context, method.getParentScope(), method.getParentScope(), args);
}
catch (JavaScriptException e)
{
e.printStackTrace();
}
finally
{
Context.exit();
}
… etc etc
Am I on the right track?
Are you going to release the dbtreeview bean as open source as well?
Regards
Willie
willieseabrook:
I’ve had a look at the source but I still don’t understand. I’m using a bean not a plugin so its a little different
No it is not, to a plugin a servoymethod object is passed see the move sample when using the scheduler plugin, in your case pass a servoymethod object to your bean and use the same logic for callback as in the scheduler plugin.