Changeset 9601 in josm


Ignore:
Timestamp:
2016-01-24T01:07:35+01:00 (8 years ago)
Author:
Don-vip
Message:

ConflictDialog: fix Sonar/Coverity issues + add basic unit test

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ConflictDialog.java

    r9452 r9601  
    7171 * This dialog displays the {@link ConflictCollection} of the active {@link OsmDataLayer} in a toggle
    7272 * dialog on the right of the main frame.
    73  *
     73 * @since 86
    7474 */
    7575public final class ConflictDialog extends ToggleDialog implements MapView.EditLayerChangeListener, IConflictListener, SelectionChangedListener {
     76
     77    /** the collection of conflicts displayed by this conflict dialog */
     78    private transient ConflictCollection conflicts;
     79
     80    /** the model for the list of conflicts */
     81    private transient ConflictListModel model;
     82    /** the list widget for the list of conflicts */
     83    private JList<OsmPrimitive> lstConflicts;
     84
     85    private final JPopupMenu popupMenu = new JPopupMenu();
     86    private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
     87
     88    private final ResolveAction actResolve = new ResolveAction();
     89    private final SelectAction actSelect = new SelectAction();
     90
     91    /**
     92     * Constructs a new {@code ConflictDialog}.
     93     */
     94    public ConflictDialog() {
     95        super(tr("Conflict"), "conflict", tr("Resolve conflicts."),
     96                Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")),
     97                KeyEvent.VK_C, Shortcut.ALT_SHIFT), 100);
     98
     99        build();
     100        refreshView();
     101    }
    76102
    77103    /**
     
    85111        return Main.pref.getColor(marktr("conflict"), Color.gray);
    86112    }
    87 
    88     /** the collection of conflicts displayed by this conflict dialog */
    89     private transient ConflictCollection conflicts;
    90 
    91     /** the model for the list of conflicts */
    92     private transient ConflictListModel model;
    93     /** the list widget for the list of conflicts */
    94     private JList<OsmPrimitive> lstConflicts;
    95 
    96     private final JPopupMenu popupMenu = new JPopupMenu();
    97     private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
    98 
    99     private ResolveAction actResolve;
    100     private SelectAction actSelect;
    101113
    102114    /**
     
    117129        });
    118130
    119         SideButton btnResolve = new SideButton(actResolve = new ResolveAction());
     131        SideButton btnResolve = new SideButton(actResolve);
    120132        addListSelectionListener(actResolve);
    121133
    122         SideButton btnSelect = new SideButton(actSelect = new SelectAction());
     134        SideButton btnSelect = new SideButton(actSelect);
    123135        addListSelectionListener(actSelect);
    124136
     
    155167    }
    156168
    157     /**
    158      * constructor
    159      */
    160     public ConflictDialog() {
    161         super(tr("Conflict"), "conflict", tr("Resolve conflicts."),
    162                 Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")),
    163                 KeyEvent.VK_C, Shortcut.ALT_SHIFT), 100);
    164 
    165         build();
    166         refreshView();
    167     }
    168 
    169169    @Override
    170170    public void showNotify() {
     
    209209    /**
    210210     * Launches a conflict resolution dialog for the first selected conflict
    211      *
    212211     */
    213212    private void resolve() {
    214         if (conflicts == null || model.getSize() == 0) return;
     213        if (conflicts == null || model.getSize() == 0)
     214            return;
    215215
    216216        int index = lstConflicts.getSelectedIndex();
     
    234234    public void refreshView() {
    235235        OsmDataLayer editLayer =  Main.main.getEditLayer();
    236         conflicts = (editLayer == null ? new ConflictCollection() : editLayer.getConflicts());
     236        conflicts = editLayer == null ? new ConflictCollection() : editLayer.getConflicts();
    237237        GuiHelper.runInEDT(new Runnable() {
    238238            @Override
     
    269269            return;
    270270        g.setColor(preferencesColor);
    271         Visitor conflictPainter = new AbstractVisitor() {
    272             // Manage a stack of visited relations to avoid infinite recursion with cyclic relations (fix #7938)
    273             private final Set<Relation> visited = new HashSet<>();
    274             @Override
    275             public void visit(Node n) {
    276                 Point p = nc.getPoint(n);
    277                 g.drawRect(p.x-1, p.y-1, 2, 2);
    278             }
    279 
    280             public void visit(Node n1, Node n2) {
    281                 Point p1 = nc.getPoint(n1);
    282                 Point p2 = nc.getPoint(n2);
    283                 g.drawLine(p1.x, p1.y, p2.x, p2.y);
    284             }
    285 
    286             @Override
    287             public void visit(Way w) {
    288                 Node lastN = null;
    289                 for (Node n : w.getNodes()) {
    290                     if (lastN == null) {
    291                         lastN = n;
    292                         continue;
    293                     }
    294                     visit(lastN, n);
    295                     lastN = n;
    296                 }
    297             }
    298 
    299             @Override
    300             public void visit(Relation e) {
    301                 if (!visited.contains(e)) {
    302                     visited.add(e);
    303                     try {
    304                         for (RelationMember em : e.getMembers()) {
    305                             em.getMember().accept(this);
    306                         }
    307                     } finally {
    308                         visited.remove(e);
    309                     }
    310                 }
    311             }
    312         };
     271        Visitor conflictPainter = new ConflictPainter(nc, g);
    313272        for (OsmPrimitive o : lstConflicts.getSelectedValuesList()) {
    314273            if (conflicts == null || !conflicts.hasConflictForMy(o)) {
     
    330289    }
    331290
    332 
    333291    /**
    334292     * replies the conflict collection currently held by this dialog; may be null
     
    346304     */
    347305    public Conflict<? extends OsmPrimitive> getSelectedConflict() {
    348         if (conflicts == null || model.getSize() == 0) return null;
     306        if (conflicts == null || model.getSize() == 0)
     307            return null;
    349308
    350309        int index = lstConflicts.getSelectedIndex();
    351         if (index < 0) return null;
    352 
    353         return conflicts.get(index);
     310
     311        return index >= 0 ? conflicts.get(index) : null;
    354312    }
    355313
    356314    private boolean isConflictSelected() {
    357         final ListSelectionModel model = lstConflicts.getSelectionModel();
    358         return model.getMinSelectionIndex() >= 0 && model.getMaxSelectionIndex() >= model.getMinSelectionIndex();
     315        final ListSelectionModel selModel = lstConflicts.getSelectionModel();
     316        return selModel.getMinSelectionIndex() >= 0 && selModel.getMaxSelectionIndex() >= selModel.getMinSelectionIndex();
    359317    }
    360318
     
    444402        @Override
    445403        public OsmPrimitive getElementAt(int index) {
    446             if (index < 0) return null;
    447             if (index >= getSize()) return null;
     404            if (index < 0 || index >= getSize())
     405                return null;
    448406            return conflicts.get(index).getMy();
    449407        }
     
    451409        @Override
    452410        public int getSize() {
    453             if (conflicts == null) return 0;
    454             return conflicts.size();
     411            return conflicts != null ? conflicts.size() : 0;
    455412        }
    456413
    457414        public int indexOf(OsmPrimitive my) {
    458             if (conflicts == null) return -1;
    459             for (int i = 0; i < conflicts.size(); i++) {
    460                 if (conflicts.get(i).isMatchingMy(my))
    461                     return i;
     415            if (conflicts != null) {
     416                for (int i = 0; i < conflicts.size(); i++) {
     417                    if (conflicts.get(i).isMatchingMy(my))
     418                        return i;
     419                }
    462420            }
    463421            return -1;
     
    465423
    466424        public OsmPrimitive get(int idx) {
    467             if (conflicts == null) return null;
    468             return conflicts.get(idx).getMy();
     425            return conflicts != null ? conflicts.get(idx).getMy() : null;
    469426        }
    470427    }
     
    529486            for (OsmPrimitive osmPrimitive : lstConflicts.getSelectedValuesList()) {
    530487                Conflict<? extends OsmPrimitive> c = conflicts.getConflictForMy(osmPrimitive);
    531                 resolver.populate(c);
    532                 resolver.decideRemaining(type);
    533                 commands.add(resolver.buildResolveCommand());
     488                if (c != null) {
     489                    resolver.populate(c);
     490                    resolver.decideRemaining(type);
     491                    commands.add(resolver.buildResolveCommand());
     492                }
    534493            }
    535494            Main.main.undoRedo.add(new SequenceCommand(name, commands));
     
    553512    }
    554513
     514    class ConflictPainter extends AbstractVisitor {
     515        // Manage a stack of visited relations to avoid infinite recursion with cyclic relations (fix #7938)
     516        private final Set<Relation> visited = new HashSet<>();
     517        private final NavigatableComponent nc;
     518        private final Graphics g;
     519
     520        ConflictPainter(NavigatableComponent nc, Graphics g) {
     521            this.nc = nc;
     522            this.g = g;
     523        }
     524
     525        @Override
     526        public void visit(Node n) {
     527            Point p = nc.getPoint(n);
     528            g.drawRect(p.x-1, p.y-1, 2, 2);
     529        }
     530
     531        public void visit(Node n1, Node n2) {
     532            Point p1 = nc.getPoint(n1);
     533            Point p2 = nc.getPoint(n2);
     534            g.drawLine(p1.x, p1.y, p2.x, p2.y);
     535        }
     536
     537        @Override
     538        public void visit(Way w) {
     539            Node lastN = null;
     540            for (Node n : w.getNodes()) {
     541                if (lastN == null) {
     542                    lastN = n;
     543                    continue;
     544                }
     545                visit(lastN, n);
     546                lastN = n;
     547            }
     548        }
     549
     550        @Override
     551        public void visit(Relation e) {
     552            if (!visited.contains(e)) {
     553                visited.add(e);
     554                try {
     555                    for (RelationMember em : e.getMembers()) {
     556                        em.getMember().accept(this);
     557                    }
     558                } finally {
     559                    visited.remove(e);
     560                }
     561            }
     562        }
     563    }
     564
    555565    /**
    556566     * Warns the user about the number of detected conflicts
     
    560570     */
    561571    public void warnNumNewConflicts(int numNewConflicts) {
    562         if (numNewConflicts == 0) return;
     572        if (numNewConflicts == 0)
     573            return;
    563574
    564575        String msg1 = trn(
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/LatLonDialogTest.java

    r8857 r9601  
    1616     */
    1717    @Test
    18     public void testparseLatLon() {
     18    public void testParseLatLon() {
    1919        assertEquals(new LatLon(49.29918, 19.24788), LatLonDialog.parseLatLon("49.29918° 19.24788°"));
    2020        assertEquals(new LatLon(49.29918, 19.24788), LatLonDialog.parseLatLon("N 49.29918 E 19.24788°"));
Note: See TracChangeset for help on using the changeset viewer.