1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.util.Arrays;
|
---|
9 |
|
---|
10 | import javax.swing.JPanel;
|
---|
11 | import javax.swing.JScrollPane;
|
---|
12 | import javax.swing.JSplitPane;
|
---|
13 | import javax.swing.JTabbedPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.data.osm.history.History;
|
---|
18 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * HistoryBrowser is an UI component which displays history information about an {@link OsmPrimitive}.
|
---|
22 | *
|
---|
23 | * @since 1709
|
---|
24 | */
|
---|
25 | public class HistoryBrowser extends JPanel implements Destroyable {
|
---|
26 |
|
---|
27 | /** the model */
|
---|
28 | private transient HistoryBrowserModel model;
|
---|
29 | private TagInfoViewer tagInfoViewer;
|
---|
30 | private NodeListViewer nodeListViewer;
|
---|
31 | private RelationMemberListViewer relationMemberListViewer;
|
---|
32 | private CoordinateInfoViewer coordinateInfoViewer;
|
---|
33 | private JTabbedPane tpViewers;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code HistoryBrowser}.
|
---|
37 | */
|
---|
38 | public HistoryBrowser() {
|
---|
39 | model = new HistoryBrowserModel();
|
---|
40 | build();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code HistoryBrowser}.
|
---|
45 | * @param history the history of an {@link OsmPrimitive}
|
---|
46 | */
|
---|
47 | public HistoryBrowser(History history) {
|
---|
48 | this();
|
---|
49 | populate(history);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * creates the table which shows the list of versions
|
---|
54 | *
|
---|
55 | * @return the panel with the version table
|
---|
56 | */
|
---|
57 | protected JPanel createVersionTablePanel() {
|
---|
58 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
59 | pnl.add(new JScrollPane(new VersionTable(model)), BorderLayout.CENTER);
|
---|
60 | return pnl;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * creates the panel which shows information about two different versions
|
---|
65 | * of the same {@link OsmPrimitive}.
|
---|
66 | *
|
---|
67 | * @return the panel
|
---|
68 | */
|
---|
69 | protected JPanel createVersionComparePanel() {
|
---|
70 | tpViewers = new JTabbedPane();
|
---|
71 |
|
---|
72 | // create the viewers, but don't add them yet.
|
---|
73 | // see populate()
|
---|
74 | //
|
---|
75 | tagInfoViewer = new TagInfoViewer(model);
|
---|
76 | nodeListViewer = new NodeListViewer(model);
|
---|
77 | relationMemberListViewer = new RelationMemberListViewer(model);
|
---|
78 | coordinateInfoViewer = new CoordinateInfoViewer(model);
|
---|
79 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
80 | pnl.add(tpViewers, BorderLayout.CENTER);
|
---|
81 |
|
---|
82 | tpViewers.addChangeListener(e -> {
|
---|
83 | if (tpViewers.getSelectedComponent() == coordinateInfoViewer) {
|
---|
84 | // while building the component size is not yet known, thus panning does not give reasonable results
|
---|
85 | coordinateInfoViewer.setDisplayToFitMapMarkers();
|
---|
86 | }
|
---|
87 | });
|
---|
88 |
|
---|
89 | return pnl;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * builds the GUI
|
---|
94 | */
|
---|
95 | protected void build() {
|
---|
96 | JPanel left = createVersionTablePanel();
|
---|
97 | JPanel right = createVersionComparePanel();
|
---|
98 | setLayout(new BorderLayout());
|
---|
99 | JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
|
---|
100 | add(pane, BorderLayout.CENTER);
|
---|
101 |
|
---|
102 | pane.setOneTouchExpandable(true);
|
---|
103 | pane.setDividerLocation(300);
|
---|
104 |
|
---|
105 | Dimension minimumSize = new Dimension(100, 50);
|
---|
106 | left.setMinimumSize(minimumSize);
|
---|
107 | right.setMinimumSize(minimumSize);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * populates the browser with the history of a specific {@link OsmPrimitive}
|
---|
112 | *
|
---|
113 | * @param history the history
|
---|
114 | */
|
---|
115 | public void populate(History history) {
|
---|
116 | boolean samePrimitive = model.isSamePrimitive(history); // needs to be before setHistory
|
---|
117 | model.setHistory(history);
|
---|
118 | if (samePrimitive) {
|
---|
119 | // no need to rebuild the UI
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | tpViewers.removeAll();
|
---|
124 |
|
---|
125 | tpViewers.add(tagInfoViewer);
|
---|
126 | tpViewers.setTitleAt(0, tr("Tags"));
|
---|
127 |
|
---|
128 | if (history.getEarliest().getType() == OsmPrimitiveType.NODE) {
|
---|
129 | tpViewers.add(coordinateInfoViewer);
|
---|
130 | tpViewers.setTitleAt(1, tr("Coordinates"));
|
---|
131 | } else if (history.getEarliest().getType() == OsmPrimitiveType.WAY) {
|
---|
132 | tpViewers.add(nodeListViewer);
|
---|
133 | tpViewers.setTitleAt(1, tr("Nodes"));
|
---|
134 | } else if (history.getEarliest().getType() == OsmPrimitiveType.RELATION) {
|
---|
135 | tpViewers.add(relationMemberListViewer);
|
---|
136 | tpViewers.setTitleAt(1, tr("Members"));
|
---|
137 | }
|
---|
138 | revalidate();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * replies the {@link History} currently displayed by this browser
|
---|
143 | *
|
---|
144 | * @return the current history
|
---|
145 | */
|
---|
146 | public History getHistory() {
|
---|
147 | return model.getHistory();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * replies the model used by this browser
|
---|
152 | * @return the model
|
---|
153 | */
|
---|
154 | public HistoryBrowserModel getModel() {
|
---|
155 | return model;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void destroy() {
|
---|
160 | if (model != null) {
|
---|
161 | model.unlinkAsListener();
|
---|
162 | model = null;
|
---|
163 | }
|
---|
164 | Arrays.asList(tagInfoViewer, nodeListViewer, relationMemberListViewer, coordinateInfoViewer).stream()
|
---|
165 | .filter(Destroyable.class::isInstance).forEach(Destroyable::destroy);
|
---|
166 | tagInfoViewer = null;
|
---|
167 | nodeListViewer = null;
|
---|
168 | relationMemberListViewer = null;
|
---|
169 | coordinateInfoViewer = null;
|
---|
170 | }
|
---|
171 | }
|
---|