1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.util.logging.Logger;
|
---|
6 |
|
---|
7 | import javax.swing.AbstractCellEditor;
|
---|
8 | import javax.swing.JTable;
|
---|
9 | import javax.swing.table.TableCellEditor;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
|
---|
12 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
|
---|
13 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
14 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * This is the table cell editor for the tag editor dialog.
|
---|
18 | *
|
---|
19 | */
|
---|
20 | @SuppressWarnings("serial")
|
---|
21 | public class TagCellEditor extends AbstractCellEditor implements TableCellEditor{
|
---|
22 |
|
---|
23 | /** the logger object */
|
---|
24 | static private Logger logger = Logger.getLogger(TagCellEditor.class.getName());
|
---|
25 |
|
---|
26 | private AutoCompletingTextField editor = null;
|
---|
27 | private TagModel currentTag = null;
|
---|
28 | private TagEditorModel tagEditorModel = null;
|
---|
29 | private int currentColumn = 0;
|
---|
30 |
|
---|
31 | /** the cache of auto completion items derived from the current JOSM data set */
|
---|
32 | private AutoCompletionCache acCache = null;
|
---|
33 |
|
---|
34 | /** user input is matched against this list of auto completion items */
|
---|
35 | private AutoCompletionList autoCompletionList = null;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * constructor
|
---|
39 | */
|
---|
40 | public TagCellEditor() {
|
---|
41 | editor = new AutoCompletingTextField();
|
---|
42 | acCache = new AutoCompletionCache();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * initializes the auto completion list when the table cell editor starts
|
---|
47 | * to edit the key of a tag. In this case the auto completion list is
|
---|
48 | * initialized with the set of standard key values and the set of current key
|
---|
49 | * values from the the current JOSM data set. Keys already present in the
|
---|
50 | * current tag model are removed from the auto completion list.
|
---|
51 | *
|
---|
52 | * @param model the tag editor model
|
---|
53 | * @param currentTag the current tag
|
---|
54 | */
|
---|
55 | protected void initAutoCompletionListForKeys(TagEditorModel model, TagModel currentTag) {
|
---|
56 |
|
---|
57 | if (autoCompletionList == null)
|
---|
58 | //logger.warning("autoCompletionList is null. Make sure an instance of AutoCompletionList is injected into TableCellEditor.");
|
---|
59 | return;
|
---|
60 | autoCompletionList.clear();
|
---|
61 |
|
---|
62 | // add the list of keys in the current data set
|
---|
63 | //
|
---|
64 | for (String key : acCache.getKeys()) {
|
---|
65 | autoCompletionList.add(
|
---|
66 | new AutoCompletionListItem(key, AutoCompletionItemPritority.IS_IN_DATASET)
|
---|
67 | );
|
---|
68 | }
|
---|
69 |
|
---|
70 | // remove the keys already present in the current tag model
|
---|
71 | //
|
---|
72 | for (String key : model.getKeys()) {
|
---|
73 | if (! key.equals(currentTag.getName())) {
|
---|
74 | autoCompletionList.remove(key);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | autoCompletionList.fireTableDataChanged();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * initializes the auto completion list when the cell editor starts to edit
|
---|
82 | * a tag value. In this case the auto completion list is initialized with the
|
---|
83 | * set of standard values for a given key and the set of values present in the
|
---|
84 | * current data set for the given key.
|
---|
85 | *
|
---|
86 | * @param forKey the key
|
---|
87 | */
|
---|
88 | protected void initAutoCompletionListForValues(String forKey) {
|
---|
89 | if (autoCompletionList == null) {
|
---|
90 | logger.warning("autoCompletionList is null. Make sure an instance of AutoCompletionList is injected into TableCellEditor.");
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | autoCompletionList.clear();
|
---|
94 | for (String value : acCache.getValues(forKey)) {
|
---|
95 | autoCompletionList.add(
|
---|
96 | new AutoCompletionListItem(value, AutoCompletionItemPritority.IS_IN_DATASET)
|
---|
97 | );
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * replies the table cell editor
|
---|
103 | */
|
---|
104 | public Component getTableCellEditorComponent(JTable table,
|
---|
105 | Object value, boolean isSelected, int row, int column) {
|
---|
106 | currentTag = (TagModel) value;
|
---|
107 |
|
---|
108 | if (column == 0) {
|
---|
109 | editor.setText(currentTag.getName());
|
---|
110 | currentColumn = 0;
|
---|
111 | TagEditorModel model = (TagEditorModel)table.getModel();
|
---|
112 | initAutoCompletionListForKeys(model, currentTag);
|
---|
113 | return editor;
|
---|
114 | } else if (column == 1) {
|
---|
115 |
|
---|
116 | if (currentTag.getValueCount() == 0) {
|
---|
117 | editor.setText("");
|
---|
118 | } else if (currentTag.getValueCount() == 1) {
|
---|
119 | editor.setText(currentTag.getValues().get(0));
|
---|
120 | } else {
|
---|
121 | editor.setText("");
|
---|
122 | }
|
---|
123 | currentColumn = 1;
|
---|
124 | initAutoCompletionListForValues(currentTag.getName());
|
---|
125 | return editor;
|
---|
126 | } else {
|
---|
127 | logger.warning("column this table cell editor is requested for is out of range. column=" + column);
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public Object getCellEditorValue() {
|
---|
133 | return editor.getText();
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public void cancelCellEditing() {
|
---|
138 | super.cancelCellEditing();
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public boolean stopCellEditing() {
|
---|
143 | if (tagEditorModel == null) {
|
---|
144 | logger.warning("no tag editor model set. Can't update edited values. Please set tag editor model first");
|
---|
145 | return super.stopCellEditing();
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (currentColumn == 0) {
|
---|
149 | tagEditorModel.updateTagName(currentTag, editor.getText());
|
---|
150 | } else if (currentColumn == 1){
|
---|
151 | if (currentTag.getValueCount() > 1 && ! editor.getText().equals("")) {
|
---|
152 | tagEditorModel.updateTagValue(currentTag, editor.getText());
|
---|
153 | } else if (currentTag.getValueCount() <= 1) {
|
---|
154 | tagEditorModel.updateTagValue(currentTag, editor.getText());
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return super.stopCellEditing();
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * replies the {@link AutoCompletionList} this table cell editor synchronizes with
|
---|
162 | *
|
---|
163 | * @return the auto completion list
|
---|
164 | */
|
---|
165 | public AutoCompletionList getAutoCompletionList() {
|
---|
166 | return autoCompletionList;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * sets the {@link AutoCompletionList} this table cell editor synchronizes with
|
---|
171 | * @param autoCompletionList the auto completion list
|
---|
172 | */
|
---|
173 | public void setAutoCompletionList(AutoCompletionList autoCompletionList) {
|
---|
174 | this.autoCompletionList = autoCompletionList;
|
---|
175 | editor.setAutoCompletionList(autoCompletionList);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public void setAutoCompletionCache(AutoCompletionCache acCache) {
|
---|
179 | this.acCache = acCache;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void autoCompletionItemSelected(String item) {
|
---|
183 | editor.setText(item);
|
---|
184 | editor.selectAll();
|
---|
185 | editor.requestFocus();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public AutoCompletingTextField getEditor() {
|
---|
189 | return editor;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * sets the tag editor model
|
---|
194 | *
|
---|
195 | * @param tagEditorModel the tag editor model
|
---|
196 | */
|
---|
197 | public void setTagEditorModel(TagEditorModel tagEditorModel) {
|
---|
198 | this.tagEditorModel = tagEditorModel;
|
---|
199 | }
|
---|
200 | }
|
---|