/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package bradswebdavclient; import java.awt.*; import java.awt.image.*; import java.awt.datatransfer.*; import java.io.*; import javax.swing.*; public class ImageSelection extends TransferHandler { private static final DataFlavor flavors[] = {DataFlavor.imageFlavor}; public int getSourceActions(JComponent c) { return TransferHandler.COPY; } public boolean canImport( JComponent comp, DataFlavor flavor[]) { if (!(comp instanceof JLabel)) { return false; } for (int i = 0, n = flavor.length; i < n; i++) { for (int j = 0, m = flavors.length; j < m; j++) { if (flavor[i].equals(flavors[j])) { return true; } } } return false; } public Transferable createTransferable( JComponent comp) { if (comp instanceof JLabel) { JLabel label = (JLabel) comp; Icon icon = label.getIcon(); if (icon instanceof ImageIcon) { final Image image = ((ImageIcon) icon).getImage(); final JLabel source = label; Transferable transferable = new Transferable() { public Object getTransferData( DataFlavor flavor) { if (isDataFlavorSupported(flavor)) { return image; } return null; } public DataFlavor[] getTransferDataFlavors() { return flavors; } public boolean isDataFlavorSupported( DataFlavor flavor) { return flavor.equals( DataFlavor.imageFlavor); } }; return transferable; } } return null; } public boolean importData( JComponent comp, Transferable t) { if (comp instanceof JLabel) { JLabel label = (JLabel) comp; if (t.isDataFlavorSupported(flavors[0])) { try { Image image = (Image) t.getTransferData(flavors[0]); ImageIcon icon = new ImageIcon(image); label.setIcon(icon); return true; } catch (UnsupportedFlavorException ignored) { } catch (IOException ignored) { } } } return false; } }