| 1 |
|
| 2 |
package org.openstreetmap.josm.gui.datatransfer; |
| 3 |
|
| 4 |
import java.awt.GraphicsEnvironment; |
| 5 |
import java.awt.HeadlessException; |
| 6 |
import java.awt.Toolkit; |
| 7 |
import java.awt.datatransfer.Clipboard; |
| 8 |
import java.awt.datatransfer.ClipboardOwner; |
| 9 |
import java.awt.datatransfer.DataFlavor; |
| 10 |
import java.awt.datatransfer.StringSelection; |
| 11 |
import java.awt.datatransfer.Transferable; |
| 12 |
import java.awt.datatransfer.UnsupportedFlavorException; |
| 13 |
import java.io.IOException; |
| 14 |
|
| 15 |
import org.openstreetmap.josm.gui.util.GuiHelper; |
| 16 |
import org.openstreetmap.josm.tools.Logging; |
| 17 |
import org.openstreetmap.josm.tools.Utils; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public final class ClipboardUtils { |
| 26 |
private static final class DoNothingClipboardOwner implements ClipboardOwner { |
| 27 |
@Override |
| 28 |
public void lostOwnership(Clipboard clpbrd, Transferable t) { |
| 29 |
|
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
private static Clipboard clipboard; |
| 34 |
|
| 35 |
private ClipboardUtils() { |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static synchronized Clipboard getClipboard() { |
| 47 |
|
| 48 |
if (clipboard == null) { |
| 49 |
try { |
| 50 |
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 51 |
} catch (HeadlessException | SecurityException e) { |
| 52 |
Logging.warn("Using fake clipboard.", e); |
| 53 |
clipboard = new Clipboard("fake"); |
| 54 |
} |
| 55 |
} |
| 56 |
return clipboard; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static Clipboard getSystemSelection() { |
| 67 |
if (GraphicsEnvironment.isHeadless()) { |
| 68 |
return null; |
| 69 |
} else { |
| 70 |
return Toolkit.getDefaultToolkit().getSystemSelection(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static String getClipboardStringContent() { |
| 79 |
try { |
| 80 |
Transferable t = getClipboardContent(); |
| 81 |
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { |
| 82 |
return (String) t.getTransferData(DataFlavor.stringFlavor); |
| 83 |
} |
| 84 |
} catch (UnsupportedFlavorException | IOException ex) { |
| 85 |
Logging.error(ex); |
| 86 |
} |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static synchronized Transferable getClipboardContent() { |
| 95 |
return getClipboardContent(getClipboard()); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static Transferable getClipboardContent(Clipboard clipboard) { |
| 104 |
Transferable t = null; |
| 105 |
for (int tries = 0; t == null && tries < 10; tries++) { |
| 106 |
try { |
| 107 |
t = clipboard.getContents(null); |
| 108 |
} catch (IllegalStateException e) { |
| 109 |
|
| 110 |
|
| 111 |
Logging.trace("Clipboard unavailable.", e); |
| 112 |
try { |
| 113 |
Thread.sleep(1); |
| 114 |
} catch (InterruptedException ex) { |
| 115 |
Logging.log(Logging.LEVEL_WARN, "InterruptedException in " + Utils.class.getSimpleName() |
| 116 |
+ " while getting clipboard content", ex); |
| 117 |
Thread.currentThread().interrupt(); |
| 118 |
} |
| 119 |
} catch (NullPointerException | NoClassDefFoundError e) { |
| 120 |
|
| 121 |
|
| 122 |
Logging.error(e); |
| 123 |
} |
| 124 |
} |
| 125 |
return t; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public static boolean copyString(String s) { |
| 134 |
return copy(new StringSelection(s)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static boolean copy(final Transferable transferable) { |
| 143 |
return GuiHelper.runInEDTAndWaitAndReturn(() -> { |
| 144 |
try { |
| 145 |
getClipboard().setContents(transferable, new DoNothingClipboardOwner()); |
| 146 |
return Boolean.TRUE; |
| 147 |
} catch (IllegalStateException ex) { |
| 148 |
Logging.error(ex); |
| 149 |
return Boolean.FALSE; |
| 150 |
} |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static DataFlavor newDataFlavor(Class<?> c, String humanPresentableName) { |
| 162 |
try { |
| 163 |
return new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class=" + c.getName(), |
| 164 |
humanPresentableName, c.getClassLoader()); |
| 165 |
} catch (ClassNotFoundException e) { |
| 166 |
throw new IllegalArgumentException(e); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public static boolean clear() { |
| 176 |
|
| 177 |
return copy(new StringSelection("")); |
| 178 |
} |
| 179 |
} |