50 * @author Alexander Gerasimov
51 */
52 public class Clipboard {
53
54 String name;
55
56 /**
57 * The owner of the clipboard.
58 */
59 protected ClipboardOwner owner;
60 /**
61 * Contents of the clipboard.
62 */
63 protected Transferable contents;
64
65 /**
66 * An aggregate of flavor listeners registered on this local clipboard.
67 *
68 * @since 1.5
69 */
70 private final Set<FlavorListener> flavorListeners = new HashSet<>();
71
72 /**
73 * A set of <code>DataFlavor</code>s that is available on
74 * this local clipboard. It is used for tracking changes
75 * of <code>DataFlavor</code>s available on this clipboard.
76 *
77 * @since 1.5
78 */
79 private Set<DataFlavor> currentDataFlavors;
80
81 /**
82 * Creates a clipboard object.
83 * @param name for the clipboard
84 * @see java.awt.Toolkit#getSystemClipboard
85 */
86 public Clipboard(String name) {
87 this.name = name;
88 currentDataFlavors = getAvailableDataFlavorSet();
89 }
90
91 /**
92 * Returns the name of this clipboard object.
93 * @return the name of this clipboard object
94 *
95 * @see java.awt.Toolkit#getSystemClipboard
96 */
97 public String getName() {
98 return name;
99 }
100
101 /**
102 * Sets the current contents of the clipboard to the specified
103 * transferable object and registers the specified clipboard owner
104 * as the owner of the new contents.
105 * <p>
106 * If there is an existing owner different from the argument
107 * <code>owner</code>, that owner is notified that it no longer
108 * holds ownership of the clipboard contents via an invocation
240
241
242 /**
243 * Registers the specified <code>FlavorListener</code> to receive
244 * <code>FlavorEvent</code>s from this clipboard.
245 * If <code>listener</code> is <code>null</code>, no exception
246 * is thrown and no action is performed.
247 *
248 * @param listener the listener to be added
249 *
250 * @see #removeFlavorListener
251 * @see #getFlavorListeners
252 * @see FlavorListener
253 * @see FlavorEvent
254 * @since 1.5
255 */
256 public synchronized void addFlavorListener(FlavorListener listener) {
257 if (listener == null) {
258 return;
259 }
260 flavorListeners.add(listener);
261 }
262
263 /**
264 * Removes the specified <code>FlavorListener</code> so that it no longer
265 * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
266 * This method performs no function, nor does it throw an exception, if
267 * the listener specified by the argument was not previously added to this
268 * <code>Clipboard</code>.
269 * If <code>listener</code> is <code>null</code>, no exception
270 * is thrown and no action is performed.
271 *
272 * @param listener the listener to be removed
273 *
274 * @see #addFlavorListener
275 * @see #getFlavorListeners
276 * @see FlavorListener
277 * @see FlavorEvent
278 * @since 1.5
279 */
280 public synchronized void removeFlavorListener(FlavorListener listener) {
281 if (listener == null) {
282 return;
283 }
284 flavorListeners.remove(listener);
285 }
286
287 /**
288 * Returns an array of all the <code>FlavorListener</code>s currently
289 * registered on this <code>Clipboard</code>.
290 *
291 * @return all of this clipboard's <code>FlavorListener</code>s or an empty
292 * array if no listeners are currently registered
293 * @see #addFlavorListener
294 * @see #removeFlavorListener
295 * @see FlavorListener
296 * @see FlavorEvent
297 * @since 1.5
298 */
299 public synchronized FlavorListener[] getFlavorListeners() {
300 return flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
301 }
302
303 /**
304 * Checks change of the <code>DataFlavor</code>s and, if necessary,
305 * notifies all listeners that have registered interest for notification
306 * on <code>FlavorEvent</code>s.
307 *
308 * @since 1.5
309 */
310 private void fireFlavorsChanged() {
311 Set<DataFlavor> prevDataFlavors = currentDataFlavors;
312 currentDataFlavors = getAvailableDataFlavorSet();
313 if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
314 return;
315 }
316 flavorListeners.forEach(listener ->
317 EventQueue.invokeLater(() ->
318 listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
319 }
320
321 /**
322 * Returns a set of <code>DataFlavor</code>s currently available
323 * on this clipboard.
324 *
325 * @return a set of <code>DataFlavor</code>s currently available
326 * on this clipboard
327 *
328 * @since 1.5
329 */
330 private Set<DataFlavor> getAvailableDataFlavorSet() {
|
50 * @author Alexander Gerasimov
51 */
52 public class Clipboard {
53
54 String name;
55
56 /**
57 * The owner of the clipboard.
58 */
59 protected ClipboardOwner owner;
60 /**
61 * Contents of the clipboard.
62 */
63 protected Transferable contents;
64
65 /**
66 * An aggregate of flavor listeners registered on this local clipboard.
67 *
68 * @since 1.5
69 */
70 private Set<FlavorListener> flavorListeners;
71
72 /**
73 * A set of <code>DataFlavor</code>s that is available on
74 * this local clipboard. It is used for tracking changes
75 * of <code>DataFlavor</code>s available on this clipboard.
76 *
77 * @since 1.5
78 */
79 private Set<DataFlavor> currentDataFlavors;
80
81 /**
82 * Creates a clipboard object.
83 * @param name for the clipboard
84 * @see java.awt.Toolkit#getSystemClipboard
85 */
86 public Clipboard(String name) {
87 this.name = name;
88 }
89
90 /**
91 * Returns the name of this clipboard object.
92 * @return the name of this clipboard object
93 *
94 * @see java.awt.Toolkit#getSystemClipboard
95 */
96 public String getName() {
97 return name;
98 }
99
100 /**
101 * Sets the current contents of the clipboard to the specified
102 * transferable object and registers the specified clipboard owner
103 * as the owner of the new contents.
104 * <p>
105 * If there is an existing owner different from the argument
106 * <code>owner</code>, that owner is notified that it no longer
107 * holds ownership of the clipboard contents via an invocation
239
240
241 /**
242 * Registers the specified <code>FlavorListener</code> to receive
243 * <code>FlavorEvent</code>s from this clipboard.
244 * If <code>listener</code> is <code>null</code>, no exception
245 * is thrown and no action is performed.
246 *
247 * @param listener the listener to be added
248 *
249 * @see #removeFlavorListener
250 * @see #getFlavorListeners
251 * @see FlavorListener
252 * @see FlavorEvent
253 * @since 1.5
254 */
255 public synchronized void addFlavorListener(FlavorListener listener) {
256 if (listener == null) {
257 return;
258 }
259
260 if (flavorListeners == null) {
261 flavorListeners = new HashSet<>();
262 currentDataFlavors = getAvailableDataFlavorSet();
263 }
264
265 flavorListeners.add(listener);
266 }
267
268 /**
269 * Removes the specified <code>FlavorListener</code> so that it no longer
270 * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>.
271 * This method performs no function, nor does it throw an exception, if
272 * the listener specified by the argument was not previously added to this
273 * <code>Clipboard</code>.
274 * If <code>listener</code> is <code>null</code>, no exception
275 * is thrown and no action is performed.
276 *
277 * @param listener the listener to be removed
278 *
279 * @see #addFlavorListener
280 * @see #getFlavorListeners
281 * @see FlavorListener
282 * @see FlavorEvent
283 * @since 1.5
284 */
285 public synchronized void removeFlavorListener(FlavorListener listener) {
286 if (listener == null || flavorListeners == null) {
287 return;
288 }
289 flavorListeners.remove(listener);
290 }
291
292 /**
293 * Returns an array of all the <code>FlavorListener</code>s currently
294 * registered on this <code>Clipboard</code>.
295 *
296 * @return all of this clipboard's <code>FlavorListener</code>s or an empty
297 * array if no listeners are currently registered
298 * @see #addFlavorListener
299 * @see #removeFlavorListener
300 * @see FlavorListener
301 * @see FlavorEvent
302 * @since 1.5
303 */
304 public synchronized FlavorListener[] getFlavorListeners() {
305 return flavorListeners == null ? new FlavorListener[0] :
306 flavorListeners.toArray(new FlavorListener[flavorListeners.size()]);
307 }
308
309 /**
310 * Checks change of the <code>DataFlavor</code>s and, if necessary,
311 * notifies all listeners that have registered interest for notification
312 * on <code>FlavorEvent</code>s.
313 *
314 * @since 1.5
315 */
316 private void fireFlavorsChanged() {
317 if (flavorListeners == null) {
318 return;
319 }
320
321 Set<DataFlavor> prevDataFlavors = currentDataFlavors;
322 currentDataFlavors = getAvailableDataFlavorSet();
323 if (Objects.equals(prevDataFlavors, currentDataFlavors)) {
324 return;
325 }
326 flavorListeners.forEach(listener ->
327 EventQueue.invokeLater(() ->
328 listener.flavorsChanged(new FlavorEvent(Clipboard.this))));
329 }
330
331 /**
332 * Returns a set of <code>DataFlavor</code>s currently available
333 * on this clipboard.
334 *
335 * @return a set of <code>DataFlavor</code>s currently available
336 * on this clipboard
337 *
338 * @since 1.5
339 */
340 private Set<DataFlavor> getAvailableDataFlavorSet() {
|