Package eas.plugins.masterScheduler

Source Code of eas.plugins.masterScheduler.AbstractDefaultKeyEventMaster

/*
* Datei:            AbstractDefaultKeyEventMaster.java
* Autor(en):        Lukas König
* Java-Version:     6.0
* Erstellt:         28.10.2010
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.plugins.masterScheduler;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.ParCollection;

/**
* Implementation of a default master scheduler that produces a standard
* behavior suitable for most types of schedulers; see comments on
* <code>AbstractDefaultMaster</code> for more information.<BR>
* <BR>
* The major difference to the parental class is that the key event master
* catches key events and propagates them to the simulation time. This master
* is registered for all events by standard implementation.
*
* @author Lukas König
*/
public abstract class AbstractDefaultKeyEventMaster<Env extends AbstractEnvironment<?>>
                        extends AbstractDefaultMaster<Env>
                        implements KeyListener {
   
    private static final long serialVersionUID = -4278531393866750293L;
    private Env env;
    private JFrame f;

    /**
     * The behavior at any simulation step is to request the focus for the
     * window that catches key events. Otherwise key events could get
     * lost.
     */
    @Override
    public void runDuringSimulation(
            Env umg,
            Wink simZyk,
            ParCollection params) {
        super.runDuringSimulation(umg, simZyk, params);
        if (simZyk.getLastTick() % 1 == 0) {
            f.requestFocus();
        }
    }
   
    /**
     * Creates an invisible JFrame that catches key events.
     */
    @Override
    public void runBeforeSimulation(Env environment, ParCollection params) {
        super.runBeforeSimulation(environment, params);
        this.env = environment;
        this.f = new JFrame("Key controller");
        this.f.addKeyListener(this);
        this.f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.f.setUndecorated(true);
        this.f.setVisible(true);
        this.f.requestFocus();
        environment.getSimTime().requestEvents(new KeyEventFilter());
    }
   
    @Override
    public void runAfterSimulation(Env umg, ParCollection params) {
        this.f.dispose();
    }
   
    /**
     * Catches and broadcasts key events
     * (and reencodes them if they were produced by arrow keys).
     */
    @Override
    public void keyPressed(KeyEvent arg0) {
        String description = ConstantsSimulation.KEY_EVENT_IDENTIFIER + arg0.getKeyChar();
        if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_LEFT;
        }
        if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_RIGHT;
        }
        if (arg0.getKeyCode() == KeyEvent.VK_UP) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_UP;
        }
        if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_DOWN;
        }
        if (arg0.isShiftDown() && arg0.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_LEFT_SHIFT;
        }
        if (arg0.isShiftDown() && arg0.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT) {
            description = ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_RIGHT_SHIFT;
        }
        EASEvent e = new EASEvent(description);
        this.env.getSimTime().broadcastEvent(e);
    }

    /**
     * No functionality implemented.
     */
    @Override
    public void keyReleased(KeyEvent arg0) {

    }

    /**
     * No functionality implemented.
     */
    @Override
    public void keyTyped(KeyEvent arg0) {

    }
   
    @Override
    public void onSimulationResumed(Env env, Wink resumeTime,
            ParCollection params) {
        super.onSimulationResumed(env, resumeTime, params);
        this.f.setVisible(true);
    }
}
TOP

Related Classes of eas.plugins.masterScheduler.AbstractDefaultKeyEventMaster

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.