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.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.Insets;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.text.DateFormat;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.Date;
|
---|
16 | import java.util.Observable;
|
---|
17 | import java.util.Observer;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.JButton;
|
---|
21 | import javax.swing.JComponent;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JTextArea;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
29 | import org.openstreetmap.josm.data.osm.User;
|
---|
30 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
31 | import org.openstreetmap.josm.gui.JosmUserIdentityManager;
|
---|
32 | import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
|
---|
33 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
35 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
36 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
37 | import org.openstreetmap.josm.tools.GBC;
|
---|
38 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
39 | import org.openstreetmap.josm.tools.Utils;
|
---|
40 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * VersionInfoPanel is an UI component which displays the basic properties of a version
|
---|
44 | * of a {@link OsmPrimitive}.
|
---|
45 | * @since 1709
|
---|
46 | */
|
---|
47 | public class VersionInfoPanel extends JPanel implements Observer {
|
---|
48 | private final PointInTimeType pointInTimeType;
|
---|
49 | private final transient HistoryBrowserModel model;
|
---|
50 | private JMultilineLabel lblInfo;
|
---|
51 | private UrlLabel lblUser;
|
---|
52 | private UrlLabel lblChangeset;
|
---|
53 | private final OpenChangesetDialogAction changesetDialogAction = new OpenChangesetDialogAction();
|
---|
54 | private final JButton changesetButton = new JButton(changesetDialogAction);
|
---|
55 | private JPanel pnlChangesetSource;
|
---|
56 | private JPanel pnlChangesetImageryUsed;
|
---|
57 | private JLabel lblSource;
|
---|
58 | private JLabel lblImageryUsed;
|
---|
59 | private JTextArea texChangesetComment;
|
---|
60 | private JTextArea texChangesetSource;
|
---|
61 | private JTextArea texChangesetImageryUsed;
|
---|
62 |
|
---|
63 | protected static JTextArea buildTextArea(String tooltip) {
|
---|
64 | JTextArea lbl = new JTextArea();
|
---|
65 | lbl.setLineWrap(true);
|
---|
66 | lbl.setWrapStyleWord(true);
|
---|
67 | lbl.setEditable(false);
|
---|
68 | lbl.setOpaque(false);
|
---|
69 | lbl.setToolTipText(tooltip);
|
---|
70 | return lbl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected static JLabel buildLabel(String text, String tooltip, JTextArea textArea) {
|
---|
74 | // We need text field to be a JTextArea for line wrapping but cannot put HTML code in here,
|
---|
75 | // so create a separate JLabel with same characteristics (margin, font)
|
---|
76 | JLabel lbl = new JLabel("<html><p style='margin-top:"+textArea.getMargin().top+"'>"+text+"</html>");
|
---|
77 | lbl.setFont(textArea.getFont());
|
---|
78 | lbl.setToolTipText(tooltip);
|
---|
79 | lbl.setLabelFor(textArea);
|
---|
80 | return lbl;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected static JPanel buildTextPanel(JLabel label, JTextArea textArea) {
|
---|
84 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
85 | pnl.add(label, GBC.std().anchor(GBC.NORTHWEST));
|
---|
86 | pnl.add(textArea, GBC.eol().insets(2, 0, 0, 0).fill());
|
---|
87 | return pnl;
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected void build() {
|
---|
91 | JPanel pnl1 = new JPanel(new BorderLayout());
|
---|
92 | lblInfo = new JMultilineLabel("");
|
---|
93 | pnl1.add(lblInfo, BorderLayout.CENTER);
|
---|
94 |
|
---|
95 | JPanel pnlUserAndChangeset = new JPanel(new GridLayout(2, 2));
|
---|
96 | lblUser = new UrlLabel("", 2);
|
---|
97 | pnlUserAndChangeset.add(new JLabel(tr("User:")));
|
---|
98 | pnlUserAndChangeset.add(lblUser);
|
---|
99 | changesetButton.setMargin(new Insets(0, 0, 0, 0));
|
---|
100 | pnlUserAndChangeset.add(changesetButton);
|
---|
101 | lblChangeset = new UrlLabel("", 2);
|
---|
102 | pnlUserAndChangeset.add(lblChangeset);
|
---|
103 |
|
---|
104 | texChangesetComment = buildTextArea(tr("Changeset comment"));
|
---|
105 | texChangesetSource = buildTextArea(tr("Changeset source"));
|
---|
106 | texChangesetImageryUsed = buildTextArea(tr("Imagery used"));
|
---|
107 |
|
---|
108 | lblSource = buildLabel(tr("<b>Source</b>:"), tr("Changeset source"), texChangesetSource);
|
---|
109 | lblImageryUsed = buildLabel(tr("<b>Imagery</b>:"), tr("Imagery used"), texChangesetImageryUsed);
|
---|
110 | pnlChangesetSource = buildTextPanel(lblSource, texChangesetSource);
|
---|
111 | pnlChangesetImageryUsed = buildTextPanel(lblImageryUsed, texChangesetImageryUsed);
|
---|
112 |
|
---|
113 | setLayout(new GridBagLayout());
|
---|
114 | GridBagConstraints gc = new GridBagConstraints();
|
---|
115 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
116 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
117 | gc.weightx = 1.0;
|
---|
118 | gc.weighty = 1.0;
|
---|
119 | add(pnl1, gc);
|
---|
120 | gc.gridy = 1;
|
---|
121 | gc.weighty = 0.0;
|
---|
122 | add(pnlUserAndChangeset, gc);
|
---|
123 | gc.gridy = 2;
|
---|
124 | add(texChangesetComment, gc);
|
---|
125 | gc.gridy = 3;
|
---|
126 | add(pnlChangesetSource, gc);
|
---|
127 | gc.gridy = 4;
|
---|
128 | add(pnlChangesetImageryUsed, gc);
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected HistoryOsmPrimitive getPrimitive() {
|
---|
132 | if (model == null || pointInTimeType == null)
|
---|
133 | return null;
|
---|
134 | return model.getPointInTime(pointInTimeType);
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected String getInfoText(final Date timestamp, final long version, final boolean isLatest) {
|
---|
138 | String text;
|
---|
139 | if (isLatest) {
|
---|
140 | OsmDataLayer editLayer = Main.main.getEditLayer();
|
---|
141 | text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
|
---|
142 | Long.toString(version),
|
---|
143 | editLayer == null ? tr("unknown") : editLayer.getName()
|
---|
144 | );
|
---|
145 | } else {
|
---|
146 | String date = "?";
|
---|
147 | if (timestamp != null) {
|
---|
148 | date = DateUtils.formatDateTime(timestamp, DateFormat.SHORT, DateFormat.SHORT);
|
---|
149 | }
|
---|
150 | text = tr(
|
---|
151 | "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
|
---|
152 | Long.toString(version), date);
|
---|
153 | }
|
---|
154 | return text;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Constructs a new {@code VersionInfoPanel}.
|
---|
159 | */
|
---|
160 | public VersionInfoPanel() {
|
---|
161 | pointInTimeType = null;
|
---|
162 | model = null;
|
---|
163 | build();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * constructor
|
---|
168 | *
|
---|
169 | * @param model the model (must not be null)
|
---|
170 | * @param pointInTimeType the point in time this panel visualizes (must not be null)
|
---|
171 | * @throws IllegalArgumentException if model is null
|
---|
172 | * @throws IllegalArgumentException if pointInTimeType is null
|
---|
173 | */
|
---|
174 | public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) {
|
---|
175 | CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
|
---|
176 | CheckParameterUtil.ensureParameterNotNull(model, "model");
|
---|
177 |
|
---|
178 | this.model = model;
|
---|
179 | this.pointInTimeType = pointInTimeType;
|
---|
180 | model.addObserver(this);
|
---|
181 | build();
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected static String getUserUrl(String username) {
|
---|
185 | return Main.getBaseUserUrl() + '/' + Utils.encodeUrl(username).replaceAll("\\+", "%20");
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public void update(Observable o, Object arg) {
|
---|
190 | HistoryOsmPrimitive primitive = getPrimitive();
|
---|
191 | Changeset cs = primitive.getChangeset();
|
---|
192 | update(cs, model.isLatest(primitive), primitive.getTimestamp(), primitive.getVersion());
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void update(final OsmPrimitive primitive, final boolean isLatest) {
|
---|
196 | update(Changeset.fromPrimitive(primitive), isLatest, primitive.getTimestamp(), primitive.getVersion());
|
---|
197 | }
|
---|
198 |
|
---|
199 | public void update(final Changeset cs, final boolean isLatest, final Date timestamp, final long version) {
|
---|
200 | lblInfo.setText(getInfoText(timestamp, version, isLatest));
|
---|
201 |
|
---|
202 | if (!isLatest) {
|
---|
203 | User user = cs.getUser();
|
---|
204 | String url = Main.getBaseBrowseUrl() + "/changeset/" + cs.getId();
|
---|
205 | lblChangeset.setUrl(url);
|
---|
206 | lblChangeset.setDescription(Long.toString(cs.getId()));
|
---|
207 | changesetDialogAction.setId(cs.getId());
|
---|
208 | changesetButton.setEnabled(true);
|
---|
209 |
|
---|
210 | String username = "";
|
---|
211 | if (user != null) {
|
---|
212 | username = user.getName();
|
---|
213 | }
|
---|
214 | lblUser.setDescription(username);
|
---|
215 | if (user != null && user != User.getAnonymous()) {
|
---|
216 | lblUser.setUrl(getUserUrl(username));
|
---|
217 | } else {
|
---|
218 | lblUser.setUrl(null);
|
---|
219 | }
|
---|
220 | } else {
|
---|
221 | String username = JosmUserIdentityManager.getInstance().getUserName();
|
---|
222 | if (username == null) {
|
---|
223 | lblUser.setDescription(tr("anonymous"));
|
---|
224 | lblUser.setUrl(null);
|
---|
225 | } else {
|
---|
226 | lblUser.setDescription(username);
|
---|
227 | lblUser.setUrl(getUserUrl(username));
|
---|
228 | }
|
---|
229 | lblChangeset.setDescription(tr("none"));
|
---|
230 | lblChangeset.setUrl(null);
|
---|
231 | changesetDialogAction.setId(null);
|
---|
232 | changesetButton.setEnabled(false);
|
---|
233 | }
|
---|
234 |
|
---|
235 | final Changeset oppCs = model != null ? model.getPointInTime(pointInTimeType.opposite()).getChangeset() : null;
|
---|
236 | updateText(cs, "comment", texChangesetComment, null, oppCs, texChangesetComment);
|
---|
237 | updateText(cs, "source", texChangesetSource, lblSource, oppCs, pnlChangesetSource);
|
---|
238 | updateText(cs, "imagery_used", texChangesetImageryUsed, lblImageryUsed, oppCs, pnlChangesetImageryUsed);
|
---|
239 | }
|
---|
240 |
|
---|
241 | protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JComponent container) {
|
---|
242 | final String text = cs != null ? cs.get(attr) : null;
|
---|
243 | // Update text, hide prefixing label if empty
|
---|
244 | if (label != null) {
|
---|
245 | label.setVisible(text != null && !Utils.strip(text).isEmpty());
|
---|
246 | }
|
---|
247 | textArea.setText(text);
|
---|
248 | // Hide container if values of both versions are empty
|
---|
249 | container.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
|
---|
250 | }
|
---|
251 |
|
---|
252 | static class OpenChangesetDialogAction extends AbstractAction {
|
---|
253 | private Integer id;
|
---|
254 |
|
---|
255 | OpenChangesetDialogAction() {
|
---|
256 | super(tr("Changeset"), new ImageProvider("dialogs/changeset", "changesetmanager").resetMaxSize(new Dimension(16, 16)).get());
|
---|
257 | putValue(SHORT_DESCRIPTION, tr("Opens the Changeset Manager window for the selected changesets"));
|
---|
258 | }
|
---|
259 |
|
---|
260 | public void setId(Integer id) {
|
---|
261 | this.id = id;
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public void actionPerformed(ActionEvent e) {
|
---|
266 | ChangesetDialog.LaunchChangesetManager.displayChangesets(Collections.singleton(id));
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|