1 36 37 import javax.jnlp.ClipboardService; 38 import javax.jnlp.ServiceManager; 39 import javax.jnlp.UnavailableServiceException; 40 import java.awt.datatransfer.Transferable ; 41 import java.awt.datatransfer.StringSelection ; 42 import java.awt.datatransfer.DataFlavor ; 43 import java.io.IOException ; 44 45 47 public class ClipboardHandler { 48 static private boolean _initialized = false; 49 static private ClipboardService _cs = null; 50 static private String _content = ""; 51 52 public static void toClipboard(String txt) { 53 System.out.println("toClipboard: " + txt); 54 initialize(); 55 if (_cs != null) { 56 _cs.setContents(new StringSelection (txt)); 57 } else { 58 _content = txt; 59 } 60 } 61 62 public static String fromClipboard() { 63 System.out.println("fromClipboard: "); 64 initialize(); 65 if (_cs != null) { 66 Transferable t = _cs.getContents(); 67 try { 68 return (String )t.getTransferData(DataFlavor.stringFlavor); 69 } catch(Throwable ex) { 70 System.out.println(ex); 71 return ""; 72 } 73 } else { 74 return _content; 75 } 76 } 77 78 static private synchronized void initialize() { 79 if (_initialized) return; 80 _initialized = true; 81 try { 82 _cs = (ClipboardService)ServiceManager.lookup("javax.jnlp.ClipboardService"); 83 } catch(UnavailableServiceException e) { 84 _cs = null; 85 } 86 } 87 } 88 | Popular Tags |