<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.wysiwyg.client.util;

import java.util.HashMap;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;
import com.xpn.xwiki.wysiwyg.client.widget.rta.RichTextArea;

/**
 * Associates {@link Command}s to shortcut keys.<br/>
 * NOTE: This class currently works only with {@link RichTextArea} because of a GWT issue that prevents us from
 * accessing the current event.
 * 
 * @version $Id$
 * @see http://code.google.com/p/google-web-toolkit/issues/detail?id=3133
 */
public class ShortcutKeyManager extends HashMap<ShortcutKey, Command> implements KeyboardListener
{
    /**
     * Field required by all {@link java.io.Serializable} classes.
     */
    private static final long serialVersionUID = 4703900727850408738L;

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyDown(Widget, char, int)
     */
    public void onKeyDown(Widget sender, char keyCode, int modifiers)
    {
        // Prevent default browser behavior.
        if (containsKey(new ShortcutKey(keyCode, modifiers))) {
            ((RichTextArea) sender).getCurrentEvent().xPreventDefault();
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyPress(Widget, char, int)
     */
    public void onKeyPress(Widget sender, char keyCode, int modifiers)
    {
        // ignore
    }

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyUp(Widget, char, int)
     */
    public void onKeyUp(Widget sender, char keyCode, int modifiers)
    {
        // Schedule commands.
        Command command = get(new ShortcutKey(keyCode, modifiers));
        if (command != null) {
            DeferredCommand.addCommand(command);
        }
    }
}<commit_msg>XWIKI-3048: Undo/Redo/Copy/Paste/Cut Mac shortcuts should be mapped to the corresponding features of the WYSIWYG editor<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.wysiwyg.client.util;

import java.util.HashMap;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;
import com.xpn.xwiki.wysiwyg.client.widget.rta.RichTextArea;

/**
 * Associates {@link Command}s to shortcut keys.<br/>
 * NOTE: This class currently works only with {@link RichTextArea} because of a GWT issue that prevents us from
 * accessing the current event.
 * 
 * @version $Id$
 * @see http://code.google.com/p/google-web-toolkit/issues/detail?id=3133
 */
public class ShortcutKeyManager extends HashMap<ShortcutKey, Command> implements KeyboardListener
{
    /**
     * Field required by all {@link java.io.Serializable} classes.
     */
    private static final long serialVersionUID = 4703900727850408738L;

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyDown(Widget, char, int)
     */
    public void onKeyDown(Widget sender, char keyCode, int modifiers)
    {
        Command command = get(new ShortcutKey(keyCode, modifiers));
        if (command != null) {
            // Prevent default browser behavior.
            ((RichTextArea) sender).getCurrentEvent().xPreventDefault();
            // Schedule command.
            DeferredCommand.addCommand(command);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyPress(Widget, char, int)
     */
    public void onKeyPress(Widget sender, char keyCode, int modifiers)
    {
        // NOTE: It seems the key modifiers are not detected on this event.
    }

    /**
     * {@inheritDoc}
     * 
     * @see KeyboardListener#onKeyUp(Widget, char, int)
     */
    public void onKeyUp(Widget sender, char keyCode, int modifiers)
    {
        // NOTE: It seems that KeyboardListener#MODIFIER_META is not detected on apple keyboards (where it is mapped to
        // apple command key) for this event. We have to detect it on key down.
    }
}
{'fraction_non_alphanumeric': 0.09444348873215207, 'fraction_numerical': 0.01307414415964218, 'mean_word_length': 3.164756446991404, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '685238', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1633, 'n_words': 579}