001package jmri.jmrit.display.palette;
002
003import java.awt.datatransfer.DataFlavor;
004import java.awt.datatransfer.UnsupportedFlavorException;
005import java.awt.event.ActionListener;
006import java.io.IOException;
007import java.util.ArrayList;
008import java.util.HashMap;
009import java.util.Map.Entry;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012import jmri.jmrit.catalog.DragJLabel;
013import jmri.jmrit.catalog.NamedIcon;
014import jmri.jmrit.display.DisplayFrame;
015import jmri.jmrit.display.Editor;
016import jmri.jmrit.display.IndicatorTrackIcon;
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020/**
021 * ItemPanel for for Indicating track blocks.
022 * @author Pete Cressman Copyright (c) 2010, 2020
023 */
024public class IndicatorItemPanel extends FamilyItemPanel {
025
026    private DetectionPanel _detectPanel;
027
028    /*
029     * Constructor for track icons.
030     */
031    public IndicatorItemPanel(DisplayFrame parentFrame, String type, String family) {
032        super(parentFrame, type, family);
033    }
034
035    /**
036     * Init for creation of insert panels for detection and train id.
037     */
038    @Override
039    public void init() {
040        if (!_initialized) {
041            super.init();
042            _detectPanel = new DetectionPanel(this);
043            add(_detectPanel, 1);
044        }
045        hideIcons();
046    }
047
048    @Override
049    protected void hideIcons() {
050        if (_detectPanel != null) {
051            _detectPanel.setVisible(true);
052            _detectPanel.invalidate();
053        }
054        super.hideIcons();
055    }
056
057    @Override
058    protected void showIcons() {
059        if (_detectPanel != null) {
060            _detectPanel.setVisible(false);
061            _detectPanel.invalidate();
062        }
063        super.showIcons();
064    }
065
066    /**
067     * Init for update of existing track block.
068     * _bottom3Panel has "Update Panel" button put onto _bottom1Panel.
069     */
070    @Override
071    public void init(ActionListener doneAction, HashMap<String, NamedIcon> iconMap) {
072        super.init(doneAction, iconMap);
073        _detectPanel = new DetectionPanel(this);
074        add(_detectPanel, 0);
075    }
076
077    /**
078     * CircuitBuilder init for conversion of plain track to indicator track.
079     */
080    @Override
081    public void init(JPanel bottomPanel) {
082        super.init(bottomPanel);
083        add(_iconFamilyPanel, 0);
084    }
085
086    @Override
087    public void dispose() {
088        if (_detectPanel != null) {
089            _detectPanel.dispose();
090        }
091    }
092
093    @Override
094    protected String getDisplayKey() {
095        return "ClearTrack";
096    }
097
098    /**
099     * ************* pseudo inheritance to DetectionPanel ******************
100     * @return getShowTrainName status from detection panel.
101     */
102    public boolean getShowTrainName() {
103        return _detectPanel.getShowTrainName();
104    }
105
106    public void setShowTrainName(boolean show) {
107        _detectPanel.setShowTrainName(show);
108    }
109
110    public String getOccSensor() {
111        return _detectPanel.getOccSensor();
112    }
113
114    public String getOccBlock() {
115        return _detectPanel.getOccBlock();
116    }
117
118    public void setOccDetector(String name) {
119        _detectPanel.setOccDetector(name);
120    }
121
122    public ArrayList<String> getPaths() {
123        return _detectPanel.getPaths();
124    }
125
126    public void setPaths(ArrayList<String> paths) {
127        _detectPanel.setPaths(paths);
128    }
129
130    /**
131     * {@inheritDoc} 
132     */
133    @Override
134    protected JLabel getDragger(DataFlavor flavor, HashMap<String, NamedIcon> map, NamedIcon icon) {
135        return new IndicatorDragJLabel(flavor, map, icon);
136    }
137
138    protected class IndicatorDragJLabel extends DragJLabel {
139
140        private final HashMap<String, NamedIcon> iconMap;
141
142        public IndicatorDragJLabel(DataFlavor flavor, HashMap<String, NamedIcon> map, NamedIcon icon) {
143            super(flavor, icon);
144            iconMap = new HashMap<>(map);
145        }
146
147        @Override
148        public boolean isDataFlavorSupported(DataFlavor flavor) {
149            return super.isDataFlavorSupported(flavor);
150        }
151
152        @Override
153        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
154            if (!isDataFlavorSupported(flavor)) {
155                return null;
156            }
157            if (iconMap == null) {
158                log.error("IconDragJLabel.getTransferData: iconMap is null!");
159                return null;
160            }
161            log.debug("IndicatorDragJLabel.getTransferData");
162            if (flavor.isMimeTypeEqual(Editor.POSITIONABLE_FLAVOR)) {
163                IndicatorTrackIcon t = new IndicatorTrackIcon(_frame.getEditor());
164
165                t.setOccBlock(_detectPanel.getOccBlock());
166                t.setOccSensor(_detectPanel.getOccSensor());
167                t.setShowTrain(_detectPanel.getShowTrainName());
168                t.setFamily(_family);
169
170                for (Entry<String, NamedIcon> entry : iconMap.entrySet()) {
171                    t.setIcon(entry.getKey(), new NamedIcon(entry.getValue()));
172                }
173                t.setLevel(Editor.TURNOUTS);
174                return t;                
175            } else if (DataFlavor.stringFlavor.equals(flavor)) {
176                return _itemType + " icons";
177            }
178            return null;
179        }
180    }
181
182    private final static Logger log = LoggerFactory.getLogger(IndicatorItemPanel.class);
183
184}