001package jmri.jmrit.beantable.beanedit;
002
003import java.awt.event.ActionEvent;
004
005import javax.annotation.Nonnull;
006import javax.swing.AbstractAction;
007
008import jmri.*;
009import jmri.jmrit.beantable.light.LightControlPane;
010import jmri.jmrit.beantable.light.LightIntensityPane;
011
012/**
013 * Provides an edit panel for a Light object.
014 *
015 * @author Kevin Dickerson Copyright (C) 2011
016 * @author Steve Young Copyright (C) 2021
017 */
018public class LightEditAction extends BeanEditAction<Light> {
019
020    private LightControlPane lcp;
021    private LightIntensityPane lip;
022
023    @Override
024    protected void initPanels() {
025        if (InstanceManager.getNullableDefault(LightManager.class) == null) {
026            setEnabled(false);
027        }
028        super.initPanels();
029        lightControlPanel();
030        lightIntensityPanel();
031        
032        applyBut.setToolTipText(Bundle.getMessage("LightUpdateButtonHint"));
033    }
034
035    @Override
036    public String helpTarget() {
037        return "package.jmri.jmrit.beantable.LightAddEdit";
038    } // NOI18N
039    
040    @Override
041    public Light getByUserName(String name) {
042        return InstanceManager.lightManagerInstance().getByUserName(name);
043    }
044    
045    @Override
046    protected void cancelButtonAction(ActionEvent e) {
047        if (lcp!=null) {
048            lcp.dispose(); // ensures add / edit single LightControl Frame is closed
049        }
050        super.cancelButtonAction(e);
051    }
052    
053    /**
054     * Hide the Bean Properties Tab.
055     * @return null
056     */
057    @Override
058    BeanItemPanel propertiesDetails() {
059        return null;
060    }
061    
062    BeanItemPanel lightControlPanel() {
063        BeanItemPanel lcPanel = new BeanItemPanel();
064        lcPanel.setName(Bundle.getMessage("LightControllerTitlePlural"));
065
066        lcp = new LightControlPane(bean);        
067        lcPanel.add(lcp);
068        
069        lcPanel.setResetItem(new AbstractAction() {
070            @Override
071            public void actionPerformed(ActionEvent e) {
072                if ( lcp != null ) {
073                    lcp.setToLight(bean);
074                }
075            }
076        });
077
078        lcPanel.setSaveItem(new AbstractAction() {
079            @Override
080            public void actionPerformed(ActionEvent e) {
081                if ( lcp != null ) {
082                    lcp.setLightFromControlTable(bean);
083                }
084            }
085        });
086        bei.add(lcPanel);
087        return lcPanel;
088    }
089
090    BeanItemPanel lightIntensityPanel() {
091        BeanItemPanel liPanel = new BeanItemPanel();
092        liPanel.setName(Bundle.getMessage("LightVariableBorder"));
093        lip = new LightIntensityPane(true);
094        lip.setToLight(bean);
095        liPanel.add(lip);
096        
097        liPanel.setResetItem(new AbstractAction() {
098            @Override
099            public void actionPerformed(ActionEvent e) {
100                if ( lip != null ) {
101                    lip.setToLight(bean);
102                }
103            }
104        });
105
106        liPanel.setSaveItem(new AbstractAction() {
107            @Override
108            public void actionPerformed(ActionEvent e) {
109                if (bean instanceof VariableLight && lip != null ) {
110                    lip.setLightFromPane((VariableLight)bean);
111                }
112            }
113        });
114        bei.add(liPanel);
115
116        if (!(bean instanceof VariableLight)) {
117            liPanel.setEnabled(false);
118            liPanel.setToolTipText(Bundle.getMessage("NoLightIntensityForHardware",getBeanManagerSystemUserName()));
119        }
120        return liPanel;
121    }
122    
123    /**
124     * Get Bean Manager System UserName.
125     * Human readable form of System Username.
126     * e.g. "Internal", "MERG"
127     * 
128     * @return Manager UserName, else empty String.
129     */
130    @Nonnull
131    private String getBeanManagerSystemUserName(){
132        Manager<Light> lm = InstanceManager.getDefault(LightManager.class);
133        if (lm instanceof ProxyManager){
134            ProxyManager<Light> plm = (ProxyManager<Light>)lm;
135            for (Manager<Light> m : plm.getManagerList()) {
136                if (m.getBySystemName(bean.getSystemName())!=null) {
137                    return m.getMemo().getUserName();
138                }
139            }
140        }
141        return "";
142    }
143    
144}