001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Component; 007import java.text.DateFormat; 008 009import javax.swing.ImageIcon; 010import javax.swing.JLabel; 011import javax.swing.JList; 012import javax.swing.ListCellRenderer; 013import javax.swing.UIManager; 014 015import org.openstreetmap.josm.data.osm.Changeset; 016import org.openstreetmap.josm.tools.ImageProvider; 017import org.openstreetmap.josm.tools.date.DateUtils; 018 019/** 020 * A {@link ListCellRenderer} for the list of changesets in the upload dialog. 021 * 022 * @since 2115 023 */ 024public class ChangesetCellRenderer extends JLabel implements ListCellRenderer<Changeset> { 025 private ImageIcon icon; 026 027 /** 028 * Constructs a new {@code ChangesetCellRenderer}. 029 */ 030 public ChangesetCellRenderer() { 031 icon = ImageProvider.get("data", "changeset"); 032 setOpaque(true); 033 } 034 035 protected String buildToolTipText(Changeset cs) { 036 StringBuilder sb = new StringBuilder(); 037 sb.append("<html>"); 038 sb.append("<strong>").append(tr("Changeset id:")).append("</strong>").append(cs.getId()).append("<br>"); 039 if (cs.getCreatedAt() != null) { 040 sb.append("<strong>").append(tr("Created at:")).append("</strong>").append( 041 DateUtils.formatDateTime(cs.getCreatedAt(), DateFormat.SHORT, DateFormat.SHORT)).append("<br>"); 042 } 043 if (cs.get("comment") != null) { 044 sb.append("<strong>").append(tr("Changeset comment:")).append("</strong>").append(cs.get("comment")).append("<br>"); 045 } 046 return sb.toString(); 047 } 048 049 @Override 050 public Component getListCellRendererComponent(JList<? extends Changeset> list, Changeset cs, int index, boolean isSelected, boolean cellHasFocus) { 051 if (isSelected) { 052 setForeground(UIManager.getColor("List.selectionForeground")); 053 setBackground(UIManager.getColor("List.selectionBackground")); 054 } else { 055 setForeground(UIManager.getColor("List.foreground")); 056 setBackground(UIManager.getColor("List.background")); 057 } 058 if (cs != null) { 059 setIcon(icon); 060 StringBuilder sb = new StringBuilder(); 061 if (cs.get("comment") != null) { 062 sb.append(cs.getId()).append(" - ").append(cs.get("comment")); 063 } else if (cs.get("name") != null) { 064 sb.append(cs.getId()).append(" - ").append(cs.get("name")); 065 } else { 066 sb.append(tr("Changeset {0}", cs.getId())); 067 } 068 setText(sb.toString()); 069 setToolTipText(buildToolTipText(cs)); 070 } else { 071 setText(tr("No open changeset")); 072 } 073 return this; 074 } 075}