Quantcast

findInstance() not generated, what is the "new" way of doing this ?

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

findInstance() not generated, what is the "new" way of doing this ?

bruehlicke
Using Nb 8.2 Wizard to create a new TopComponent I see that the "findInstance()" method is not created anymore linke in older versions. I understand we want to allow for Singletons as well as non-singletons, but
1) Is it still practice to create a method "findInstance" ?
2) If not what is the new way of finding an instance ?  

Regards
Bernd




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

findInstance() not generated, what is the "new" way of doing this ?

bruehlicke
Something similar was asked back in 2013 and not directly answered. So the questions about the "unwritten rule" of having a method called "findInstance()" is still not answered in my view.

The link is here: http://ww.planetnetbeans.org/post-149853.html




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

Re: findInstance() not generated, what is the "new" way of doing this ?

Geertjan Wielenga-3

windowmanager.getdefault().findtopcomponent

Gj

On 17-1-2017 19:57, bruehlicke wrote:
> Something similar was asked back in 2013 and not directly answered. So the questions about the "unwritten rule" of having a method called "findInstance()" is still not answered in my view.
>
> The link is here: http://ww.planetnetbeans.org/post-149853.html
>
>
>
>

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

findInstance() not generated, what is the "new" way of doing this ?

bruehlicke
In reply to this post by bruehlicke
Thanx Geertjan, though this guy could return null. Hence in "old days" aka before introduction of annotations (i guess before 7.0) the TopComponent creation Wizard created code with the "findInstance" method like :


Code:

    /**
     * Obtain the MyTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized MyTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(MyTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof MyTopComponent) {
            return (MyTopComponent) win;
        }
        Logger.getLogger(MyTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }




Of course this assumed each TopComponent is a Singleton.  So I guess that the "findInstance" method is hereby grand-fathered and not "standard" anymore and up to each developer to create or not - fair enough. It was anyhow never in the TopComponent interface. (same as getDefault()).

Bests and Thanx again for all the effort being but into the move to Apache.




Loading...