Alright, but only because I made you suffer so much with the initial bugs you found in cvs ;)

The File Tree demo that you are looking at uses the "comparator" parameter of tree, which as the documentation states, is used to sort the given elements in your ITreeContentProvider interface.

This makes sense in the context of the FileTree demo, because the contents aren't sorted by default and could result in random results for each request.

The Comparator interface is native to the java api, you can find it in any javadoc for it. (ie http://java.sun.com/j2se/1.4.2/docs/api/index.html ). Basically, it's almost the same idea as the good old equals(Object obj) interface that every java object has. Comparing two values for ordering isn't quite as simple to provide by default, which is why the interface exists.

The values that you return aren't strict, it's just ~your~ way of comparing them. Tell me what objects you're displaying and I can tell you how to compare them. So...being hypothetical, let's say that you have a tree of umm....Drugs...Not that a tree makes sense in that instance, but we'll say that's what we're doing anyways...

You could implement Comparator this way:

Drug d1 = (Drug)obj1;
Drug d2 = (Drug)obj2;

if (d1.getBillPackSize() > d2.getBillPackSize())
 return +1;
else if (d1.getBillPackSize() == d2.getBillPackSize ())
 return 0;
else
 return -1; //it has to be less than

You could also sort the drugs by their generic names:
Drug d1 = (Drug)obj1;
Drug d2 = (Drug)obj2;

return d1.getGenericName().compareTo(d2.getGenericName());

You'll notice I'm being a little tricky here, in my version of a Drug object getGenericName returns a String. The tricky part is that if you go lookup the javadocs for String you'll see that it implements Comparable. So they've done the work for us already, woo hoo! ;) Here is what String says it does to implement it:

Compares this String to another Object. If the Object is a String, this function behaves like compareTo(String). Otherwise, it throws a ClassCastException (as Strings are comparable only to other Strings).

Specified by:
compareTo in interface Comparable
Parameters:
o - the Object to be compared.
Returns:
the value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.

So...You can really return whatever the hell you feel like, just make sure that your logic is consistent.


On 11/29/05, Cosmin Bucur <[EMAIL PROTECTED]> wrote:
if there are any hints or tips that you think a noob might make good
use of when starting with Tree let me know , other than that i'll just
do my best to formulate some answerable questions :)

On 11/29/05, Cosmin Bucur < [EMAIL PROTECTED]> wrote:
> UGH ... i'm failing to see the big picture with the tree big time ...
>
> But I  think that this sorter isn't as critical as other things . I'll
> keep on banging my head against it , dooing test deploy after test
> deploy and will try to form some more educated questions ...
>
> it's obvious i'm sure , but this is so unclear and confusing to me ,
> that i can't even form a proper question that would help me get it . .
> .
>
> On 11/29/05, Jesse Kuhnert <[EMAIL PROTECTED]> wrote:
> > public int compare(Object o1,
> >
> Object o2)
> > Compares its two arguments for order. Returns a negative integer, zero, or a
> > positive integer as the first argument is less than, equal to, or greater
> > than the second.
> >
> > The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x))
> > for all x and y. (This implies that compare(x, y) must throw an exception if
> > and only if compare(y, x) throws an exception.)
> >
> > The implementor must also ensure that the relation is transitive:
> > ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.
> >
> > Finally, the implementer must ensure that compare(x, y)==0 implies that
> > sgn(compare(x, z))==sgn(compare(y, z)) for all z.
> >
> > It is generally the case, but not strictly required that (compare(x, y)==0)
> > == (x.equals(y)). Generally speaking, any comparator that violates this
> > condition should clearly indicate this fact. The recommended language is
> > "Note: this comparator imposes orderings that are inconsistent with equals."
> >
> > Parameters:o1 - the first object to be compared.o2 - the second object to be
> > compared. Returns:a negative integer, zero, or a positive integer as the
> > first argument is less than, equal to, or greater than the second. Throws:
> > ClassCastException - if the arguments' types prevent them from being
> > compared by this Comparator.
> >
> >
> > On 11/29/05, Cosmin Bucur <[EMAIL PROTECTED]> wrote:
> > >
> > > The value returned does not come from the comparator logic , but from
> > > a method inside the File object . I won't be using File so I need to
> > > know what int value i need to return from compare(Object o , Object o2
> > > ) under which cases
> > >
> > > On 11/29/05, Cosmin Bucur <[EMAIL PROTECTED]> wrote:
> > > > I wanted to know what values the Tacos tree is expecting for what cases
> > > >
> > > > Maybe it's just me having holes in my basic java knowledge then , and
> > > > in this case working with the Comparator interface is a prerequisite
> > > > of beeing able to manipulate tacos:Tree ?
> > > >
> > > > On 11/29/05, Jesse Kuhnert <[EMAIL PROTECTED] > wrote:
> > > > > The Comparator interface is a java construct. Lots of the java
> > internals
> > > > > already implement Comparable, like String/numbers/etc.. (I think).
> > > > >
> > > > > Not sure what you want explained here? There are probably lots of
> > tutorials
> > > > > on this on the net.
> > > > >
> > > > >
> > > > > On 11/29/05, Cosmin Bucur < [EMAIL PROTECTED]> wrote:
> > > > > >
> > > > > > I just want to build my own type of PartialTree , that instead of
> > > > > > files shows other objects .
> > > > > >
> > > > > > In fact the scenario is a bit different as the objects shown in the
> > > > > > tree are not a single type of object ( allthohugh I might need to
> > > > > > write a single type of object enccapsulating diferentiation logic
> > ...
> > > > > > i don't know , still trying to get how the tree's working )
> > > > > >
> > > > > > Right now i'm stuck at the FileComparator . My first baby step was
> > to
> > > > > > recreate Partial Tree in my app , and get it working . Got that .
> > Now
> > > > > > next step is to replace the File objects in the tree with something
> > > > > > else that at least gets shown on the new level , and the link logic
> > > > > > works ...
> > > > > >
> > > > > > My question here is . The compare method returns and int . I looked
> > at
> > > > > > the javaDocs and it looks like the method used on the file could
> > > > > > return a 0 , negativev or positive int . . .
> > > > > >
> > > > > > What and how would I return stuff here ? ? ??
> > > > > >
> > > > > > I am very confused , a simple tree with no links it's easy to get ,
> > > > > > but when you need something that actually has functionality ...
> > > > > >
> > > > > > Thanks .
> > > > > > cosmin
> > > > > >
> > > > > >
> > > > > >
> > -------------------------------------------------------
> > > > > > This SF.net email is sponsored by: Splunk Inc. Do you grep through
> > log
> > > > > files
> > > > > > for problems?  Stop!  Download the new AJAX search engine that makes
> > > > > > searching your log files as easy as surfing the  web.  DOWNLOAD
> > SPLUNK!
> > > > > > http://ads.osdn.com/?ad_idv37&alloc_id865&opclick
> > > > > > _______________________________________________
> > > > > > Tacos-devel mailing list
> > > > > > [email protected]
> > > > > >
> > https://lists.sourceforge.net/lists/listinfo/tacos-devel
> > > > > >
> > > > >
> > > > >
> > > >
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> > files
> > > for problems?  Stop!  Download the new AJAX search engine that makes
> > > searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> > > http://ads.osdn.com/?ad_idv37&alloc_id865&opclick
> > > _______________________________________________
> > > Tacos-devel mailing list
> > > [email protected]
> > > https://lists.sourceforge.net/lists/listinfo/tacos-devel
> > >
> >
> >
>


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&opclick
_______________________________________________
Tacos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tacos-devel

Reply via email to