Пример #1
0
 /// <summary>
 /// Returns true if this object is identical to the specified object.
 /// </summary>
 /// <param name="anObject"> the Object to compare this object to </param>
 /// <returns> true if the objects are identical </returns>
 public sealed override bool Equals(Object anObject)
 {
     if (anObject is AWTKeyStroke)
     {
         AWTKeyStroke ks = (AWTKeyStroke)anObject;
         return(ks.KeyChar_Renamed == KeyChar_Renamed && ks.KeyCode_Renamed == KeyCode_Renamed && ks.OnKeyRelease_Renamed == OnKeyRelease_Renamed && ks.Modifiers_Renamed == Modifiers_Renamed);
     }
     return(false);
 }
Пример #2
0
        private static AWTKeyStroke GetCachedStroke(char keyChar, int keyCode, int modifiers, bool onKeyRelease)
        {
            lock (typeof(AWTKeyStroke))
            {
                IDictionary <AWTKeyStroke, AWTKeyStroke> cache = (IDictionary)AppContext.AppContext.get(APP_CONTEXT_CACHE_KEY);
                AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.AppContext.get(APP_CONTEXT_KEYSTROKE_KEY);

                if (cache == null)
                {
                    cache = new Dictionary <>();
                    AppContext.AppContext.put(APP_CONTEXT_CACHE_KEY, cache);
                }

                if (cacheKey == null)
                {
                    try
                    {
                        Class clazz = AWTKeyStrokeClass;
                        cacheKey = (AWTKeyStroke)GetCtor(clazz).newInstance((Object[])null);
                        AppContext.AppContext.put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
                    }
                    catch (InstantiationException)
                    {
                        assert(false);
                    }
                    catch (IllegalAccessException)
                    {
                        assert(false);
                    }
                    catch (InvocationTargetException)
                    {
                        assert(false);
                    }
                }
                cacheKey.KeyChar_Renamed      = keyChar;
                cacheKey.KeyCode_Renamed      = keyCode;
                cacheKey.Modifiers_Renamed    = MapNewModifiers(MapOldModifiers(modifiers));
                cacheKey.OnKeyRelease_Renamed = onKeyRelease;

                AWTKeyStroke stroke = (AWTKeyStroke)cache[cacheKey];
                if (stroke == null)
                {
                    stroke        = cacheKey;
                    cache[stroke] = stroke;
                    AppContext.AppContext.remove(APP_CONTEXT_KEYSTROKE_KEY);
                }
                return(stroke);
            }
        }
Пример #3
0
        /// <summary>
        /// Registers a new class which the factory methods in
        /// <code>AWTKeyStroke</code> will use when generating new
        /// instances of <code>AWTKeyStroke</code>s. After invoking this
        /// method, the factory methods will return instances of the specified
        /// Class. The specified Class must be either <code>AWTKeyStroke</code>
        /// or derived from <code>AWTKeyStroke</code>, and it must have a
        /// no-arg constructor. The constructor can be of any accessibility,
        /// including <code>private</code>. This operation
        /// flushes the current <code>AWTKeyStroke</code> cache.
        /// </summary>
        /// <param name="subclass"> the new Class of which the factory methods should create
        ///        instances </param>
        /// <exception cref="IllegalArgumentException"> if subclass is <code>null</code>,
        ///         or if subclass does not have a no-arg constructor </exception>
        /// <exception cref="ClassCastException"> if subclass is not
        ///         <code>AWTKeyStroke</code>, or a class derived from
        ///         <code>AWTKeyStroke</code> </exception>
        protected internal static void RegisterSubclass(Class subclass)
        {
            if (subclass == null)
            {
                throw new IllegalArgumentException("subclass cannot be null");
            }
            lock (typeof(AWTKeyStroke))
            {
                Class keyStrokeClass = (Class)AppContext.AppContext.get(typeof(AWTKeyStroke));
                if (keyStrokeClass != null && keyStrokeClass.Equals(subclass))
                {
                    // Already registered
                    return;
                }
            }
            if (!subclass.IsSubclassOf(typeof(AWTKeyStroke)))
            {
                throw new ClassCastException("subclass is not derived from AWTKeyStroke");
            }

            Constructor ctor = GetCtor(subclass);

            String couldNotInstantiate = "subclass could not be instantiated";

            if (ctor == null)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }
            try
            {
                AWTKeyStroke stroke = (AWTKeyStroke)ctor.newInstance((Object[])null);
                if (stroke == null)
                {
                    throw new IllegalArgumentException(couldNotInstantiate);
                }
            }
            catch (NoSuchMethodError)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }
            catch (ExceptionInInitializerError)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }
            catch (InstantiationException)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }
            catch (IllegalAccessException)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }
            catch (InvocationTargetException)
            {
                throw new IllegalArgumentException(couldNotInstantiate);
            }

            lock (typeof(AWTKeyStroke))
            {
                AppContext.AppContext.put(typeof(AWTKeyStroke), subclass);
                AppContext.AppContext.remove(APP_CONTEXT_CACHE_KEY);
                AppContext.AppContext.remove(APP_CONTEXT_KEYSTROKE_KEY);
            }
        }