Search in sources :

Example 41 with DataFlavor

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

the class ControlPanelEditor method makeDataFlavors.

/**
     * ************************** DnD *************************************
     */
protected void makeDataFlavors() {
    //        _targetPanel.setTransferHandler(new DnDIconHandler(this));
    try {
        _positionableDataFlavor = new DataFlavor(POSITIONABLE_FLAVOR);
        _namedIconDataFlavor = new DataFlavor(ImageIndexEditor.IconDataFlavorMime);
        _positionableListDataFlavor = new DataFlavor(List.class, "JComponentList");
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
    }
    new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) DropTarget(java.awt.dnd.DropTarget) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 42 with DataFlavor

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

the class SignalMastItemPanel method makeDndIconPanel.

@Override
protected void makeDndIconPanel(HashMap<String, NamedIcon> iconMap, String displayKey) {
    if (_update) {
        return;
    }
    _dragIconPanel.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
    NamedIcon icon = getDragIcon();
    JPanel panel = new JPanel();
    String borderName = ItemPalette.convertText("dragToPanel");
    panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), borderName));
    JLabel label;
    try {
        label = getDragger(new DataFlavor(Editor.POSITIONABLE_FLAVOR), icon);
        label.setToolTipText(Bundle.getMessage("ToolTipDragIcon"));
    } catch (java.lang.ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
        label = new JLabel();
    }
    label.setName(borderName);
    panel.add(label);
    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);
}
Also used : JPanel(javax.swing.JPanel) NamedIcon(jmri.jmrit.catalog.NamedIcon) JLabel(javax.swing.JLabel) DragJLabel(jmri.jmrit.catalog.DragJLabel) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 43 with DataFlavor

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

the class CatalogPanel method setIcons.

/**
     * Display the icons in the preview panel
     */
private String setIcons() {
    Thread.UncaughtExceptionHandler exceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
    resetPanel();
    CatalogTreeNode node = getSelectedNode();
    if (node == null) {
        return null;
    }
    List<CatalogTreeLeaf> leaves = node.getLeaves();
    if (leaves == null) {
        return null;
    }
    int numCol = 1;
    while (numCol * numCol < leaves.size()) {
        numCol++;
    }
    if (numCol > 1) {
        numCol--;
    }
    int numRow = leaves.size() / numCol;
    boolean newCol = false;
    _noMemory = false;
    // VM launches another thread to run ImageFetcher.
    // This handler will catch memory exceptions from that thread
    Thread.setDefaultUncaughtExceptionHandler(new MemoryExceptionHandler());
    GridBagLayout gridbag = new GridBagLayout();
    _preview.setLayout(gridbag);
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridy = 0;
    c.gridx = -1;
    for (int i = 0; i < leaves.size(); i++) {
        if (_noMemory) {
            continue;
        }
        CatalogTreeLeaf leaf = leaves.get(i);
        NamedIcon icon = new NamedIcon(leaf.getPath(), leaf.getName());
        double scale = icon.reduceTo(ICON_WIDTH, ICON_HEIGHT, ICON_SCALE);
        if (_noMemory) {
            continue;
        }
        if (c.gridx < numCol) {
            c.gridx++;
        } else if (c.gridy < numRow) {
            //start next row
            c.gridy++;
            if (!newCol) {
                c.gridx = 0;
            }
        } else if (!newCol) {
            // start new column
            c.gridx++;
            numCol++;
            c.gridy = 0;
            newCol = true;
        } else {
            // start new row
            c.gridy++;
            numRow++;
            c.gridx = 0;
            newCol = false;
        }
        c.insets = new Insets(5, 5, 0, 0);
        JLabel nameLabel;
        if (_noDrag) {
            nameLabel = new JLabel();
        } else {
            try {
                nameLabel = new DragJLabel(new DataFlavor(ImageIndexEditor.IconDataFlavorMime));
            } catch (java.lang.ClassNotFoundException cnfe) {
                log.warn("Unable to create drag label", cnfe);
                continue;
            }
        }
        //            nameLabel.setText(leaf.getName());
        nameLabel.setName(leaf.getName());
        //            nameLabel.setVerticalTextPosition(JLabel.TOP);
        nameLabel.setBackground(_currentBackground);
        nameLabel.setIcon(icon);
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.add(nameLabel);
        JLabel label = new JLabel(Bundle.getMessage("scale", CatalogPanel.printDbl(scale, 2)));
        p.add(label);
        label = new JLabel(leaf.getName());
        p.add(label);
        if (_noDrag) {
            p.addMouseListener(this);
        }
        gridbag.setConstraints(p, c);
        _preview.add(p);
        if (log.isDebugEnabled()) {
            log.debug("{} inserted at ({}, {})", leaf.getName(), c.gridx, c.gridy);
        }
    }
    c.gridy++;
    c.gridx++;
    JLabel bottom = new JLabel();
    gridbag.setConstraints(bottom, c);
    _preview.add(bottom);
    Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
    return Bundle.getMessage("numImagesInNode", node.getUserObject(), leaves.size());
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) Point(java.awt.Point) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 44 with DataFlavor

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

the class DnDStringImportHandler method importData.

@Override
public boolean importData(JComponent comp, Transferable tr) {
    //if (log.isDebugEnabled()) log.debug("DnDStringImportHandler.importData ");
    DataFlavor[] flavors = tr.getTransferDataFlavors();
    if (!canImport(comp, flavors)) {
        return false;
    }
    try {
        String data = (String) tr.getTransferData(DataFlavor.stringFlavor);
        JTextField field = (JTextField) comp;
        field.setText(data);
        //Notify listeners drop happened
        field.firePropertyChange("DnDrop", 0, 1);
        return true;
    } catch (UnsupportedFlavorException ufe) {
        log.warn("DnDStringImportHandler.importData: " + ufe.getMessage());
    } catch (IOException ioe) {
        log.warn("DnDStringImportHandler.importData: " + ioe.getMessage());
    }
    return false;
}
Also used : IOException(java.io.IOException) JTextField(javax.swing.JTextField) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) DataFlavor(java.awt.datatransfer.DataFlavor)

Example 45 with DataFlavor

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

the class FileDrop method isDragOk.

// end dropListener
/**
     * Determine if the dragged data is a file list.
     */
private boolean isDragOk(final java.io.PrintStream out, final java.awt.dnd.DropTargetDragEvent evt) {
    boolean ok = false;
    // Get data flavors being dragged
    java.awt.datatransfer.DataFlavor[] flavors = evt.getCurrentDataFlavors();
    // See if any of the flavors are a file list
    int i = 0;
    while (!ok && i < flavors.length) {
        // BEGIN 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
        // Is the flavor a file list?
        final DataFlavor curFlavor = flavors[i];
        if (curFlavor.equals(java.awt.datatransfer.DataFlavor.javaFileListFlavor) || curFlavor.isRepresentationClassReader()) {
            ok = true;
        }
        // END 2007-09-12 Nathan Blomquist -- Linux (KDE/Gnome) support added.
        i++;
    }
    // If logging is enabled, show data flavors
    if (out != null) {
        if (flavors.length == 0) {
            log(out, "FileDrop: no data flavors.");
        }
        for (i = 0; i < flavors.length; i++) {
            log(out, flavors[i].toString());
        }
    }
    return ok;
}
Also used : DataFlavor(java.awt.datatransfer.DataFlavor)

Aggregations

DataFlavor (java.awt.datatransfer.DataFlavor)53 Transferable (java.awt.datatransfer.Transferable)13 IOException (java.io.IOException)13 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)11 List (java.util.List)11 JPanel (javax.swing.JPanel)11 JLabel (javax.swing.JLabel)9 ArrayList (java.util.ArrayList)8 Point (java.awt.Point)7 Clipboard (java.awt.datatransfer.Clipboard)6 File (java.io.File)6 NamedIcon (jmri.jmrit.catalog.NamedIcon)6 URL (java.net.URL)4 DragJLabel (jmri.jmrit.catalog.DragJLabel)4 InputStream (java.io.InputStream)3 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 BorderLayout (java.awt.BorderLayout)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 GridBagConstraints (java.awt.GridBagConstraints)2 GridBagLayout (java.awt.GridBagLayout)2