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;
017
018/**
019 * A {@link ListCellRenderer} for the list of changesets in the upload dialog.
020 *
021 *
022 */
023public class ChangesetCellRenderer extends JLabel implements ListCellRenderer {
024    private ImageIcon icon ;
025    public ChangesetCellRenderer() {
026        icon = ImageProvider.get("data", "changeset");
027        setOpaque(true);
028    }
029
030    protected String buildToolTipText(Changeset cs) {
031        StringBuilder sb = new StringBuilder();
032        sb.append("<html>");
033        sb.append("<strong>").append(tr("Changeset id:")).append("</strong>").append(cs.getId()).append("<br>");
034        if (cs.getCreatedAt() != null) {
035            sb.append("<strong>").append(tr("Created at:")).append("</strong>").append(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(cs.getCreatedAt())).append("<br>");
036        }
037        if (cs.get("comment") != null) {
038            sb.append("<strong>").append(tr("Changeset comment:")).append("</strong>").append(cs.get("comment")).append("<br>");
039        }
040        return sb.toString();
041    }
042
043    @Override
044    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
045            boolean cellHasFocus) {
046        Changeset cs = (Changeset)value;
047        if (isSelected) {
048            setForeground(UIManager.getColor("List.selectionForeground"));
049            setBackground(UIManager.getColor("List.selectionBackground"));
050        } else {
051            setForeground(UIManager.getColor("List.foreground"));
052            setBackground(UIManager.getColor("List.background"));
053        }
054        if (cs != null) {
055            setIcon(icon);
056            StringBuffer sb = new StringBuffer();
057            if (cs.get("comment") != null) {
058                sb.append(cs.getId()).append(" - ").append(cs.get("comment"));
059            } else if (cs.get("name") != null) {
060                sb.append(cs.getId()).append(" - ").append(cs.get("name"));
061            } else {
062                sb.append(tr("Changeset {0}", cs.getId()));
063            }
064            setText(sb.toString());
065            setToolTipText(buildToolTipText(cs));
066        } else {
067            setText(tr("No open changeset"));
068        }
069        return this;
070    }
071}