001package jmri.jmrit.display.layoutEditor;
002
003import java.awt.BorderLayout;
004import java.awt.FlowLayout;
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007
008import javax.swing.BoxLayout;
009import javax.swing.ButtonGroup;
010import javax.swing.JButton;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.JRadioButton;
014import javax.swing.JSeparator;
015
016import jmri.jmrit.display.MultiSensorIcon;
017import jmri.util.JmriJFrame;
018import jmri.util.swing.JmriJOptionPane;
019
020/**
021 * Provides a simple editor for creating a MultiSensorIcon object
022 * <p>
023 * To work right, the MultiSensorIcon needs to have all images the same size,
024 * but this is not enforced here. It should be.
025 *
026 * @author Bob Jacobsen Copyright (c) 2007
027 */
028public class MultiSensorIconFrame extends JmriJFrame {
029
030    private final JPanel content = new JPanel();
031    private JmriJFrame defaultsFrame;
032    private MultiIconEditor defaultIcons;
033    private LayoutEditor layoutEditor = null;
034    private final JRadioButton updown = new JRadioButton(Bundle.getMessage("UpDown"));
035    private final JRadioButton rightleft = new JRadioButton(Bundle.getMessage("RightLeft"));
036    private final ButtonGroup group = new ButtonGroup();
037
038    MultiSensorIconFrame(LayoutEditor p) {
039        super("Enter MultiSensor");
040        layoutEditor = p;
041
042        addHelpMenu("package.jmri.jmrit.display.MultiSensorIconFrame", true);
043    }
044
045    int isEmpty = 0; // check for empty Fields in panel
046    int _numberOfPositions = 3; // add an index to Sensor label
047
048    @Override
049    public void initComponents() {
050        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
051
052        JPanel p = new JPanel();
053        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
054        p.add(new JLabel(Bundle.getMessage("IconChecksClickLabel")));
055        group.add(updown);
056        group.add(rightleft);
057        rightleft.setSelected(true);
058        p.add(rightleft);
059        p.add(updown);
060        this.getContentPane().add(p);
061
062        this.getContentPane().add(content);
063        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
064        // start with three Entrys; there's no reason to have less
065        content.add(new Entry(content, this, "resources/icons/USS/plate/levers/l-left.gif", 1));
066        content.add(new Entry(content, this, "resources/icons/USS/plate/levers/l-vertical.gif", 2));
067        content.add(new Entry(content, this, "resources/icons/USS/plate/levers/l-right.gif", 3));
068
069        this.getContentPane().add(new JSeparator());
070        JButton b = new JButton(Bundle.getMessage("ButtonAddAdditionalSensor"));
071        ActionListener a = new ActionListener() {
072            @Override
073            public void actionPerformed(ActionEvent a) {
074                // add another entry
075                _numberOfPositions++;
076                self.add(new Entry(self, frame, "resources/icons/USSpanels/Plates/lever-v.gif", _numberOfPositions));
077                frame.pack();
078            }
079            JPanel self;
080            JmriJFrame frame;
081
082            ActionListener init(JPanel self, JmriJFrame frame) {
083                this.frame = frame;
084                this.self = self;
085                return this;
086            }
087        }.init(content, this);
088        b.addActionListener(a);
089        this.getContentPane().add(b);
090
091        this.getContentPane().add(new JSeparator());
092        b = new JButton(Bundle.getMessage("SetStateIcons"));
093        defaultIcons = new MultiIconEditor(3);
094        defaultIcons.setIcon(0, Bundle.getMessage("MakeLabel", Bundle.getMessage("BeanStateUnknown")),
095                "resources/icons/USS/plate/levers/l-inactive.gif");
096        defaultIcons.setIcon(1, Bundle.getMessage("MakeLabel", Bundle.getMessage("BeanStateInconsistent")),
097                "resources/icons/USS/plate/levers/l-unknown.gif");
098        defaultIcons.setIcon(2, Bundle.getMessage("MakeLabel", Bundle.getMessage("SensorStateInactive")),
099                "resources/icons/USS/plate/levers/l-inconsistent.gif");
100        defaultIcons.complete();
101        defaultsFrame = new JmriJFrame("", false, true);
102        defaultsFrame.getContentPane().add(new JLabel(Bundle.getMessage("IconChangeInfo")), BorderLayout.NORTH);
103        defaultsFrame.getContentPane().add(defaultIcons);
104        defaultsFrame.pack();
105        defaultsFrame.addHelpMenu("package.jmri.jmrit.display.MultiSensorIconDefaultsFrame", true);
106        b.addActionListener(a12 -> defaultsFrame.setVisible(true));
107        this.getContentPane().add(b);
108
109        this.getContentPane().add(new JSeparator());
110        b = new JButton(Bundle.getMessage("ButtonCreateIcon"));
111        b.addActionListener(a1 -> {
112            make();
113            if (isEmpty != 1){
114                removeWindows();
115            }
116        });
117        this.getContentPane().add(b);
118    }
119
120    // Remove an Entry from the panel,
121    // and therefore from the eventual sensor
122    void remove(Entry e) {
123        content.remove(e);
124        this.pack();
125    }
126
127    void make() {
128        MultiSensorIcon m = new MultiSensorIcon(layoutEditor);
129        m.setUnknownIcon(defaultIcons.getIcon(0));
130        m.setInconsistentIcon(defaultIcons.getIcon(1));
131        m.setInactiveIcon(defaultIcons.getIcon(2));
132
133        for (int i = 0; i < content.getComponentCount(); i++) {
134            Entry e = (Entry) content.getComponent(i);
135            if (e.sensor.getSelectedItem() == null) {
136                JmriJOptionPane.showMessageDialog(this,
137                        Bundle.getMessage("Error19", i+1),
138                        Bundle.getMessage("WarningTitle"), JmriJOptionPane.WARNING_MESSAGE);
139                isEmpty = 1;
140                return; // Keep Panel open to edit entry
141            }
142            m.addEntry(e.sensor.getSelectedItemDisplayName(), e.ed.getIcon(0));
143        }
144        m.setUpDown(updown.isSelected());
145        m.setDisplayLevel(jmri.jmrit.display.Editor.SENSORS);
146
147        layoutEditor.addMultiSensor(m);
148
149    }
150
151    void removeWindows() {
152        for (int i = 0; i < content.getComponentCount(); i++) {
153            ((Entry) content.getComponent(i)).dispose();
154        }
155        defaultsFrame.dispose();
156        super.dispose();
157    }
158
159    class Entry extends JPanel {
160        jmri.swing.NamedBeanComboBox<jmri.Sensor> sensor = new jmri.swing.NamedBeanComboBox<>(
161                jmri.InstanceManager.getDefault(jmri.SensorManager.class), null, jmri.NamedBean.DisplayOptions.DISPLAYNAME);
162        JPanel self;
163        MultiIconEditor ed = new MultiIconEditor(1);
164        JmriJFrame edf = new JmriJFrame("", false, true);
165
166        @Override
167        public String toString() {
168            return ed.getIcon(0).toString();
169        }
170
171        Entry(JPanel self, JmriJFrame frame, String name, int position) {
172            this.self = self;
173            this.setLayout(new FlowLayout());
174            this.add(new JLabel(Bundle.getMessage("MakeLabel", (Bundle.getMessage("BeanNameSensor") + " "  + Bundle.getMessage("MultiSensorPosition", position)))));
175
176            this.add(sensor);
177            jmri.util.swing.JComboBoxUtil.setupComboBoxMaxRows(sensor);
178
179            ed.setIcon(0, Bundle.getMessage("MakeLabel", (Bundle.getMessage("SensorStateActive") + " "  + Bundle.getMessage("MultiSensorPosition", position))), name);
180            ed.complete();
181            edf.getContentPane().add(new JLabel(Bundle.getMessage("IconChangeInfo")), BorderLayout.NORTH);
182            edf.getContentPane().add(ed);
183            edf.pack();
184
185            JButton b = new JButton(Bundle.getMessage("SetIconButton"));
186            b.addActionListener((ActionEvent a) -> edf.setVisible(true));
187            this.add(b);
188
189            // button to remove this entry from its parent
190            b = new JButton(Bundle.getMessage("ButtonDelete"));
191            ActionListener a = new ActionListener() {
192                @Override
193                public void actionPerformed(ActionEvent a) {
194                    // remove this entry
195                    self.remove(entry);
196                    _numberOfPositions--;
197                    frame.pack();
198                }
199                Entry entry;
200                JPanel self;
201                JmriJFrame frame;
202
203                ActionListener init(Entry entry, JPanel self, JmriJFrame frame) {
204                    this.entry = entry;
205                    this.self = self;
206                    this.frame = frame;
207                    return this;
208                }
209            }.init(this, self, frame);
210            b.addActionListener(a);
211
212            this.add(b);
213        }
214
215        public void dispose() {
216            edf.dispose();
217        }
218    }
219}