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

Last change on this file since 9059 was 9059, checked in by Don-vip, 8 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 5.1 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 */
19public class TagCellEditor extends AbstractCellEditor implements TableCellEditor {
20
21 protected AutoCompletingTextField editor;
22 protected transient TagModel currentTag;
23
24 /** the cache of auto completion items derived from the current JOSM data set */
25 protected transient AutoCompletionManager autocomplete;
26
27 /** user input is matched against this list of auto completion items */
28 protected AutoCompletionList autoCompletionList;
29
30 /**
31 * constructor
32 * @param maxCharacters maximum number of characters allowed, 0 for unlimited
33 */
34 public TagCellEditor(final int maxCharacters) {
35 editor = new AutoCompletingTextField(0, false);
36 if (maxCharacters > 0) {
37 editor.setMaxChars(maxCharacters);
38 }
39 editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
40 }
41
42 /**
43 * initializes the auto completion list when the table cell editor starts
44 * to edit the key of a tag. In this case the auto completion list is
45 * initialized with the set of standard key values and the set of current key
46 * values from the current JOSM data set. Keys already present in the
47 * current tag model are removed from the auto completion list.
48 *
49 * @param model the tag editor model
50 * @param currentTag the current tag
51 */
52 protected void initAutoCompletionListForKeys(TagEditorModel model, TagModel currentTag) {
53
54 if (autoCompletionList == null)
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 return;
83 }
84 autoCompletionList.clear();
85 autocomplete.populateWithTagValues(autoCompletionList, forKey);
86 }
87
88 /**
89 * replies the table cell editor
90 */
91 @Override
92 public Component getTableCellEditorComponent(JTable table,
93 Object value, boolean isSelected, int row, int column) {
94 currentTag = (TagModel) value;
95
96 // no autocompletion for initial editor#setText()
97 if (autoCompletionList != null) {
98 autoCompletionList.clear();
99 }
100 if (column == 0) {
101 editor.setText(currentTag.getName());
102 TagEditorModel model = (TagEditorModel) table.getModel();
103 initAutoCompletionListForKeys(model, currentTag);
104 return editor;
105 } else if (column == 1) {
106
107 if (currentTag.getValueCount() == 0) {
108 editor.setText("");
109 } else if (currentTag.getValueCount() == 1) {
110 editor.setText(currentTag.getValues().get(0));
111 } else {
112 editor.setText("");
113 }
114 initAutoCompletionListForValues(currentTag.getName());
115 return editor;
116 } else {
117 return null;
118 }
119 }
120
121 @Override
122 public Object getCellEditorValue() {
123 return editor.getText();
124 }
125
126 /**
127 * replies the {@link AutoCompletionList} this table cell editor synchronizes with
128 *
129 * @return the auto completion list
130 */
131 public AutoCompletionList getAutoCompletionList() {
132 return autoCompletionList;
133 }
134
135 /**
136 * sets the {@link AutoCompletionList} this table cell editor synchronizes with
137 * @param autoCompletionList the auto completion list
138 */
139 public void setAutoCompletionList(AutoCompletionList autoCompletionList) {
140 this.autoCompletionList = autoCompletionList;
141 editor.setAutoCompletionList(autoCompletionList);
142 }
143
144 public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
145 this.autocomplete = autocomplete;
146 }
147
148 public void autoCompletionItemSelected(String item) {
149 editor.setText(item);
150 editor.selectAll();
151 editor.requestFocus();
152 }
153
154 public AutoCompletingTextField getEditor() {
155 return editor;
156 }
157}
Note: See TracBrowser for help on using the repository browser.