Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36808
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Custom JTree does this make sense? |
| Date | 2017-07-06 21:33 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <ojmab7$dgf$1@dont-email.me> (permalink) |
| References | <e7950596-536f-46cd-8af7-34bf7b12fcdc@googlegroups.com> <7dd7b213-f067-406b-845c-b4849d5106eb@googlegroups.com> |
On Thu, 06 Jul 2017 11:13:27 -0700, Eric Douglas wrote:
> On Thursday, July 6, 2017 at 1:52:33 PM UTC-4, Eric Douglas wrote:
>> I have a custom class which extends JTree.
>> Every node on my tree is required to be of my custom node class which
>> extends DefaultMutableTreeNode.
>>
>> I was trying to think of how to fix a problem I had linking nodes when
>> I noticed the implementation. DefaultMutableTreeNode stores it's
>> children in a Vector, so you can have one child in more than one
>> parent. Each node just needs to implement the TreeNode interface which
>> requires each node have at least one parent with a getParent() method
>> returning a single TreeNode object.
>>
>>
> Actually it couldn't be the same node because I want them to have
> separate states, so it would have to be different child node instances
> with shared property objects, so if you call add/remove parent or child
> on one, or setUserObject(), isLeaf(), getPath(), all that stuff should
> affect both. I just want to be able to expand one and not the other..
Do you have any DBA experience? I ask because this is a classic Bill-of
Materials problem and something I find easiest to describe with an Entity
Relationship Diagram (ERD) and to implement with an RDBMS, though once
you understand the ERD, it is also easy enough to implement as a linked
Class structure. The usual BOM ERD looks something like this:
_____
/ \
| Part *|
\_____/
| |
is-made-of is-used-in
| |
/|\ /|\
-------
/ \
|Component|
\_______/
which is a recursive structure where Component is really just a link that
can be traversed following the 'is-made-of' relation to see what
components make up a part. Traversing the is-used-in relation shows what
larger things a part is a subcomponent of.
In this diagram the '*' indicates that this is an entry point and so
entities marked this way would have a unique key. The inverted trees are
known as "crows feet" and indicate a 1:many relationship between the
owning entity (at the single end) and zero or more instances of the owned
entity at the 'many' end.
In this case Part would be a class that has a unique key (name) and is
held in a TreeMap or HashMap structure. Its also a convenient place to
hold attributes like price, dimensions, and how many are in stock. The
list of Components needed to build a composite Part is conveniently held
as an ArrayList of Component references.
The Component class has no natural key. It contains:
(a) a reference Part its a component of
(b) a reference to the subordinate Part used to assemble its owning Part
(c) number of instances of the subordinate Part needed
In an RDBMS its key would be the combination of (a) and (b).
If you think if it this way (and draw a few diagrams that describe
moderately complicated composite Parts with at least one common
component) it will soon become obvious how the structure works and how
you can easily build, maintain and traverse it.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-06 10:52 -0700
Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-06 11:13 -0700
Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-06 21:33 +0000
Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 08:45 -0700
Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-12 16:56 +0000
Re: Custom JTree does this make sense? Eric Douglas <e.d.programmer@gmail.com> - 2017-07-12 10:27 -0700
Re: Custom JTree does this make sense? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-12 18:31 +0000
csiph-web