001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.io;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.awt.Component;
007 import java.text.DateFormat;
008
009 import javax.swing.ImageIcon;
010 import javax.swing.JLabel;
011 import javax.swing.JList;
012 import javax.swing.ListCellRenderer;
013 import javax.swing.UIManager;
014
015 import org.openstreetmap.josm.data.osm.Changeset;
016 import org.openstreetmap.josm.tools.ImageProvider;
017
018 /**
019 * A {@link ListCellRenderer} for the list of changesets in the upload dialog.
020 *
021 *
022 */
023 public 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 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
044 boolean cellHasFocus) {
045 Changeset cs = (Changeset)value;
046 if (isSelected) {
047 setForeground(UIManager.getColor("List.selectionForeground"));
048 setBackground(UIManager.getColor("List.selectionBackground"));
049 } else {
050 setForeground(UIManager.getColor("List.foreground"));
051 setBackground(UIManager.getColor("List.background"));
052 }
053 if (cs != null) {
054 setIcon(icon);
055 StringBuffer sb = new StringBuffer();
056 if (cs.get("comment") != null) {
057 sb.append(cs.getId()).append(" - ").append(cs.get("comment"));
058 } else if (cs.get("name") != null) {
059 sb.append(cs.getId()).append(" - ").append(cs.get("name"));
060 } else {
061 sb.append(tr("Changeset {0}", cs.getId()));
062 }
063 setText(sb.toString());
064 setToolTipText(buildToolTipText(cs));
065 } else {
066 setText(tr("No open changeset"));
067 }
068 return this;
069 }
070 }