/** * recursive method to find nearest item in this layer (or any layer it contains). * * @param pl the one we want to be near to * @param nearest the place to put the results */ private void findNearest(final Plottable pl, final dataStruct nearest) { if (pl.getVisible()) { // is this a layer? if (pl instanceof Layer) { // do the check for this item // NOTE: we only check the distance of elements, not whole tracks // nearest.compare(pl); // we need to call ourselves for each layer inside it final Layer l = (Layer) pl; final java.util.Enumeration<Editable> enumer = l.elements(); while (enumer.hasMoreElements()) { Editable next = enumer.nextElement(); if (next instanceof Plottable) { final Plottable this_plottable = (Plottable) next; findNearest(this_plottable, nearest); } } } else { // so, we've got to a node - compare it to our current data nearest.compare(pl); } } }
/** * compare method, is this new plottable nearest than the current one. * * @param newPl the item we're looking at */ private final void compare(final Plottable newPl) { // is it even visible? if (!newPl.getVisible()) return; // calculate the range final double range = newPl.rangeFrom(location); // did it produce a valid range? if (range == Plottable.INVALID_RANGE) { return; } // so, we have a valid range. is it our first? if (distance == Plottable.INVALID_RANGE) { object = newPl; distance = range; } else { if (range < distance) { distance = range; object = newPl; } } }
public int compareTo(final Plottable arg0) { final Plottable other = (Plottable) arg0; return this.getName().compareTo(other.getName()); }