/*
 * (c) Copyright: Artenum SARL, 101-103 Boulevard Mac Donald, 
 *                75019, Paris, France 2005.
 *                http://www.artenum.com
 *
 * License:
 *
 *  This program is free software; you can redistribute it 
 *  and/or modify it under the terms of the Q Public License; 
 *  either version 1 of the License.
 * 
 *  This program 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 Q Public License for more details.
 * 
 *  You should have received a copy of the Q Public License 
 *  License along with this program;
 *  if not, write to:
 *    Artenum SARL, 101-103 Boulevard Mac Donald,
 *    75019, PARIS, FRANCE, e-mail: contact@artenum.com
 */

package com.artenum.jyconsole.action;

import com.artenum.jyconsole.JyConsole;

import java.awt.Event;
import java.awt.event.KeyEvent;

import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.KeyStroke;

/**
 * <pre>
 * <b>Project ref           :</b> JyConsole project
 * <b>Copyright and license :</b> See relevant sections
 * <b>Status                :</b> under development
 * <b>Creation              :</b> 04/03/2005
 * <b>Modification          :</b>
 *
 * <b>Description  :</b> Set the whole default key binding.
 *               => UP / DOWN    : Command history
 *               => CTRL + SPACE : Completion
 *               => Enter        : Validation
 *
 * </pre>
 * <table cellpadding="3" cellspacing="0" border="1" width="100%">
 * <tr BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><td><b>Version number</b></td><td><b>Author (name, e-mail)</b></td><td><b>Corrections/Modifications</b></td></tr>
 * <tr><td>0.1</td><td>Sebastien Jourdain, jourdain@artenum.com</td><td>Creation</td></tr>
 * </table>
 *
 * @author        Sebastien Jourdain
 * @version       0.1
 */
public class DefaultKeyBinding {
    public final static String EXECUTE_COMMAND = "EXECUTE_COMMAND";
    public final static String ESCAPE_COMMAND = "ESCAPE_COMMAND";
    public final static String UP_COMMAND = "UP_COMMAND";
    public final static String DOWN_COMMAND = "DOWN_COMMAND";
    public final static String COMPLETION_COMMAND = "COMPLETION_COMMAND";

    public static void applyDefaultMapping(JyConsole console) {
        InputMap inputMap = console.getInputComponent().getInputMap();
        ActionMap actionMap = console.getInputComponent().getActionMap();

        // Register default input
        inputMap.put(KeyStroke.getKeyStroke(10, 0), EXECUTE_COMMAND);
        inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), ESCAPE_COMMAND);
        inputMap.put(KeyStroke.getKeyStroke(38, 0), UP_COMMAND);
        inputMap.put(KeyStroke.getKeyStroke(40, 0), DOWN_COMMAND);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, Event.CTRL_MASK), COMPLETION_COMMAND);
        // Register default action
        actionMap.put(EXECUTE_COMMAND, new DefaultValidationAction(console));
        actionMap.put(ESCAPE_COMMAND, new DefaultEscapeAction(console));
        actionMap.put(UP_COMMAND, new DefaultHistoryNavigationAction(console, DefaultHistoryNavigationAction.UP));
        actionMap.put(DOWN_COMMAND, new DefaultHistoryNavigationAction(console, DefaultHistoryNavigationAction.DOWN));
        actionMap.put(COMPLETION_COMMAND, new DefaultCompletionAction(console));
    }
}
