001: package org.uispec4j;
002:
003: import java.awt.*;
004: import java.awt.datatransfer.*;
005: import java.io.*;
006: import java.nio.ByteBuffer;
007: import java.nio.CharBuffer;
008:
009: /**
010: * Utility for changing and checking the contents of the system's clipboard.
011: */
012: public class Clipboard {
013:
014: public static final TextType HTML = new TextType("text/html");
015: public static final TextType PLAIN = new TextType("text/plain");
016:
017: public static final Charset UTF8 = new Charset("UTF-8");
018: public static final Charset UTF16 = new Charset("UTF-16");
019: public static final Charset UNICODE = new Charset("unicode");
020: public static final Charset US_ASCII = new Charset("US-ASCII");
021:
022: public static final TransferType READER = new TransferType(
023: Reader.class);
024: public static final TransferType INPUT_STREAM = new TransferType(
025: InputStream.class);
026: public static final TransferType CHAR_BUFFER = new TransferType(
027: CharBuffer.class);
028: public static final TransferType BYTE_BUFFER = new TransferType(
029: ByteBuffer.class);
030:
031: private Clipboard() {
032: }
033:
034: /**
035: * Dumps a given text (either String or StringBuffer) into the Clipboard, with a default MIME type
036: */
037: public static void putText(CharSequence data) {
038: StringSelection copy = new StringSelection(data.toString());
039: getSystemClipboard().setContents(copy, copy);
040: }
041:
042: /**
043: * Dumps a given text (either String or StringBuffer) into the Clipboard with a specified MIME type
044: */
045: public static void putText(TextType type, Charset charset,
046: TransferType transferType, CharSequence data) {
047: String mimeType = type + "; charset=" + charset + "; class="
048: + transferType;
049: TextTransferable transferable = new TextTransferable(mimeType,
050: data.toString());
051: getSystemClipboard().setContents(transferable, transferable);
052: }
053:
054: private static java.awt.datatransfer.Clipboard getSystemClipboard() {
055: return Toolkit.getDefaultToolkit().getSystemClipboard();
056: }
057:
058: private static class TextTransferable implements Transferable,
059: ClipboardOwner {
060: private String data;
061: private DataFlavor flavor;
062:
063: public TextTransferable(String mimeType, String data) {
064: flavor = new DataFlavor(mimeType, "Text");
065: this .data = data;
066: }
067:
068: public DataFlavor[] getTransferDataFlavors() {
069: return new DataFlavor[] { flavor };
070: }
071:
072: public boolean isDataFlavorSupported(DataFlavor flavor) {
073: boolean b = this .flavor.getPrimaryType().equals(
074: flavor.getPrimaryType());
075: return b;
076: }
077:
078: public Object getTransferData(DataFlavor flavor)
079: throws UnsupportedFlavorException, IOException {
080: if (flavor.isRepresentationClassInputStream()) {
081: return new StringBufferInputStream(data);
082: } else if (flavor.isRepresentationClassReader()) {
083: return new StringReader(data);
084: } else if (flavor.isRepresentationClassCharBuffer()) {
085: return CharBuffer.wrap(data);
086: } else if (flavor.isRepresentationClassByteBuffer()) {
087: return ByteBuffer.wrap(data.getBytes());
088: }
089: throw new UnsupportedFlavorException(flavor);
090: }
091:
092: public void lostOwnership(
093: java.awt.datatransfer.Clipboard clipboard,
094: Transferable contents) {
095: }
096: }
097:
098: /**
099: * Enumeration for the text type property in MIME types
100: */
101: public static class TextType {
102: private String type;
103:
104: private TextType(String type) {
105: this .type = type;
106: }
107:
108: public String toString() {
109: return type;
110: }
111: }
112:
113: /**
114: * Enumeration for the charset property in MIME types (UTF-8, UTF-16, etc.)
115: */
116: public static class Charset {
117: private String name;
118:
119: private Charset(String name) {
120: this .name = name;
121: }
122:
123: public String toString() {
124: return name;
125: }
126: }
127:
128: /**
129: * Enumeration for the transfert type property in MIME types (InputStream, CharBuffer, etc.)
130: */
131: public static class TransferType {
132: private Class dataClass;
133:
134: private TransferType(Class streamClass) {
135: this .dataClass = streamClass;
136: }
137:
138: public Class getDataClass() {
139: return dataClass;
140: }
141:
142: public String toString() {
143: return dataClass.getName();
144: }
145: }
146:
147: }
|