question about extending forms, and "AppRouter"

28 views
Skip to first unread message

Shay Tessler

unread,
Aug 5, 2014, 1:11:55 PM8/5/14
to codenameone...@googlegroups.com
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA: eclipse
Desktop OS: windows 7 
Simulator : iphone 3gd
Device: ---

i guess i don't know a API that well..
so i am trying to implement something that maybe there is a solution for it....
hope i help my understand better if an there is already an existing solution  

1) is it a good practice to extend a form, so all  form will handle UI/Events by it self ?

2) also when i want to call one From from another Form. should i create a singleton that holds all Forms as static variables  (see example AppRouter)?

3) in case one form calling another form, and i want to pass an argument between them is it ok what i made in the attached example ?

thank you,
shay 


public class AppRouter {
    private static AppRouter m_appRouter = new AppRouter();
    
    private HashMap<String, FromArgs> m_forms = new HashMap<String, FromArgs>();
    
    private AppRouter() {
        m_forms.put("FROM_MAIN", new MainForm());
        m_forms.put("FROM_SEARCH", new SearchForm());
        m_forms.put("FROM_BLAH", new BlahForm());
        ...
        ...
        ...
        ...
        
    }
    
    public static AppRouter get() {
        return m_appRouter;
    }
    
    public void showForm(String formname, Object arg) {
        FromArgs form = m_forms.get(formname);
        if(form != null) {
            form.showForm(arg);
        }
    } 
}



abstract class FromArgs extends Form {
    
    public abstract void setFormArgs(Object arg); //used when user get argument from previous form 
    
    public void showForm(Object... arg) {
        setFormArgs(arg);
        show();
    }
}



Shay Tessler

unread,
Aug 5, 2014, 1:19:41 PM8/5/14
to codenameone...@googlegroups.com
another thinking adds 4
4) is it possible to get "MyApplication" class from some Singleton service ?

Shai Almog

unread,
Aug 6, 2014, 2:10:27 AM8/6/14
to codenameone...@googlegroups.com
Our GUI builder applications revolve around a state machine that handles the navigation (controller pattern similar to struts).
You can derive form and its OK but I'm not sure if that is the best place to place event handling code, this is really high level and abstract in that sense.

You can check out what we did in the UIBuilder class to see how we solved some of these problems in the GUI builder, its not for the faint of heart though.

Shay Tessler

unread,
Aug 6, 2014, 3:07:43 PM8/6/14
to codenameone...@googlegroups.com
One final question, dose CN1 expect me to create a new Form each time i want to display a form ? OR to keep the Form instance as i am doing now ? , what is the best practice ?

i fond it hard to get around examples etc, lots of CN1 examples are videos about Gui bulder, i prefer to program, i will be nice to have some example code at the bottom of each video  , and or an Example of a sample StateMachine for people like me. 
i guess writing a StateMachine it will be the best way since you know best how to operate CN1.

thank shai

Shai Almog

unread,
Aug 6, 2014, 11:59:11 PM8/6/14
to codenameone...@googlegroups.com
The kitchen sink keeps the main form instance but always recreates the demo forms. You can go both ways, if you have many forms that have a lot of references in them it might be wise to discard them but you don't have to.
Both ways should work fine.
Reply all
Reply to author
Forward
0 new messages