keybindingmanager.java

来自「The ElectricTM VLSI Design System is an 」· Java 代码 · 共 1,021 行 · 第 1/3 页

JAVA
1,021
字号
    /**     * Set the faked event source of the KeyBindings object.  See     * KeyBindings.setEventSource() for details.     * @param actionDesc the action description used to find the KeyBindings object.     * @param source the object to use as the source of the event. (Event.getSource()).     */    public synchronized void setEventSource(String actionDesc, Object source) {        KeyBindings keys = (KeyBindings)actionMap.get(actionDesc);        keys.setEventSource(source);    }    /**     * Returns true if KeyBindings for the action described by     * actionDesc are already present in hash tables.     * @param actionDesc the action description of the KeyBindings     * @return true if key binding found in manager, false otherwise.     */    /*    public synchronized boolean hasKeyBindings(String actionDesc) {        KeyBindings k = (KeyBindings)actionMap.get(actionDesc);        if (k != null) return true;        return false;    }*/    /**     * Get a list of KeyBindings that conflict with the key combo     * <code>prefixStroke, stroke</code>.  A conflict is registered if:     * an existing stroke is the same as <code>prefixStroke</code>; or     * an existing stroke is the same <code>stroke</code>     * (if <code>prefixStroke</code> is null);     * or an existing prefixStroke,stroke combo is the same as     * <code>prefixStroke,stroke</code>.     * <p>     * The returned list consists of newly created KeyBindings objects, not     * KeyBindings objects that are used in the key manager database.     * This is because not all KeyStrokePairs in an existing KeyBindings     * object will necessarily conflict.  However, there may be more than     * one KeyStrokePair in a returned KeyBindings object from the list if     * more than one KeyStrokePair does actually conflict.     * <p>     * Returns an empty list if there are no conflicts.     * @param pair the KeyStrokePair     * @return a list of conflicting <code>KeyBindings</code>.  Empty list if no conflicts.     */    public synchronized List<KeyBindings> getConflictingKeyBindings(KeyStrokePair pair) {        List<KeyBindings> conflicts = new ArrayList<KeyBindings>();               // list of actual KeyBindings        List<String> conflictsStrings = new ArrayList<String>();        // list of action strings        Map<KeyStroke,Set<String>> inputMapToUse = inputMap;        if (pair.getPrefixStroke() != null) {            // check if conflicts with any single key Binding            Set<String> set = inputMap.get(pair.getPrefixStroke());            if (set != null) {                for (String str : set) {                    if (str.equals(PrefixAction.actionDesc)) continue;                    // add to conflicts                    conflictsStrings.add(str);                }            }            inputMapToUse = prefixedInputMapMaps.get(pair.getPrefixStroke());        }        // find stroke conflicts        if (inputMapToUse != null) {            Set<String> set = inputMapToUse.get(pair.getStroke());            if (set != null) {                for (String str : set) {                    if (str.equals(PrefixAction.actionDesc)) {                        // find all string associated with prefix in prefix map                        // NOTE: this condition is never true if prefixStroke is valid                        // and we are using a prefixed map...prefixActions are only in primary inputMap.                        Map<KeyStroke,Set<String>> prefixMap = prefixedInputMapMaps.get(pair.getStroke());                        if (prefixMap != null) {                            for (Iterator<Set<String>> it2 = prefixMap.values().iterator(); it2.hasNext(); ) {                                // all existing prefixStroke,stroke combos conflict, so add them all                                Set<String> prefixList = it2.next(); // this is a set of strings                                conflictsStrings.addAll(prefixList);                            }                        }                    } else {                        conflictsStrings.add(str);              // otherwise this is a key actionDesc                    }                }            }        }        // get all KeyBindings from ActionMap        for (String aln : conflictsStrings) {            ActionListener action = (ActionListener)actionMap.get(aln);            if (action == null) continue;            if (action instanceof PrefixAction) continue;            KeyBindings keys = (KeyBindings)action;            KeyBindings conflicting = new KeyBindings(keys.getActionDesc());            for (Iterator<KeyStrokePair> it2 = keys.getKeyStrokePairs(); it2.hasNext(); ) {                // Unfortunately, any keyBinding can map to this action, including                // ones that don't actually conflict.  So we need to double check                // if binding really conflicts.                KeyStrokePair pair2 = it2.next();                if (pair.getPrefixStroke() != null) {                    // check prefix conflict                    if (pair2.getPrefixStroke() != null) {                        // only conflict is if both prefix and stroke match                        if (pair.getStroke() == pair2.getStroke())                            conflicting.addKeyBinding(pair2);                    } else {                        // conflict if prefixStroke matches pair2.stroke                        if (pair.getPrefixStroke() == pair2.getStroke())                            conflicting.addKeyBinding(pair2);                    }                } else {                    // no prefixStroke                    if (pair2.getPrefixStroke() != null) {                        // conflict if stroke matches pair2.prefixStroke                        if (pair.getStroke() == pair2.getPrefixStroke())                            conflicting.addKeyBinding(pair2);                    } else {                        // no prefixStroke, both only have stroke                        if (pair.getStroke() == pair2.getStroke())                            conflicting.addKeyBinding(pair2);                    }                }            }            // add conflicting KeyBindings to list if it has bindings in it            Iterator conflictingIt = conflicting.getKeyStrokePairs();            if (conflictingIt.hasNext()) conflicts.add(conflicting);        }        return conflicts;    }    /**     * Sets the enabled state of the action to 'b'. If b is false, it     * disables all events that occur when actionDesc takes place. If b is     * true, it enables all resulting events.     * @param actionDesc the describing action     * @param b true to enable, false to disable.     */    public synchronized void setEnabled(String actionDesc, boolean b) {        ActionListener action = (ActionListener)actionMap.get(actionDesc);        if (action == null) return;        if (action instanceof PrefixAction) return;        KeyBindings k = (KeyBindings)action;        k.setEnabled(b);    }    /**     * Get the enabled state of the action described by 'actionDesc'.     * @param actionDesc the describing action.     * @return true if the action is enabled, false otherwise.     */    public synchronized boolean getEnabled(String actionDesc) {        ActionListener action = (ActionListener)actionMap.get(actionDesc);        if (action == null) return false;        if (action instanceof PrefixAction) return false;        KeyBindings k = (KeyBindings)action;        return k.getEnabled();    }    /**     * Check if there are any bindings that do not have any     * associated actions.     */    public synchronized void deleteEmptyBindings() {        Set<String> keys = actionMap.keySet();        for (String key : keys) {            ActionListener action = (ActionListener)actionMap.get(key);            if (action instanceof KeyBindings) {                KeyBindings bindings = (KeyBindings)action;                Iterator listenersIt = bindings.getActionListeners();                if (!listenersIt.hasNext()) {                    // no listeners on the action                    System.out.println("Warning: Deleting defunct binding for "+key+" [ "+bindings.bindingsToString()+ " ]...action does not exist anymore");                    // delete bindings                    removeBindingsFromPrefs(key);                }            }        }    }    // --------------------------------- Private -------------------------------------    /**     * Adds a KeyStrokePair <i>pair</i> as an active binding for action <i>actionDesc</i>.     * @param actionDesc the action description     * @param pair a key stroke pair     * @return the new KeyBindings object, or an existing KeyBindings object for actionDesc     */    private synchronized KeyBindings addKeyBinding(String actionDesc, KeyStrokePair pair) {        if (pair == null) return null;        // warn if conflicting key bindings created        List<KeyBindings> conflicts = getConflictingKeyBindings(pair);        if (conflicts.size() > 0) {            System.out.println("WARNING: Key binding for "+actionDesc+" [ " +pair.toString()+" ] conflicts with:");            for (KeyBindings k : conflicts) {                System.out.println("  > "+k.getActionDesc()+" [ "+k.bindingsToString()+" ]");            }        }        if (DEBUG) System.out.println("Adding binding for "+actionDesc+": "+pair.toString());        KeyStroke prefixStroke = pair.getPrefixStroke();        KeyStroke stroke = pair.getStroke();        Map<KeyStroke,Set<String>> inputMapToUse = inputMap;        if (prefixStroke != null) {            // find HashMap based on prefixAction            inputMapToUse = prefixedInputMapMaps.get(prefixStroke);            if (inputMapToUse == null) {                inputMapToUse = new HashMap<KeyStroke,Set<String>>();                prefixedInputMapMaps.put(prefixStroke, inputMapToUse);            }            // add prefix action to primary input map            Set<String> set = inputMap.get(prefixStroke);            if (set == null) {                set = new HashSet<String>();                inputMap.put(prefixStroke, set);            }            set.add(PrefixAction.actionDesc);        }        // add stroke to input map to use        Set<String> set = inputMapToUse.get(stroke);        if (set == null) {            set = new HashSet<String>();            inputMapToUse.put(stroke, set);        }        set.add(actionDesc);        // add stroke to KeyBindings        KeyBindings keys = (KeyBindings)actionMap.get(actionDesc);        if (keys == null) {            // no bindings for actionDesc            keys = new KeyBindings(actionDesc);            actionMap.put(actionDesc, keys);        }        keys.addKeyBinding(pair);        return keys;    }    // ---------------------------- Preferences Storage ------------------------------    /**     * Add KeyBinding to stored user preferences.     * @param actionDesc the action description under which to store all the key bindings     */    private synchronized void setBindingsToPrefs(String actionDesc) {        if (prefs == null) return;        if (actionDesc == null || actionDesc.equals("")) return;        KeyBindings keyBindings = (KeyBindings)actionMap.get(actionDesc);        if (keyBindings == null) return;        String actionDescAbbrev = actionDesc;        if ((actionDesc.length() + prefPrefix.length()) > Preferences.MAX_KEY_LENGTH) {            int start = actionDesc.length() + prefPrefix.length() - Preferences.MAX_KEY_LENGTH;            actionDescAbbrev = actionDesc.substring(start, actionDesc.length());        }        if (debugPrefs) System.out.println("Writing to pref '"+prefPrefix+actionDescAbbrev+"': "+keyBindings.bindingsToString());        prefs.put(prefPrefix+actionDescAbbrev, keyBindings.bindingsToString());    }    /**     * Get KeyBindings for <code>actionDesc</code> from Preferences.     * Returns null if actionDesc not present in prefs.     * @param actionDesc the action description associated with these bindings     * @return a list of KeyStrokePairs     */    private synchronized List<KeyStrokePair> getBindingsFromPrefs(String actionDesc) {        if (prefs == null) return null;        if (actionDesc == null || actionDesc.equals("")) return null;        String actionDescAbbrev = actionDesc;        if ((actionDesc.length() + prefPrefix.length()) > Preferences.MAX_KEY_LENGTH) {            int start = actionDesc.length() + prefPrefix.length() - Preferences.MAX_KEY_LENGTH;            actionDescAbbrev = actionDesc.substring(start, actionDesc.length());        }        String keys = prefs.get(prefPrefix+actionDescAbbrev, null);        if (keys == null) return null;        if (debugPrefs) System.out.println("Read from prefs for "+prefPrefix+actionDescAbbrev+": "+keys);        KeyBindings k = new KeyBindings(actionDesc);        k.addKeyBindings(keys);        if (debugPrefs) System.out.println("  turned into: "+k.describe());        List<KeyStrokePair> bindings = new ArrayList<KeyStrokePair>();        for (Iterator<KeyStrokePair> it = k.getKeyStrokePairs(); it.hasNext(); ) {            bindings.add(it.next());        }        return bindings;    }    /**     * Restored saved bindings from preferences.  Usually called after     * menu has been created.     */    public synchronized void restoreSavedBindings(boolean initialCall) {        if (initialCall && initialized == true) return;        initialized = true;        if (prefs == null) return;        // try to see if binding saved in preferences for each action        for (Map.Entry<String,Object> entry : actionMap.entrySet()) {            String actionDesc = entry.getKey();            if (actionDesc == null || actionDesc.equals("")) continue;            // clear current bindings            if (entry.getValue() instanceof PrefixAction) {                continue;            }            KeyBindings bindings = (KeyBindings)entry.getValue();            bindings.clearKeyBindings();            // look up bindings in prefs            List<KeyStrokePair> keyPairs = getBindingsFromPrefs(bindings.getActionDesc());            if (keyPairs == null) {                // no entry found, use default settings                bindings.setUsingDefaultKeys(true);                for (Iterator<KeyStrokePair> it2 = bindings.getDefaultKeyStrokePairs(); it2.hasNext(); ) {                    KeyStrokePair pair = it2.next();                    addKeyBinding(actionDesc, pair);                }            } else {                // otherwise, add bindings found                bindings.setUsingDefaultKeys(false);                for (KeyStrokePair pair : keyPairs) {                    addKeyBinding(actionDesc, pair);                }            }        }    }    /**     * Remove any bindings stored for actionDesc.     */    private synchronized void removeBindingsFromPrefs(String actionDesc) {        if (prefs == null) return;        if (actionDesc == null || actionDesc.equals("")) return;        prefs.remove(prefPrefix+actionDesc);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?