1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.time.Instant;
|
---|
8 | import java.time.format.FormatStyle;
|
---|
9 |
|
---|
10 | import javax.swing.ImageIcon;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JList;
|
---|
13 | import javax.swing.ListCellRenderer;
|
---|
14 | import javax.swing.UIManager;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * A {@link ListCellRenderer} for the list of changesets in the upload dialog.
|
---|
23 | *
|
---|
24 | * @since 2115
|
---|
25 | */
|
---|
26 | public class ChangesetCellRenderer extends JLabel implements ListCellRenderer<Changeset> {
|
---|
27 | private final ImageIcon icon;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code ChangesetCellRenderer}.
|
---|
31 | */
|
---|
32 | public ChangesetCellRenderer() {
|
---|
33 | icon = ImageProvider.get("data", "changeset");
|
---|
34 | setOpaque(true);
|
---|
35 | }
|
---|
36 |
|
---|
37 | protected String buildToolTipText(Changeset cs) {
|
---|
38 | StringBuilder sb = new StringBuilder(64);
|
---|
39 | sb.append("<html><strong>").append(tr("Changeset id:")).append("</strong>").append(cs.getId()).append("<br>");
|
---|
40 | Instant createdDate = cs.getCreatedAt();
|
---|
41 | if (createdDate != null) {
|
---|
42 | sb.append("<strong>").append(tr("Created at:")).append("</strong>").append(
|
---|
43 | DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(createdDate)).append("<br>");
|
---|
44 | }
|
---|
45 | String comment = cs.getComment();
|
---|
46 | if (!comment.isEmpty()) {
|
---|
47 | sb.append("<strong>").append(tr("Changeset comment:")).append("</strong>")
|
---|
48 | .append(Utils.escapeReservedCharactersHTML(comment)).append("<br>");
|
---|
49 | }
|
---|
50 | return sb.toString();
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public Component getListCellRendererComponent(JList<? extends Changeset> list, Changeset cs, int index, boolean isSelected,
|
---|
55 | boolean cellHasFocus) {
|
---|
56 | if (isSelected) {
|
---|
57 | setForeground(UIManager.getColor("List.selectionForeground"));
|
---|
58 | setBackground(UIManager.getColor("List.selectionBackground"));
|
---|
59 | } else {
|
---|
60 | setForeground(UIManager.getColor("List.foreground"));
|
---|
61 | setBackground(UIManager.getColor("List.background"));
|
---|
62 | }
|
---|
63 | if (cs != null) {
|
---|
64 | setIcon(icon);
|
---|
65 | if (cs.getId() == 0) {
|
---|
66 | setText("New changeset");
|
---|
67 | } else {
|
---|
68 | StringBuilder sb = new StringBuilder();
|
---|
69 | String comment = cs.getComment();
|
---|
70 | if (!comment.isEmpty()) {
|
---|
71 | sb.append(cs.getId()).append(" - ").append(comment);
|
---|
72 | } else if (cs.get("name") != null) {
|
---|
73 | sb.append(cs.getId()).append(" - ").append(cs.get("name"));
|
---|
74 | } else {
|
---|
75 | sb.append(tr("Changeset {0}", cs.getId()));
|
---|
76 | }
|
---|
77 | setText(sb.toString());
|
---|
78 | }
|
---|
79 | setToolTipText(buildToolTipText(cs));
|
---|
80 | } else {
|
---|
81 | setIcon(null);
|
---|
82 | setText("");
|
---|
83 | }
|
---|
84 | return this;
|
---|
85 | }
|
---|
86 | }
|
---|