001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation;
003
004import java.awt.Component;
005
006import javax.swing.AbstractCellEditor;
007import javax.swing.BorderFactory;
008import javax.swing.CellEditor;
009import javax.swing.JTable;
010import javax.swing.table.TableCellEditor;
011
012import org.openstreetmap.josm.data.osm.Relation;
013import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
014import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
015import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
016
017/**
018 * The {@link CellEditor} for the role cell in the table. Supports autocompletion.
019 */
020public class MemberRoleCellEditor extends AbstractCellEditor implements TableCellEditor {
021    private final AutoCompletingTextField editor;
022    private final AutoCompletionManager autoCompletionManager;
023    private final transient Relation relation;
024
025    /** user input is matched against this list of auto completion items */
026    private final AutoCompletionList autoCompletionList;
027
028    /**
029     * Constructs a new {@code MemberRoleCellEditor}.
030     * @param autoCompletionManager the auto completion manager. Must not be null
031     * @param relation the relation. Can be null
032     * @since 13675
033     */
034    public MemberRoleCellEditor(AutoCompletionManager autoCompletionManager, Relation relation) {
035        this.autoCompletionManager = autoCompletionManager;
036        this.relation = relation;
037        editor = new AutoCompletingTextField(0, false);
038        editor.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
039        autoCompletionList = new AutoCompletionList();
040        editor.setAutoCompletionList(autoCompletionList);
041    }
042
043    @Override
044    public Component getTableCellEditorComponent(JTable table,
045            Object value, boolean isSelected, int row, int column) {
046
047        String role = (String) value;
048        editor.setText(role);
049        autoCompletionList.clear();
050        autoCompletionManager.populateWithMemberRoles(autoCompletionList, relation);
051        return editor;
052    }
053
054    @Override
055    public Object getCellEditorValue() {
056        return editor.getText();
057    }
058
059    /**
060     * Returns the edit field for this cell editor.
061     * @return the edit field for this cell editor
062     */
063    public AutoCompletingTextField getEditor() {
064        return editor;
065    }
066}