1 /*
   2  * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.windows;
  26 
  27 abstract class WObjectPeer {
  28 
  29     static {
  30         initIDs();
  31     }
  32 
  33     // The Windows handle for the native widget.
  34     volatile long pData;
  35     // if the native peer has been destroyed
  36     volatile boolean destroyed = false;
  37     // The associated AWT object.
  38     Object target;
  39 
  40     private volatile boolean disposed;
  41 
  42     // set from JNI if any errors in creating the peer occur
  43     protected Error createError = null;
  44 
  45     // used to synchronize the state of this peer
  46     private final Object stateLock = new Object();
  47 
  48     public static WObjectPeer getPeerForTarget(Object t) {
  49         WObjectPeer peer = (WObjectPeer) WToolkit.targetToPeer(t);
  50         return peer;
  51     }
  52 
  53     public long getData() {
  54         return pData;
  55     }
  56 
  57     public Object getTarget() {
  58         return target;
  59     }
  60 
  61     public final Object getStateLock() {
  62         return stateLock;
  63     }
  64 
  65     /*
  66      * Subclasses should override disposeImpl() instead of dispose(). Client
  67      * code should always invoke dispose(), never disposeImpl().
  68      */
  69     protected abstract void disposeImpl();
  70     public final void dispose() {
  71         boolean call_disposeImpl = false;
  72 
  73         synchronized (this) {
  74             if (!disposed) {
  75                 disposed = call_disposeImpl = true;
  76             }
  77         }
  78 
  79         if (call_disposeImpl) {
  80             disposeImpl();
  81         }
  82     }
  83     protected final boolean isDisposed() {
  84         return disposed;
  85     }
  86 
  87     /**
  88      * Initialize JNI field and method IDs
  89      */
  90     private static native void initIDs();
  91 }