All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openide.awt.GlobalManager Maven / Gradle / Ivy

There is a newer version: RELEASE240
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.openide.awt;

import java.beans.PropertyChangeListener;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.ActionMap;
import org.openide.awt.ContextManager.LookupRef;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;
import org.openide.util.Mutex;
import org.openide.util.Utilities;
import org.openide.util.WeakSet;


/** Listener on a global context.
 */
class GlobalManager extends Object implements LookupListener {
    private static final Logger LOG = GeneralAction.LOG;
    
    private static final Map> CACHE = new HashMap>();
    private static final Map> SURVIVE = new HashMap>();
    
    private Lookup.Result result;
    private Reference actionMap = new WeakReference(null);
    private Map> listeners;
    private PropertyChangeListener changeL;
    
    private GlobalManager(Lookup lookup) {
        this.listeners = new HashMap>();
        this.result = lookup.lookupResult(ActionMap.class);
        result.addLookupListener(this);
        resultChanged(null);
    }
    
    public static GlobalManager findManager(Lookup context, boolean survive) {
        synchronized (CACHE) {
            Map> map = survive ? SURVIVE : CACHE;
            LookupRef lr = new LookupRef(context);
            Reference ref = map.get(lr);
            GlobalManager g = ref == null ? null : ref.get();
            if (g == null) {
                g = survive ? new SurviveManager(context) : new GlobalManager(context);
                ref = new GMReference(g, lr, survive);
                map.put(lr, ref);
            }
            return g;
        }
    }
    
    static void clearCache(LookupRef lr, GMReference ref, boolean survive) {
        synchronized (CACHE) {
            Map> map = survive ? SURVIVE : CACHE;
            if (map.get(lr) == ref) {
                map.remove(lr);
            }
        }
    }
    
    public Action findGlobalAction(Object key) {
        if (key == null) {
            return null;
        }
        ActionMap map = actionMap.get();
        Action a = (map == null) ? null : map.get(key);
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Action for key: {0} is: {1}", new Object[]{key, a}); // NOI18N
        }
        
        return a;
    }
    
    public final void registerListener(Object key, GeneralAction.BaseDelAction a) {
        if (key == null) {
            return;
        }
        synchronized (CACHE) {
            Set existing = listeners.get(key);
            if (existing == null) {
                existing = new WeakSet();
                listeners.put(key, existing);
            }
            existing.add(a);
            a.updateState(new ActionMap(), actionMap.get(), false);
        }
    }

    public final void unregisterListener(Object key, GeneralAction.BaseDelAction a) {
        if (key == null) {
            return;
        }
        synchronized (CACHE) {
            Set existing = listeners.get(key);
            if (existing != null) {
                existing.remove(a);
                if (existing.isEmpty()) {
                    listeners.remove(key);
                }
            }
        }
    }
    
    /** Change all that do not survive ActionMap change */
    @Override
    public final void resultChanged(org.openide.util.LookupEvent ev) {
        Collection> all = result.allItems();
        ActionMap a = all.isEmpty() ? null : all.iterator().next().getInstance();
        
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "changed map : {0}", a); // NOI18N
            LOG.log(Level.FINE, "previous map: {0}", actionMap.get()); // NOI18N
        }
        
        final ActionMap prev = actionMap.get();
        if (a == prev) {
            return;
        }
        
        final ActionMap newMap = newMap(prev, a);
        
        actionMap = new WeakReference(newMap);
        
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("clearActionPerformers"); // NOI18N
        }
        
        Mutex.EVENT.readAccess(new Runnable() {
            @Override
            public void run() {
                notifyListeners(prev, newMap);
            }
        });
    }

    final void notifyListeners(ActionMap prev, ActionMap now) {
        if (prev == null) prev = new ActionMap();
        if (now == null) now = new ActionMap();
        
        HashSet keys = new HashSet();
        Object[] allPrev = prev.allKeys();
        Object[] allNow = now.allKeys();
        if (allPrev != null) {
            keys.addAll(Arrays.asList(allPrev));
        }
        if (allNow != null) {
            keys.addAll(Arrays.asList(allNow));
        }

        for (Object k : keys) {
            Set actions = listeners.get(k);
            if (actions == null) {
                continue;
            }
            for (GeneralAction.BaseDelAction del : actions) {
                if (del != null) {
                    del.updateState(prev, now, true);
                }
            }
        }
    }

    /** Does not survive focus change */
    public boolean isSurvive() {
        return false;
    }

    /** Method that can be overridden to provide "different" behaviour for
     * keeping previous maps, like in case of "surviveFocusChange"
     */
    protected ActionMap newMap(ActionMap prev, ActionMap newMap) {
        return newMap;
    }

    private static final class GMReference extends WeakReference 
    implements Runnable {
        private LookupRef context;
        private boolean survive;
        
        public GMReference(GlobalManager m, LookupRef context, boolean survive) {
            super(m, Utilities.activeReferenceQueue());
            this.context = context;
            this.survive = survive;
        }
        
        @Override
        public void run() {
            clearCache(context, this, survive);
        }
    } // end of GMReference

    /** Manager with special behaviour.
     */
    private static final class SurviveManager extends GlobalManager {
        private SurviveManager(Lookup context) {
            super(context);
        }
        
        @Override
        public boolean isSurvive() {
            return true;
        }
        
        
        @Override
        protected ActionMap newMap(ActionMap prev, ActionMap newMap) {
            ArrayList old = new ArrayList();
            if (prev != null) {
                Object[] all = prev.allKeys();
                if (all != null) {
                    old.addAll(Arrays.asList(all));
                    if (newMap != null) {
                        Object[] toRem = newMap.allKeys();
                        if (toRem != null) {
                            old.removeAll(Arrays.asList(toRem));
                        }
                    }
                }
            }

            ActionMap merged = new ActionMap();
            if (newMap != null) {
                Object[] allK = newMap.allKeys();
                if (allK != null) {
                    for (int i = 0; i < allK.length; i++) {
                        Object o = allK[i];
                        merged.put(o, newMap.get(o));
                    }
                }
            }
            
            for (Object o : old) {
                merged.put(o, prev.get(o));
            }
            
            return merged;
        }
    }
}