All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xiongyingqi.util.ClipBoardHelper Maven / Gradle / Ivy

There is a newer version: 0.1.10
Show newest version
/**
 * RichTextTest
 */
package com.xiongyingqi.util;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

/**
 * @author 瑛琪 xiongyingqi.com
 * @version 2013-8-29 下午4:03:40
 */
public class ClipBoardHelper {

    public static Map getClipBoardTypeMap() {
        Map map = new LinkedHashMap();
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        DataFlavor[] dataFlavors = t.getTransferDataFlavors();
        for (int i = 0; i < dataFlavors.length; i++) {
            DataFlavor dataFlavor = dataFlavors[i];
            ClipBoardType clipBoardType = parseType(dataFlavor);
            try {
                Object object = t.getTransferData(dataFlavor);
                if (dataFlavor.isMimeTypeEqual("text/html")
                        && dataFlavor.isRepresentationClassCharBuffer()) {
                    System.out.println(dataFlavor);
                    System.out.println("object.toString() =========== " + object.toString());
                }
                if (clipBoardType != null) {
                    map.put(object, clipBoardType);
                }
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // if(t.isDataFlavorSupported(DataFlavor.imageFlavor)){
        // try {
        // BufferedImage image = (BufferedImage)
        // t.getTransferData(DataFlavor.imageFlavor);
        // System.out.println(image.toString());
        // } catch (UnsupportedFlavorException e) {
        // e.printStackTrace();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }
        // }
        return map;
    }

    private static ClipBoardType parseType(DataFlavor dataFlavor) {
        //		EntityHelper.print(dataFlavor);
        if (dataFlavor == null) {
            return null;
        }
        /**
         *
         System.out.println("dataFlavor.isFlavorJavaFileListType() ======== "
         * + dataFlavor.isFlavorJavaFileListType()); System.out.println(
         * "dataFlavor.isFlavorRemoteObjectType() ======== " +
         * dataFlavor.isFlavorRemoteObjectType()); System.out.println(
         * "dataFlavor.isFlavorSerializedObjectType() ======== " +
         * dataFlavor.isFlavorSerializedObjectType());
         * System.out.println("dataFlavor.isFlavorTextType() ======== " +
         * dataFlavor.isFlavorTextType());
         * System.out.println("dataFlavor.isMimeTypeEqual() ======== " +
         * dataFlavor.isMimeTypeEqual("text/html")); System.out.println(
         * "dataFlavor.isMimeTypeSerializedObject() ======== " +
         * dataFlavor.isMimeTypeSerializedObject()); System.out.println(
         * "dataFlavor.isRepresentationClassByteBuffer() ======== " +
         * dataFlavor.isRepresentationClassByteBuffer()); System.out.println(
         * "dataFlavor.isRepresentationClassByteBuffer() ======== " +
         * dataFlavor.isRepresentationClassCharBuffer()); System.out.println(
         * "dataFlavor.isRepresentationClassInputStream() ======== " +
         * dataFlavor.isRepresentationClassInputStream()); System.out.println(
         * "dataFlavor.isRepresentationClassReader() ======== " +
         * dataFlavor.isRepresentationClassReader()); System.out.println(
         * "dataFlavor.isRepresentationClassRemote() ======== " +
         * dataFlavor.isRepresentationClassRemote()); System.out.println(
         * "dataFlavor.isRepresentationClassSerializable() ======== " +
         * dataFlavor.isRepresentationClassSerializable());
         */

        ClipBoardType type = null;// 默认转换为字符串类型
        if (dataFlavor.equals(DataFlavor.imageFlavor)) {
            type = ClipBoardType.IMAGE;
        } else if (dataFlavor.isMimeTypeEqual("text/html")
                && dataFlavor.isRepresentationClassCharBuffer()) {
            type = ClipBoardType.HTML;
        } else if (dataFlavor.isMimeTypeEqual("application/rtf")
                && dataFlavor.isRepresentationClassInputStream()) {
            type = ClipBoardType.HTML;
        } else if (dataFlavor.equals(DataFlavor.javaFileListFlavor)) {
            type = ClipBoardType.FILE;
        } else if (dataFlavor.equals(DataFlavor.plainTextFlavor)) {
            type = ClipBoardType.STRINGREADER;
        } else if (dataFlavor.equals(DataFlavor.stringFlavor)) {
            type = ClipBoardType.STRING;
        } else if (dataFlavor.isRepresentationClassInputStream()) {
            type = ClipBoardType.INPUT_STREAM;
        }
        return type;
    }

    /**
     * 剪切板内容类型枚举
     *
     * @author 瑛琪 xiongyingqi.com
     * @version 2013-8-29 下午4:55:59
     */
    public enum ClipBoardType {
        /**
         * 文件类型
         */
        FILE,
        /**
         * 图片类型
         */
        IMAGE,
        /**
         * 字符串类型
         */
        STRING,
        /**
         * 字符流
         */
        STRINGREADER,
        /**
         * IO流
         */
        INPUT_STREAM,
        /**
         * HTML
         */
        HTML
    }

    public static JLabel[] buildClipBoard() {
        Map map = getClipBoardTypeMap();
        Set> entries = map.entrySet();
        Collection labels = new ArrayList();
        int i = 0;
        for (Iterator iterator = entries.iterator(); iterator.hasNext(); ) {
            Entry entry = (Entry) iterator.next();
            Object object = entry.getKey();
            ClipBoardType clipBoardType = entry.getValue();
            JLabel label = null;
            switch (clipBoardType) {
                case IMAGE:
                    final Image image = (Image) object;
                    label = new JLabel() {
                        {
                            this.setSize(image.getWidth(null), image.getHeight(null));
                        }

                        /**
                         * 
* 2013-8-29 下午5:18:22 * * @see javax.swing.JComponent#paint(java.awt.Graphics) */ @Override public void paint(Graphics g) { g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null); } }; break; case FILE: break; case STRING: break; default: break; } if (label != null) { labels.add(label); } i++; } return labels.toArray(new JLabel[]{}); } public static void main(String[] args) { Map map = getClipBoardTypeMap(); Set> entries = map.entrySet(); for (Iterator iterator = entries.iterator(); iterator.hasNext(); ) { Entry entry = (Entry) iterator.next(); // EntityHelper.printDetail(entry.getKey()); // EntityHelper.printDetail(entry.getValue()); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy