You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Jacques Granduel <jg...@gmail.com> on 2010/11/21 10:36:06 UTC

Re: launching Pivot programmatically

Hi,

I'm sorry for coming back on this old thread about learning how to launch a
Pivot application from a shell (Rhino shell in this case). I've made a big
step forward trying bxml+ScriptApplication, but it's still not enough! What
am I missing??

I've saved  this file from the 2.0 tutorial:
<Window title="Hello JavaScript!" maximized="true"
    xmlns:bxml="http://pivot.apache.org/bxml"
    xmlns="org.apache.pivot.wtk">
    <windowStateListeners>
        importPackage(org.apache.pivot.wtk);

        function windowOpened(window) {
            var label = new Label();
            label.setText("Hello JavaScript!");
            label.getStyles().put("font", "Arial BOLD 24");
            label.getStyles().put("color", "#ff0000");
            label.getStyles().put("horizontalAlignment",
HorizontalAlignment.CENTER);
            label.getStyles().put("verticalAlignment",
VerticalAlignment.CENTER);

            window.setContent(label);
        }
    </windowStateListeners>
</Window>
into a file test.bxml.
I use from Java, ScriptApplication.main(new
String[]{"--src=path/to/test.bxml"}); or from Rhino
ScriptApplication.main(["--src="path/to/test.bxml"])
but in both cases, I get an IllegalArgumentException although the text.bxml
is found by Java.

java.lang.IllegalArgumentException: Cannot find source file "test.bxml".
        at
org.apache.pivot.wtk.ScriptApplication.startup(ScriptApplication.java:47)
        at
org.apache.pivot.wtk.DesktopApplicationContext$2.run(DesktopApplicationContext.java:594)
        at
org.apache.pivot.wtk.ApplicationContext$QueuedCallback.run(ApplicationContext.java:1457)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Thanks again,
regards
jqg

Re: launching Pivot programmatically

Posted by Greg Brown <gk...@mac.com>.
By the way, you can easily write your own implementation of the Application interface if ScriptApplication does not suit your needs. In fact, most Pivot applications are coded this way.
G

On Nov 22, 2010, at 8:05 AM, Greg Brown wrote:

> Sorry, didn't read the entire message. Passing the script source as a string has obvious limitations, as you have discovered. Processing from standard in might be a good enhancement. At one point we actually did support local files but that code was removed. There was some issue with it, though I don't remember exactly what it was at the moment.
> 
>> On Nov 22, 2010, at 7:59 AM, Jacques Granduel wrote:
>> 
>>> Thanks for your remark and I had indeed seen I could use "Pivot scripting", but I want to dynamically shape my view from the outside, according to some input.
>>> 
>>> What could be the best way to send this object (as a String or ByteArrayInputStream ?) to BXMLSerializer as the actual API seems to be 
>>> 
>>> java.lang.Object readObject(java.net.URL)
>>> java.lang.Object readObject(java.io.InputStream)
>>> java.lang.Object readObject(java.lang.Class,java.lang.String,boolean)
>>> java.lang.Object readObject(java.lang.Class,java.lang.String)
>>> java.lang.Object readObject(java.net.URL,org.apache.pivot.util.Resources).
>>> 
>>> I modified the ScriptApplication.java for allowing path/to/file.bxml to be accepted beside classpath resource using File(...).toURI().toURL().
>>> 
>>> As for the String, I wanted the equivalent, say (example in JS/Rhino)
>>> JS>var win = <Window title="Hello BXML!" maximized="true"
>>>     xmlns:bxml="http://pivot.apache.org/bxml"
>>>     xmlns="org.apache.pivot.wtk">
>>>     <Label text="Hello BXML!"
>>>         styles="{font:'Arial bold 24', color:'#ff0000',
>>>             horizontalAlignment:'center', verticalAlignment:'center'}"/>
>>> </Window>
>>> JS>ScriptApplication.main(["--src=" + win]);
>>> The latest crashed because DesktopApplicationContext.java splits args this way:
>>> if (arg.startsWith("--")) {
>>>                 String[] property = arg.split("=");
>>> and my bxml snippet is full of "=" due to the attribute values!
>>> So, as a quick workaround, I kept the first element of the property table, and joined back the rest.
>>> 
>>> ScriptApplication.java:
>>> String src = properties.get(SRC_KEY);
>>> InputStream ins = new ByteArrayInputStream(src.getBytes());
>>> BXMLSerializer bxmlSerializer = new BXMLSerializer();
>>> window = (Window)bxmlSerializer.readObject(ins);
>>> window.open(display);
>>> 
>>> This way it seems to work nicely.
>>> 
>>> So... may I suggest adding loading bxml from File and String also?
>>> 
>>> Thanks once again for your help,
>>> jqg
>>> 
>>> 
>>> 2010/11/22 Greg Brown <gk...@mac.com>
>>> > So my reasons for doing that kind of stuff, well, er, curiosity? learning and playing Pivot from a tiny environment, manipulating the bxml xml with E4X and dynamically injecting js code in the GUI ?... I have to learn how to directly pass a bxml String or E4X/XML object to ScriptApplication and to manipulate objects from the rhino shell, as it's doable in Swing.
>>> 
>>> OK. Just in case you were not aware, BXML allows you to embed script code directly within your markup, and will invoke the appropriate script engine as needed. So don't necessarily need to launch Pivot from within Rhino to use Rhino.
>>> 
>>> G
>>> 
>>> 
>> 
> 


Re: launching Pivot programmatically

Posted by Greg Brown <gk...@mac.com>.
Sorry, didn't read the entire message. Passing the script source as a string has obvious limitations, as you have discovered. Processing from standard in might be a good enhancement. At one point we actually did support local files but that code was removed. There was some issue with it, though I don't remember exactly what it was at the moment.

> On Nov 22, 2010, at 7:59 AM, Jacques Granduel wrote:
> 
>> Thanks for your remark and I had indeed seen I could use "Pivot scripting", but I want to dynamically shape my view from the outside, according to some input.
>> 
>> What could be the best way to send this object (as a String or ByteArrayInputStream ?) to BXMLSerializer as the actual API seems to be 
>> 
>> java.lang.Object readObject(java.net.URL)
>> java.lang.Object readObject(java.io.InputStream)
>> java.lang.Object readObject(java.lang.Class,java.lang.String,boolean)
>> java.lang.Object readObject(java.lang.Class,java.lang.String)
>> java.lang.Object readObject(java.net.URL,org.apache.pivot.util.Resources).
>> 
>> I modified the ScriptApplication.java for allowing path/to/file.bxml to be accepted beside classpath resource using File(...).toURI().toURL().
>> 
>> As for the String, I wanted the equivalent, say (example in JS/Rhino)
>> JS>var win = <Window title="Hello BXML!" maximized="true"
>>     xmlns:bxml="http://pivot.apache.org/bxml"
>>     xmlns="org.apache.pivot.wtk">
>>     <Label text="Hello BXML!"
>>         styles="{font:'Arial bold 24', color:'#ff0000',
>>             horizontalAlignment:'center', verticalAlignment:'center'}"/>
>> </Window>
>> JS>ScriptApplication.main(["--src=" + win]);
>> The latest crashed because DesktopApplicationContext.java splits args this way:
>> if (arg.startsWith("--")) {
>>                 String[] property = arg.split("=");
>> and my bxml snippet is full of "=" due to the attribute values!
>> So, as a quick workaround, I kept the first element of the property table, and joined back the rest.
>> 
>> ScriptApplication.java:
>> String src = properties.get(SRC_KEY);
>> InputStream ins = new ByteArrayInputStream(src.getBytes());
>> BXMLSerializer bxmlSerializer = new BXMLSerializer();
>> window = (Window)bxmlSerializer.readObject(ins);
>> window.open(display);
>> 
>> This way it seems to work nicely.
>> 
>> So... may I suggest adding loading bxml from File and String also?
>> 
>> Thanks once again for your help,
>> jqg
>> 
>> 
>> 2010/11/22 Greg Brown <gk...@mac.com>
>> > So my reasons for doing that kind of stuff, well, er, curiosity? learning and playing Pivot from a tiny environment, manipulating the bxml xml with E4X and dynamically injecting js code in the GUI ?... I have to learn how to directly pass a bxml String or E4X/XML object to ScriptApplication and to manipulate objects from the rhino shell, as it's doable in Swing.
>> 
>> OK. Just in case you were not aware, BXML allows you to embed script code directly within your markup, and will invoke the appropriate script engine as needed. So don't necessarily need to launch Pivot from within Rhino to use Rhino.
>> 
>> G
>> 
>> 
> 


Re: launching Pivot programmatically

Posted by Greg Brown <gk...@mac.com>.
Why not pass the name of a file as an argument instead of the entire string? Or maybe you can read from standard in?

On Nov 22, 2010, at 7:59 AM, Jacques Granduel wrote:

> Thanks for your remark and I had indeed seen I could use "Pivot scripting", but I want to dynamically shape my view from the outside, according to some input.
> 
> What could be the best way to send this object (as a String or ByteArrayInputStream ?) to BXMLSerializer as the actual API seems to be 
> 
> java.lang.Object readObject(java.net.URL)
> java.lang.Object readObject(java.io.InputStream)
> java.lang.Object readObject(java.lang.Class,java.lang.String,boolean)
> java.lang.Object readObject(java.lang.Class,java.lang.String)
> java.lang.Object readObject(java.net.URL,org.apache.pivot.util.Resources).
> 
> I modified the ScriptApplication.java for allowing path/to/file.bxml to be accepted beside classpath resource using File(...).toURI().toURL().
> 
> As for the String, I wanted the equivalent, say (example in JS/Rhino)
> JS>var win = <Window title="Hello BXML!" maximized="true"
>     xmlns:bxml="http://pivot.apache.org/bxml"
>     xmlns="org.apache.pivot.wtk">
>     <Label text="Hello BXML!"
>         styles="{font:'Arial bold 24', color:'#ff0000',
>             horizontalAlignment:'center', verticalAlignment:'center'}"/>
> </Window>
> JS>ScriptApplication.main(["--src=" + win]);
> The latest crashed because DesktopApplicationContext.java splits args this way:
> if (arg.startsWith("--")) {
>                 String[] property = arg.split("=");
> and my bxml snippet is full of "=" due to the attribute values!
> So, as a quick workaround, I kept the first element of the property table, and joined back the rest.
> 
> ScriptApplication.java:
> String src = properties.get(SRC_KEY);
> InputStream ins = new ByteArrayInputStream(src.getBytes());
> BXMLSerializer bxmlSerializer = new BXMLSerializer();
> window = (Window)bxmlSerializer.readObject(ins);
> window.open(display);
> 
> This way it seems to work nicely.
> 
> So... may I suggest adding loading bxml from File and String also?
> 
> Thanks once again for your help,
> jqg
> 
> 
> 2010/11/22 Greg Brown <gk...@mac.com>
> > So my reasons for doing that kind of stuff, well, er, curiosity? learning and playing Pivot from a tiny environment, manipulating the bxml xml with E4X and dynamically injecting js code in the GUI ?... I have to learn how to directly pass a bxml String or E4X/XML object to ScriptApplication and to manipulate objects from the rhino shell, as it's doable in Swing.
> 
> OK. Just in case you were not aware, BXML allows you to embed script code directly within your markup, and will invoke the appropriate script engine as needed. So don't necessarily need to launch Pivot from within Rhino to use Rhino.
> 
> G
> 
> 


Re: launching Pivot programmatically

Posted by Jacques Granduel <jg...@gmail.com>.
Thanks for your remark and I had indeed seen I could use "Pivot scripting",
but I want to dynamically shape my view from the outside, according to some
input.

What could be the best way to send this object (as a String or
ByteArrayInputStream ?) to BXMLSerializer as the actual API seems to be

java.lang.Object readObject(java.net.URL)
java.lang.Object readObject(java.io.InputStream)
java.lang.Object readObject(java.lang.Class,java.lang.String,boolean)
java.lang.Object readObject(java.lang.Class,java.lang.String)
java.lang.Object readObject(java.net.URL,org.apache.pivot.util.Resources).

I modified the ScriptApplication.java for allowing path/to/file.bxml to be
accepted beside classpath resource using File(...).toURI().toURL().

As for the String, I wanted the equivalent, say (example in JS/Rhino)
JS>var win = <Window title="Hello BXML!" maximized="true"
    xmlns:bxml="http://pivot.apache.org/bxml"
    xmlns="org.apache.pivot.wtk">
    <Label text="Hello BXML!"
        styles="{font:'Arial bold 24', color:'#ff0000',
            horizontalAlignment:'center', verticalAlignment:'center'}"/>
</Window>
JS>ScriptApplication.main(["--src=" + win]);
The latest crashed because DesktopApplicationContext.java splits args this
way:
if (arg.startsWith("--")) {
                String[] property = arg.split("=");
and my bxml snippet is full of "=" due to the attribute values!
So, as a quick workaround, I kept the first element of the property table,
and joined back the rest.

ScriptApplication.java:
String src = properties.get(SRC_KEY);
InputStream ins = new ByteArrayInputStream(src.getBytes());
BXMLSerializer bxmlSerializer = new BXMLSerializer();
window = (Window)bxmlSerializer.readObject(ins);
window.open(display);

This way it seems to work nicely.

So... may I suggest adding loading bxml from File and String also?

Thanks once again for your help,
jqg


2010/11/22 Greg Brown <gk...@mac.com>

> > So my reasons for doing that kind of stuff, well, er, curiosity? learning
> and playing Pivot from a tiny environment, manipulating the bxml xml with
> E4X and dynamically injecting js code in the GUI ?... I have to learn how to
> directly pass a bxml String or E4X/XML object to ScriptApplication and to
> manipulate objects from the rhino shell, as it's doable in Swing.
>
> OK. Just in case you were not aware, BXML allows you to embed script code
> directly within your markup, and will invoke the appropriate script engine
> as needed. So don't necessarily need to launch Pivot from within Rhino to
> use Rhino.
>
> G
>
>

Re: launching Pivot programmatically

Posted by Greg Brown <gk...@mac.com>.
> So my reasons for doing that kind of stuff, well, er, curiosity? learning and playing Pivot from a tiny environment, manipulating the bxml xml with E4X and dynamically injecting js code in the GUI ?... I have to learn how to directly pass a bxml String or E4X/XML object to ScriptApplication and to manipulate objects from the rhino shell, as it's doable in Swing.

OK. Just in case you were not aware, BXML allows you to embed script code directly within your markup, and will invoke the appropriate script engine as needed. So don't necessarily need to launch Pivot from within Rhino to use Rhino.

G


Re: launching Pivot programmatically

Posted by Jacques Granduel <jg...@gmail.com>.
Hi,

2010/11/21 Greg Brown <gk...@mac.com>

> Is the test.bxml on your classpath? I'm not sure how the classpath is
> managed in Rhino, so I don't have any specific suggestions.
>
> I am curious to know why you want to launch your app via the Rhino shell,
> though. Any reason you can't simply launch it via
> DesktopApplicationContext#main()?
>


thank you for your reply.

At last, I checked ScriptApplication source,

I changed the URL object to be created from a path...
from:
URL location = classLoader.getResource(src.substring(1));
to:
String path = properties.get(SRC_KEY);
URL location = new File(src).toURI().toURL();

and that was it! I can now see my first Pivot Hello world!!

So my reasons for doing that kind of stuff, well, er, curiosity? learning
and playing Pivot from a tiny environment, manipulating the bxml xml with
E4X and dynamically injecting js code in the GUI ?... I have to learn how to
directly pass a bxml String or E4X/XML object to ScriptApplication and to
manipulate objects from the rhino shell, as it's doable in Swing.

Thanks again,
jqg

Re: launching Pivot programmatically

Posted by Greg Brown <gk...@mac.com>.
Is the test.bxml on your classpath? I'm not sure how the classpath is managed in Rhino, so I don't have any specific suggestions.

I am curious to know why you want to launch your app via the Rhino shell, though. Any reason you can't simply launch it via DesktopApplicationContext#main()?

G

On Nov 21, 2010, at 4:36 AM, Jacques Granduel wrote:

> Hi, 
> 
> I'm sorry for coming back on this old thread about learning how to launch a Pivot application from a shell (Rhino shell in this case). I've made a big step forward trying bxml+ScriptApplication, but it's still not enough! What am I missing??
> 
> I've saved  this file from the 2.0 tutorial:
> <Window title="Hello JavaScript!" maximized="true"
>     xmlns:bxml="http://pivot.apache.org/bxml"
>     xmlns="org.apache.pivot.wtk">
>     <windowStateListeners>
>         importPackage(org.apache.pivot.wtk);
>  
>         function windowOpened(window) {
>             var label = new Label();
>             label.setText("Hello JavaScript!");
>             label.getStyles().put("font", "Arial BOLD 24");
>             label.getStyles().put("color", "#ff0000");
>             label.getStyles().put("horizontalAlignment", HorizontalAlignment.CENTER);
>             label.getStyles().put("verticalAlignment", VerticalAlignment.CENTER);
>  
>             window.setContent(label);
>         }
>     </windowStateListeners>
> </Window>
> into a file test.bxml.
> I use from Java, ScriptApplication.main(new String[]{"--src=path/to/test.bxml"}); or from Rhino ScriptApplication.main(["--src="path/to/test.bxml"])
> but in both cases, I get an IllegalArgumentException although the text.bxml is found by Java.
> 
> java.lang.IllegalArgumentException: Cannot find source file "test.bxml".
>         at org.apache.pivot.wtk.ScriptApplication.startup(ScriptApplication.java:47)
>         at org.apache.pivot.wtk.DesktopApplicationContext$2.run(DesktopApplicationContext.java:594)
>         at org.apache.pivot.wtk.ApplicationContext$QueuedCallback.run(ApplicationContext.java:1457)
>         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
>         at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
>         at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
>         at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
>         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
>         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
>         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
>         at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
> 
> Thanks again, 
> regards
> jqg
>