KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > KillRing


1 /*
2  * KillRing.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: KillRing.java,v 1.2 2003/08/13 15:16:01 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.datatransfer.Clipboard JavaDoc;
26 import java.awt.datatransfer.ClipboardOwner JavaDoc;
27 import java.awt.datatransfer.StringSelection JavaDoc;
28 import java.awt.datatransfer.Transferable JavaDoc;
29
30 public final class KillRing extends Ring implements ClipboardOwner JavaDoc
31 {
32     private ClipboardOwner JavaDoc clipboardOwner; // Either this or null.
33

34     public KillRing()
35     {
36         super(30);
37     }
38
39     public void copyLastKillToSystemClipboard()
40     {
41         String JavaDoc kill = pop();
42         if (kill != null)
43             setClipboardContents(kill);
44     }
45
46     public void promoteLastPaste()
47     {
48         promoteLast();
49         // Don't change the contents of the system clipboard unless we are the
50
// clipboard owner!
51
if (clipboardOwner == this)
52             setClipboardContents(peek());
53     }
54
55     private void setClipboardContents(String JavaDoc s)
56     {
57         // Work around Java bug 4213197. Make sure the string we put on the
58
// system clipboard was not generated by a substring operation.
59
StringSelection JavaDoc ss = new StringSelection JavaDoc(new String JavaDoc(s));
60         try {
61             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, this);
62             clipboardOwner = this;
63         }
64         catch (IllegalStateException JavaDoc e) {
65             Log.debug(e);
66         }
67     }
68
69     public void lostOwnership(Clipboard JavaDoc clipboard, Transferable JavaDoc contents)
70     {
71         clipboardOwner = null;
72     }
73 }
74
Popular Tags