⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keybindingmanager.java

📁 The ElectricTM VLSI Design System is an open-source Electronic Design Automation (EDA) system that c
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }        public void keyReleased(KeyEvent e) {}        public void keyTyped(KeyEvent e) {}    }    public boolean postProcessKeyEvent(KeyEvent e) {        return processKeyEvent(e);    }*/    /**     * Says whether or not KeyBindingManager will bind to this key event     * @param e the KeyEvent     * @return true if the KeyBindingManager can bind to this event     */    public static boolean validKeyEvent(KeyEvent e) {        // only look at key pressed events//        if (e.getID() != KeyEvent.KEY_PRESSED && e.getID() != KeyEvent.KEY_TYPED) return false;        if (e.getID() != KeyEvent.KEY_PRESSED) return false;        // ignore modifier only events (CTRL, SHIFT etc just by themselves)        if (e.getKeyCode() == KeyEvent.VK_CONTROL) return false;        if (e.getKeyCode() == KeyEvent.VK_SHIFT) return false;        if (e.getKeyCode() == KeyEvent.VK_ALT) return false;        if (e.getKeyCode() == KeyEvent.VK_META) return false;        return true;    }    /**     * Process a KeyEvent by finding what actionListeners should be     * activated as a result of the event.  The keyBindingManager keeps     * one stroke of history so that two-stroke events can be distinguished.     * @param e the KeyEvent     * @return true if event consumed, false if not and nothing done.     */    public synchronized boolean processKeyEvent(KeyEvent e) {        if (DEBUG) System.out.println("got event (consumed="+e.isConsumed()+") "+e);        // see if this is a valid key event        if (!validKeyEvent(e)) return false;        // get KeyStroke		KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);		// remove shift modifier from Events.  Lets KeyStrokes like '<' register correctly,		// because they are always delivered as SHIFT-'<'.//      if (stroke.getKeyCode() == KeyEvent.VK_UNDEFINED)		if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0 && !Character.isLetter(e.getKeyCode()) && !Character.isDigit(e.getKeyCode()))			stroke = KeyStroke.getKeyStroke(e.getKeyChar());        if (DEBUG) System.out.println("  Current key is "+stroke+", last prefix key is "+lastPrefix);        // ignore if consumed        if (e.isConsumed()) {            lastPrefix = null;              // someone did something with it, null prefix key            return false;        }        Map<KeyStroke,Set<String>> inputMapToUse = inputMap;        // check if we should use prefixed key map instead of regular inputMap        if (lastPrefix != null) {            // get input map based on prefix key            inputMapToUse = prefixedInputMapMaps.get(lastPrefix);            if (inputMapToUse == null) { lastPrefix = null; return false; }        }        ActionListener action = null;        ActionEvent evt = new ActionEvent(e, ActionEvent.ACTION_PERFORMED, stroke.toString(), stroke.getModifiers());        boolean actionPerformed = false;        boolean prefixActionPerformed = false;        // get set of action strings, iterate over them        Set<String> keyBindingList = inputMapToUse.get(stroke);        if (keyBindingList != null) {            for (String actionDesc : keyBindingList) {                // get KeyBinding object from action map, activate its action                // note that if this is a prefixed action, this could actually be a                // PrefixAction object instead of a KeyBinding object.                action = (ActionListener)actionMap.get(actionDesc);                if (action instanceof PrefixAction) {                    if (!prefixActionPerformed) {                        action.actionPerformed(evt);        // only do this once                        prefixActionPerformed = true;                    }                } else {                    action.actionPerformed(evt);                    lastPrefix = null;                }                actionPerformed = true;            }        }        if (!actionPerformed) {            // if no action to perform, perhaps the user hit a prefix key, then            // decided to start another prefix-key-combo (that does not result in            // a valid binding with the first prefix, obviously).  We'll be nice            // and check for this case            Map prefixMap = prefixedInputMapMaps.get(stroke);            if (prefixMap != null) {                // valid prefix key, fire prefix event                prefixAction.actionPerformed(evt);                actionPerformed = true;            } else {                lastPrefix = null;              // nothing to do            }        }        if (DEBUG) System.out.println(" actionPerformed="+actionPerformed);        if (actionPerformed) {            e.consume();                // consume event if we did something useful with it            return true;                // let KeyboardFocusManager know we consumed event        }        // otherwise, do not consume, and return false to let KeyboardFocusManager        // know that we did nothing with Event, and to pass it on        return false;    }    // -------------- Static Methods Applied to All KeyBindingManagers ----------------    /**     * Get a list of conflicting key bindings from all KeyBindingManagers.     * @param pair the keystrokepair     * @return a list of conflicting KeyBindings from all KeyBindingManagers     */    public static List<KeyBindings> getConflictsAllManagers(KeyStrokePair pair) {        List<KeyBindings> conflicts = new ArrayList<KeyBindings>();        synchronized(allManagers) {            for (KeyBindingManager m : allManagers) {                conflicts.addAll(m.getConflictingKeyBindings(pair));            }        }        return conflicts;    }    // --------------------- Public Methods to Manage Bindings ----------------------    /**     * Adds a default KeyBinding. If any keyBindings are found for     * <code>k.actionDesc</code>, those are used instead.  Note that <code>k</code>     * cannot be null, but it's stroke and prefixStroke can be null.  However,     * it's actionDesc and action must be valid.     * @param actionDesc the action description     * @param pair a key stroke pair     */    public synchronized void addDefaultKeyBinding(String actionDesc, KeyStrokePair pair) {        if (pair == null) return;        // add to default bindings        KeyBindings keys = (KeyBindings)actionMap.get(actionDesc);        if (keys == null) {            keys = new KeyBindings(actionDesc);            actionMap.put(actionDesc, keys);        }        keys.addDefaultKeyBinding(pair);/*        if (keys.getUsingDefaultKeys()) {            // using default keys, add default key to active maps            addKeyBinding(actionDesc, pair);        }*/    }    /**     * Adds a user specified KeyBindings. Also adds it to stored user preference.     * @param actionDesc the action description     * @param pair a key stroke pair     */    public synchronized void addUserKeyBinding(String actionDesc, KeyStrokePair pair) {        if (pair == null) return;        // add to active bindings (also adds to KeyBindings object)        KeyBindings keys = addKeyBinding(actionDesc, pair);        // now using user specified key bindings, set usingDefaults false        keys.setUsingDefaultKeys(false);        // user has modified bindings, write all current bindings to prefs        setBindingsToPrefs(keys.getActionDesc());    }    /**     * Add an action listener on actionDesc     * @param actionDesc the action description     * @param action the action listener to add     */    public synchronized void addActionListener(String actionDesc, ActionListener action) {        // add to default set of KeyBindings        KeyBindings keys = (KeyBindings)actionMap.get(actionDesc);        if (keys == null) {            keys = new KeyBindings(actionDesc);            actionMap.put(actionDesc, keys);        }        keys.addActionListener(action);    }    /**     * Removes a key binding from the active bindings, and writes new bindings     * set to preferences.     * @param actionDesc the describing action     * @param k the KeyStrokePair to remove     */    public synchronized void removeKeyBinding(String actionDesc, KeyStrokePair k) {        Map<KeyStroke,Set<String>> inputMapToUse = inputMap;        // if prefix stroke exists, remove one prefixAction key string        // (may be more than one if more than one binding has prefixStroke as it's prefix)        if (k.getPrefixStroke() != null) {            Set<String> set = inputMap.get(k.getPrefixStroke());            if (set != null) {                for (String str : set) {                    if (str.equals(PrefixAction.actionDesc)) {                        set.remove(str);                        break;                    }                }            }            // get input map to use            inputMapToUse = prefixedInputMapMaps.get(k.getPrefixStroke());        }        // remove stroke        if (inputMapToUse != null) {            Set<String> set = inputMapToUse.get(k.getStroke());            if (set != null) set.remove(actionDesc);        }        // remove action        KeyBindings bindings = (KeyBindings)actionMap.get(actionDesc);        bindings.removeKeyBinding(k);        bindings.setUsingDefaultKeys(false);        // user has modified bindings, write all current bindings to prefs        setBindingsToPrefs(actionDesc);    }    /**     * Get list of default KeyBindings for <code>actionDesc</code>     * @param actionDesc the action description     * @return list of KeyStrokePairs.     */    /*    public synchronized List getDefaultKeyBindingsFor(String actionDesc) {        KeyBindings keys = (KeyBindings)defaultActionMap.get(actionDesc);        List bindings = new ArrayList();        for (Iterator it = keys.getKeyStrokePairs(); it.hasNext(); ) {            bindings.add((KeyStrokePair)it.next());        }        return bindings;    }*/    /**     * Set <code>actionDesc<code> to use default KeyBindings     * @param actionDesc the action description     */    public synchronized void resetKeyBindings(String actionDesc) {        // remove all previous bindings        KeyBindings keys = (KeyBindings)actionMap.get(actionDesc);        if (keys != null) {            // get new iterator each time, because removeKeyStrokePair modifies the list            while(true) {                Iterator<KeyStrokePair> it = keys.getKeyStrokePairs();                if (!it.hasNext()) break;                KeyStrokePair pair = it.next();                removeKeyBinding(actionDesc, pair);            }        }        // remove any user saved preferences        //prefs.remove(actionDesc);        prefs.remove(prefPrefix+actionDesc);        // add in default key bindings        for (Iterator<KeyStrokePair> it = keys.getDefaultKeyStrokePairs(); it.hasNext(); ) {            KeyStrokePair k = it.next();            addKeyBinding(actionDesc, k);        }        keys.setUsingDefaultKeys(true);    }    /**     * Get bindings for action string     * @param actionDesc string describing action (KeyBinding.actionDesc)     * @return a KeyBindings object, or null.     */    public synchronized KeyBindings getKeyBindings(String actionDesc) {        return (KeyBindings)actionMap.get(actionDesc);    }    /**     * Class that converts internal key mappings to InputMap and ActionMap objects.     */    public static class KeyMaps    {    	private InputMap im;    	private ActionMap am;    	KeyMaps(KeyBindingManager kbm, Map<KeyStroke,Set<String>> inputMap, Map<String,Object> actionMap)    	{        	im = new InputMap();        	am = new ActionMap();        	for(KeyStroke ks : inputMap.keySet())        	{        		Set<String> theSet = inputMap.get(ks);        		if (theSet.size() > 0)        		{	        		String actionName = theSet.iterator().next();	        		im.put(ks, actionName);	        		am.put(actionName, new MyAbstractAction(actionName, kbm));        		}        	}        }    	public InputMap getInputMap() { return im; }    	public ActionMap getActionMap() { return am; }    }    private static class MyAbstractAction extends AbstractAction    {    	private String actionName;    	private KeyBindingManager kbm;    	MyAbstractAction(String actionName, KeyBindingManager kbm)    	{    		this.actionName = actionName;    		this.kbm = kbm;    	}    	public void actionPerformed(ActionEvent event)    	{    		KeyBindings kb = kbm.getKeyBindings(actionName);    		kb.actionPerformed(event);    	}    }    /**     * Method to return an object that has real InputMap and ActionMap objects.     * @return a KeyMaps object.     */    public KeyMaps getKeyMaps()    {    	KeyMaps km = new KeyMaps(this, inputMap, actionMap);    	return km;    }

⌨️ 快捷键说明

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