001package jmri.jmrit.display.palette;
002
003//import java.awt.datatransfer.Transferable; 
004import java.awt.datatransfer.DataFlavor;
005import java.awt.datatransfer.UnsupportedFlavorException;
006import java.io.IOException;
007import java.util.HashMap;
008
009import javax.swing.BoxLayout;
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012
013import jmri.jmrit.catalog.DragJLabel;
014import jmri.jmrit.catalog.NamedIcon;
015import jmri.jmrit.display.DisplayFrame;
016import jmri.jmrit.display.Editor;
017import jmri.jmrit.display.RpsPositionIcon;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021public class RPSItemPanel extends FamilyItemPanel {
022
023    public RPSItemPanel(DisplayFrame parentFrame, String type, String family) {
024        super(parentFrame, type, family);
025    }
026
027    @Override
028    protected JPanel instructions() {
029        JPanel blurb = new JPanel();
030        blurb.setLayout(new BoxLayout(blurb, BoxLayout.Y_AXIS));
031        blurb.add(new JLabel(Bundle.getMessage("DragIconPanel")));
032        JPanel panel = new JPanel();
033        panel.add(blurb);
034        return panel;
035    }
036
037    @Override
038    protected String getDisplayKey() {
039        return "active";
040    }
041
042    /*
043     * ****************************************************
044     */
045    @Override
046    protected JLabel getDragger(DataFlavor flavor, HashMap<String, NamedIcon> map, NamedIcon icon) {
047        return new IconDragJLabel(flavor, map, icon);
048    }
049
050    protected class IconDragJLabel extends DragJLabel {
051
052        HashMap<String, NamedIcon> iconMap;
053
054        public IconDragJLabel(DataFlavor flavor, HashMap<String, NamedIcon> map, NamedIcon icon) {
055            super(flavor, icon);
056            iconMap = new HashMap<>(map);
057        }
058
059        @Override
060        public boolean isDataFlavorSupported(DataFlavor flavor) {
061            return super.isDataFlavorSupported(flavor);
062        }
063
064        @Override
065        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
066            if (!isDataFlavorSupported(flavor)) {
067                return null;
068            }
069
070            if (log.isDebugEnabled()) {
071                log.debug("IconDragJLabel.getTransferData");
072            }
073            if (flavor.isMimeTypeEqual(Editor.POSITIONABLE_FLAVOR)) {
074                RpsPositionIcon r = new RpsPositionIcon(_frame.getEditor());
075                r.setActiveIcon(new NamedIcon(iconMap.get("active")));
076                r.setErrorIcon(new NamedIcon(iconMap.get("error")));
077                r.setSize(r.getPreferredSize().width, r.getPreferredSize().height);
078                r.setLevel(Editor.SENSORS);
079                return r;                
080            } else if (DataFlavor.stringFlavor.equals(flavor)) {
081                return _itemType + " icons";
082            }
083            return null;
084        }
085    }
086
087    private final static Logger log = LoggerFactory.getLogger(RPSItemPanel.class);
088
089}