Merge from the pain train
[official-gcc.git] / libjava / gnu / java / awt / peer / gtk / GtkClipboard.java
blobb9cc1613b7850cb97fcb6c165e64a9097cd48970
1 /* GtkClipboard.java
2 Copyright (C) 1999, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.awt.peer.gtk;
41 import java.awt.datatransfer.Clipboard;
42 import java.awt.datatransfer.ClipboardOwner;
43 import java.awt.datatransfer.DataFlavor;
44 import java.awt.datatransfer.StringSelection;
45 import java.awt.datatransfer.Transferable;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
50 public class GtkClipboard extends Clipboard
52 /* the number of milliseconds that we'll wait around for the
53 owner of the GDK_SELECTION_PRIMARY selection to convert
54 the requested data */
55 static final int SELECTION_RECEIVED_TIMEOUT = 5000;
57 /* We currently only support transferring of text between applications */
58 static String selection;
59 static Object selectionLock = new Object ();
61 static boolean hasSelection = false;
63 protected GtkClipboard()
65 super("System Clipboard");
66 initNativeState();
69 public Transferable getContents(Object requestor)
71 synchronized (this)
73 if (hasSelection)
74 return contents;
77 /* Java doesn't own the selection, so we need to ask X11 */
78 // XXX: Does this hold with Swing too ?
79 synchronized (selectionLock)
81 requestStringConversion();
83 try
85 selectionLock.wait(SELECTION_RECEIVED_TIMEOUT);
87 catch (InterruptedException e)
89 return null;
92 return selection == null ? null : new StringSelection(selection);
96 void stringSelectionReceived(String newSelection)
98 synchronized (selectionLock)
100 selection = newSelection;
101 selectionLock.notify();
105 /* convert Java clipboard data into a String suitable for sending
106 to another application */
107 synchronized String stringSelectionHandler() throws IOException
109 String selection = null;
113 if (contents.isDataFlavorSupported(DataFlavor.stringFlavor))
114 selection = (String)contents.getTransferData(DataFlavor.stringFlavor);
115 else if (contents.isDataFlavorSupported(DataFlavor.plainTextFlavor))
117 StringBuffer sbuf = new StringBuffer();
118 InputStreamReader reader;
119 char readBuf[] = new char[512];
120 int numChars;
122 reader = new InputStreamReader
123 ((InputStream)
124 contents.getTransferData(DataFlavor.plainTextFlavor), "UNICODE");
126 while (true)
128 numChars = reader.read(readBuf);
129 if (numChars == -1)
130 break;
131 sbuf.append(readBuf, 0, numChars);
134 selection = new String(sbuf);
137 catch (Exception e)
141 return selection;
144 public synchronized void setContents(Transferable contents,
145 ClipboardOwner owner)
147 selectionGet();
149 this.contents = contents;
150 this.owner = owner;
152 hasSelection = true;
155 synchronized void selectionClear()
157 hasSelection = false;
159 if (owner != null)
161 owner.lostOwnership(this, contents);
162 owner = null;
163 contents = null;
167 native void initNativeState();
168 static native void requestStringConversion();
169 static native void selectionGet();