source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/TagCellRenderer.java@ 1916

Last change on this file since 1916 was 1762, checked in by Gubaer, 15 years ago

added: improved tag editor grid in relation editor (borrowed code from tag editor plugin)
removed: realEqual() on OsmPrimitive, Node, etc.

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