001package jmri.jmrit.beantable.light;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionEvent;
005import java.util.List;
006
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.Light;
011import jmri.LightControl;
012
013// import org.slf4j.Logger;
014// import org.slf4j.LoggerFactory;
015
016/**
017 * Pane to add / edit Light Controls for a new or given Light.
018 * <p>
019 * Light Control Table with new control / edit individual Control buttons.
020 * Uses separate JFrame to Edit a Single Control.
021 * <p>
022 * Defaults to No Light Controls for a New Light.
023 * <p>
024 * Code originally within LightTableAction.
025 * 
026 * @author Dave Duchamp Copyright (C) 2004
027 * @author Egbert Broerse Copyright (C) 2017
028 * @author Steve Young Copyright (C) 2021
029 */
030public class LightControlPane extends JPanel {
031
032    private LightControlTableModel lightControlTableModel;
033    private JButton addControl;
034    
035    /**
036     * Create a Panel for Light Controls.
037     * No Controls as default.
038     */
039    public LightControlPane(){
040        super();
041        init();
042    }
043    
044    /**
045     * Create a Panel for Light Controls.
046     * @param l Light to display Light Controls for.
047     */
048    public LightControlPane(Light l){
049        super();
050        init();
051        setToLight(l);
052    }
053    
054    private void init(){
055        
056        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
057        lightControlTableModel = new LightControlTableModel(this);
058        JTable lightControlTable = new JTable(lightControlTableModel);
059        
060        lightControlTableModel.configureJTable(lightControlTable);
061        lightControlTable.setPreferredScrollableViewportSize(new java.awt.Dimension(600, 100));
062        JScrollPane lightControlTableScrollPane = new JScrollPane(lightControlTable);
063        add(lightControlTableScrollPane);
064
065        addControl = new JButton(Bundle.getMessage("LightAddControlButton"));
066        addControl.addActionListener(this::addControlPressed);
067        addControl.setToolTipText(Bundle.getMessage("LightAddControlButtonHint"));
068        
069        JPanel panel35 = new JPanel();
070        panel35.setLayout(new FlowLayout());
071        panel35.add(addControl);
072        add(panel35);
073        
074    }
075    
076    /**
077     * Respond to pressing the Add Control button.
078     *
079     * @param e the event containing the press action
080     */
081    protected void addControlPressed(ActionEvent e) {
082        // Use separate Runnable so window is created on top
083        javax.swing.SwingUtilities.invokeLater(() -> {
084                addEditControlWindow(null);
085        });
086    }
087    
088    /**
089     * Respond to pressing the Update Control button in the New / Edit Control Frame.
090     *
091     * @param oldControl the LightControl to remove
092     * @param newControl the LightControl to add
093     */
094    protected void updateControlPressed(LightControl oldControl, LightControl newControl) {
095        lightControlTableModel.removeControl(oldControl);
096        lightControlTableModel.addControl(newControl);
097    }
098    
099    /**
100     * Set Controls from the Control Table to the Light.
101     * Removes any existing Light Controls on the Light.
102     * @param g Light to set Controls to.
103     */
104    public void setLightFromControlTable(Light g) {
105        g.deactivateLight();
106        g.clearLightControls(); // clear list on Light
107        getControlList().stream().map(control -> {
108            control.setParentLight(g);
109            return control;
110        }).forEachOrdered(control -> {
111            g.addLightControl(control);
112        });
113        g.activateLight();
114    }
115    
116    /**
117     * Respond to Edit button on row in the Light Control Table.
118     *
119     * @param lc the Light Control to edit.
120     */
121    protected void editControlAction(@Nonnull LightControl lc) {
122        addEditControlWindow(lc);
123    }
124    
125    /**
126     * Add a Single Light Control to the Table.
127     * @param lc the Light Control to add.
128     */
129    protected void addControlToTable(LightControl lc) {
130        lightControlTableModel.addControl(lc);
131    }
132
133    /**
134     * Get Light Control List currently displayed in the Table.
135     * Returned by the TableModel as unmodifiable.
136     * @return List of Light Controls.
137     */
138    public List<LightControl> getControlList(){
139        return lightControlTableModel.getControlList();
140    }
141    
142    /**
143     * Set the Table to the Light Controls of a single Light.
144     * @param l the Light to set display for.
145     */
146    public final void setToLight(Light l){
147        lightControlTableModel.setTableToLight(l);
148    }
149    
150    private AddEditSingleLightControlFrame addEditCtrlFrame;
151    
152    /**
153     * UI Function to get Last Selected Light Control Index within
154     * AddEditSingleLightControl.java
155     * @return Light Control Index.
156     */
157    protected int getLastSelectedControlIndex(){
158        return defaultControlIndex;
159    }
160    
161    protected void setLastSelectedControlIndex(int newIndex){
162        defaultControlIndex = newIndex;
163    }
164    
165    private int defaultControlIndex = Light.NO_CONTROL;
166    
167    /**
168     * Create the Add/Edit Light Control pane.
169     */
170    private void addEditControlWindow(LightControl lc) {
171        closeEditControlWindow();
172        addEditCtrlFrame = new AddEditSingleLightControlFrame(this, lc);
173    }
174    
175    protected void closeEditControlWindow(){
176        if (!(addEditCtrlFrame == null)) {
177            addEditCtrlFrame.dispose();
178            addEditCtrlFrame = null;
179        }
180    }
181    
182    public void dispose(){
183        closeEditControlWindow();
184    }
185    
186    // private final static Logger log = LoggerFactory.getLogger(LightControlPane.class);
187    
188}