1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.util.logging.Logger;
|
---|
10 |
|
---|
11 | import javax.swing.BorderFactory;
|
---|
12 | import javax.swing.ImageIcon;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.border.Border;
|
---|
16 | import javax.swing.border.EmptyBorder;
|
---|
17 | import javax.swing.table.TableCellRenderer;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This is the table cell renderer for cells for the table of tags
|
---|
21 | * in the tag editor dialog.
|
---|
22 | *
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class TagCellRenderer extends JLabel implements TableCellRenderer {
|
---|
26 |
|
---|
27 | private static Logger logger = Logger.getLogger(TagCellRenderer.class.getName());
|
---|
28 |
|
---|
29 | public static final Color BG_COLOR_SELECTED = new Color(143,170,255);
|
---|
30 | public static final Color BG_COLOR_HIGHLIGHTED = new Color(255,255,204);
|
---|
31 |
|
---|
32 | public static final Border BORDER_EMPHASIZED = BorderFactory.createLineBorder(new Color(253,75,45));
|
---|
33 |
|
---|
34 | /** the icon displayed for deleting a tag */
|
---|
35 | private ImageIcon deleteIcon = null;
|
---|
36 |
|
---|
37 | private Font fontStandard = null;
|
---|
38 | private Font fontItalic = null;
|
---|
39 |
|
---|
40 | public TagCellRenderer() {
|
---|
41 | fontStandard = getFont();
|
---|
42 | fontItalic = fontStandard.deriveFont(Font.ITALIC);
|
---|
43 | setOpaque(true);
|
---|
44 | setBorder(new EmptyBorder(5,5,5,5));
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * renders the name of a tag in the second column of
|
---|
49 | * the table
|
---|
50 | *
|
---|
51 | * @param tag the tag
|
---|
52 | */
|
---|
53 | protected void renderTagName(TagModel tag) {
|
---|
54 | setText(tag.getName());
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * renders the value of a a tag in the third column of
|
---|
59 | * the table
|
---|
60 | *
|
---|
61 | * @param tag the tag
|
---|
62 | */
|
---|
63 | protected void renderTagValue(TagModel tag) {
|
---|
64 | if (tag.getValueCount() == 0) {
|
---|
65 | setText("");
|
---|
66 | } else if (tag.getValueCount() == 1) {
|
---|
67 | setText(tag.getValues().get(0));
|
---|
68 | } else if (tag.getValueCount() > 1) {
|
---|
69 | setText(tr("<multiple>"));
|
---|
70 | setFont(fontItalic);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * resets the renderer
|
---|
76 | */
|
---|
77 | protected void resetRenderer() {
|
---|
78 | setText("");
|
---|
79 | setIcon(null);
|
---|
80 | setFont(fontStandard);
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected TagEditorModel getModel(JTable table) {
|
---|
84 | return (TagEditorModel)table.getModel();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * renders the background color. The default color is white. It is
|
---|
89 | * set to {@see TableCellRenderer#BG_COLOR_HIGHLIGHTED} if this cell
|
---|
90 | * displays the tag which is suggested by the currently selected
|
---|
91 | * preset.
|
---|
92 | *
|
---|
93 | * @param tagModel the tag model
|
---|
94 | * @param model the tag editor model
|
---|
95 | */
|
---|
96 | protected void renderBackgroundColor(TagModel tagModel, TagEditorModel model) {
|
---|
97 | setBackground(Color.WHITE); // standard color
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * replies the cell renderer component for a specific cell
|
---|
102 | *
|
---|
103 | * @param table the table
|
---|
104 | * @param value the value to be rendered
|
---|
105 | * @param isSelected true, if the value is selected
|
---|
106 | * @param hasFocus true, if the cell has focus
|
---|
107 | * @param rowIndex the row index
|
---|
108 | * @param vColIndex the column index
|
---|
109 | *
|
---|
110 | * @return the renderer component
|
---|
111 | */
|
---|
112 | public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
113 | boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
|
---|
114 | resetRenderer();
|
---|
115 |
|
---|
116 | // set background color
|
---|
117 | //
|
---|
118 | if (isSelected){
|
---|
119 | setBackground(BG_COLOR_SELECTED);
|
---|
120 | } else {
|
---|
121 | renderBackgroundColor(getModel(table).get(rowIndex), getModel(table));
|
---|
122 | }
|
---|
123 |
|
---|
124 | switch(vColIndex) {
|
---|
125 | case 0: renderTagName((TagModel)value); break;
|
---|
126 | case 1: renderTagValue((TagModel)value); break;
|
---|
127 |
|
---|
128 | default: throw new RuntimeException("unexpected index in switch statement");
|
---|
129 | }
|
---|
130 | if (hasFocus && isSelected) {
|
---|
131 | if (table.getSelectedColumnCount() == 1 && table.getSelectedRowCount() == 1) {
|
---|
132 | boolean success = table.editCellAt(rowIndex, vColIndex);
|
---|
133 |
|
---|
134 | if (table.getEditorComponent() != null) {
|
---|
135 | table.getEditorComponent().requestFocusInWindow();
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | return this;
|
---|
140 | }
|
---|
141 | }
|
---|