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