Re: LogicalViewProvider.createLogicalView is called twice when project explorer is created. Why?

Tim Boudreau <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <CA+qecRNPZhhxPfUDwPyfMgJiu2mcVg8g8hnOpJyx1z3JXVWJQQ@mail.gmail.com>
I'll reiterate:  Cache the Node.  If you need it to be a singleton with
respect to the project, that is the only way.

*Anything* that can get hold of your project can get the
LogicalViewProvider from its lookup and, for whatever reason it wants to,
fetch the node and display it.  Project chooser dialogs, any piece of UI
that wants to show your project can do that - perfectly legally.
LogicalViewProvider is public API and your Project object *has* to expose
it.  That means any part of the system can call it, period.

The bug is that the method name starts with "create".  Make the node cheap
to create, and do the work when the children are expanded or some other
action that requires the structure to be known is invoked.  Let the node
itself be just an icon.

If that would mean changing a sea of code and you don't want to do it, see
if you can use a FilterNode with just the right icon and name, and
initialize the real one on-demand.  See getOriginal() / setOriginal().

-Tim


On Tue, Feb 2, 2016 at 12:59 PM, Eirik Bakke <[email protected]> wrote:

> > The constructor of the Node adds itself as a PropertyChangeListener (I
> use a WeakListener, by the way) to the preference manager object.   When
> the preference manager fires a property change, both instances of the node
> respond to that change, causing the action to be fired twice.
>
> That sounds like the correct behavior, then. Could the act of responding
> to the change be made into a cheap operation? For instance by running the
> expensive initialization in a separate thread that is only started once, or
> when actually necessary?
>
> As Tim mentions, there's also the opportunity to do additional expensive
> work as other descendant nodes are expanded, using the ChildFactory API.
>
> -- Eirik
>
> From: Todd Stevenson <[email protected]>
> Reply-To: "[email protected]" <[email protected]>
> Date: Tuesday, February 2, 2016 at 11:24 AM
>
> To: "[email protected]" <[email protected]>
> Subject: [platform-dev] Re: LogicalViewProvider.createLogicalView is
> called twice when project explorer is created. Why?
>
> I misspoke on the propertyChangeListener.  The constructor of the Node
> adds itself as a PropertyChangeListener (I use a WeakListener, by the way)
> to the preference manager object.   When the preference manager fires a
> property change, both instances of the node respond to that change, causing
> the action to be fired twice.
>
>
>
>
>
>
>
> *Todd Stevenson*
>
> Sr Clinical Modeling Engineer
>
> Clinical Modeling
>
> *3930 Parkway Blvd |Salt Lake City, UT 84120*
>
> Office: 801-442-5112 | Cell: 801-589-1115
>
> [email protected]
>
>
>
> *From:* Eirik Bakke [mailto:[email protected] <[email protected]>]
> *Sent:* Tuesday, February 02, 2016 9:11 AM
> *To:* [email protected]
> *Subject:* [platform-dev] Re: LogicalViewProvider.createLogicalView is
> called twice when project explorer is created. Why?
>
>
>
> It's the Node object that shouldn't be cached--you're free to cache data
> structures that you define yourself in your business logic. Generally,
> nodes should be cheap to create, and they can be created multiple times.
>
>
>
> > My problem is that I’m setting a PropertyChangedListener in the
> constructor of the root node.  This is getting set twice and then later on
> when the property changes, all the nodes in the project are updated twice.
>
>
>
> This sounds odd. If each node adds a PropertyChangeListener in its own
> constructor, then each node will only be subscribed once, since a
> constructor runs only once per object instance.
>
>
>
> One note, though: When attaching listeners to external model objects from
> new Node implementations, it's important to use weak listeners [1] only.
> Otherwise your Node objects will stay in memory and receive events even
> after the UI has thrown away its last references to them.
>
>
>
> [1]
> http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/WeakListeners.html
>
>
>
> *From: *Todd Stevenson <[email protected]>
> *Reply-To: *"[email protected]" <[email protected]>
> *Date: *Tuesday, February 2, 2016 at 10:35 AM
> *To: *"[email protected]" <[email protected]>
> *Subject: *[platform-dev] Re: LogicalViewProvider.createLogicalView is
> called twice when project explorer is created. Why?
>
>
>
> The documentation in LogicalViewProvider says not to cache the call.
>
>
>
>
> http://bits.netbeans.org/8.1/javadoc/org-netbeans-modules-projectuiapi/org/netbeans/spi/project/ui/LogicalViewProvider.html
>
>
>
> My problem is that I’m setting a PropertyChangedListener in the
> constructor of the root node.  This is getting set twice and then later on
> when the property changes, all the nodes in the project are updated
> twice.
>
>
>
> *Todd Stevenson*
>
> Sr Clinical Modeling Engineer
>
> Clinical Modeling
>
> *3930 Parkway Blvd |Salt Lake City, UT 84120*
>
> Office: 801-442-5112 | Cell: 801-589-1115
>
> [email protected]
>
>
>
> *From:* Tim Boudreau [mailto:[email protected] <[email protected]>]
> *Sent:* Monday, February 01, 2016 8:57 PM
> *To:* [email protected]
> *Subject:* [platform-dev] Re: LogicalViewProvider.createLogicalView is
> called twice when project explorer is created. Why?
>
>
>
> Cache the result of the first call.
>
> Better yet, initialize as little as possible up-front (you can do
> structural stuff on-demand when the node is expanded), but it's perfectly
> fine to cache the result.  There's nothing in the API that guarantees that
> it will only be called once.
>
>
>
> -Tim
>
>
>
> On Mon, Feb 1, 2016 at 6:34 PM, Todd Stevenson <[email protected]>
> wrote:
>
> I am looking at the tutorial found at
>
>
>
>
> https://platform.netbeans.org/tutorials/nbm-projecttype.html#projectlogicalview
>
>
>
> and I’ve built it from the source found in the repository at:
>
>
>
>
> https://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.2/tutorials/CustomerProjectType
>
>
>
> After I create a customer project as outlined in the tutorial and save it
> and restart the component I can see that createLogicalView() is called
> twice.  (line 223 of CustomerProject.java).  Is this correct behavior?  It
> seems that it should be called once when initializing the app.
>
>
>
> This is causing performance problems in my app because the initialization
> of the project structure in my app is somewhat expensive.
>
>
>
> I’m using Netbeans 8.0.2.
>
>
>
> Any ideas?  Any suggestions?
>
>
>
> Thank you.
>
>
>
> *Todd Stevenson*
>
>
>
>
>
>
>
> --
>
> http://timboudreau.com
>



-- 
http://timboudreau.com
image002.jpg (image/jpeg, 2.9 KB) - not displayed
image003.jpg (image/jpeg, 2.9 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.