1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.GridLayout;
|
---|
10 | import java.awt.Point;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseAdapter;
|
---|
15 | import java.awt.event.MouseEvent;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.LinkedList;
|
---|
19 | import java.util.Map;
|
---|
20 |
|
---|
21 | import javax.swing.DefaultListModel;
|
---|
22 | import javax.swing.JButton;
|
---|
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.SimplePaintVisitor;
|
---|
41 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
42 | import org.openstreetmap.josm.gui.ConflictResolver;
|
---|
43 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
44 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
45 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
46 |
|
---|
47 | public final class ConflictDialog extends ToggleDialog {
|
---|
48 |
|
---|
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);
|
---|
52 |
|
---|
53 | public ConflictDialog() {
|
---|
54 | super(tr("Conflict"), "conflict", tr("Merging conflicts."), KeyEvent.VK_C, 100);
|
---|
55 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
56 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
57 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
58 | @Override public void mouseClicked(MouseEvent e) {
|
---|
59 | if (e.getClickCount() >= 2)
|
---|
60 | resolve();
|
---|
61 | }
|
---|
62 | });
|
---|
63 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
64 |
|
---|
65 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
66 | JButton button = new JButton(tr("Resolve"), ImageProvider.get("dialogs", "conflict"));
|
---|
67 | button.setToolTipText(tr("Open a merge dialog of all selected items in the list above."));
|
---|
68 | button.addActionListener(new ActionListener(){
|
---|
69 | public void actionPerformed(ActionEvent e) {
|
---|
70 | resolve();
|
---|
71 | }
|
---|
72 | });
|
---|
73 | button.putClientProperty("help", "Dialog/Conflict/Resolve");
|
---|
74 | buttonPanel.add(button);
|
---|
75 |
|
---|
76 | button = new JButton(tr("Select"), ImageProvider.get("mapmode/selection/select"));
|
---|
77 | button.setToolTipText(tr("Set the selected elements on the map to the selected items in the list above."));
|
---|
78 | button.addActionListener(new ActionListener(){
|
---|
79 | public void actionPerformed(ActionEvent e) {
|
---|
80 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
81 | for (Object o : displaylist.getSelectedValues())
|
---|
82 | sel.add((OsmPrimitive)o);
|
---|
83 | Main.ds.setSelected(sel);
|
---|
84 | }
|
---|
85 | });
|
---|
86 | button.putClientProperty("help", "Dialog/Conflict/Select");
|
---|
87 | buttonPanel.add(button);
|
---|
88 |
|
---|
89 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
90 |
|
---|
91 | DataSet.selListeners.add(new SelectionChangedListener(){
|
---|
92 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
93 | displaylist.clearSelection();
|
---|
94 | for (OsmPrimitive osm : newSelection) {
|
---|
95 | if (conflicts.containsKey(osm)) {
|
---|
96 | int pos = model.indexOf(osm);
|
---|
97 | displaylist.addSelectionInterval(pos, pos);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | });
|
---|
102 | displaylist.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
|
---|
103 | public void valueChanged(ListSelectionEvent e) {
|
---|
104 | Main.map.mapView.repaint();
|
---|
105 | }
|
---|
106 | });
|
---|
107 | }
|
---|
108 |
|
---|
109 | private final void resolve() {
|
---|
110 | if (displaylist.getSelectedIndex() == -1) {
|
---|
111 | JOptionPane.showMessageDialog(Main.parent,tr("Please select something from the conflict list."));
|
---|
112 | return;
|
---|
113 | }
|
---|
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 = JOptionPane.showConfirmDialog(Main.parent, resolver, tr("Resolve Conflicts"), JOptionPane.OK_CANCEL_OPTION);
|
---|
121 | if (answer != JOptionPane.OK_OPTION)
|
---|
122 | return;
|
---|
123 | Main.main.undoRedo.add(new ConflictResolveCommand(resolver.conflicts, sel));
|
---|
124 | Main.map.mapView.repaint();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public final void rebuildList() {
|
---|
128 | model.removeAllElements();
|
---|
129 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
130 | if (osm instanceof Node)
|
---|
131 | model.addElement(osm);
|
---|
132 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
133 | if (osm instanceof Way)
|
---|
134 | model.addElement(osm);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public final void add(Map<OsmPrimitive, OsmPrimitive> conflicts) {
|
---|
138 | this.conflicts.putAll(conflicts);
|
---|
139 | rebuildList();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Paint all conflicts that can be expressed on the main window.
|
---|
144 | */
|
---|
145 | public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
|
---|
146 | Color preferencesColor = SimplePaintVisitor.getPreferencesColor("conflict", Color.gray);
|
---|
147 | if (preferencesColor.equals(Color.BLACK))
|
---|
148 | return;
|
---|
149 | g.setColor(preferencesColor);
|
---|
150 | Visitor conflictPainter = new Visitor(){
|
---|
151 | public void visit(Node n) {
|
---|
152 | Point p = nc.getPoint(n.eastNorth);
|
---|
153 | g.drawRect(p.x-1, p.y-1, 2, 2);
|
---|
154 | }
|
---|
155 | public void visit(Node n1, Node n2) {
|
---|
156 | Point p1 = nc.getPoint(n1.eastNorth);
|
---|
157 | Point p2 = nc.getPoint(n2.eastNorth);
|
---|
158 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
159 | }
|
---|
160 | public void visit(Way w) {
|
---|
161 | Node lastN = null;
|
---|
162 | for (Node n : w.nodes) {
|
---|
163 | if (lastN == null) {
|
---|
164 | lastN = n;
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | visit(lastN, n);
|
---|
168 | lastN = n;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | public void visit(Relation e) {
|
---|
172 | for (RelationMember em : e.members)
|
---|
173 | em.member.visit(this);
|
---|
174 | }
|
---|
175 | };
|
---|
176 | for (Object o : displaylist.getSelectedValues())
|
---|
177 | conflicts.get(o).visit(conflictPainter);
|
---|
178 | }
|
---|
179 | }
|
---|