[2512] | 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;
|
---|
[5319] | 7 | import java.text.DateFormat;
|
---|
[11878] | 8 | import java.util.Date;
|
---|
[2512] | 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;
|
---|
[11848] | 18 | import org.openstreetmap.josm.tools.Utils;
|
---|
[7299] | 19 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
[2512] | 20 |
|
---|
| 21 | /**
|
---|
[5266] | 22 | * A {@link ListCellRenderer} for the list of changesets in the upload dialog.
|
---|
[2512] | 23 | *
|
---|
[7299] | 24 | * @since 2115
|
---|
[2512] | 25 | */
|
---|
[7001] | 26 | public class ChangesetCellRenderer extends JLabel implements ListCellRenderer<Changeset> {
|
---|
[9078] | 27 | private final ImageIcon icon;
|
---|
[7001] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Constructs a new {@code ChangesetCellRenderer}.
|
---|
| 31 | */
|
---|
[2512] | 32 | public ChangesetCellRenderer() {
|
---|
| 33 | icon = ImageProvider.get("data", "changeset");
|
---|
| 34 | setOpaque(true);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | protected String buildToolTipText(Changeset cs) {
|
---|
[10242] | 38 | StringBuilder sb = new StringBuilder(64);
|
---|
[8390] | 39 | sb.append("<html><strong>").append(tr("Changeset id:")).append("</strong>").append(cs.getId()).append("<br>");
|
---|
[11878] | 40 | Date createdDate = cs.getCreatedAt();
|
---|
| 41 | if (createdDate != null) {
|
---|
[7299] | 42 | sb.append("<strong>").append(tr("Created at:")).append("</strong>").append(
|
---|
[11878] | 43 | DateUtils.formatDateTime(createdDate, DateFormat.SHORT, DateFormat.SHORT)).append("<br>");
|
---|
[2512] | 44 | }
|
---|
[12494] | 45 | String comment = cs.getComment();
|
---|
| 46 | if (!comment.isEmpty()) {
|
---|
[11848] | 47 | sb.append("<strong>").append(tr("Changeset comment:")).append("</strong>")
|
---|
| 48 | .append(Utils.escapeReservedCharactersHTML(comment)).append("<br>");
|
---|
[2512] | 49 | }
|
---|
| 50 | return sb.toString();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[6084] | 53 | @Override
|
---|
[8509] | 54 | public Component getListCellRendererComponent(JList<? extends Changeset> list, Changeset cs, int index, boolean isSelected,
|
---|
| 55 | boolean cellHasFocus) {
|
---|
[2512] | 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);
|
---|
[6822] | 65 | StringBuilder sb = new StringBuilder();
|
---|
[12494] | 66 | String comment = cs.getComment();
|
---|
| 67 | if (!comment.isEmpty()) {
|
---|
[11608] | 68 | sb.append(cs.getId()).append(" - ").append(comment);
|
---|
[2512] | 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 | }
|
---|