001package jmri.util.table;
002
003// This was adapted from Core Swing Advanced Programming, Prentice Hall
004// Changes:  Change package
005import java.beans.PropertyChangeEvent;
006import java.beans.PropertyChangeListener;
007import java.util.EventObject;
008import javax.swing.CellEditor;
009import javax.swing.JComponent;
010import javax.swing.event.CellEditorListener;
011import javax.swing.event.ChangeEvent;
012import javax.swing.event.EventListenerList;
013
014public class BasicCellEditor implements CellEditor,
015        PropertyChangeListener {
016
017    public BasicCellEditor() {
018        this.editor = null;
019    }
020
021    public BasicCellEditor(JComponent editor) {
022        this.editor = editor;
023        editor.addPropertyChangeListener(this);
024    }
025
026    @Override
027    public Object getCellEditorValue() {
028        return null;
029    }
030
031    @Override
032    public boolean isCellEditable(EventObject evt) {
033        editingEvent = evt;
034        return true;
035    }
036
037    @Override
038    public boolean shouldSelectCell(EventObject evt) {
039        return true;
040    }
041
042    @Override
043    public boolean stopCellEditing() {
044        fireEditingStopped();
045        return true;
046    }
047
048    @Override
049    public void cancelCellEditing() {
050        fireEditingCanceled();
051    }
052
053    @Override
054    public void addCellEditorListener(CellEditorListener l) {
055        listeners.add(CellEditorListener.class, l);
056    }
057
058    @Override
059    public void removeCellEditorListener(CellEditorListener l) {
060        listeners.remove(CellEditorListener.class, l);
061    }
062
063    // Returns the editing component
064    public JComponent getComponent() {
065        return editor;
066    }
067
068    // Sets the editing component
069    public void setComponent(JComponent comp) {
070        editor = comp;
071    }
072
073    // Returns the event that triggered the edit
074    public EventObject getEditingEvent() {
075        return editingEvent;
076    }
077
078    // Method invoked when the editor is installed in the table.
079    // Overridden in derived classes to take any convenient
080    // action.
081    public void editingStarted(EventObject event) {
082    }
083
084    protected void fireEditingStopped() {
085        Object[] l = listeners.getListenerList();
086        for (int i = l.length - 2; i >= 0; i -= 2) {
087            if (l[i] == CellEditorListener.class) {
088                if (changeEvent == null) {
089                    changeEvent = new ChangeEvent(this);
090                }
091                ((CellEditorListener) l[i + 1]).editingStopped(changeEvent);
092            }
093        }
094    }
095
096    protected void fireEditingCanceled() {
097        Object[] l = listeners.getListenerList();
098        for (int i = l.length - 2; i >= 0; i -= 2) {
099            if (l[i] == CellEditorListener.class) {
100                if (changeEvent == null) {
101                    changeEvent = new ChangeEvent(this);
102                }
103                ((CellEditorListener) l[i + 1]).editingCanceled(changeEvent);
104            }
105        }
106    }
107
108    // Implementation of the PropertyChangeListener interface
109    @Override
110    public void propertyChange(PropertyChangeEvent evt) {
111        if (evt.getPropertyName().equals("ancestor")
112                && evt.getNewValue() != null) {
113            // Added to table - notify the editor
114            editingStarted(editingEvent);
115        }
116    }
117
118    static ChangeEvent changeEvent;
119    protected JComponent editor;
120    protected EventListenerList listeners = new EventListenerList();
121    protected EventObject editingEvent;
122}