| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Component;
|
|---|
| 8 | import java.awt.Font;
|
|---|
| 9 | import java.text.DateFormat;
|
|---|
| 10 | import java.util.Date;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JLabel;
|
|---|
| 13 | import javax.swing.JTable;
|
|---|
| 14 | import javax.swing.UIManager;
|
|---|
| 15 | import javax.swing.table.TableCellRenderer;
|
|---|
| 16 |
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.User;
|
|---|
| 19 | import org.openstreetmap.josm.tools.date.DateUtils;
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * The cell renderer for the changeset table
|
|---|
| 23 | * @since 2689
|
|---|
| 24 | */
|
|---|
| 25 | public class ChangesetCacheTableCellRenderer extends JLabel implements TableCellRenderer{
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Constructs a new {@code ChangesetCacheTableCellRenderer}.
|
|---|
| 29 | */
|
|---|
| 30 | public ChangesetCacheTableCellRenderer() {
|
|---|
| 31 | setOpaque(true);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | protected void reset() {
|
|---|
| 35 | setBackground(UIManager.getColor("Table.background"));
|
|---|
| 36 | setForeground(UIManager.getColor("Table.foreground"));
|
|---|
| 37 | setFont(UIManager.getFont("Table.font"));
|
|---|
| 38 | setToolTipText("");
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | protected void renderColors(boolean isSelected) {
|
|---|
| 42 | if (isSelected) {
|
|---|
| 43 | setBackground(UIManager.getColor("Table.selectionBackground"));
|
|---|
| 44 | setForeground(UIManager.getColor("Table.selectionForeground"));
|
|---|
| 45 | } else {
|
|---|
| 46 | setBackground(UIManager.getColor("Table.background"));
|
|---|
| 47 | setForeground(UIManager.getColor("Table.foreground"));
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | protected void renderId(Changeset cs) {
|
|---|
| 52 | setText(Integer.toString(cs.getId()));
|
|---|
| 53 | setToolTipText("");
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | protected void renderUploadComment(Changeset cs) {
|
|---|
| 57 | String comment = cs.get("comment");
|
|---|
| 58 | if (comment == null || comment.trim().isEmpty()) {
|
|---|
| 59 | setText(trc("changeset.upload-comment", "empty"));
|
|---|
| 60 | setFont(UIManager.getFont("Table.font").deriveFont(Font.ITALIC));
|
|---|
| 61 | } else {
|
|---|
| 62 | setText(comment);
|
|---|
| 63 | setToolTipText(comment);
|
|---|
| 64 | setFont(UIManager.getFont("Table.font"));
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | protected void renderOpen(Changeset cs) {
|
|---|
| 69 | if (cs.isOpen()) {
|
|---|
| 70 | setText(trc("changeset.state", "Open"));
|
|---|
| 71 | } else {
|
|---|
| 72 | setText(trc("changeset.state", "Closed"));
|
|---|
| 73 | }
|
|---|
| 74 | setToolTipText("");
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | protected void renderUser(Changeset cs) {
|
|---|
| 78 | User user = cs.getUser();
|
|---|
| 79 | if (user == null || user.getName().trim().isEmpty()) {
|
|---|
| 80 | setFont(UIManager.getFont("Table.font").deriveFont(Font.ITALIC));
|
|---|
| 81 | setText(tr("anonymous"));
|
|---|
| 82 | } else {
|
|---|
| 83 | setFont(UIManager.getFont("Table.font"));
|
|---|
| 84 | setText(user.getName());
|
|---|
| 85 | setToolTipText(user.getName());
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | protected void renderDate(Date d) {
|
|---|
| 90 | if (d == null) {
|
|---|
| 91 | setText("");
|
|---|
| 92 | } else {
|
|---|
| 93 | setText(DateUtils.formatDateTime(d, DateFormat.SHORT, DateFormat.SHORT));
|
|---|
| 94 | }
|
|---|
| 95 | setToolTipText("");
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Override
|
|---|
| 99 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
|---|
| 100 | int row, int column) {
|
|---|
| 101 | if (value == null)
|
|---|
| 102 | return this;
|
|---|
| 103 | reset();
|
|---|
| 104 | renderColors(isSelected);
|
|---|
| 105 | Changeset cs = (Changeset)value;
|
|---|
| 106 | switch(column) {
|
|---|
| 107 | case 0: /* id */ renderId(cs); break;
|
|---|
| 108 | case 1: /* upload comment */ renderUploadComment(cs); break;
|
|---|
| 109 | case 2: /* open/closed */ renderOpen(cs); break;
|
|---|
| 110 | case 3: /* user */ renderUser(cs); break;
|
|---|
| 111 | case 4: /* created at */ renderDate(cs.getCreatedAt()); break;
|
|---|
| 112 | case 5: /* closed at */ renderDate(cs.getClosedAt()); break;
|
|---|
| 113 | }
|
|---|
| 114 | return this;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|