001package jmri.jmrit.display.palette;
002
003import java.awt.datatransfer.DataFlavor;
004import java.awt.datatransfer.UnsupportedFlavorException;
005import java.io.IOException;
006import java.util.HashMap;
007
008import javax.swing.JComponent;
009import javax.swing.JPanel;
010
011import jmri.Reporter;
012import jmri.jmrit.catalog.NamedIcon;
013import jmri.jmrit.display.DisplayFrame;
014import jmri.jmrit.display.Editor;
015import jmri.jmrit.display.ReporterIcon;
016import jmri.jmrit.picker.PickListModel;
017import jmri.util.swing.JmriJOptionPane;
018
019public class ReporterItemPanel extends TableItemPanel<Reporter> {
020
021    private final JPanel _dragPanel;  // appearance never changes - just make it once
022
023    public ReporterItemPanel(DisplayFrame parentFrame, String type, String family, PickListModel<jmri.Reporter> model) {
024        super(parentFrame, type, family, model);
025        _dragPanel = makeDraggerPanel(parentFrame);
026        _currentIconMap = new HashMap<>();
027    }
028
029    private JPanel makeDraggerPanel(DisplayFrame frame) {
030        JPanel panel = new JPanel();
031        panel.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
032        panel.setOpaque(false);
033        JPanel dragger;
034        ReporterIcon reporter = new ReporterIcon(frame.getEditor());
035        try {
036            dragger = new IconDragJComponent(new DataFlavor(Editor.POSITIONABLE_FLAVOR), reporter);
037        } catch (java.lang.ClassNotFoundException cnfe) {
038            log.error("Unable to find class supporting {}", Editor.POSITIONABLE_FLAVOR, cnfe);
039            dragger = new JPanel();
040        }
041        dragger.setOpaque(false);
042        dragger.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
043        panel.add(dragger);
044        int width = Math.max(100, panel.getPreferredSize().width);
045        panel.setPreferredSize(new java.awt.Dimension(width, panel.getPreferredSize().height));
046        panel.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
047        return panel;
048    }
049
050    @Override
051    protected void makeDndIcon(HashMap<String, NamedIcon> iconMap) {
052        _dragIconPanel.add(_dragPanel);
053    }
054
055    @Override
056    protected JPanel makeItemButtonPanel() {
057        JPanel panel = new JPanel();
058        panel.add(makeShowIconsButton());
059        return panel;
060    }
061
062    @Override
063    protected JPanel makeSpecialBottomPanel(boolean update) {
064        JPanel panel = new JPanel();
065        panel.add(makeShowIconsButton());
066        return panel;
067    }
068
069    protected class IconDragJComponent extends DragJComponent {
070
071        public IconDragJComponent(DataFlavor flavor, JComponent comp) {
072            super(flavor, comp);
073        }
074
075        @Override
076        protected boolean okToDrag() {
077            Reporter bean = getDeviceNamedBean();
078            if (bean == null) {
079                JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("noRowSelected"),
080                        Bundle.getMessage("WarningTitle"), JmriJOptionPane.WARNING_MESSAGE);
081                return false;
082            }
083            return true;
084        }
085
086        @Override
087        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
088            if (!isDataFlavorSupported(flavor)) {
089                return null;
090            }
091            Reporter bean = getDeviceNamedBean();
092            if (bean == null) {
093                return null;
094            }
095
096            if (flavor.isMimeTypeEqual(Editor.POSITIONABLE_FLAVOR)) {
097                ReporterIcon r = new ReporterIcon(_frame.getEditor());
098                r.setReporter(bean.getDisplayName());
099                r.setLevel(Editor.REPORTERS);
100                return r;
101            } else if (DataFlavor.stringFlavor.equals(flavor)) {
102                return _itemType + " icon for \"" + bean.getDisplayName() + "\"";
103            }
104            return null;
105        }
106    }
107
108    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ReporterItemPanel.class);
109
110}