package sun.awt.X11;
import java.awt.datatransfer.Transferable;
import java.util.SortedMap;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.io.IOException;
import java.security.AccessController;
import sun.awt.datatransfer.DataTransferer;
import sun.awt.datatransfer.SunClipboard;
import sun.awt.datatransfer.ClipboardTransferable;
import sun.security.action.GetIntegerAction;
public class XClipboard extends SunClipboard implements Runnable {
private final XSelection selection;
private static final Object classLock = new Object();
private static final int defaultPollInterval = 200;
private static int pollInterval;
private static Set listenedClipboards;
public XClipboard(String name, String selectionName) {
super(name);
selection = new XSelection(XAtom.get(selectionName), this);
}
public void run() {
lostOwnershipImpl();
}
protected synchronized void setContentsNative(Transferable contents) {
SortedMap formatMap = DataTransferer.getInstance().getFormatsForTransferable
(contents, DataTransferer.adaptFlavorMap(getDefaultFlavorTable()));
long[] formats =
DataTransferer.getInstance().keysToLongArray(formatMap);
if (!selection.setOwner(contents, formatMap, formats,
XToolkit.getCurrentServerTime())) {
this.owner = null;
this.contents = null;
}
}
public long getID() {
return selection.getSelectionAtom().getAtom();
}
public synchronized Transferable getContents(Object requestor) {
if (contents != null) {
return contents;
}
return new ClipboardTransferable(this);
}
protected void clearNativeContext() {
selection.reset();
}
protected long[] getClipboardFormats() {
return selection.getTargets(XToolkit.getCurrentServerTime());
}
protected byte[] getClipboardData(long format) throws IOException {
return selection.getData(format, XToolkit.getCurrentServerTime());
}
public void checkChange(long[] formats) {
if (!selection.isOwner()) {
super.checkChange(formats);
}
}
void checkChangeHere(Transferable contents) {
if (areFlavorListenersRegistered()) {
super.checkChange(DataTransferer.getInstance().
getFormatsForTransferableAsArray(contents, getDefaultFlavorTable()));
}
}
protected void registerClipboardViewerChecked() {
if (pollInterval <= 0) {
pollInterval = ((Integer)AccessController.doPrivileged(
new GetIntegerAction("awt.datatransfer.clipboard.poll.interval",
defaultPollInterval))).intValue();
if (pollInterval <= 0) {
pollInterval = defaultPollInterval;
}
}
selection.initializeSelectionForTrackingChanges();
boolean mustSchedule = false;
synchronized (XClipboard.classLock) {
if (listenedClipboards == null) {
listenedClipboards = new HashSet(2);
}
mustSchedule = listenedClipboards.isEmpty();
listenedClipboards.add(this);
}
if (mustSchedule) {
XToolkit.schedule(new CheckChangeTimerTask(), pollInterval);
}
}
private static class CheckChangeTimerTask implements Runnable {
public void run() {
for (Iterator iter = listenedClipboards.iterator(); iter.hasNext();) {
XClipboard clpbrd = (XClipboard)iter.next();
clpbrd.selection.getTargetsDelayed();
}
synchronized (XClipboard.classLock) {
if (listenedClipboards != null && !listenedClipboards.isEmpty()) {
XToolkit.schedule(this, pollInterval);
}
}
}
}
protected void unregisterClipboardViewerChecked() {
selection.deinitializeSelectionForTrackingChanges();
synchronized (XClipboard.classLock) {
listenedClipboards.remove(this);
}
}
}