src/java.desktop/share/classes/sun/awt/datatransfer/SunClipboard.java

Print this page




 127                 contentsContext.removePropertyChangeListener
 128                     (AppContext.DISPOSED_PROPERTY_NAME, this);
 129             }
 130             contentsContext = context;
 131         }
 132     }
 133 
 134     public synchronized Transferable getContents(Object requestor) {
 135         if (contents != null) {
 136             return contents;
 137         }
 138         return new ClipboardTransferable(this);
 139     }
 140 
 141 
 142     /**
 143      * @return the contents of this clipboard if it has been set from the same
 144      *         AppContext as it is currently retrieved or null otherwise
 145      * @since 1.5
 146      */
 147     private synchronized Transferable getContextContents() {
 148         AppContext context = AppContext.getAppContext();
 149         return (context == contentsContext) ? contents : null;
 150     }
 151 
 152 
 153     /**
 154      * @see java.awt.Clipboard#getAvailableDataFlavors
 155      * @since 1.5
 156      */
 157     public DataFlavor[] getAvailableDataFlavors() {
 158         Transferable cntnts = getContextContents();
 159         if (cntnts != null) {
 160             return cntnts.getTransferDataFlavors();
 161         }
 162 
 163         long[] formats = getClipboardFormatsOpenClose();
 164 
 165         return DataTransferer.getInstance().
 166             getFlavorsForFormatsAsArray(formats, getDefaultFlavorTable());
 167     }


 258     protected void lostOwnershipImpl() {
 259         lostOwnershipLater(null);
 260     }
 261 
 262     /**
 263      * Clears the clipboard state (contents, owner and contents context) and
 264      * notifies the current owner that ownership is lost. Does nothing if the
 265      * argument is not <code>null</code> and is not equal to the current
 266      * contents context.
 267      *
 268      * @param disposedContext the AppContext that is disposed or
 269      *        <code>null</code> if the ownership is lost because another
 270      *        application acquired ownership.
 271      */
 272     protected void lostOwnershipLater(final AppContext disposedContext) {
 273         final AppContext context = this.contentsContext;
 274         if (context == null) {
 275             return;
 276         }
 277 
 278         final Runnable runnable = new Runnable() {
 279                 public void run() {



 280                     final SunClipboard sunClipboard = SunClipboard.this;
 281                     ClipboardOwner owner = null;
 282                     Transferable contents = null;
 283 
 284                     synchronized (sunClipboard) {
 285                         final AppContext context = sunClipboard.contentsContext;
 286 
 287                         if (context == null) {
 288                             return;
 289                         }
 290 
 291                         if (disposedContext == null || context == disposedContext) {
 292                             owner = sunClipboard.owner;
 293                             contents = sunClipboard.contents;
 294                             sunClipboard.contentsContext = null;
 295                             sunClipboard.owner = null;
 296                             sunClipboard.contents = null;
 297                             sunClipboard.clearNativeContext();
 298                             context.removePropertyChangeListener
 299                                 (AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
 300                         } else {
 301                             return;
 302                         }
 303                     }
 304                     if (owner != null) {
 305                         owner.lostOwnership(sunClipboard, contents);
 306                     }
 307                 }
 308             };
 309 
 310         SunToolkit.postEvent(context, new PeerEvent(this, runnable,
 311                                                     PeerEvent.PRIORITY_EVENT));
 312     }
 313 
 314     protected abstract void clearNativeContext();
 315 
 316     protected abstract void setContentsNative(Transferable contents);
 317 
 318     /**
 319      * @since 1.5
 320      */
 321     protected long[] getClipboardFormatsOpenClose() {
 322         try {
 323             openClipboard(null);
 324             return getClipboardFormats();
 325         } finally {
 326             closeClipboard();
 327         }
 328     }
 329 
 330     /**
 331      * Returns zero-length array (not null) if the number of available formats is zero.
 332      *




 127                 contentsContext.removePropertyChangeListener
 128                     (AppContext.DISPOSED_PROPERTY_NAME, this);
 129             }
 130             contentsContext = context;
 131         }
 132     }
 133 
 134     public synchronized Transferable getContents(Object requestor) {
 135         if (contents != null) {
 136             return contents;
 137         }
 138         return new ClipboardTransferable(this);
 139     }
 140 
 141 
 142     /**
 143      * @return the contents of this clipboard if it has been set from the same
 144      *         AppContext as it is currently retrieved or null otherwise
 145      * @since 1.5
 146      */
 147     protected synchronized Transferable getContextContents() {
 148         AppContext context = AppContext.getAppContext();
 149         return (context == contentsContext) ? contents : null;
 150     }
 151 
 152 
 153     /**
 154      * @see java.awt.Clipboard#getAvailableDataFlavors
 155      * @since 1.5
 156      */
 157     public DataFlavor[] getAvailableDataFlavors() {
 158         Transferable cntnts = getContextContents();
 159         if (cntnts != null) {
 160             return cntnts.getTransferDataFlavors();
 161         }
 162 
 163         long[] formats = getClipboardFormatsOpenClose();
 164 
 165         return DataTransferer.getInstance().
 166             getFlavorsForFormatsAsArray(formats, getDefaultFlavorTable());
 167     }


 258     protected void lostOwnershipImpl() {
 259         lostOwnershipLater(null);
 260     }
 261 
 262     /**
 263      * Clears the clipboard state (contents, owner and contents context) and
 264      * notifies the current owner that ownership is lost. Does nothing if the
 265      * argument is not <code>null</code> and is not equal to the current
 266      * contents context.
 267      *
 268      * @param disposedContext the AppContext that is disposed or
 269      *        <code>null</code> if the ownership is lost because another
 270      *        application acquired ownership.
 271      */
 272     protected void lostOwnershipLater(final AppContext disposedContext) {
 273         final AppContext context = this.contentsContext;
 274         if (context == null) {
 275             return;
 276         }
 277 
 278         SunToolkit.postEvent(context, new PeerEvent(this, () -> lostOwnershipNow(disposedContext),
 279                                                     PeerEvent.PRIORITY_EVENT));
 280     }
 281 
 282     protected void lostOwnershipNow(final AppContext disposedContext) {
 283         final SunClipboard sunClipboard = SunClipboard.this;
 284         ClipboardOwner owner = null;
 285         Transferable contents = null;
 286 
 287         synchronized (sunClipboard) {
 288             final AppContext context = sunClipboard.contentsContext;
 289 
 290             if (context == null) {
 291                 return;
 292             }
 293 
 294             if (disposedContext == null || context == disposedContext) {
 295                 owner = sunClipboard.owner;
 296                 contents = sunClipboard.contents;
 297                 sunClipboard.contentsContext = null;
 298                 sunClipboard.owner = null;
 299                 sunClipboard.contents = null;
 300                 sunClipboard.clearNativeContext();
 301                 context.removePropertyChangeListener
 302                         (AppContext.DISPOSED_PROPERTY_NAME, sunClipboard);
 303             } else {
 304                 return;
 305             }
 306         }
 307         if (owner != null) {
 308             owner.lostOwnership(sunClipboard, contents);
 309         }
 310     }

 311 



 312 
 313     protected abstract void clearNativeContext();
 314 
 315     protected abstract void setContentsNative(Transferable contents);
 316 
 317     /**
 318      * @since 1.5
 319      */
 320     protected long[] getClipboardFormatsOpenClose() {
 321         try {
 322             openClipboard(null);
 323             return getClipboardFormats();
 324         } finally {
 325             closeClipboard();
 326         }
 327     }
 328 
 329     /**
 330      * Returns zero-length array (not null) if the number of available formats is zero.
 331      *