Changeset 229 in josm for src


Ignore:
Timestamp:
2007-05-08T00:01:08+02:00 (17 years ago)
Author:
framm
Message:

Refactor ReorderWay into a static method that may be called from plugins. Patch by Francisco R. Santos <frsantos@…>.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/ReorderAction.java

    r222 r229  
    5555                        {                       
    5656                                doneSomething = true;
    57                                 final LinkedList<Segment> sel = new LinkedList<Segment>(sortSegments(new LinkedList<Segment>(way.segments)));           
    58        
    59                         Collection<Command> c = new LinkedList<Command>();
    60        
    61                         boolean direction = false;
    62                         // work out the "average" direction of the way, we use this to direct the rest of the segments
    63                         int dirCounter = 0;
    64                         for(int i = 0; i < sel.size() - 1; i++)
    65                         {
    66                                 Segment firstSegment = sel.get(i);
    67                                 Segment secondSegment = sel.get(i+1);
    68                                 if ( firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to ) // direction = true when 'from' is the first node in the Way
    69                                         dirCounter++;
    70                                 else
    71                                         dirCounter--;
    72                         }
    73                         if ( dirCounter <= 0 )
    74                                 direction = false;
    75                         else
    76                                 direction = true;
    77                        
    78                         Node lastNode = null;
     57                                Command c = reorderWay(way);
    7958
    80                         // we need to calculate what the first node in the way is, we work from there
    81                         Segment firstSegment = sel.getFirst();
    82                         Segment secondSegment = sel.get(1);
    83                         if (firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to)
    84                                 lastNode = firstSegment.from;
    85                         else
    86                                 lastNode = firstSegment.to;
    87                        
    88                         // go through each segment and flip them if required
    89                         for (Segment s : sel) {
    90                                 Segment snew = new Segment(s);
    91                                 boolean segDirection = s.from == lastNode;
    92                                 // segDirection = true when the 'from' node occurs before the 'to' node in the Way
    93                                 if (direction != segDirection)
    94                                 {                       
    95                                         // reverse the segment's direction
    96                                         Node n = snew.from;
    97                                         snew.from = snew.to;
    98                                         snew.to = n;
    99                                         c.add(new ChangeCommand(s, snew));
    100                                 }       
    101                                
    102                                 if (direction) // if its facing forwards,
    103                                         lastNode = snew.to; // our next node is the 'to' one
    104                                 else
    105                                         lastNode = snew.from; // otherwise its the 'from' one
    106                         }
    107 
    108                         LinkedList<Segment> segments = new LinkedList<Segment>();
    109                        
    110                         // Now we recreate the segment list, in the correct order of the direction
    111                         for (Segment s : sel)
    112                                 if (!direction)
    113                                         segments.addFirst(s);
    114                                 else
    115                                         segments.addLast(s);
    116                                
    117                         // Check if the new segment list is actually different from the old one
    118                         // before we go and add a change command for it
    119                         for(int i = 0; i < segments.size(); i++)
    120                                 if (way.segments.get(i) != segments.get(i))
    121                                 {
    122                                         Way newWay = new Way(way);
    123                                         newWay.segments.clear();
    124                                                 newWay.segments.addAll(segments);
    125                                                 c.add(new ChangeCommand(way, newWay));
    126                                                 break;
    127                                 }
    128        
    129                         // Check we've got some change commands before we add a sequence command
    130                         if (c.size() != 0) {
    131                                 NameVisitor v = new NameVisitor();
    132                                 way.visit(v);
    133                                 Main.main.editLayer().add(new SequenceCommand(tr("Reorder segments for way {0}",v.name), c));
    134                         }
     59                                if( c != null )
     60                                        Main.main.editLayer().add( c );
    13561                        }
    13662                }
     
    14369                Main.map.repaint();
    14470        }
     71
     72        /**
     73         * This method first sorts all the segments in a way, then makes sure that all
     74         * the segments are facing the same direction as the first one.
     75         * @param way The way to reorder
     76     * @return The command needed to reorder the way
     77     */
     78    public static Command reorderWay(Way way) {
     79            final LinkedList<Segment> sel = new LinkedList<Segment>(sortSegments(new LinkedList<Segment>(way.segments)));       
     80
     81            Collection<Command> c = new LinkedList<Command>();
     82
     83            boolean direction = false;
     84            // work out the "average" direction of the way, we use this to direct the rest of the segments
     85            int dirCounter = 0;
     86            for(int i = 0; i < sel.size() - 1; i++)
     87            {
     88                Segment firstSegment = sel.get(i);
     89                Segment secondSegment = sel.get(i+1);
     90                if ( firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to ) // direction = true when 'from' is the first node in the Way
     91                        dirCounter++;
     92                else
     93                        dirCounter--;
     94            }
     95            if ( dirCounter <= 0 )
     96                direction = false;
     97            else
     98                direction = true;
     99           
     100            Node lastNode = null;
     101
     102            // we need to calculate what the first node in the way is, we work from there
     103            Segment firstSegment = sel.getFirst();
     104            Segment secondSegment = sel.get(1);
     105            if (firstSegment.to == secondSegment.from || firstSegment.to == secondSegment.to)
     106                lastNode = firstSegment.from;
     107            else
     108                lastNode = firstSegment.to;
     109           
     110            // go through each segment and flip them if required
     111            for (Segment s : sel) {
     112                Segment snew = new Segment(s);
     113                boolean segDirection = s.from == lastNode;
     114                // segDirection = true when the 'from' node occurs before the 'to' node in the Way
     115                if (direction != segDirection)
     116                {                       
     117                        // reverse the segment's direction
     118                        Node n = snew.from;
     119                        snew.from = snew.to;
     120                        snew.to = n;
     121                        c.add(new ChangeCommand(s, snew));
     122                }       
     123               
     124                if (direction) // if its facing forwards,
     125                        lastNode = snew.to; // our next node is the 'to' one
     126                else
     127                        lastNode = snew.from; // otherwise its the 'from' one
     128            }
     129
     130            LinkedList<Segment> segments = new LinkedList<Segment>();
     131           
     132            // Now we recreate the segment list, in the correct order of the direction
     133            for (Segment s : sel)
     134                if (!direction)
     135                        segments.addFirst(s);
     136                else
     137                        segments.addLast(s);
     138               
     139            // Check if the new segment list is actually different from the old one
     140            // before we go and add a change command for it
     141            for(int i = 0; i < segments.size(); i++)
     142                if (way.segments.get(i) != segments.get(i))
     143                {
     144                        Way newWay = new Way(way);
     145                        newWay.segments.clear();
     146                        newWay.segments.addAll(segments);
     147                        c.add(new ChangeCommand(way, newWay));
     148                        break;
     149                }
     150           
     151            // Check we've got some change commands before we add a sequence command
     152                if (c.size() != 0) {
     153                        NameVisitor v = new NameVisitor();
     154                        way.visit(v);
     155                        return new SequenceCommand(tr("Reorder segments for way {0}",v.name), c);
     156                }
     157                else
     158                        return null;
     159    }
    145160
    146161        /**
Note: See TracChangeset for help on using the changeset viewer.