pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ilya Zimnovich <zimnov...@gmail.com>
Subject Re: Unserialize an object in class constructor
Date Mon, 24 Mar 2014 19:41:47 GMT
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:zimnovich@gmail.com] 
> Sent: Monday, March 24, 2014 9:26 AM
> To: user@pivot.apache.org
> 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
>
>


Mime
View raw message