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