Unserialize an object in class constructor

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Unserialize an object in class constructor

Ilya Zimnovich
Dear Apache Pivot experts,

All the Apache Pivot examples unserialize an objects with
BXMLSerializer's readObject method:

....
    public void startup(Display display, Map<String, String> properties)
throws Exception
    {
        String language = properties.get("language");
        Locale locale = (language == null) ? Locale.getDefault() : new
Locale(language);
        Resources resources = new
Resources(FinanceWindow.class.getName(), locale);
       
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        window =
(FinanceWindow)bxmlSerializer.readObject(getClass().getResource("FinanceWindow.bxml"),
resources);
        window.open(display);
    }
....

Is there a way to unserialize the object in its constructor? I mean
something like the below:

....
    public void startup(Display display, Map<String, String> properties)
throws Exception
    {
        window = new FinanceWindow();
        window.open(display);
    }
....

public class FinanceWindow extends Frame implements Bindable
{
....

    public FinanceWindow()
    {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();

        // Load localized resources and unserialize the object
        String language = properties.get("language");
        Locale locale = (language == null) ? Locale.getDefault() : new
Locale(language);
        Resources resources = new Resources(getClass().getName(), locale);

        bxmlSerializer.readObjectXXXX(this, "FinanceWindow.bxml");

....


Best Regards,
Ilya A. Zimnovich
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Unserialize an object in class constructor

Roger Whitcomb-2
Hi Ilya,
        Well, the answer is yes, but ....  If your FinanceWindow.bxml has the top-level object as a FinanceWindow, then if in the FinanceWindow constructor you load that bxml file, you will get into an infinite recursion.  But, if your FinanceWindow.bxml has just the content of your window and you create the top-level Window object some other way (i.e. just do a "new FinanceWindow" in the startup code as you have it), then you could just read the contents of the window and set the window's child from the BXML file (which is NOT what you have in your code), then that should work.
        I'm curious why you want to do this?  It is pretty much the same code either way, but if you let the BXMLSerializer create all the objects (including the top-level window) it is a tiny bit less work in your code.  Or is there a design pattern you're trying to achieve?

HTH,
~Roger

-----Original Message-----
From: Ilya Zimnovich [mailto:[hidden email]]
Sent: Monday, March 24, 2014 9:26 AM
To: [hidden email]
Subject: Unserialize an object in class constructor

Dear Apache Pivot experts,

All the Apache Pivot examples unserialize an objects with BXMLSerializer's readObject method:

....
    public void startup(Display display, Map<String, String> properties) throws Exception
    {
        String language = properties.get("language");
        Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
        Resources resources = new
Resources(FinanceWindow.class.getName(), locale);
       
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        window =
(FinanceWindow)bxmlSerializer.readObject(getClass().getResource("FinanceWindow.bxml"),
resources);
        window.open(display);
    }
....

Is there a way to unserialize the object in its constructor? I mean something like the below:

....
    public void startup(Display display, Map<String, String> properties) throws Exception
    {
        window = new FinanceWindow();
        window.open(display);
    }
....

public class FinanceWindow extends Frame implements Bindable { ....

    public FinanceWindow()
    {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();

        // Load localized resources and unserialize the object
        String language = properties.get("language");
        Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
        Resources resources = new Resources(getClass().getName(), locale);

        bxmlSerializer.readObjectXXXX(this, "FinanceWindow.bxml");

....


Best Regards,
Ilya A. Zimnovich


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unserialize an object in class constructor

Ilya Zimnovich
Hi Roger,

So you mean I can put some child control of the FinanceWindow class into
a FinanceWindow.bxml file, then create this child control using
BXMLSerializer's readObject method and attach it to a FinanceWindow
instance in the constructor, right? As far as I understand the
FinanceWindow.bxml file can contain only one root element. Something
like BoxPane or TablePane. This way it will be possible to just do a
"new FinanceWindow" in the Application derived object.

Why do I want to do it? Well, I would like to properly decompose my
application, just like it is done in wxWidgets or QT C++ frameworks I
used to work with. Below is how it is done in wxWidgets (C++):

class TestWnd_Base : public wxFrame
{
protected:
    wxTextCtrl* A;
    wxButton* B;

public:
    TestWnd::TestWnd()
    {
        wxXmlResource::Get()->LoadObject(this, NULL, "TestWnd", "wxFrame");
        A = XRCCTRL(*this, "A", wxTextCtrl);
        B = XRCCTRL(*this, "B", wxButton);
    }
};

Well, don't forget the Android Activities:

public class SetWallpaperActivity extends FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Inflate XML layout
        setContentView(R.layout.set_wallpaper_activity);
    }
}

I sure that it is the Window object itself who should take the
responsibility to create the children objects and to load its
localization resources. The other windows and panels should also load
their localization resources and create their children from bxml file.
Of course I do understand that it's a matter of ones taste!

Best Regards,
Ilya A. Zimnovich

On 24.03.2014 21:42, Roger Whitcomb wrote:

> Hi Ilya,
> Well, the answer is yes, but ....  If your FinanceWindow.bxml has the top-level object as a FinanceWindow, then if in the FinanceWindow constructor you load that bxml file, you will get into an infinite recursion.  But, if your FinanceWindow.bxml has just the content of your window and you create the top-level Window object some other way (i.e. just do a "new FinanceWindow" in the startup code as you have it), then you could just read the contents of the window and set the window's child from the BXML file (which is NOT what you have in your code), then that should work.
> I'm curious why you want to do this?  It is pretty much the same code either way, but if you let the BXMLSerializer create all the objects (including the top-level window) it is a tiny bit less work in your code.  Or is there a design pattern you're trying to achieve?
>
> HTH,
> ~Roger
>
> -----Original Message-----
> From: Ilya Zimnovich [mailto:[hidden email]]
> Sent: Monday, March 24, 2014 9:26 AM
> To: [hidden email]
> Subject: Unserialize an object in class constructor
>
> Dear Apache Pivot experts,
>
> All the Apache Pivot examples unserialize an objects with BXMLSerializer's readObject method:
>
> ....
>     public void startup(Display display, Map<String, String> properties) throws Exception
>     {
>         String language = properties.get("language");
>         Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
>         Resources resources = new
> Resources(FinanceWindow.class.getName(), locale);
>        
>         BXMLSerializer bxmlSerializer = new BXMLSerializer();
>         window =
> (FinanceWindow)bxmlSerializer.readObject(getClass().getResource("FinanceWindow.bxml"),
> resources);
>         window.open(display);
>     }
> ....
>
> Is there a way to unserialize the object in its constructor? I mean something like the below:
>
> ....
>     public void startup(Display display, Map<String, String> properties) throws Exception
>     {
>         window = new FinanceWindow();
>         window.open(display);
>     }
> ....
>
> public class FinanceWindow extends Frame implements Bindable { ....
>
>     public FinanceWindow()
>     {
>         BXMLSerializer bxmlSerializer = new BXMLSerializer();
>
>         // Load localized resources and unserialize the object
>         String language = properties.get("language");
>         Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
>         Resources resources = new Resources(getClass().getName(), locale);
>
>         bxmlSerializer.readObjectXXXX(this, "FinanceWindow.bxml");
>
> ....
>
>
> Best Regards,
> Ilya A. Zimnovich
>
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Unserialize an object in class constructor

Roger Whitcomb-2
Right.

-----Original Message-----
From: Ilya Zimnovich [mailto:[hidden email]]
Sent: Monday, March 24, 2014 12:42 PM
To: [hidden email]
Subject: Re: Unserialize an object in class constructor

Hi Roger,

So you mean I can put some child control of the FinanceWindow class into a FinanceWindow.bxml file, then create this child control using BXMLSerializer's readObject method and attach it to a FinanceWindow instance in the constructor, right? As far as I understand the FinanceWindow.bxml file can contain only one root element. Something like BoxPane or TablePane. This way it will be possible to just do a "new FinanceWindow" in the Application derived object.

Why do I want to do it? Well, I would like to properly decompose my application, just like it is done in wxWidgets or QT C++ frameworks I used to work with. Below is how it is done in wxWidgets (C++):

class TestWnd_Base : public wxFrame
{
protected:
    wxTextCtrl* A;
    wxButton* B;

public:
    TestWnd::TestWnd()
    {
        wxXmlResource::Get()->LoadObject(this, NULL, "TestWnd", "wxFrame");
        A = XRCCTRL(*this, "A", wxTextCtrl);
        B = XRCCTRL(*this, "B", wxButton);
    }
};

Well, don't forget the Android Activities:

public class SetWallpaperActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Inflate XML layout
        setContentView(R.layout.set_wallpaper_activity);
    }
}

I sure that it is the Window object itself who should take the responsibility to create the children objects and to load its localization resources. The other windows and panels should also load their localization resources and create their children from bxml file.
Of course I do understand that it's a matter of ones taste!

Best Regards,
Ilya A. Zimnovich

On 24.03.2014 21:42, Roger Whitcomb wrote:

> Hi Ilya,
> Well, the answer is yes, but ....  If your FinanceWindow.bxml has the top-level object as a FinanceWindow, then if in the FinanceWindow constructor you load that bxml file, you will get into an infinite recursion.  But, if your FinanceWindow.bxml has just the content of your window and you create the top-level Window object some other way (i.e. just do a "new FinanceWindow" in the startup code as you have it), then you could just read the contents of the window and set the window's child from the BXML file (which is NOT what you have in your code), then that should work.
> I'm curious why you want to do this?  It is pretty much the same code either way, but if you let the BXMLSerializer create all the objects (including the top-level window) it is a tiny bit less work in your code.  Or is there a design pattern you're trying to achieve?
>
> HTH,
> ~Roger
>
> -----Original Message-----
> From: Ilya Zimnovich [mailto:[hidden email]]
> Sent: Monday, March 24, 2014 9:26 AM
> To: [hidden email]
> Subject: Unserialize an object in class constructor
>
> Dear Apache Pivot experts,
>
> All the Apache Pivot examples unserialize an objects with BXMLSerializer's readObject method:
>
> ....
>     public void startup(Display display, Map<String, String> properties) throws Exception
>     {
>         String language = properties.get("language");
>         Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
>         Resources resources = new
> Resources(FinanceWindow.class.getName(), locale);
>        
>         BXMLSerializer bxmlSerializer = new BXMLSerializer();
>         window =
> (FinanceWindow)bxmlSerializer.readObject(getClass().getResource("Finan
> ceWindow.bxml"),
> resources);
>         window.open(display);
>     }
> ....
>
> Is there a way to unserialize the object in its constructor? I mean something like the below:
>
> ....
>     public void startup(Display display, Map<String, String> properties) throws Exception
>     {
>         window = new FinanceWindow();
>         window.open(display);
>     }
> ....
>
> public class FinanceWindow extends Frame implements Bindable { ....
>
>     public FinanceWindow()
>     {
>         BXMLSerializer bxmlSerializer = new BXMLSerializer();
>
>         // Load localized resources and unserialize the object
>         String language = properties.get("language");
>         Locale locale = (language == null) ? Locale.getDefault() : new Locale(language);
>         Resources resources = new Resources(getClass().getName(),
> locale);
>
>         bxmlSerializer.readObjectXXXX(this, "FinanceWindow.bxml");
>
> ....
>
>
> Best Regards,
> Ilya A. Zimnovich
>
>



Loading...