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

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

new: Ctrl-DblClick in the relation list dialog opens the relation editor
new: Duplicate button in the relation list dialog
new: autocompletion on member roles in the member table
new: autocompletion on the member field for role assignments on multiple members
new: two-way synchronization between the selection in the map and the selection in the member table of the relation editor

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