001: package snow.datatransfer;
002:
003: import java.util.*;
004: import java.awt.datatransfer.*;
005: import java.awt.Toolkit;
006:
007: /** also offer a simple history.
008: */
009: public final class ClipboardUtils implements FlavorListener {
010: Vector<String> history = new Vector<String>();
011:
012: // singleton
013: private static ClipboardUtils clipboardUtils;
014:
015: private ClipboardUtils() {
016: Toolkit.getDefaultToolkit().getSystemClipboard()
017: .addFlavorListener(this );
018:
019: } // Constructor
020:
021: public synchronized static ClipboardUtils getInstance() {
022: if (clipboardUtils == null)
023: clipboardUtils = new ClipboardUtils();
024: return clipboardUtils;
025: }
026:
027: public List<String> getHistory() {
028: return this .history;
029: }
030:
031: public void clearHistory() {
032: history.clear();
033: }
034:
035: private void addToHistory(String txt) {
036: if (history.contains(txt))
037: return;
038: {
039: history.add(0, txt);
040: if (history.size() > 20) {
041: history.setSize(20);
042: }
043: }
044: }
045:
046: /** from system clipboard, occurs when CTRL+C is pressed somewhere in the whole system !
047: * excel, word, OR TIDE !
048: * java.awt.datatransfer.FlavorEvent[source=sun.awt.windows.WClipboard@1959352]
049: */
050: public void flavorsChanged(FlavorEvent _fe) {
051: Clipboard clipboard = Toolkit.getDefaultToolkit()
052: .getSystemClipboard();
053: Transferable content = null;
054:
055: for (int ti = 0; ti < 2; ti++) // [Jan2007] try again...
056: {
057: try {
058: content = clipboard.getContents(null);
059: } catch (IllegalStateException e) {
060: // The method throws IllegalStateException if the clipboard is currently unavailable.
061: // For example, on some platforms, the system clipboard is unavailable while it is accessed by another application.
062: //e.printStackTrace();
063: // retry later...
064: try {
065: Thread.sleep(100);
066: } catch (Exception exi) {
067: }
068: }
069: }
070:
071: if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
072: try {
073: String cont = (String) content
074: .getTransferData(DataFlavor.stringFlavor);
075: addToHistory(cont);
076: // success
077: return;
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082: }
083:
084: // general utils
085:
086: public static void copyToClipboard(String txt) {
087: if (txt == null)
088: return;
089: //getInstance().add(txt);
090: //Toolkit.getDefaultToolkit().getSystemClipboard().setContents( new TransferableText(txt), null);
091: Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
092: new StringSelection(txt), null);
093:
094: // comes in the history through the listener above !!
095: }
096:
097: public static String getClipboardStringContent() {
098: Clipboard clipboard = Toolkit.getDefaultToolkit()
099: .getSystemClipboard();
100: Transferable content = clipboard.getContents(null);
101: if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
102: try {
103: return ""
104: + content
105: .getTransferData(DataFlavor.stringFlavor);
106: } catch (Exception e) {
107: return "error: " + e.getMessage();
108: }
109: } else {
110: System.out.println("nothing to paste !");
111: return "";
112: }
113:
114: }
115:
116: /* the StringSelection makes it exactely !!
117: *
118: static class TransferableText implements Transferable
119: {
120: String text;
121: public TransferableText(String text)
122: {
123: this.text = text;
124: }
125: public Object getTransferData(DataFlavor flavor)
126: {
127: if(flavor.equals(DataFlavor.stringFlavor))
128: {
129: return text;
130: }
131: return "?";
132: }
133: public DataFlavor[] getTransferDataFlavors()
134: {
135: return new DataFlavor[]{DataFlavor.stringFlavor};
136: }
137:
138: public boolean isDataFlavorSupported(DataFlavor flavor)
139: {
140: return flavor.equals( DataFlavor.stringFlavor);
141: }
142: }
143: */
144:
145: public static void main(String[] arguments) {
146: copyToClipboard("Hello323");
147: }
148:
149: }
|