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.DataSet; 013import org.openstreetmap.josm.data.osm.Relation; 014import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 015import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 016import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 017 018/** 019 * The {@link CellEditor} for the role cell in the table. Supports autocompletion. 020 */ 021public class MemberRoleCellEditor extends AbstractCellEditor implements TableCellEditor { 022 private final AutoCompletingTextField editor; 023 private final transient DataSet ds; 024 private final transient Relation relation; 025 026 /** user input is matched against this list of auto completion items */ 027 private final AutoCompletionList autoCompletionList; 028 029 /** 030 * Constructs a new {@code MemberRoleCellEditor}. 031 * @param ds the data set. Must not be null 032 * @param relation the relation. Can be null 033 */ 034 public MemberRoleCellEditor(DataSet ds, Relation relation) { 035 this.ds = ds; 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.of(ds).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}