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 |
|
---|
9 | import javax.swing.ImageIcon;
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JList;
|
---|
12 | import javax.swing.ListCellRenderer;
|
---|
13 | import javax.swing.UIManager;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A {@link ListCellRenderer} for the list of changesets in the upload dialog.
|
---|
21 | *
|
---|
22 | * @since 2115
|
---|
23 | */
|
---|
24 | public class ChangesetCellRenderer extends JLabel implements ListCellRenderer<Changeset> {
|
---|
25 | private final ImageIcon icon;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code ChangesetCellRenderer}.
|
---|
29 | */
|
---|
30 | public ChangesetCellRenderer() {
|
---|
31 | icon = ImageProvider.get("data", "changeset");
|
---|
32 | setOpaque(true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected String buildToolTipText(Changeset cs) {
|
---|
36 | StringBuilder sb = new StringBuilder(64);
|
---|
37 | sb.append("<html><strong>").append(tr("Changeset id:")).append("</strong>").append(cs.getId()).append("<br>");
|
---|
38 | if (cs.getCreatedAt() != null) {
|
---|
39 | sb.append("<strong>").append(tr("Created at:")).append("</strong>").append(
|
---|
40 | DateUtils.formatDateTime(cs.getCreatedAt(), DateFormat.SHORT, DateFormat.SHORT)).append("<br>");
|
---|
41 | }
|
---|
42 | if (cs.get("comment") != null) {
|
---|
43 | sb.append("<strong>").append(tr("Changeset comment:")).append("</strong>").append(cs.get("comment")).append("<br>");
|
---|
44 | }
|
---|
45 | return sb.toString();
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public Component getListCellRendererComponent(JList<? extends Changeset> list, Changeset cs, int index, boolean isSelected,
|
---|
50 | boolean cellHasFocus) {
|
---|
51 | if (isSelected) {
|
---|
52 | setForeground(UIManager.getColor("List.selectionForeground"));
|
---|
53 | setBackground(UIManager.getColor("List.selectionBackground"));
|
---|
54 | } else {
|
---|
55 | setForeground(UIManager.getColor("List.foreground"));
|
---|
56 | setBackground(UIManager.getColor("List.background"));
|
---|
57 | }
|
---|
58 | if (cs != null) {
|
---|
59 | setIcon(icon);
|
---|
60 | StringBuilder sb = new StringBuilder();
|
---|
61 | if (cs.get("comment") != null) {
|
---|
62 | sb.append(cs.getId()).append(" - ").append(cs.get("comment"));
|
---|
63 | } else if (cs.get("name") != null) {
|
---|
64 | sb.append(cs.getId()).append(" - ").append(cs.get("name"));
|
---|
65 | } else {
|
---|
66 | sb.append(tr("Changeset {0}", cs.getId()));
|
---|
67 | }
|
---|
68 | setText(sb.toString());
|
---|
69 | setToolTipText(buildToolTipText(cs));
|
---|
70 | } else {
|
---|
71 | setText(tr("No open changeset"));
|
---|
72 | }
|
---|
73 | return this;
|
---|
74 | }
|
---|
75 | }
|
---|