1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
public class Common {}
class FlavorListenerImpl implements FlavorListener {
public boolean notified1, notified2;
private int count;
public void flavorsChanged(FlavorEvent evt) {
switch (count) {
case 0:
notified1 = true;
break;
case 1:
notified2 = true;
break;
}
count++;
System.err.println("listener's " + this +
" flavorChanged() called " + count + " time");
}
public String toString() {
return "notified1=" + notified1 + " notified2=" + notified2 +
" count=" + count;
}
};
class Util {
public static void setClipboardContents(Clipboard cb,
Transferable contents,
ClipboardOwner owner) {
while (true) {
try {
cb.setContents(contents, owner);
return;
} catch (IllegalStateException ise) {
ise.printStackTrace();
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit
.getDefaultToolkit().
createImage(new java.awt.image.MemoryImageSource(
w, h, pix, 0, w
));
}
}
class TransferableUnion implements Transferable {
private static final DataFlavor[] ZERO_LENGTH_ARRAY = new DataFlavor[0];
private final Transferable TRANSF1, TRANSF2;
private final DataFlavor[] FLAVORS;
public TransferableUnion(Transferable t1, Transferable t2) {
if (t1 == null) {
throw new NullPointerException("t1");
}
if (t2 == null) {
throw new NullPointerException("t2");
}
this.TRANSF1 = t1;
this.TRANSF2 = t2;
java.util.Set<DataFlavor> flavorSet = new java.util.HashSet<>();
flavorSet.addAll(java.util.Arrays.asList(t1.getTransferDataFlavors()));
flavorSet.addAll(java.util.Arrays.asList(t2.getTransferDataFlavors()));
FLAVORS = flavorSet.toArray(ZERO_LENGTH_ARRAY);
}
/**
* Returns an array of flavors in which this <code>Transferable</code>
* can provide the data.
*/
public DataFlavor[] getTransferDataFlavors() {
return FLAVORS.clone();
}
/**
* Returns whether the requested flavor is supported by this
* <code>Transferable</code>.
*
* @param flavor the requested flavor for the data
* @throws NullPointerException if flavor is <code>null</code>
*/
public boolean isDataFlavorSupported(DataFlavor flavor) {
if (flavor == null) {
throw new NullPointerException("flavor");
}
return TRANSF1.isDataFlavorSupported(flavor)
|| TRANSF2.isDataFlavorSupported(flavor);
}
/**
* Returns the <code>Transferable</code>'s data in the requested
* <code>DataFlavor</code> if possible.
*
* @param flavor the requested flavor for the data
* @return the data in the requested flavor
* @throws UnsupportedFlavorException if the requested data flavor is
* not supported by this Transferable
* @throws IOException if an <code>IOException</code> occurs while
* retrieving the data.
* @throws NullPointerException if flavor is <code>null</code>
*/
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, java.io.IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
java.io.IOException ioexc = null;
if (TRANSF1.isDataFlavorSupported(flavor)) {
try {
return TRANSF1.getTransferData(flavor);
} catch (java.io.IOException exc) {
ioexc = exc;
}
}
if (TRANSF2.isDataFlavorSupported(flavor)) {
return TRANSF2.getTransferData(flavor);
}
if (ioexc != null) {
throw ioexc;
}
// unreachable
return null;
}
}
/**
* A <code>Transferable</code> that implements the capability required
* to transfer an <code>Image</code>.
*
* This <code>Transferable</code> properly supports
* <code>DataFlavor.imageFlavor</code>
* and all equivalent flavors.
* No other <code>DataFlavor</code>s are supported.
*
* @see java.awt.datatransfer.DataFlavor.imageFlavor
*/
class ImageSelection implements Transferable {
private static final DataFlavor[] flavors = { DataFlavor.imageFlavor };
private Image data;
/**
* Creates a <code>Transferable</code> capable of transferring
* the specified <code>Image</code>.
*/
public ImageSelection(Image data) {
this.data = data;
}
/**
* Returns an array of flavors in which this <code>Transferable</code>
* can provide the data. <code>DataFlavor.stringFlavor</code>
* is supported.
*
* @return an array of length one, whose element is <code>DataFlavor.
* imageFlavor</code>
*/
public DataFlavor[] getTransferDataFlavors() {
return flavors.clone();
}
/**
* Returns whether the requested flavor is supported by this
* <code>Transferable</code>.
*
* @param flavor the requested flavor for the data
* @return true if <code>flavor</code> is equal to
* <code>DataFlavor.imageFlavor</code>;
* false otherwise
* @throws NullPointerException if flavor is <code>null</code>
*/
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (int i = 0; i < flavors.length; i++) {
if (flavor.equals(flavors[i])) {
return true;
}
}
return false;
}
/**
* Returns the <code>Transferable</code>'s data in the requested
* <code>DataFlavor</code> if possible. If the desired flavor is
* <code>DataFlavor.imageFlavor</code>, or an equivalent flavor,
* the <code>Image</code> representing the selection is
* returned.
*
* @param flavor the requested flavor for the data
* @return the data in the requested flavor, as outlined above
* @throws UnsupportedFlavorException if the requested data flavor is
* not equivalent to <code>DataFlavor.imageFlavor</code>
* @throws IOException if an <code>IOException</code> occurs while
* retrieving the data. By default, <code>ImageSelection</code>
* never throws this exception, but a subclass may.
* @throws NullPointerException if flavor is <code>null</code>
*/
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, java.io.IOException {
if (flavor.equals(DataFlavor.imageFlavor)) {
return data;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
} // class ImageSelection
|