001package jmri.jmrit.display.controlPanelEditor;
002
003import java.awt.Dimension;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import java.util.ArrayList;
007import java.util.HashMap;
008import java.util.Map.Entry;
009
010import javax.swing.JButton;
011import javax.swing.JDialog;
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014
015import jmri.InstanceManager;
016import jmri.NamedBeanHandleManager;
017import jmri.jmrit.catalog.NamedIcon;
018import jmri.jmrit.display.DisplayFrame;
019import jmri.jmrit.display.Editor;
020import jmri.jmrit.display.IndicatorTrackIcon;
021import jmri.jmrit.display.IndicatorTurnoutIcon;
022import jmri.jmrit.display.Positionable;
023import jmri.jmrit.display.PositionableLabel;
024import jmri.jmrit.display.TurnoutIcon;
025import jmri.jmrit.display.palette.FamilyItemPanel;
026import jmri.jmrit.display.palette.IndicatorItemPanel;
027import jmri.jmrit.display.palette.IndicatorTOItemPanel;
028import jmri.jmrit.logix.OBlock;
029
030class ConvertDialog extends JDialog {
031
032        private final CircuitBuilder _parent;
033        private final PositionableLabel _pos;
034        FamilyItemPanel _panel;
035        DisplayFrame _filler;
036
037        ConvertDialog(CircuitBuilder cb, PositionableLabel pos, OBlock block) {
038            
039            super(cb._editor, true);
040            _parent = cb;
041            _pos = pos;
042            addWindowListener(new java.awt.event.WindowAdapter() {
043                @Override
044                public void windowClosing(java.awt.event.WindowEvent e) {
045                    _parent._editor.highlight(null);
046                }
047            });
048            if (pos == null) {
049                dispose();
050                return;
051            }
052            _filler = pos.makePaletteFrame("Dummy");
053            String title;
054            ActionListener updateAction;
055            if (pos instanceof TurnoutIcon) {
056                title = "IndicatorTO";
057                _panel = new IndicatorTOItemPanel(_filler, title, null, null) {
058                    @Override
059                    protected void showIcons() {
060                         super.showIcons();
061                         displayIcons();
062                    }
063                    @Override
064                    protected void hideIcons() {
065                        super.hideIcons();
066                        displayIcons();
067                    }
068                };
069                updateAction = (ActionEvent a) -> convertTO(block);
070            } else {
071                title = "IndicatorTrack";
072                _panel = new IndicatorItemPanel(_filler, title, null) {
073                    @Override
074                    protected void showIcons() {
075                        super.showIcons();
076                        displayIcons();
077                    }
078                    @Override
079                    protected void hideIcons() {
080                        super.hideIcons();
081                        displayIcons();
082                    }
083                };
084                updateAction = (ActionEvent a) -> convertSeg(block);
085            }
086
087            JPanel p = new JPanel();
088            p.add(new JLabel(Bundle.getMessage("notIndicatorIcon")));
089            _panel.add(p);
090            _panel.init(makeBottomPanel(updateAction, block));
091            Dimension dim = _panel.getPreferredSize();
092
093            javax.swing.JScrollPane sp = new javax.swing.JScrollPane(_panel);
094            dim = new Dimension(dim.width +25, dim.height + 25);
095            sp.setPreferredSize(dim);
096            sp.setPreferredSize(dim);
097            add(sp);
098            setTitle(Bundle.getMessage(title));
099            pack();
100            jmri.InstanceManager.getDefault(jmri.util.PlaceWindow.class).nextTo(cb._editor, pos, this);
101             setVisible(true);
102        }
103
104        private JPanel makeBottomPanel(ActionListener updateAction, OBlock block) {
105            JPanel panel = new JPanel();
106            JButton button = new JButton(Bundle.getMessage("convert"));
107            button.addActionListener(updateAction);
108            panel.add(button);
109            
110            button = new JButton(Bundle.getMessage("skip"));
111            button.addActionListener((ActionEvent a) -> dispose());
112            panel.add(button);
113            return panel;
114        }
115        /*
116         * Do for dialog what FamilyItemPanel, ItemPanel and DisplayFrame 
117         * need to do for reSizeDisplay and reSize
118         */
119        private void displayIcons() {
120            Dimension newDim = _panel.getPreferredSize();
121            Dimension deltaDim = _panel.shellDimension(_panel);
122            Dimension dim = new Dimension(deltaDim.width + newDim.width, deltaDim.height + newDim.height);
123            setPreferredSize(dim);
124            invalidate();
125            pack();
126        }
127
128        private void convertTO(OBlock block) {
129            IndicatorTurnoutIcon t = new IndicatorTurnoutIcon(_parent._editor);
130            t.setOccBlockHandle(InstanceManager.getDefault(NamedBeanHandleManager.class).getNamedBeanHandle(block.getSystemName(), block));
131            if ( _pos instanceof TurnoutIcon ) {
132                t.setTurnout(((TurnoutIcon) _pos).getNamedTurnout());
133            }
134            t.setFamily(_panel.getFamilyName());
135
136            HashMap<String, HashMap<String, NamedIcon>> iconMap = ((IndicatorTOItemPanel)_panel).getIconMaps();
137            for (Entry<String, HashMap<String, NamedIcon>> entry : iconMap.entrySet()) {
138                String status = entry.getKey();
139                for (Entry<String, NamedIcon> ent : entry.getValue().entrySet()) {
140                    t.setIcon(status, ent.getKey(), new NamedIcon(ent.getValue()));
141                }
142            }
143            t.setLevel(Editor.TURNOUTS);
144            t.setScale(_pos.getScale());
145            t.rotate(_pos.getDegrees());
146            finishConvert(t, block);
147        }
148
149        private void convertSeg(OBlock block) {
150            IndicatorTrackIcon t = new IndicatorTrackIcon(_parent._editor);
151            t.setOccBlockHandle(InstanceManager.getDefault(NamedBeanHandleManager.class).getNamedBeanHandle(block.getSystemName(), block));
152            t.setFamily(_panel.getFamilyName());
153
154            HashMap<String, NamedIcon> iconMap = _panel.getIconMap();
155            for (Entry<String, NamedIcon> entry : iconMap.entrySet()) {
156                t.setIcon(entry.getKey(), new NamedIcon(entry.getValue()));
157            }
158            t.setLevel(Editor.TURNOUTS);
159            t.setScale(_pos.getScale());
160            t.rotate(_pos.getDegrees());
161            finishConvert(t, block);
162        }
163
164        /*
165         * Replace references to _oldIcon with pos
166         */
167        private void finishConvert(Positionable pos, OBlock block) {
168            ArrayList<Positionable> selectionGroup = _parent._editor.getSelectionGroup();
169            selectionGroup.remove(_pos);
170            selectionGroup.add(pos);
171            ArrayList<Positionable> circuitIcons = _parent.getCircuitIcons(block);
172            circuitIcons.remove(_pos);
173            circuitIcons.add(pos);
174            pos.setLocation(_pos.getLocation());
175            _pos.remove();
176            try {
177                _parent._editor.putItem(pos);
178            } catch (Positionable.DuplicateIdException e) {
179                log.error("Editor.putItem() has thrown DuplicateIdException", e);
180            }
181            pos.updateSize();
182            _parent._editor.highlight(null);
183            dispose();
184            _filler.dispose();
185        }
186
187        private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConvertDialog.class);
188    }