Re: Children.getNodes() gives copy of nodes

Tim Boudreau <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <CA+qecRNQwe+Dq9PR3F+fhi40GkOBn0QeLfyTFV6MywDoRYcc+g@mail.gmail.com>
Yes, you've pretty much got it.  Nodes are created on demand when the tree
is expanded.  When it is collapsed, the nodes can become unreferenced and
be garbage collected.  And that's a good thing - if you consider just in
the IDE, the number of temporary trees that get shown for one reason or
another - in the navigator, debugger or other components.  You wouldn't
want those hanging around taking up memory for the life of the JVM, when
there is no reason to expect the user will ever expand or interact with
them again.  So the API does not cache them.

As you note, if you cache them yourself, you will get back the same nodes
every time.

But your problem is a design problem:  If you need to access nodes
programmatically and update their state, then you are misusing nodes.
Nodes are *presentation objects* - they should *represent* other objects
that have state, and provide actions, children, human readable text and
icons *over* a business object.  If you need to access a node and update
some state that *it* holds, then you are using the nodes themselves as
business objects.  You're far better off separating the business objects -
things that actually represent data and hold state - from the nodes that
represent them, because:

 - You'll have problems like the one you're having

 - Sooner or later you'll want a different hierarchy or presentation for
the same stuff - the same way a project in the Projects tab and a folder in
the Files tab in the IDE are two views over the same object, and if the
business logic is welded into the node implementation, you're going to have
a nasty refactoring job or a lot of copy/pasted code.  You're better off
fixing it now than either of those solutions, before the code grows any
further.

-Tim


On Mon, Sep 12, 2016 at 2:14 PM, rsobies <[email protected]> wrote:

> thank you Tim.
> Do you suggest that if a node is expanded by the user and its subnodes are
> created on demand and after collapsing, the subnodes can be destroyd by gc?
> or maybe in this case they have strong reference kept by BeanTreeView?
>
> i try to understand the mechanism of cacheing nodes in Tree.
> in my application i modify state of subnodes when Tree is not fully
> expanded yet. sometimes i loose state of subnode and sometimes not, seems
> randomly
>
> maybe there is a way to get nodes through BeanTree object to make sure
> that new refereces are kept by the tree?
>
>
>
>
>


-- 
http://timboudreau.com
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.