source: josm/trunk/src/org/openstreetmap/josm/gui/history/HistoryBrowserPanel.java@ 14675

Last change on this file since 14675 was 14463, checked in by Don-vip, 5 years ago

fix #17040 - fix memory leaks when calling history dialog

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import java.awt.GridBagLayout;
5import java.util.ArrayList;
6import java.util.List;
7
8import javax.swing.AbstractAction;
9import javax.swing.JPanel;
10
11import org.openstreetmap.josm.actions.JosmAction;
12import org.openstreetmap.josm.tools.Destroyable;
13
14/**
15 * Superclass of history browsing panels, backed by an {@link HistoryBrowserModel}.
16 * @since 14463
17 */
18public abstract class HistoryBrowserPanel extends JPanel implements Destroyable {
19
20 /** the model */
21 protected transient HistoryBrowserModel model;
22 /** the common info panel for the history object in role REFERENCE_POINT_IN_TIME */
23 protected VersionInfoPanel referenceInfoPanel;
24 /** the common info panel for the history object in role CURRENT_POINT_IN_TIME */
25 protected VersionInfoPanel currentInfoPanel;
26
27 private final List<JosmAction> josmActions = new ArrayList<>();
28
29 protected HistoryBrowserPanel() {
30 super(new GridBagLayout());
31 }
32
33 protected void registerAsChangeListener(HistoryBrowserModel model) {
34 if (currentInfoPanel != null) {
35 model.addChangeListener(currentInfoPanel);
36 }
37 if (referenceInfoPanel != null) {
38 model.addChangeListener(referenceInfoPanel);
39 }
40 }
41
42 protected void unregisterAsChangeListener(HistoryBrowserModel model) {
43 if (currentInfoPanel != null) {
44 model.removeChangeListener(currentInfoPanel);
45 }
46 if (referenceInfoPanel != null) {
47 model.removeChangeListener(referenceInfoPanel);
48 }
49 }
50
51 /**
52 * Sets the history browsing model for this viewer.
53 *
54 * @param model the history browsing model
55 */
56 protected final void setModel(HistoryBrowserModel model) {
57 if (this.model != null) {
58 unregisterAsChangeListener(this.model);
59 }
60 this.model = model;
61 if (this.model != null) {
62 registerAsChangeListener(model);
63 }
64 }
65
66 protected final <T extends AbstractAction> T trackJosmAction(T action) {
67 if (action instanceof JosmAction) {
68 josmActions.add((JosmAction) action);
69 }
70 return action;
71 }
72
73 @Override
74 public void destroy() {
75 setModel(null);
76 if (referenceInfoPanel != null)
77 referenceInfoPanel.destroy();
78 if (currentInfoPanel != null)
79 currentInfoPanel.destroy();
80 josmActions.forEach(JosmAction::destroy);
81 josmActions.clear();
82 }
83}
Note: See TracBrowser for help on using the repository browser.