KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > datatransfer > ClipboardUtils


1 package snow.datatransfer;
2
3 import java.util.*;
4 import java.awt.datatransfer.*;
5 import java.awt.Toolkit JavaDoc;
6
7 /** also offer a simple history.
8 */

9 public final class ClipboardUtils implements FlavorListener
10 {
11   Vector<String JavaDoc> history = new Vector<String JavaDoc>();
12
13   // singleton
14
private static ClipboardUtils clipboardUtils;
15   private ClipboardUtils()
16   {
17      Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(this);
18
19   } // Constructor
20

21   public synchronized static ClipboardUtils getInstance()
22   {
23      if(clipboardUtils==null) clipboardUtils = new ClipboardUtils();
24      return clipboardUtils;
25   }
26
27   public List<String JavaDoc> getHistory() { return this.history; }
28   public void clearHistory() { history.clear(); }
29   private void addToHistory(String JavaDoc txt)
30   {
31      if(history.contains(txt)) return;
32      {
33         history.add(0, txt);
34         if(history.size()>20)
35         {
36            history.setSize(20);
37         }
38      }
39   }
40
41   /** from system clipboard, occurs when CTRL+C is pressed somewhere in the whole system !
42   * excel, word, OR TIDE !
43   * java.awt.datatransfer.FlavorEvent[source=sun.awt.windows.WClipboard@1959352]
44   */

45   public void flavorsChanged(FlavorEvent _fe)
46   {
47      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
48      Transferable content = null;
49
50      for(int ti=0; ti<2; ti++) // [Jan2007] try again...
51
{
52         try
53         {
54            content = clipboard.getContents(null);
55         }
56         catch(IllegalStateException JavaDoc e)
57         {
58           // The method throws IllegalStateException if the clipboard is currently unavailable.
59
// For example, on some platforms, the system clipboard is unavailable while it is accessed by another application.
60
//e.printStackTrace();
61
// retry later...
62
try{ Thread.sleep(100); } catch(Exception JavaDoc exi) {}
63         }
64      }
65
66      if(content.isDataFlavorSupported(DataFlavor.stringFlavor))
67      {
68        try
69        {
70          String JavaDoc cont = (String JavaDoc) content.getTransferData(DataFlavor.stringFlavor);
71          addToHistory(cont);
72          // success
73
return;
74        }
75        catch(Exception JavaDoc e)
76        {
77          e.printStackTrace();
78        }
79      }
80   }
81
82   // general utils
83

84   public static void copyToClipboard(String JavaDoc txt)
85   {
86      if(txt==null) return;
87      //getInstance().add(txt);
88
//Toolkit.getDefaultToolkit().getSystemClipboard().setContents( new TransferableText(txt), null);
89
Toolkit.getDefaultToolkit().getSystemClipboard().setContents( new StringSelection(txt), null);
90
91      // comes in the history through the listener above !!
92
}
93
94   public static String JavaDoc getClipboardStringContent()
95   {
96      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
97      Transferable content = clipboard.getContents(null);
98      if(content.isDataFlavorSupported(DataFlavor.stringFlavor))
99      {
100        try
101        {
102          return ""+ content.getTransferData(DataFlavor.stringFlavor);
103        }
104        catch(Exception JavaDoc e)
105        {
106          return "error: "+e.getMessage();
107        }
108      }
109      else
110      {
111        System.out.println("nothing to paste !");
112        return "";
113      }
114
115   }
116
117
118 /* the StringSelection makes it exactely !!
119 *
120    static class TransferableText implements Transferable
121    {
122      String text;
123      public TransferableText(String text)
124      {
125         this.text = text;
126      }
127      public Object getTransferData(DataFlavor flavor)
128      {
129        if(flavor.equals(DataFlavor.stringFlavor))
130        {
131          return text;
132        }
133        return "?";
134      }
135      public DataFlavor[] getTransferDataFlavors()
136      {
137        return new DataFlavor[]{DataFlavor.stringFlavor};
138      }
139
140      public boolean isDataFlavorSupported(DataFlavor flavor)
141      {
142        return flavor.equals( DataFlavor.stringFlavor);
143      }
144    }
145    */

146
147    public static void main(String JavaDoc[] arguments)
148    {
149       copyToClipboard( "Hello323");
150    }
151
152 }
Popular Tags