001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.history; 003 004import java.awt.GridBagLayout; 005import java.util.ArrayList; 006import java.util.List; 007 008import javax.swing.AbstractAction; 009import javax.swing.JPanel; 010 011import org.openstreetmap.josm.actions.JosmAction; 012import org.openstreetmap.josm.data.osm.DataSet; 013import org.openstreetmap.josm.data.osm.OsmPrimitive; 014import org.openstreetmap.josm.data.osm.PrimitiveId; 015import org.openstreetmap.josm.gui.MainApplication; 016import org.openstreetmap.josm.tools.Destroyable; 017 018/** 019 * Superclass of history browsing panels, backed by an {@link HistoryBrowserModel}. 020 * @since 14463 021 */ 022public abstract class HistoryBrowserPanel extends JPanel implements Destroyable { 023 024 /** the model */ 025 protected transient HistoryBrowserModel model; 026 /** the common info panel for the history object in role REFERENCE_POINT_IN_TIME */ 027 protected VersionInfoPanel referenceInfoPanel; 028 /** the common info panel for the history object in role CURRENT_POINT_IN_TIME */ 029 protected VersionInfoPanel currentInfoPanel; 030 031 private final List<JosmAction> josmActions = new ArrayList<>(); 032 033 protected HistoryBrowserPanel() { 034 super(new GridBagLayout()); 035 } 036 037 protected void registerAsChangeListener(HistoryBrowserModel model) { 038 if (currentInfoPanel != null) { 039 model.addChangeListener(currentInfoPanel); 040 } 041 if (referenceInfoPanel != null) { 042 model.addChangeListener(referenceInfoPanel); 043 } 044 } 045 046 protected void unregisterAsChangeListener(HistoryBrowserModel model) { 047 if (currentInfoPanel != null) { 048 model.removeChangeListener(currentInfoPanel); 049 } 050 if (referenceInfoPanel != null) { 051 model.removeChangeListener(referenceInfoPanel); 052 } 053 } 054 055 /** 056 * Sets the history browsing model for this viewer. 057 * 058 * @param model the history browsing model 059 */ 060 protected final void setModel(HistoryBrowserModel model) { 061 if (this.model != null) { 062 unregisterAsChangeListener(this.model); 063 } 064 this.model = model; 065 if (this.model != null) { 066 registerAsChangeListener(model); 067 } 068 } 069 070 protected OsmPrimitive getPrimitiveFromDataSet(PointInTimeType pointInTime) { 071 DataSet dataSet = MainApplication.getLayerManager().getEditDataSet(); 072 PrimitiveId primitiveId = model.getPointInTime(pointInTime); 073 if (dataSet == null || primitiveId == null) { 074 return null; 075 } 076 return dataSet.getPrimitiveById(primitiveId.getUniqueId(), primitiveId.getType()); 077 } 078 079 protected final <T extends AbstractAction> T trackJosmAction(T action) { 080 if (action instanceof JosmAction) { 081 josmActions.add((JosmAction) action); 082 } 083 return action; 084 } 085 086 @Override 087 public void destroy() { 088 setModel(null); 089 if (referenceInfoPanel != null) 090 referenceInfoPanel.destroy(); 091 if (currentInfoPanel != null) 092 currentInfoPanel.destroy(); 093 josmActions.forEach(JosmAction::destroy); 094 josmActions.clear(); 095 } 096}