source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/TagCellEditor.java@ 3779

Last change on this file since 3779 was 3210, checked in by bastiK, 14 years ago

autocompletion rework; breaks tageditor plugin and possibly other plugins
(merged the 2 autocompletion systems; adds presets values for the properties dialog )

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
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.tagging.ac.AutoCompletingTextField;
12import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
13import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
14
15/**
16 * This is the table cell editor for the tag editor dialog.
17 *
18 */
19@SuppressWarnings("serial")
20public class TagCellEditor extends AbstractCellEditor implements TableCellEditor{
21
22 /** the logger object */
23 static private Logger logger = Logger.getLogger(TagCellEditor.class.getName());
24
25 protected AutoCompletingTextField editor = null;
26 protected TagModel currentTag = null;
27
28 /** the cache of auto completion items derived from the current JOSM data set */
29 protected AutoCompletionManager autocomplete = null;
30
31 /** user input is matched against this list of auto completion items */
32 protected AutoCompletionList autoCompletionList = null;
33
34 /**
35 * constructor
36 */
37 public TagCellEditor() {
38 editor = new AutoCompletingTextField();
39 }
40
41 /**
42 * initializes the auto completion list when the table cell editor starts
43 * to edit the key of a tag. In this case the auto completion list is
44 * initialized with the set of standard key values and the set of current key
45 * values from the current JOSM data set. Keys already present in the
46 * current tag model are removed from the auto completion list.
47 *
48 * @param model the tag editor model
49 * @param currentTag the current tag
50 */
51 protected void initAutoCompletionListForKeys(TagEditorModel model, TagModel currentTag) {
52
53 if (autoCompletionList == null)
54 //logger.warning("autoCompletionList is null. Make sure an instance of AutoCompletionList is injected into TableCellEditor.");
55 return;
56 autoCompletionList.clear();
57
58 // add the list of keys in the current data set
59 //
60 autocomplete.populateWithKeys(autoCompletionList);
61
62 // remove the keys already present in the current tag model
63 //
64 for (String key : model.getKeys()) {
65 if (! key.equals(currentTag.getName())) {
66 autoCompletionList.remove(key);
67 }
68 }
69 autoCompletionList.fireTableDataChanged();
70 }
71
72 /**
73 * initializes the auto completion list when the cell editor starts to edit
74 * a tag value. In this case the auto completion list is initialized with the
75 * set of standard values for a given key and the set of values present in the
76 * current data set for the given key.
77 *
78 * @param forKey the key
79 */
80 protected void initAutoCompletionListForValues(String forKey) {
81 if (autoCompletionList == null) {
82 logger.warning("autoCompletionList is null. Make sure an instance of AutoCompletionList is injected into TableCellEditor.");
83 return;
84 }
85 autoCompletionList.clear();
86 autocomplete.populateWithTagValues(autoCompletionList, forKey);
87 }
88
89 /**
90 * replies the table cell editor
91 */
92 public Component getTableCellEditorComponent(JTable table,
93 Object value, boolean isSelected, int row, int column) {
94 currentTag = (TagModel) value;
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 logger.warning("column this table cell editor is requested for is out of range. column=" + column);
114 return null;
115 }
116 }
117
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}
Note: See TracBrowser for help on using the repository browser.