You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Tony Herstell <to...@cambridgesoftware.co.nz> on 2005/12/12 22:35:22 UTC

contrib:tree working with objects! - Solution - Shoud add to Tapestry Help on contrib:Tree page(s) for 3 and 4.


Finally got the contrib:tree to work with an object rather than just a
string...

The trick (as people have asked me to post this if I manage to get it to
work) is:

In the Object (in my case it was called Organisation) you have to supply
these methods:

	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}

		if (!(other instanceof Organisation)) {
			return false;
		}

		Organisation otherOrganisation = (Organisation) other;

		return name.equals(otherOrganisation.name);
	}

	public int hashCode() {
		int result = 17;
		result = result * 37 + (name == null ? 0 : name.hashCode());
		return result;
	}

Now, I don’t take the credit for this.. A guru friend of mine found the
answer...

Refer to:

Tony,

I had a quick look, it's actually quite a simple thing to fix.  The tree
component uses Java Collections under the hood (no great suprise) so you
need a decent equals and hashCode method on the element in the tree's model
(Organisation).  Below are two methods that work, but they probably aren't
what you want equality should probably be based on a unique identifier for
each Organisation (not name).  If you try using the ones below check out
what happens with two organisations with the same name!!

Check out chapter 3 of EffectiveJava on how to implement these methods well.

  (http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
- if you don't have a copy).
You may find yourself needing these methods in several more objects.

Ivan
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.13.13/198 - Release Date: 12/12/2005
 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org