1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 |
|
---|
6 | import javax.swing.AbstractCellEditor;
|
---|
7 | import javax.swing.BorderFactory;
|
---|
8 | import javax.swing.JTable;
|
---|
9 | import javax.swing.table.TableCellEditor;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
12 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
13 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This is the table cell editor for the tag editor dialog.
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class TagCellEditor extends AbstractCellEditor implements TableCellEditor{
|
---|
20 |
|
---|
21 | protected AutoCompletingTextField editor = null;
|
---|
22 | protected transient TagModel currentTag = null;
|
---|
23 |
|
---|
24 | /** the cache of auto completion items derived from the current JOSM data set */
|
---|
25 | protected transient AutoCompletionManager autocomplete = null;
|
---|
26 |
|
---|
27 | /** user input is matched against this list of auto completion items */
|
---|
28 | protected AutoCompletionList autoCompletionList = null;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * constructor
|
---|
32 | */
|
---|
33 | public TagCellEditor() {
|
---|
34 | editor = new AutoCompletingTextField(0, false);
|
---|
35 | editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * initializes the auto completion list when the table cell editor starts
|
---|
40 | * to edit the key of a tag. In this case the auto completion list is
|
---|
41 | * initialized with the set of standard key values and the set of current key
|
---|
42 | * values from the current JOSM data set. Keys already present in the
|
---|
43 | * current tag model are removed from the auto completion list.
|
---|
44 | *
|
---|
45 | * @param model the tag editor model
|
---|
46 | * @param currentTag the current tag
|
---|
47 | */
|
---|
48 | protected void initAutoCompletionListForKeys(TagEditorModel model, TagModel currentTag) {
|
---|
49 |
|
---|
50 | if (autoCompletionList == null)
|
---|
51 | return;
|
---|
52 | autoCompletionList.clear();
|
---|
53 |
|
---|
54 | // add the list of keys in the current data set
|
---|
55 | //
|
---|
56 | autocomplete.populateWithKeys(autoCompletionList);
|
---|
57 |
|
---|
58 | // remove the keys already present in the current tag model
|
---|
59 | //
|
---|
60 | for (String key : model.getKeys()) {
|
---|
61 | if (!key.equals(currentTag.getName())) {
|
---|
62 | autoCompletionList.remove(key);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | autoCompletionList.fireTableDataChanged();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * initializes the auto completion list when the cell editor starts to edit
|
---|
70 | * a tag value. In this case the auto completion list is initialized with the
|
---|
71 | * set of standard values for a given key and the set of values present in the
|
---|
72 | * current data set for the given key.
|
---|
73 | *
|
---|
74 | * @param forKey the key
|
---|
75 | */
|
---|
76 | protected void initAutoCompletionListForValues(String forKey) {
|
---|
77 | if (autoCompletionList == null) {
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | autoCompletionList.clear();
|
---|
81 | autocomplete.populateWithTagValues(autoCompletionList, forKey);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * replies the table cell editor
|
---|
86 | */
|
---|
87 | @Override
|
---|
88 | public Component getTableCellEditorComponent(JTable table,
|
---|
89 | Object value, boolean isSelected, int row, int column) {
|
---|
90 | currentTag = (TagModel) value;
|
---|
91 |
|
---|
92 | // no autocompletion for initial editor#setText()
|
---|
93 | if(autoCompletionList != null) {
|
---|
94 | autoCompletionList.clear();
|
---|
95 | }
|
---|
96 | if (column == 0) {
|
---|
97 | editor.setText(currentTag.getName());
|
---|
98 | TagEditorModel model = (TagEditorModel)table.getModel();
|
---|
99 | initAutoCompletionListForKeys(model, currentTag);
|
---|
100 | return editor;
|
---|
101 | } else if (column == 1) {
|
---|
102 |
|
---|
103 | if (currentTag.getValueCount() == 0) {
|
---|
104 | editor.setText("");
|
---|
105 | } else if (currentTag.getValueCount() == 1) {
|
---|
106 | editor.setText(currentTag.getValues().get(0));
|
---|
107 | } else {
|
---|
108 | editor.setText("");
|
---|
109 | }
|
---|
110 | initAutoCompletionListForValues(currentTag.getName());
|
---|
111 | return editor;
|
---|
112 | } else {
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public Object getCellEditorValue() {
|
---|
119 | return editor.getText();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * replies the {@link AutoCompletionList} this table cell editor synchronizes with
|
---|
124 | *
|
---|
125 | * @return the auto completion list
|
---|
126 | */
|
---|
127 | public AutoCompletionList getAutoCompletionList() {
|
---|
128 | return autoCompletionList;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * sets the {@link AutoCompletionList} this table cell editor synchronizes with
|
---|
133 | * @param autoCompletionList the auto completion list
|
---|
134 | */
|
---|
135 | public void setAutoCompletionList(AutoCompletionList autoCompletionList) {
|
---|
136 | this.autoCompletionList = autoCompletionList;
|
---|
137 | editor.setAutoCompletionList(autoCompletionList);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
|
---|
141 | this.autocomplete = autocomplete;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void autoCompletionItemSelected(String item) {
|
---|
145 | editor.setText(item);
|
---|
146 | editor.selectAll();
|
---|
147 | editor.requestFocus();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public AutoCompletingTextField getEditor() {
|
---|
151 | return editor;
|
---|
152 | }
|
---|
153 | }
|
---|