KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > apollo > SecureClipboardHandler


1 /*
2 ** Apollo - Test Skeleton Toolkit for Web Start/JNLP
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package apollo;
24
25 import java.awt.datatransfer.*;
26
27 class SecureClipboardHandler
28 {
29    private static SecureClipboardHandler _handler;
30
31    private ClipboardService _clipboardService;
32
33    private SecureClipboardHandler()
34    {
35       _clipboardService = ServiceManager.lookupClipboardService();
36    }
37
38
39    public void setContents( Transferable trans )
40    {
41       _clipboardService.setContents( trans );
42    }
43
44    public static SecureClipboardHandler getHandler()
45    {
46       if( _handler == null )
47          _handler = new SecureClipboardHandler();
48       return _handler;
49    }
50
51    public Transferable getContents()
52    {
53       return _clipboardService.getContents();
54    }
55
56    public void copyText( String JavaDoc text )
57    {
58       // aka copy to clipboard
59
setContents( new StringSelection( text ) );
60    }
61
62    public String JavaDoc pasteText()
63    {
64       // aka paste from clipboard
65
Transferable t = getContents();
66       try
67       {
68          return ( String JavaDoc ) t.getTransferData( DataFlavor.stringFlavor );
69       }
70       catch( Exception JavaDoc ex )
71       {
72          // todo: should I catch exception?
73
// should I print a error message
74
// should I return null instead of an empty string?
75
return "";
76       }
77    }
78 }
79
Popular Tags