001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.sort;
003
004import static org.openstreetmap.josm.gui.dialogs.relation.sort.WayConnectionType.Direction.NONE;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007/**
008 * A class used by the {@link RelationSorter} to store if two ways are already connected
009 */
010public class WayConnectionType {
011
012    /** True, if the corresponding primitive is not a way or the way is incomplete */
013    private final boolean invalid;
014
015    /** True, if linked to the previous member.  */
016    public boolean linkPrev;
017    /** True, if linked to the next member.  */
018    public boolean linkNext;
019
020    /**
021     * direction is FORWARD if the first node of this way is connected to the previous way
022     * and / or the last node of this way is connected to the next way.
023     * direction is BACKWARD if it is the other way around.
024     * direction has a ROUNDABOUT value, if it is tagged as such and it is somehow
025     * connected to the previous / next member.
026     * If there is no connection to the previous or next member, then
027     * direction has the value NONE.
028     */
029    public Direction direction;
030
031    public enum Direction {
032        FORWARD, BACKWARD, ROUNDABOUT_LEFT, ROUNDABOUT_RIGHT, NONE;
033
034        public boolean isRoundabout() {
035            return this == ROUNDABOUT_RIGHT || this == ROUNDABOUT_LEFT;
036        }
037    }
038
039    /** True, if the element is part of a closed loop of ways. */
040    public boolean isLoop;
041
042    public boolean isOnewayLoopForwardPart;
043    public boolean isOnewayLoopBackwardPart;
044    public boolean isOnewayHead;
045    public boolean isOnewayTail;
046
047    public WayConnectionType(boolean linkPrev, boolean linkNext, Direction direction) {
048        this.linkPrev = linkPrev;
049        this.linkNext = linkNext;
050        this.isLoop = false;
051        this.direction = direction;
052        invalid = false;
053    }
054
055    public WayConnectionType(boolean invalid) {
056        this.invalid = invalid;
057    }
058
059    /** construct invalid instance */
060    public WayConnectionType() {
061        this.linkPrev = false;
062        this.linkNext = false;
063        this.isLoop = false;
064        this.direction = NONE;
065        invalid = true;
066    }
067
068    public boolean isValid() {
069        return !invalid;
070    }
071
072    @Override
073    public String toString() {
074        return "[P "+linkPrev+" ;N "+linkNext+" ;D "+direction+" ;L "+isLoop+
075                " ;FP " + isOnewayLoopForwardPart+";BP " + isOnewayLoopBackwardPart+
076                ";OH " + isOnewayHead+";OT " + isOnewayTail+']';
077    }
078
079    /**
080     * Returns tooltip.
081     * @return tooltip
082     * @since 10248
083     */
084    public String getTooltip() {
085        if (!isValid())
086            return "";
087        else if (linkPrev && linkNext)
088            return tr("way is connected");
089        else if (linkPrev)
090            return tr("way is connected to previous relation member");
091        else if (linkNext)
092            return tr("way is connected to next relation member");
093        else
094            return tr("way is not connected to previous or next relation member");
095    }
096}