[608] | 1 | // License: GPL. See LICENSE file for details.
|
---|
[626] | 2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[758] | 5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[626] | 6 |
|
---|
| 7 | import java.awt.BorderLayout;
|
---|
| 8 | import java.awt.Color;
|
---|
| 9 | import java.awt.Graphics;
|
---|
| 10 | import java.awt.GridLayout;
|
---|
| 11 | import java.awt.Point;
|
---|
| 12 | import java.awt.event.ActionEvent;
|
---|
| 13 | import java.awt.event.ActionListener;
|
---|
| 14 | import java.awt.event.KeyEvent;
|
---|
| 15 | import java.awt.event.MouseAdapter;
|
---|
| 16 | import java.awt.event.MouseEvent;
|
---|
| 17 | import java.util.Collection;
|
---|
| 18 | import java.util.HashMap;
|
---|
| 19 | import java.util.LinkedList;
|
---|
| 20 | import java.util.Map;
|
---|
| 21 |
|
---|
| 22 | import javax.swing.DefaultListModel;
|
---|
| 23 | import javax.swing.JList;
|
---|
| 24 | import javax.swing.JOptionPane;
|
---|
| 25 | import javax.swing.JPanel;
|
---|
| 26 | import javax.swing.JScrollPane;
|
---|
| 27 | import javax.swing.ListSelectionModel;
|
---|
| 28 | import javax.swing.event.ListSelectionEvent;
|
---|
| 29 | import javax.swing.event.ListSelectionListener;
|
---|
| 30 |
|
---|
| 31 | import org.openstreetmap.josm.Main;
|
---|
| 32 | import org.openstreetmap.josm.command.ConflictResolveCommand;
|
---|
| 33 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
| 34 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 35 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 36 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
| 37 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 38 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 39 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 40 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
| 41 | import org.openstreetmap.josm.gui.ConflictResolver;
|
---|
| 42 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
| 43 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
[744] | 44 | import org.openstreetmap.josm.gui.SideButton;
|
---|
[1084] | 45 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 46 |
|
---|
| 47 | public final class ConflictDialog extends ToggleDialog {
|
---|
| 48 |
|
---|
[1169] | 49 | public final Map<OsmPrimitive, OsmPrimitive> conflicts = new HashMap<OsmPrimitive, OsmPrimitive>();
|
---|
| 50 | private final DefaultListModel model = new DefaultListModel();
|
---|
| 51 | private final JList displaylist = new JList(model);
|
---|
[626] | 52 |
|
---|
[1169] | 53 | public ConflictDialog() {
|
---|
| 54 | super(tr("Conflict"), "conflict", tr("Merging conflicts."),
|
---|
| 55 | Shortcut.registerShortcut("subwindow:conflict", tr("Toggle: {0}", tr("Conflict")), KeyEvent.VK_C, Shortcut.GROUP_LAYER), 100);
|
---|
| 56 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
| 57 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
| 58 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
| 59 | @Override public void mouseClicked(MouseEvent e) {
|
---|
| 60 | if (e.getClickCount() >= 2)
|
---|
| 61 | resolve();
|
---|
| 62 | }
|
---|
| 63 | });
|
---|
| 64 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
[626] | 65 |
|
---|
[1169] | 66 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
| 67 | buttonPanel.add(new SideButton(marktr("Resolve"), "conflict", "Conflict",
|
---|
| 68 | tr("Open a merge dialog of all selected items in the list above."), new ActionListener(){
|
---|
| 69 | public void actionPerformed(ActionEvent e) {
|
---|
| 70 | resolve();
|
---|
| 71 | }
|
---|
| 72 | }));
|
---|
[626] | 73 |
|
---|
[1169] | 74 | buttonPanel.add(new SideButton(marktr("Select"), "select", "Conflict",
|
---|
| 75 | tr("Set the selected elements on the map to the selected items in the list above."), new ActionListener(){
|
---|
| 76 | public void actionPerformed(ActionEvent e) {
|
---|
| 77 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
| 78 | for (Object o : displaylist.getSelectedValues())
|
---|
| 79 | sel.add((OsmPrimitive)o);
|
---|
| 80 | Main.ds.setSelected(sel);
|
---|
| 81 | }
|
---|
| 82 | }));
|
---|
| 83 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
[626] | 84 |
|
---|
[1169] | 85 | DataSet.selListeners.add(new SelectionChangedListener(){
|
---|
| 86 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
| 87 | displaylist.clearSelection();
|
---|
| 88 | for (OsmPrimitive osm : newSelection) {
|
---|
| 89 | if (conflicts.containsKey(osm)) {
|
---|
| 90 | int pos = model.indexOf(osm);
|
---|
| 91 | displaylist.addSelectionInterval(pos, pos);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | });
|
---|
| 96 | displaylist.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
|
---|
| 97 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 98 | Main.map.mapView.repaint();
|
---|
| 99 | }
|
---|
| 100 | });
|
---|
| 101 | }
|
---|
[626] | 102 |
|
---|
[1169] | 103 | private final void resolve() {
|
---|
| 104 | if (displaylist.getSelectedIndex() == -1) {
|
---|
| 105 | JOptionPane.showMessageDialog(Main.parent,tr("Please select something from the conflict list."));
|
---|
| 106 | return;
|
---|
| 107 | }
|
---|
| 108 | Map<OsmPrimitive, OsmPrimitive> sel = new HashMap<OsmPrimitive, OsmPrimitive>();
|
---|
| 109 | for (int i : displaylist.getSelectedIndices()) {
|
---|
| 110 | OsmPrimitive s = (OsmPrimitive)model.get(i);
|
---|
| 111 | sel.put(s, conflicts.get(s));
|
---|
| 112 | }
|
---|
| 113 | ConflictResolver resolver = new ConflictResolver(sel);
|
---|
| 114 | int answer = JOptionPane.showConfirmDialog(Main.parent, resolver, tr("Resolve Conflicts"), JOptionPane.OK_CANCEL_OPTION);
|
---|
| 115 | if (answer != JOptionPane.OK_OPTION)
|
---|
| 116 | return;
|
---|
| 117 | Main.main.undoRedo.add(new ConflictResolveCommand(resolver.conflicts, sel));
|
---|
| 118 | Main.map.mapView.repaint();
|
---|
| 119 | }
|
---|
[626] | 120 |
|
---|
[1169] | 121 | public final void rebuildList() {
|
---|
| 122 | model.removeAllElements();
|
---|
[1117] | 123 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
[1169] | 124 | if (osm instanceof Node)
|
---|
| 125 | model.addElement(osm);
|
---|
| 126 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
| 127 | if (osm instanceof Way)
|
---|
| 128 | model.addElement(osm);
|
---|
| 129 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
[1117] | 130 | if (osm instanceof Relation)
|
---|
| 131 | model.addElement(osm);
|
---|
[1228] | 132 |
|
---|
| 133 | if(model.size() != 0) {
|
---|
| 134 | setTitle(tr("Conflicts: {0}", model.size()), true);
|
---|
| 135 | } else {
|
---|
| 136 | setTitle(tr("Conflicts"), false);
|
---|
| 137 | }
|
---|
[1117] | 138 | }
|
---|
[626] | 139 |
|
---|
[1169] | 140 | public final void add(Map<OsmPrimitive, OsmPrimitive> conflicts) {
|
---|
| 141 | this.conflicts.putAll(conflicts);
|
---|
| 142 | rebuildList();
|
---|
| 143 | }
|
---|
[626] | 144 |
|
---|
[1221] | 145 | static public Color getColor()
|
---|
| 146 | {
|
---|
| 147 | return Main.pref.getColor(marktr("conflict"), Color.gray);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[1169] | 150 | /**
|
---|
| 151 | * Paint all conflicts that can be expressed on the main window.
|
---|
| 152 | */
|
---|
| 153 | public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
|
---|
[1221] | 154 | Color preferencesColor = getColor();
|
---|
| 155 | if (preferencesColor.equals(Main.pref.getColor(marktr("background"), Color.black)))
|
---|
[1169] | 156 | return;
|
---|
| 157 | g.setColor(preferencesColor);
|
---|
| 158 | Visitor conflictPainter = new Visitor(){
|
---|
| 159 | public void visit(Node n) {
|
---|
| 160 | Point p = nc.getPoint(n.eastNorth);
|
---|
| 161 | g.drawRect(p.x-1, p.y-1, 2, 2);
|
---|
| 162 | }
|
---|
| 163 | public void visit(Node n1, Node n2) {
|
---|
| 164 | Point p1 = nc.getPoint(n1.eastNorth);
|
---|
| 165 | Point p2 = nc.getPoint(n2.eastNorth);
|
---|
| 166 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
| 167 | }
|
---|
| 168 | public void visit(Way w) {
|
---|
| 169 | Node lastN = null;
|
---|
| 170 | for (Node n : w.nodes) {
|
---|
| 171 | if (lastN == null) {
|
---|
| 172 | lastN = n;
|
---|
| 173 | continue;
|
---|
| 174 | }
|
---|
| 175 | visit(lastN, n);
|
---|
| 176 | lastN = n;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | public void visit(Relation e) {
|
---|
| 180 | for (RelationMember em : e.members)
|
---|
| 181 | em.member.visit(this);
|
---|
| 182 | }
|
---|
| 183 | };
|
---|
| 184 | for (Object o : displaylist.getSelectedValues())
|
---|
| 185 | conflicts.get(o).visit(conflictPainter);
|
---|
| 186 | }
|
---|
[626] | 187 | }
|
---|