[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.tagging;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Component;
|
---|
| 5 |
|
---|
| 6 | import javax.swing.AbstractCellEditor;
|
---|
[4215] | 7 | import javax.swing.BorderFactory;
|
---|
[2512] | 8 | import javax.swing.JTable;
|
---|
| 9 | import javax.swing.table.TableCellEditor;
|
---|
| 10 |
|
---|
[3210] | 11 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
[2512] | 12 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
[3210] | 13 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
[2512] | 14 |
|
---|
| 15 | /**
|
---|
| 16 | * This is the table cell editor for the tag editor dialog.
|
---|
| 17 | *
|
---|
| 18 | */
|
---|
[9059] | 19 | public class TagCellEditor extends AbstractCellEditor implements TableCellEditor {
|
---|
[2512] | 20 |
|
---|
[8840] | 21 | protected AutoCompletingTextField editor;
|
---|
| 22 | protected transient TagModel currentTag;
|
---|
[2512] | 23 |
|
---|
| 24 | /** the cache of auto completion items derived from the current JOSM data set */
|
---|
[8840] | 25 | protected transient AutoCompletionManager autocomplete;
|
---|
[2512] | 26 |
|
---|
| 27 | /** user input is matched against this list of auto completion items */
|
---|
[8840] | 28 | protected AutoCompletionList autoCompletionList;
|
---|
[2512] | 29 |
|
---|
| 30 | /**
|
---|
| 31 | * constructor
|
---|
[8760] | 32 | * @param maxCharacters maximum number of characters allowed, 0 for unlimited
|
---|
[2512] | 33 | */
|
---|
[8760] | 34 | public TagCellEditor(final int maxCharacters) {
|
---|
[8005] | 35 | editor = new AutoCompletingTextField(0, false);
|
---|
[8760] | 36 | if (maxCharacters > 0) {
|
---|
| 37 | editor.setMaxChars(maxCharacters);
|
---|
| 38 | }
|
---|
[4215] | 39 | editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
|
---|
[2512] | 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
|
---|
[2711] | 46 | * values from the current JOSM data set. Keys already present in the
|
---|
[2512] | 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 | //
|
---|
[3210] | 60 | autocomplete.populateWithKeys(autoCompletionList);
|
---|
[2512] | 61 |
|
---|
| 62 | // remove the keys already present in the current tag model
|
---|
| 63 | //
|
---|
| 64 | for (String key : model.getKeys()) {
|
---|
[8444] | 65 | if (!key.equals(currentTag.getName())) {
|
---|
[2512] | 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 | }
|
---|
[3210] | 84 | autoCompletionList.clear();
|
---|
| 85 | autocomplete.populateWithTagValues(autoCompletionList, forKey);
|
---|
[2512] | 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * replies the table cell editor
|
---|
| 90 | */
|
---|
[6084] | 91 | @Override
|
---|
[2512] | 92 | public Component getTableCellEditorComponent(JTable table,
|
---|
| 93 | Object value, boolean isSelected, int row, int column) {
|
---|
| 94 | currentTag = (TagModel) value;
|
---|
| 95 |
|
---|
[4092] | 96 | // no autocompletion for initial editor#setText()
|
---|
[8510] | 97 | if (autoCompletionList != null) {
|
---|
[4092] | 98 | autoCompletionList.clear();
|
---|
| 99 | }
|
---|
[2512] | 100 | if (column == 0) {
|
---|
| 101 | editor.setText(currentTag.getName());
|
---|
[8510] | 102 | TagEditorModel model = (TagEditorModel) table.getModel();
|
---|
[2512] | 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 |
|
---|
[6084] | 121 | @Override
|
---|
[2512] | 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 |
|
---|
[3210] | 144 | public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
|
---|
| 145 | this.autocomplete = autocomplete;
|
---|
[2512] | 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 | }
|
---|