01: /*
02: * KillRing.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: KillRing.java,v 1.2 2003/08/13 15:16:01 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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;
25: import java.awt.datatransfer.Clipboard;
26: import java.awt.datatransfer.ClipboardOwner;
27: import java.awt.datatransfer.StringSelection;
28: import java.awt.datatransfer.Transferable;
29:
30: public final class KillRing extends Ring implements ClipboardOwner {
31: private ClipboardOwner clipboardOwner; // Either this or null.
32:
33: public KillRing() {
34: super (30);
35: }
36:
37: public void copyLastKillToSystemClipboard() {
38: String kill = pop();
39: if (kill != null)
40: setClipboardContents(kill);
41: }
42:
43: public void promoteLastPaste() {
44: promoteLast();
45: // Don't change the contents of the system clipboard unless we are the
46: // clipboard owner!
47: if (clipboardOwner == this )
48: setClipboardContents(peek());
49: }
50:
51: private void setClipboardContents(String s) {
52: // Work around Java bug 4213197. Make sure the string we put on the
53: // system clipboard was not generated by a substring operation.
54: StringSelection ss = new StringSelection(new String(s));
55: try {
56: Toolkit.getDefaultToolkit().getSystemClipboard()
57: .setContents(ss, this );
58: clipboardOwner = this ;
59: } catch (IllegalStateException e) {
60: Log.debug(e);
61: }
62: }
63:
64: public void lostOwnership(Clipboard clipboard, Transferable contents) {
65: clipboardOwner = null;
66: }
67: }
|