source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/TagCellEditor.java@ 1762

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