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

Last change on this file since 4215 was 4215, checked in by stoecker, 13 years ago

fix #6547 - patch by Hojoe - reduce space usage in relation editor

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging;
3
4import java.awt.Component;
5
6import javax.swing.AbstractCellEditor;
7import javax.swing.BorderFactory;
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 protected AutoCompletingTextField editor = null;
23 protected TagModel currentTag = null;
24
25 /** the cache of auto completion items derived from the current JOSM data set */
26 protected AutoCompletionManager autocomplete = null;
27
28 /** user input is matched against this list of auto completion items */
29 protected AutoCompletionList autoCompletionList = null;
30
31 /**
32 * constructor
33 */
34 public TagCellEditor() {
35 editor = new AutoCompletingTextField();
36 editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
37 }
38
39 /**
40 * initializes the auto completion list when the table cell editor starts
41 * to edit the key of a tag. In this case the auto completion list is
42 * initialized with the set of standard key values and the set of current key
43 * values from the current JOSM data set. Keys already present in the
44 * current tag model are removed from the auto completion list.
45 *
46 * @param model the tag editor model
47 * @param currentTag the current tag
48 */
49 protected void initAutoCompletionListForKeys(TagEditorModel model, TagModel currentTag) {
50
51 if (autoCompletionList == null)
52 return;
53 autoCompletionList.clear();
54
55 // add the list of keys in the current data set
56 //
57 autocomplete.populateWithKeys(autoCompletionList);
58
59 // remove the keys already present in the current tag model
60 //
61 for (String key : model.getKeys()) {
62 if (! key.equals(currentTag.getName())) {
63 autoCompletionList.remove(key);
64 }
65 }
66 autoCompletionList.fireTableDataChanged();
67 }
68
69 /**
70 * initializes the auto completion list when the cell editor starts to edit
71 * a tag value. In this case the auto completion list is initialized with the
72 * set of standard values for a given key and the set of values present in the
73 * current data set for the given key.
74 *
75 * @param forKey the key
76 */
77 protected void initAutoCompletionListForValues(String forKey) {
78 if (autoCompletionList == null) {
79 return;
80 }
81 autoCompletionList.clear();
82 autocomplete.populateWithTagValues(autoCompletionList, forKey);
83 }
84
85 /**
86 * replies the table cell editor
87 */
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 public Object getCellEditorValue() {
118 return editor.getText();
119 }
120
121 /**
122 * replies the {@link AutoCompletionList} this table cell editor synchronizes with
123 *
124 * @return the auto completion list
125 */
126 public AutoCompletionList getAutoCompletionList() {
127 return autoCompletionList;
128 }
129
130 /**
131 * sets the {@link AutoCompletionList} this table cell editor synchronizes with
132 * @param autoCompletionList the auto completion list
133 */
134 public void setAutoCompletionList(AutoCompletionList autoCompletionList) {
135 this.autoCompletionList = autoCompletionList;
136 editor.setAutoCompletionList(autoCompletionList);
137 }
138
139 public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
140 this.autocomplete = autocomplete;
141 }
142
143 public void autoCompletionItemSelected(String item) {
144 editor.setText(item);
145 editor.selectAll();
146 editor.requestFocus();
147 }
148
149 public AutoCompletingTextField getEditor() {
150 return editor;
151 }
152}
Note: See TracBrowser for help on using the repository browser.