| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Component;
|
|---|
| 5 |
|
|---|
| 6 | import javax.swing.AbstractCellEditor;
|
|---|
| 7 | import javax.swing.BorderFactory;
|
|---|
| 8 | import javax.swing.CellEditor;
|
|---|
| 9 | import javax.swing.JTable;
|
|---|
| 10 | import javax.swing.table.TableCellEditor;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 13 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
|---|
| 14 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
|---|
| 15 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * The {@link CellEditor} for the role cell in the table. Supports autocompletion.
|
|---|
| 19 | */
|
|---|
| 20 | public class MemberRoleCellEditor extends AbstractCellEditor implements TableCellEditor {
|
|---|
| 21 | private final AutoCompletingTextField editor;
|
|---|
| 22 | private final AutoCompletionManager autoCompletionManager;
|
|---|
| 23 | private final transient Relation relation;
|
|---|
| 24 |
|
|---|
| 25 | /** user input is matched against this list of auto completion items */
|
|---|
| 26 | private final AutoCompletionList autoCompletionList;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Constructs a new {@code MemberRoleCellEditor}.
|
|---|
| 30 | * @param autoCompletionManager the auto completion manager. Must not be null
|
|---|
| 31 | * @param relation the relation. Can be null
|
|---|
| 32 | * @since 13675
|
|---|
| 33 | */
|
|---|
| 34 | public MemberRoleCellEditor(AutoCompletionManager autoCompletionManager, Relation relation) {
|
|---|
| 35 | this.autoCompletionManager = autoCompletionManager;
|
|---|
| 36 | this.relation = relation;
|
|---|
| 37 | editor = new AutoCompletingTextField(0, false);
|
|---|
| 38 | editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
|
|---|
| 39 | autoCompletionList = new AutoCompletionList();
|
|---|
| 40 | editor.setAutoCompletionList(autoCompletionList);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override
|
|---|
| 44 | public Component getTableCellEditorComponent(JTable table,
|
|---|
| 45 | Object value, boolean isSelected, int row, int column) {
|
|---|
| 46 |
|
|---|
| 47 | String role = (String) value;
|
|---|
| 48 | editor.setText(role);
|
|---|
| 49 | autoCompletionList.clear();
|
|---|
| 50 | autoCompletionManager.populateWithMemberRoles(autoCompletionList, relation);
|
|---|
| 51 | return editor;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | public Object getCellEditorValue() {
|
|---|
| 56 | return editor.getText();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Returns the edit field for this cell editor.
|
|---|
| 61 | * @return the edit field for this cell editor
|
|---|
| 62 | */
|
|---|
| 63 | public AutoCompletingTextField getEditor() {
|
|---|
| 64 | return editor;
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|