source: josm/trunk/src/org/openstreetmap/josm/gui/history/VersionInfoPanel.java@ 13654

Last change on this file since 13654 was 13535, checked in by Don-vip, 6 years ago

fix #16097 - NPE + disable arrow button in history dialog with changeset button

  • Property svn:eol-style set to native
File size: 14.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.BorderLayout;
8import java.awt.Dimension;
9import java.awt.GridBagConstraints;
10import java.awt.GridBagLayout;
11import java.awt.Insets;
12import java.awt.event.ActionEvent;
13import java.text.DateFormat;
14import java.util.Collections;
15import java.util.Date;
16
17import javax.swing.AbstractAction;
18import javax.swing.AbstractButton;
19import javax.swing.JButton;
20import javax.swing.JComponent;
21import javax.swing.JLabel;
22import javax.swing.JPanel;
23import javax.swing.JTextArea;
24import javax.swing.event.ChangeEvent;
25import javax.swing.event.ChangeListener;
26import javax.swing.plaf.basic.BasicArrowButton;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.data.UserIdentityManager;
30import org.openstreetmap.josm.data.osm.Changeset;
31import org.openstreetmap.josm.data.osm.OsmPrimitive;
32import org.openstreetmap.josm.data.osm.User;
33import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
34import org.openstreetmap.josm.gui.MainApplication;
35import org.openstreetmap.josm.gui.dialogs.ChangesetDialog;
36import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager;
37import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetDiscussionPanel;
38import org.openstreetmap.josm.gui.layer.OsmDataLayer;
39import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
40import org.openstreetmap.josm.gui.widgets.UrlLabel;
41import org.openstreetmap.josm.tools.CheckParameterUtil;
42import org.openstreetmap.josm.tools.GBC;
43import org.openstreetmap.josm.tools.ImageProvider;
44import org.openstreetmap.josm.tools.Utils;
45import org.openstreetmap.josm.tools.date.DateUtils;
46
47/**
48 * VersionInfoPanel is an UI component which displays the basic properties of a version
49 * of a {@link OsmPrimitive}.
50 * @since 1709
51 */
52public class VersionInfoPanel extends JPanel implements ChangeListener {
53 private final PointInTimeType pointInTimeType;
54 private final transient HistoryBrowserModel model;
55 private JMultilineLabel lblInfo;
56 private UrlLabel lblUser;
57 private UrlLabel lblChangeset;
58 private final JButton lblChangesetComments = new JButton(ImageProvider.get("dialogs/notes/note_comment"));
59 private final OpenChangesetDialogAction changesetCommentsDialogAction = new OpenChangesetDialogAction(ChangesetDiscussionPanel.class);
60 private final OpenChangesetDialogAction changesetDialogAction = new OpenChangesetDialogAction(null);
61 private final JButton changesetButton = new JButton(changesetDialogAction);
62 private final BasicArrowButton arrowButton = new BasicArrowButton(BasicArrowButton.SOUTH);
63 private JPanel pnlChangesetSource;
64 private JPanel pnlChangesetImageryUsed;
65 private JLabel lblSource;
66 private JLabel lblImageryUsed;
67 private JTextArea texChangesetComment;
68 private JTextArea texChangesetSource;
69 private JTextArea texChangesetImageryUsed;
70
71 protected static JTextArea buildTextArea(String tooltip) {
72 JTextArea lbl = new JTextArea();
73 lbl.setLineWrap(true);
74 lbl.setWrapStyleWord(true);
75 lbl.setEditable(false);
76 lbl.setOpaque(false);
77 lbl.setToolTipText(tooltip);
78 return lbl;
79 }
80
81 protected static JLabel buildLabel(String text, String tooltip, JTextArea textArea) {
82 // We need text field to be a JTextArea for line wrapping but cannot put HTML code in here,
83 // so create a separate JLabel with same characteristics (margin, font)
84 JLabel lbl = new JLabel("<html><p style='margin-top:"+textArea.getMargin().top+"'>"+text+"</html>");
85 lbl.setFont(textArea.getFont());
86 lbl.setToolTipText(tooltip);
87 lbl.setLabelFor(textArea);
88 return lbl;
89 }
90
91 protected static JPanel buildTextPanel(JLabel label, JTextArea textArea) {
92 JPanel pnl = new JPanel(new GridBagLayout());
93 pnl.add(label, GBC.std().anchor(GBC.NORTHWEST));
94 pnl.add(textArea, GBC.eol().insets(2, 0, 0, 0).fill());
95 return pnl;
96 }
97
98 protected void build() {
99 JPanel pnl1 = new JPanel(new BorderLayout());
100 lblInfo = new JMultilineLabel("");
101 pnl1.add(lblInfo, BorderLayout.CENTER);
102
103 // +-----------------------+-------------------------------------+
104 // | User: | lblUser |
105 // +-----------------------+-------------------------------------+
106 // | changesetButton | lblChangeset | lblChangesetComments |
107 // +-----------------------+-------------------------------------+
108 JPanel pnlUserAndChangeset = new JPanel(new GridBagLayout());
109 pnlUserAndChangeset.add(new JLabel(tr("User:")), GBC.std());
110
111 lblUser = new UrlLabel("", 2);
112 pnlUserAndChangeset.add(lblUser, GBC.eol().insets(5, 0, 0, 0).weight(1, 0));
113
114 final JPanel changesetPanel = new JPanel(new BorderLayout());
115 changesetButton.setMargin(new Insets(0, 0, 0, 2));
116 changesetPanel.add(changesetButton, BorderLayout.CENTER);
117 arrowButton.addActionListener(action -> {
118 if (changesetDialogAction.id != null) { // fix #15444, #16097
119 final OpenChangesetPopupMenu popupMenu = new OpenChangesetPopupMenu(changesetDialogAction.id);
120 popupMenu.insert(changesetDialogAction, 0);
121 ((AbstractButton) popupMenu.getComponent(0)).setText(tr("Open Changeset Manager"));
122 popupMenu.show(arrowButton);
123 }
124 });
125 changesetPanel.add(arrowButton, BorderLayout.EAST);
126 pnlUserAndChangeset.add(changesetPanel, GBC.std().fill().weight(0, 0));
127
128 lblChangeset = new UrlLabel("", 2);
129 pnlUserAndChangeset.add(lblChangeset, GBC.std().insets(5, 0, 0, 0).weight(1, 0));
130
131 lblChangesetComments.setAction(changesetCommentsDialogAction);
132 lblChangesetComments.setMargin(new Insets(0, 0, 0, 0));
133 lblChangesetComments.setIcon(new ImageProvider("dialogs/notes/note_comment").setMaxSize(12).get());
134 pnlUserAndChangeset.add(lblChangesetComments, GBC.eol());
135
136 texChangesetComment = buildTextArea(tr("Changeset comment"));
137 texChangesetSource = buildTextArea(tr("Changeset source"));
138 texChangesetImageryUsed = buildTextArea(tr("Imagery used"));
139
140 lblSource = buildLabel(tr("<b>Source</b>:"), tr("Changeset source"), texChangesetSource);
141 lblImageryUsed = buildLabel(tr("<b>Imagery</b>:"), tr("Imagery used"), texChangesetImageryUsed);
142 pnlChangesetSource = buildTextPanel(lblSource, texChangesetSource);
143 pnlChangesetImageryUsed = buildTextPanel(lblImageryUsed, texChangesetImageryUsed);
144
145 setLayout(new GridBagLayout());
146 GridBagConstraints gc = new GridBagConstraints();
147 gc.anchor = GridBagConstraints.NORTHWEST;
148 gc.fill = GridBagConstraints.HORIZONTAL;
149 gc.weightx = 1.0;
150 gc.weighty = 1.0;
151 add(pnl1, gc);
152 gc.gridy = 1;
153 gc.weighty = 0.0;
154 add(pnlUserAndChangeset, gc);
155 gc.gridy = 2;
156 add(texChangesetComment, gc);
157 gc.gridy = 3;
158 add(pnlChangesetSource, gc);
159 gc.gridy = 4;
160 add(pnlChangesetImageryUsed, gc);
161 }
162
163 protected HistoryOsmPrimitive getPrimitive() {
164 if (model == null || pointInTimeType == null)
165 return null;
166 return model.getPointInTime(pointInTimeType);
167 }
168
169 protected String getInfoText(final Date timestamp, final long version, final boolean isLatest) {
170 String text;
171 if (isLatest) {
172 OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
173 text = tr("<html>Version <strong>{0}</strong> currently edited in layer ''{1}''</html>",
174 Long.toString(version),
175 editLayer == null ? tr("unknown") : Utils.escapeReservedCharactersHTML(editLayer.getName())
176 );
177 } else {
178 String date = "?";
179 if (timestamp != null) {
180 date = DateUtils.formatDateTime(timestamp, DateFormat.SHORT, DateFormat.SHORT);
181 }
182 text = tr(
183 "<html>Version <strong>{0}</strong> created on <strong>{1}</strong></html>",
184 Long.toString(version), date);
185 }
186 return text;
187 }
188
189 /**
190 * Constructs a new {@code VersionInfoPanel}.
191 */
192 public VersionInfoPanel() {
193 pointInTimeType = null;
194 model = null;
195 build();
196 }
197
198 /**
199 * constructor
200 *
201 * @param model the model (must not be null)
202 * @param pointInTimeType the point in time this panel visualizes (must not be null)
203 * @throws IllegalArgumentException if model is null
204 * @throws IllegalArgumentException if pointInTimeType is null
205 */
206 public VersionInfoPanel(HistoryBrowserModel model, PointInTimeType pointInTimeType) {
207 CheckParameterUtil.ensureParameterNotNull(pointInTimeType, "pointInTimeType");
208 CheckParameterUtil.ensureParameterNotNull(model, "model");
209
210 this.model = model;
211 this.pointInTimeType = pointInTimeType;
212 model.addChangeListener(this);
213 build();
214 }
215
216 protected static String getUserUrl(String username) {
217 return Main.getBaseUserUrl() + '/' + Utils.encodeUrl(username).replaceAll("\\+", "%20");
218 }
219
220 @Override
221 public void stateChanged(ChangeEvent e) {
222 HistoryOsmPrimitive primitive = getPrimitive();
223 if (primitive != null) {
224 Changeset cs = primitive.getChangeset();
225 update(cs, model.isLatest(primitive), primitive.getTimestamp(), primitive.getVersion());
226 }
227 }
228
229 /**
230 * Updates the content of this panel based on the changeset information given by {@code primitive}.
231 * @param primitive the primitive to extract the changeset information from
232 * @param isLatest whether this relates to a not yet commited changeset
233 */
234 public void update(final OsmPrimitive primitive, final boolean isLatest) {
235 update(Changeset.fromPrimitive(primitive), isLatest, primitive.getTimestamp(), primitive.getVersion());
236 }
237
238 /**
239 * Updates the content of this panel based on the changeset information given by {@code cs}.
240 * @param cs the changeset information
241 * @param isLatest whether this relates to a not yet commited changeset
242 * @param timestamp the timestamp
243 * @param version the version of the primitive
244 */
245 public void update(final Changeset cs, final boolean isLatest, final Date timestamp, final long version) {
246 lblInfo.setText(getInfoText(timestamp, version, isLatest));
247
248 if (!isLatest && cs != null) {
249 User user = cs.getUser();
250 String url = Main.getBaseBrowseUrl() + "/changeset/" + cs.getId();
251 lblChangeset.setUrl(url);
252 lblChangeset.setDescription(Long.toString(cs.getId()));
253 changesetCommentsDialogAction.setId(cs.getId());
254 lblChangesetComments.setVisible(cs.getCommentsCount() > 0);
255 lblChangesetComments.setText(String.valueOf(cs.getCommentsCount()));
256 lblChangesetComments.setToolTipText(trn("This changeset has {0} comment", "This changeset has {0} comments",
257 cs.getCommentsCount(), cs.getCommentsCount()));
258 changesetDialogAction.setId(cs.getId());
259 changesetButton.setEnabled(true);
260 arrowButton.setEnabled(true);
261
262 String username = "";
263 if (user != null) {
264 username = user.getName();
265 }
266 lblUser.setDescription(username);
267 if (user != null && user != User.getAnonymous()) {
268 lblUser.setUrl(getUserUrl(username));
269 } else {
270 lblUser.setUrl(null);
271 }
272 } else {
273 String username = UserIdentityManager.getInstance().getUserName();
274 if (username == null) {
275 lblUser.setDescription(tr("anonymous"));
276 lblUser.setUrl(null);
277 } else {
278 lblUser.setDescription(username);
279 lblUser.setUrl(getUserUrl(username));
280 }
281 lblChangeset.setDescription(tr("none"));
282 lblChangeset.setUrl(null);
283 lblChangesetComments.setVisible(false);
284 changesetDialogAction.setId(null);
285 changesetButton.setEnabled(false);
286 arrowButton.setEnabled(false);
287 }
288
289 final Changeset oppCs = model != null ? model.getPointInTime(pointInTimeType.opposite()).getChangeset() : null;
290 updateText(cs, "comment", texChangesetComment, null, oppCs, texChangesetComment);
291 updateText(cs, "source", texChangesetSource, lblSource, oppCs, pnlChangesetSource);
292 updateText(cs, "imagery_used", texChangesetImageryUsed, lblImageryUsed, oppCs, pnlChangesetImageryUsed);
293 }
294
295 protected static void updateText(Changeset cs, String attr, JTextArea textArea, JLabel label, Changeset oppCs, JComponent container) {
296 final String text = cs != null ? cs.get(attr) : null;
297 // Update text, hide prefixing label if empty
298 if (label != null) {
299 label.setVisible(text != null && !Utils.isStripEmpty(text));
300 }
301 textArea.setText(text);
302 // Hide container if values of both versions are empty
303 container.setVisible(text != null || (oppCs != null && oppCs.get(attr) != null));
304 }
305
306 static class OpenChangesetDialogAction extends AbstractAction {
307 private final Class<? extends JComponent> componentToSelect;
308 private Integer id;
309
310 OpenChangesetDialogAction(Class<? extends JComponent> componentToSelect) {
311 super(tr("Changeset"));
312 new ImageProvider("dialogs/changeset", "changesetmanager").resetMaxSize(new Dimension(16, 16))
313 .getResource().attachImageIcon(this, true);
314 putValue(SHORT_DESCRIPTION, tr("Opens the Changeset Manager window for the selected changesets"));
315 this.componentToSelect = componentToSelect;
316 }
317
318 void setId(Integer id) {
319 this.id = id;
320 }
321
322 @Override
323 public void actionPerformed(ActionEvent e) {
324 if (id != null) {
325 ChangesetDialog.LaunchChangesetManager.displayChangesets(Collections.singleton(id));
326 }
327 if (componentToSelect != null) {
328 ChangesetCacheManager.getInstance().setSelectedComponentInDetailPanel(componentToSelect);
329 }
330 }
331 }
332}
Note: See TracBrowser for help on using the repository browser.