Search in sources :

Example 16 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.

the class FamilyItemPanel method makeDndIconPanel.

protected void makeDndIconPanel(HashMap<String, NamedIcon> iconMap, String displayKey) {
    if (_supressDragging) {
        return;
    }
    if (!jmri.util.ThreadingUtil.isGUIThread())
        log.error("Not on GUI thread", new Exception("traceback"));
    _dragIconPanel.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
    if (iconMap != null) {
        if (iconMap.get(displayKey) == null) {
            displayKey = (String) iconMap.keySet().toArray()[0];
        }
        NamedIcon ic = iconMap.get(displayKey);
        if (ic != null) {
            NamedIcon icon = new NamedIcon(ic);
            JPanel panel = new JPanel(new FlowLayout());
            String borderName = ItemPalette.convertText("dragToPanel");
            panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
            JLabel label;
            try {
                label = getDragger(new DataFlavor(Editor.POSITIONABLE_FLAVOR), iconMap, icon);
                if (label != null) {
                    label.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
                    //                        label.setIcon(icon);
                    label.setName(borderName);
                    panel.add(label);
                }
            } catch (java.lang.ClassNotFoundException cnfe) {
                log.warn("no DndIconPanel {} created", borderName, cnfe);
            }
            int width = Math.max(100, panel.getPreferredSize().width);
            panel.setPreferredSize(new java.awt.Dimension(width, panel.getPreferredSize().height));
            panel.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
            _dragIconPanel.add(panel);
            return;
        }
    } else {
        log.error("No iconMap for makeDndIconPanel");
    }
}
Also used : JPanel(javax.swing.JPanel) NamedIcon(jmri.jmrit.catalog.NamedIcon) FlowLayout(java.awt.FlowLayout) JLabel(javax.swing.JLabel) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 17 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.

the class ControlPanelEditor method drop.

@SuppressWarnings("unchecked")
@Override
public void drop(DropTargetDropEvent evt) {
    try {
        //Point pt = evt.getLocation(); coords relative to entire window
        Point pt = _targetPanel.getMousePosition(true);
        Transferable tr = evt.getTransferable();
        if (log.isDebugEnabled()) {
            // avoid string building if not debug
            DataFlavor[] flavors = tr.getTransferDataFlavors();
            StringBuilder flavor = new StringBuilder();
            for (DataFlavor flavor1 : flavors) {
                flavor.append(flavor1.getRepresentationClass().getName()).append(", ");
            }
            log.debug("Editor Drop: flavor classes={}", flavor);
        }
        if (tr.isDataFlavorSupported(_positionableDataFlavor)) {
            Positionable item = (Positionable) tr.getTransferData(_positionableDataFlavor);
            if (item == null) {
                return;
            }
            item.setLocation(pt.x, pt.y);
            // now set display level in the pane.
            item.setDisplayLevel(item.getDisplayLevel());
            item.setEditor(this);
            putItem(item);
            item.updateSize();
            //if (_debug) log.debug("Drop positionable "+item.getNameString()+
            //                                    " as "+item.getClass().getName()+
            //                                    ", w= "+item.maxWidth()+", h= "+item.maxHeight());
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(_namedIconDataFlavor)) {
            NamedIcon newIcon = new NamedIcon((NamedIcon) tr.getTransferData(_namedIconDataFlavor));
            String url = newIcon.getURL();
            NamedIcon icon = NamedIcon.getIconByName(url);
            PositionableLabel ni = new PositionableLabel(icon, this);
            // infer a background icon from the size
            if (icon.getIconHeight() > 500 || icon.getIconWidth() > 600) {
                ni.setDisplayLevel(BKG);
            } else {
                ni.setDisplayLevel(ICONS);
            }
            ni.setLocation(pt.x, pt.y);
            ni.setEditor(this);
            putItem(ni);
            ni.updateSize();
            evt.dropComplete(true);
            return;
        } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String text = (String) tr.getTransferData(DataFlavor.stringFlavor);
            PositionableLabel l = new PositionableLabel(text, this);
            l.setSize(l.getPreferredSize().width, l.getPreferredSize().height);
            l.setDisplayLevel(LABELS);
            l.setLocation(pt.x, pt.y);
            l.setEditor(this);
            putItem(l);
            evt.dropComplete(true);
        } else if (tr.isDataFlavorSupported(_positionableListDataFlavor)) {
            List<Positionable> dragGroup = (List<Positionable>) tr.getTransferData(_positionableListDataFlavor);
            for (Positionable pos : dragGroup) {
                pos.setEditor(this);
                putItem(pos);
                pos.updateSize();
                log.debug("DnD Add {}", pos.getNameString());
            }
        } else {
            log.warn("Editor DropTargetListener  supported DataFlavors not avaialable at drop from " + tr.getClass().getName());
        }
    } catch (IOException ioe) {
        log.warn("Editor DropTarget caught IOException", ioe);
    } catch (UnsupportedFlavorException ufe) {
        log.warn("Editor DropTarget caught UnsupportedFlavorException", ufe);
    }
    log.debug("Editor DropTargetListener drop REJECTED!");
    evt.rejectDrop();
}
Also used : NamedIcon(jmri.jmrit.catalog.NamedIcon) Transferable(java.awt.datatransfer.Transferable) PositionableLabel(jmri.jmrit.display.PositionableLabel) List(java.util.List) ArrayList(java.util.ArrayList) Point(java.awt.Point) Positionable(jmri.jmrit.display.Positionable) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 18 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.

the class ClockItemPanel method addIconsToPanel.

@Override
protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
    _iconPanel = new JPanel();
    Iterator<Entry<String, NamedIcon>> it = iconMap.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, NamedIcon> entry = it.next();
        // make copy for possible reduction
        NamedIcon icon = new NamedIcon(entry.getValue());
        JPanel panel = new JPanel();
        String borderName = ItemPalette.convertText(entry.getKey());
        panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
        try {
            JLabel label = new ClockDragJLabel(new DataFlavor(Editor.POSITIONABLE_FLAVOR));
            if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) {
                label.setText(Bundle.getMessage("invisibleIcon"));
                label.setForeground(Color.lightGray);
            } else {
                icon.reduceTo(100, 100, 0.2);
            }
            label.setIcon(icon);
            label.setName(borderName);
            panel.add(label);
        } catch (java.lang.ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        _iconPanel.add(panel);
    }
    add(_iconPanel, 1);
}
Also used : JPanel(javax.swing.JPanel) Entry(java.util.Map.Entry) NamedIcon(jmri.jmrit.catalog.NamedIcon) JLabel(javax.swing.JLabel) DragJLabel(jmri.jmrit.catalog.DragJLabel) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 19 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.

the class MemoryItemPanel method makeDragIcon.

private JPanel makeDragIcon(JComponent mem, Type type) {
    JPanel panel = new JPanel();
    JPanel comp;
    try {
        comp = getDragger(new DataFlavor(Editor.POSITIONABLE_FLAVOR), type, mem);
        comp.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
    } catch (java.lang.ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
        comp = new JPanel();
    }
    panel.add(comp);
    return panel;
}
Also used : JPanel(javax.swing.JPanel) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 20 with DataFlavor

use of java.awt.datatransfer.DataFlavor in project JMRI by JMRI.

the class ControlPanelEditor method pasteFromClipboard.

private void pasteFromClipboard() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor[] flavors = clipboard.getAvailableDataFlavors();
    for (DataFlavor flavor : flavors) {
        if (_positionableListDataFlavor.equals(flavor)) {
            try {
                @SuppressWarnings("unchecked") List<Positionable> clipGroup = (List<Positionable>) clipboard.getData(_positionableListDataFlavor);
                if (clipGroup != null && clipGroup.size() > 0) {
                    Positionable pos = clipGroup.get(0);
                    int minX = pos.getLocation().x;
                    int minY = pos.getLocation().y;
                    // locate group at mouse point
                    for (int i = 1; i < clipGroup.size(); i++) {
                        pos = clipGroup.get(i);
                        minX = Math.min(minX, pos.getLocation().x);
                        minY = Math.min(minY, pos.getLocation().y);
                    }
                    if (_pastePending) {
                        abortPasteItems();
                    }
                    _selectionGroup = new ArrayList<Positionable>();
                    for (int i = 0; i < clipGroup.size(); i++) {
                        pos = clipGroup.get(i);
                        // make positionable belong to this editor
                        pos.setEditor(this);
                        pos.setLocation(pos.getLocation().x + _anchorX - minX, pos.getLocation().y + _anchorY - minY);
                        // now set display level in the pane.
                        pos.setDisplayLevel(pos.getDisplayLevel());
                        putItem(pos);
                        pos.updateSize();
                        pos.setVisible(true);
                        _selectionGroup.add(pos);
                        if (pos instanceof PositionableIcon) {
                            jmri.NamedBean bean = pos.getNamedBean();
                            if (bean != null) {
                                ((PositionableIcon) pos).displayState(bean.getState());
                            }
                        } else if (pos instanceof MemoryIcon) {
                            ((MemoryIcon) pos).displayState();
                        } else if (pos instanceof PositionableJComponent) {
                            ((PositionableJComponent) pos).displayState();
                        }
                        log.debug("Paste Added at ({}, {})", pos.getLocation().x, pos.getLocation().y);
                    }
                }
                return;
            } catch (IOException ioe) {
                log.warn("Editor Paste caught IOException", ioe);
            } catch (UnsupportedFlavorException ufe) {
                log.warn("Editor Paste caught UnsupportedFlavorException", ufe);
            }
        }
    }
}
Also used : MemoryIcon(jmri.jmrit.display.MemoryIcon) PositionableIcon(jmri.jmrit.display.PositionableIcon) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Point(java.awt.Point) DataFlavor(java.awt.datatransfer.DataFlavor) PositionableJComponent(jmri.jmrit.display.PositionableJComponent) List(java.util.List) ArrayList(java.util.ArrayList) Clipboard(java.awt.datatransfer.Clipboard) Positionable(jmri.jmrit.display.Positionable)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)50 IOException (java.io.IOException)12 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 JPanel (javax.swing.JPanel)11 Transferable (java.awt.datatransfer.Transferable)10 JLabel (javax.swing.JLabel)9 List (java.util.List)8 Point (java.awt.Point)7 ArrayList (java.util.ArrayList)7 Clipboard (java.awt.datatransfer.Clipboard)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 File (java.io.File)3 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2 Image (java.awt.Image)2 Insets (java.awt.Insets)2