8
8
import java .awt .GridBagLayout ;
9
9
import java .awt .event .ActionEvent ;
10
10
import java .awt .event .KeyEvent ;
11
+ import java .awt .event .MouseEvent ;
12
+ import java .awt .event .MouseListener ;
11
13
import java .util .ArrayList ;
12
14
import java .util .Arrays ;
15
+ import java .util .concurrent .ConcurrentHashMap ;
13
16
14
17
import javax .swing .AbstractAction ;
15
18
import javax .swing .Box ;
16
19
import javax .swing .JPanel ;
17
20
import javax .swing .JSeparator ;
18
21
import javax .swing .JTree ;
19
22
import javax .swing .SwingUtilities ;
23
+ import javax .swing .event .TreeSelectionEvent ;
24
+ import javax .swing .event .TreeSelectionListener ;
20
25
import javax .swing .tree .DefaultTreeCellRenderer ;
21
26
import javax .swing .tree .DefaultTreeModel ;
27
+ import javax .swing .tree .TreeSelectionModel ;
22
28
23
29
import org .openstreetmap .josm .gui .SideButton ;
24
30
import org .openstreetmap .josm .gui .dialogs .ToggleDialog ;
25
31
import org .openstreetmap .josm .plugins .mapillary .history .MapillaryRecord ;
26
32
import org .openstreetmap .josm .plugins .mapillary .history .MapillaryRecordListener ;
33
+ import org .openstreetmap .josm .plugins .mapillary .history .commands .CommandDelete ;
27
34
import org .openstreetmap .josm .plugins .mapillary .history .commands .MapillaryCommand ;
35
+ import org .openstreetmap .josm .plugins .mapillary .utils .MapillaryUtils ;
28
36
import org .openstreetmap .josm .tools .GBC ;
29
37
import org .openstreetmap .josm .tools .ImageProvider ;
30
38
import org .openstreetmap .josm .tools .Shortcut ;
@@ -47,6 +55,9 @@ public class MapillaryHistoryDialog extends ToggleDialog implements
47
55
48
56
private static MapillaryHistoryDialog INSTANCE ;
49
57
58
+ private transient UndoRedoSelectionListener undoSelectionListener ;
59
+ private transient UndoRedoSelectionListener redoSelectionListener ;
60
+
50
61
private final DefaultTreeModel undoTreeModel = new DefaultTreeModel (
51
62
new DefaultMutableTreeNode ());
52
63
private final DefaultTreeModel redoTreeModel = new DefaultTreeModel (
@@ -60,6 +71,8 @@ public class MapillaryHistoryDialog extends ToggleDialog implements
60
71
private SideButton undoButton ;
61
72
private SideButton redoButton ;
62
73
74
+ private ConcurrentHashMap <Object , MapillaryCommand > map ;
75
+
63
76
private MapillaryHistoryDialog () {
64
77
super (tr ("Mapillary history" ), "mapillaryhistory.png" ,
65
78
tr ("Open Mapillary history dialog" ), Shortcut .registerShortcut (
@@ -68,14 +81,29 @@ private MapillaryHistoryDialog() {
68
81
69
82
MapillaryRecord .getInstance ().addListener (this );
70
83
84
+ this .map = new ConcurrentHashMap <>();
85
+
71
86
this .undoTree .expandRow (0 );
72
87
this .undoTree .setShowsRootHandles (true );
73
88
this .undoTree .setRootVisible (false );
74
89
this .undoTree .setCellRenderer (new MapillaryCellRenderer ());
90
+ this .undoTree .getSelectionModel ().setSelectionMode (
91
+ TreeSelectionModel .SINGLE_TREE_SELECTION );
92
+ this .undoTree .addMouseListener (new MouseEventHandler ());
93
+ this .undoSelectionListener = new UndoRedoSelectionListener (this .undoTree );
94
+ this .undoTree .getSelectionModel ().addTreeSelectionListener (
95
+ this .undoSelectionListener );
96
+
75
97
this .redoTree .expandRow (0 );
76
98
this .redoTree .setCellRenderer (new MapillaryCellRenderer ());
77
99
this .redoTree .setShowsRootHandles (true );
78
100
this .redoTree .setRootVisible (false );
101
+ this .redoTree .getSelectionModel ().setSelectionMode (
102
+ TreeSelectionModel .SINGLE_TREE_SELECTION );
103
+ this .redoTree .addMouseListener (new MouseEventHandler ());
104
+ this .redoSelectionListener = new UndoRedoSelectionListener (this .redoTree );
105
+ this .redoTree .getSelectionModel ().addTreeSelectionListener (
106
+ this .redoSelectionListener );
79
107
80
108
JPanel treesPanel = new JPanel (new GridBagLayout ());
81
109
treesPanel .add (this .spacer , GBC .eol ());
@@ -129,13 +157,22 @@ private void buildTree() {
129
157
DefaultMutableTreeNode redoRoot = new DefaultMutableTreeNode ();
130
158
DefaultMutableTreeNode undoRoot = new DefaultMutableTreeNode ();
131
159
160
+ this .map .clear ();
132
161
for (MapillaryCommand command : undoCommands ) {
133
- if (command != null )
134
- undoRoot .add (new DefaultMutableTreeNode (command .toString ()));
162
+ if (command != null ) {
163
+ DefaultMutableTreeNode node = new DefaultMutableTreeNode (
164
+ command .toString ());
165
+ this .map .put (node , command );
166
+ undoRoot .add (node );
167
+ }
135
168
}
136
169
for (MapillaryCommand command : redoCommands ) {
137
- if (command != null )
138
- redoRoot .add (new DefaultMutableTreeNode (command .toString ()));
170
+ if (command != null ) {
171
+ DefaultMutableTreeNode node = new DefaultMutableTreeNode (
172
+ command .toString ());
173
+ this .map .put (node , command );
174
+ redoRoot .add (node );
175
+ }
139
176
}
140
177
141
178
this .separator .setVisible (!undoCommands .isEmpty ()
@@ -212,4 +249,70 @@ public Component getTreeCellRendererComponent(JTree tree, Object value,
212
249
public static void destroyInstance () {
213
250
MapillaryHistoryDialog .INSTANCE = null ;
214
251
}
252
+
253
+ private class MouseEventHandler implements MouseListener {
254
+
255
+ @ Override
256
+ public void mouseClicked (MouseEvent e ) {
257
+ }
258
+
259
+ @ Override
260
+ public void mouseEntered (MouseEvent e ) {
261
+ }
262
+
263
+ @ Override
264
+ public void mouseExited (MouseEvent e ) {
265
+ }
266
+
267
+ @ Override
268
+ public void mousePressed (MouseEvent e ) {
269
+ if (e .getClickCount () == 2 ) {
270
+ if (MapillaryHistoryDialog .this .undoTree .getSelectionPath () != null ) {
271
+ MapillaryCommand cmd = MapillaryHistoryDialog .this .map
272
+ .get (MapillaryHistoryDialog .this .undoTree .getSelectionPath ()
273
+ .getLastPathComponent ());
274
+ if (!(cmd instanceof CommandDelete ))
275
+ MapillaryUtils .showPictures (cmd .images , true );
276
+ } else
277
+ MapillaryUtils .showPictures (MapillaryHistoryDialog .this .map
278
+ .get (MapillaryHistoryDialog .this .redoTree .getSelectionPath ()
279
+ .getLastPathComponent ()).images , true );
280
+ }
281
+ }
282
+
283
+ @ Override
284
+ public void mouseReleased (MouseEvent e ) {
285
+ }
286
+ }
287
+
288
+ private class UndoRedoSelectionListener implements TreeSelectionListener {
289
+
290
+ private JTree source ;
291
+
292
+ private UndoRedoSelectionListener (JTree source ) {
293
+ this .source = source ;
294
+ }
295
+
296
+ @ Override
297
+ public void valueChanged (TreeSelectionEvent e ) {
298
+ if (this .source == MapillaryHistoryDialog .this .undoTree ) {
299
+ MapillaryHistoryDialog .this .redoTree .getSelectionModel ()
300
+ .removeTreeSelectionListener (
301
+ MapillaryHistoryDialog .this .redoSelectionListener );
302
+ MapillaryHistoryDialog .this .redoTree .clearSelection ();
303
+ MapillaryHistoryDialog .this .redoTree .getSelectionModel ()
304
+ .addTreeSelectionListener (
305
+ MapillaryHistoryDialog .this .redoSelectionListener );
306
+ }
307
+ if (this .source == MapillaryHistoryDialog .this .redoTree ) {
308
+ MapillaryHistoryDialog .this .undoTree .getSelectionModel ()
309
+ .removeTreeSelectionListener (
310
+ MapillaryHistoryDialog .this .undoSelectionListener );
311
+ MapillaryHistoryDialog .this .undoTree .clearSelection ();
312
+ MapillaryHistoryDialog .this .undoTree .getSelectionModel ()
313
+ .addTreeSelectionListener (
314
+ MapillaryHistoryDialog .this .undoSelectionListener );
315
+ }
316
+ }
317
+ }
215
318
}
0 commit comments