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.text.DateFormat;
|
---|
8 | import java.util.Date;
|
---|
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 | Date createdDate = cs.getCreatedAt();
|
---|
41 | if (createdDate != null) {
|
---|
42 | sb.append("<strong>").append(tr("Created at:")).append("</strong>").append(
|
---|
43 | DateUtils.formatDateTime(createdDate, DateFormat.SHORT, DateFormat.SHORT)).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 | StringBuilder sb = new StringBuilder();
|
---|
66 | String comment = cs.getComment();
|
---|
67 | if (!comment.isEmpty()) {
|
---|
68 | sb.append(cs.getId()).append(" - ").append(comment);
|
---|
69 | } else if (cs.get("name") != null) {
|
---|
70 | sb.append(cs.getId()).append(" - ").append(cs.get("name"));
|
---|
71 | } else {
|
---|
72 | sb.append(tr("Changeset {0}", cs.getId()));
|
---|
73 | }
|
---|
74 | setText(sb.toString());
|
---|
75 | setToolTipText(buildToolTipText(cs));
|
---|
76 | } else {
|
---|
77 | setText(tr("No open changeset"));
|
---|
78 | }
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 | }
|
---|