1 | package org.openstreetmap.josm.gui.dialogs;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import javax.swing.DefaultListModel;
|
---|
19 | import javax.swing.JButton;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.ListSelectionModel;
|
---|
25 | import javax.swing.event.ListSelectionEvent;
|
---|
26 | import javax.swing.event.ListSelectionListener;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.command.ConflictResolveCommand;
|
---|
30 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
31 | import org.openstreetmap.josm.data.osm.Node;
|
---|
32 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
33 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
34 | import org.openstreetmap.josm.data.osm.Way;
|
---|
35 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
36 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
37 | import org.openstreetmap.josm.gui.ConflictResolver;
|
---|
38 | import org.openstreetmap.josm.gui.MapView;
|
---|
39 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
40 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
41 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
42 |
|
---|
43 | public final class ConflictDialog extends ToggleDialog {
|
---|
44 |
|
---|
45 | public final Map<OsmPrimitive, OsmPrimitive> conflicts = new HashMap<OsmPrimitive, OsmPrimitive>();
|
---|
46 | public final DefaultListModel model = new DefaultListModel();
|
---|
47 | private final JList displaylist = new JList(model);
|
---|
48 |
|
---|
49 | public ConflictDialog() {
|
---|
50 | super("Conflict", "Conflict Dialog", "merge", "Merging conflicts.", "C", KeyEvent.VK_C, "conflict");
|
---|
51 | displaylist.setCellRenderer(new OsmPrimitivRenderer());
|
---|
52 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
53 | displaylist.addMouseListener(new MouseAdapter(){
|
---|
54 | @Override public void mouseClicked(MouseEvent e) {
|
---|
55 | if (e.getClickCount() >= 2)
|
---|
56 | resolve();
|
---|
57 | }
|
---|
58 | });
|
---|
59 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
60 |
|
---|
61 | JPanel buttonPanel = new JPanel(new GridLayout(1,2));
|
---|
62 | JButton button = new JButton("Resolve", ImageProvider.get("dialogs", "merge"));
|
---|
63 | button.setToolTipText("Open a merge dialog of all selected items in the list above.");
|
---|
64 | button.addActionListener(new ActionListener(){
|
---|
65 | public void actionPerformed(ActionEvent e) {
|
---|
66 | resolve();
|
---|
67 | }
|
---|
68 | });
|
---|
69 | buttonPanel.add(button);
|
---|
70 |
|
---|
71 | button = new JButton("Select", ImageProvider.get("mapmode", "selection"));
|
---|
72 | button.setToolTipText("Set the selected elements on the map to the selected items in the list above.");
|
---|
73 | button.addActionListener(new ActionListener(){
|
---|
74 | public void actionPerformed(ActionEvent e) {
|
---|
75 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
76 | for (Object o : displaylist.getSelectedValues())
|
---|
77 | sel.add((OsmPrimitive)o);
|
---|
78 | Main.ds.setSelected(sel);
|
---|
79 | }
|
---|
80 | });
|
---|
81 | buttonPanel.add(button);
|
---|
82 |
|
---|
83 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
84 |
|
---|
85 | Main.ds.addSelectionChangedListener(new SelectionChangedListener(){
|
---|
86 | public void selectionChanged(Collection<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.main.getMapFrame().mapView.repaint();
|
---|
99 | }
|
---|
100 | });
|
---|
101 | }
|
---|
102 |
|
---|
103 | private final void resolve() {
|
---|
104 | if (displaylist.getSelectedIndex() == -1) {
|
---|
105 | JOptionPane.showMessageDialog(Main.main, "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.main, resolver, "Resolve Conflicts", JOptionPane.OK_CANCEL_OPTION);
|
---|
115 | if (answer != JOptionPane.OK_OPTION)
|
---|
116 | return;
|
---|
117 | MapView mv = Main.main.getMapFrame().mapView;
|
---|
118 | mv.editLayer().add(new ConflictResolveCommand(resolver.conflicts, sel));
|
---|
119 | mv.repaint();
|
---|
120 | }
|
---|
121 |
|
---|
122 | public final void add(Map<OsmPrimitive, OsmPrimitive> conflicts) {
|
---|
123 | this.conflicts.putAll(conflicts);
|
---|
124 | model.removeAllElements();
|
---|
125 | for (OsmPrimitive osm : this.conflicts.keySet())
|
---|
126 | model.addElement(osm);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Paint all conflicts that can be expressed on the main window.
|
---|
131 | */
|
---|
132 | public void paintConflicts(final Graphics g, final NavigatableComponent nc) {
|
---|
133 | Color preferencesColor = SimplePaintVisitor.getPreferencesColor("conflict", Color.GRAY);
|
---|
134 | if (preferencesColor.equals(Color.BLACK))
|
---|
135 | return;
|
---|
136 | g.setColor(preferencesColor);
|
---|
137 | Visitor conflictPainter = new Visitor(){
|
---|
138 | public void visit(Node n) {
|
---|
139 | Point p = nc.getPoint(n.eastNorth);
|
---|
140 | g.drawRect(p.x-1, p.y-1, 2, 2);
|
---|
141 | }
|
---|
142 | public void visit(Segment ls) {
|
---|
143 | if (ls.incomplete)
|
---|
144 | return;
|
---|
145 | Point p1 = nc.getPoint(ls.from.eastNorth);
|
---|
146 | Point p2 = nc.getPoint(ls.to.eastNorth);
|
---|
147 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
148 | }
|
---|
149 | public void visit(Way w) {
|
---|
150 | for (Segment ls : w.segments)
|
---|
151 | visit(ls);
|
---|
152 | }
|
---|
153 | };
|
---|
154 | for (Object o : displaylist.getSelectedValues())
|
---|
155 | conflicts.get(o).visit(conflictPainter);
|
---|
156 | }
|
---|
157 | }
|
---|