text:
package com.sikulix.core;

import com.sikulix.api.*;
import com.sikulix.api.Event;
import com.sikulix.util.animation.Animator;
import com.sikulix.util.animation.AnimatorOutQuarticEase;
import com.sikulix.util.animation.AnimatorTimeBased;
import org.sikuli.script.Screen;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class LocalDevice implements IDevice {

  private static SXLog log = SX.getLogger("SX.LocalDevice");

  //<editor-fold desc="*** houskeeping ***">
  @Override
  public IDevice start(Object... args) {
    if (0 < initMonitors()) {
      try {
        robot = new LocalRobot();
      } catch (AWTException e) {
        log.terminate(1, "LocalRobot: %s", e.getMessage());
      }
    } else {
      log.terminate(1, "No monitors - might be headless %s");
    }
    if (SX.isOption("SX.withHook", false)) {
      hook = NativeHook.start();
    }
    return this;
  }

  @Override
  public void stop() {
    SX.setLOCALDEVICE(null);
  }

  public boolean isValid() {
    return true;
  }

  public NativeHook getHook() {
    return hook;
  }

  NativeHook hook = null;

  private Object synchObject = new Object();
  private boolean locked = false;

//  private Element owner = new Element();
  private LocalRobot robot = null;

  private final int LEFT = InputEvent.BUTTON1_MASK;
  private final int MIDDLE = InputEvent.BUTTON2_MASK;
  private final int RIGHT = InputEvent.BUTTON3_MASK;

  public final double WHEEL_STEP_DELAY = 0.05;

  private double beforeButtonDown = 0;
  private double afterButtonDown = 0;
  private double beforeButtonUp = 0;
  private double afterButtonUp = 0;
  private double beforeButton = 0;
  private double afterButton = 0;
  private double clickDelay = 0;
  private double stepDelay = WHEEL_STEP_DELAY;
  private double beforeMove = 0;
  private double afterMove = 0;

  //TODO MouseButtons
  private boolean areExtraMouseButtonsEnabled = false;
  private int numberOfButtons = 0;
  private int maskForButton1 = 0;

  public void getMouseSetup() {
    areExtraMouseButtonsEnabled = Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled();
    numberOfButtons = MouseInfo.getNumberOfButtons();
    maskForButton1 = InputEvent.getMaskForButton(1);
  }

  public String toString() {
    return "LocalDevice";
  }
  //</editor-fold>

  //<editor-fold desc="*** coordinate usage">
  public synchronized void lock() {
    synchronized (synchObject) {
      while (locked) {
        try {
          synchObject.wait();
          locked = true;
          log.trace("lock start");
          checkLastPos();
          checkShouldRunCallback();
          if (shouldTerminate) {
            shouldTerminate = false;
            throw new AssertionError(String.format("Device: termination after return from callback"));
          }
        } catch (InterruptedException e) {
        }
      }
    }
  }

  public synchronized void unlock() {
    if (locked) {
      locked = false;
      synchObject.notify();
      log.trace("lock stop");
    }
  }
  //</editor-fold>

  //<editor-fold desc="*** mouse pointer location ***">
  private Element lastPos = null;

  public void setLastPos() {
    lastPos = at();
  }

  public void checkLastPos() {
    if (lastPos == null) {
      return;
    }
    Element pos = at();
    if (pos != null && (lastPos.x != pos.x || lastPos.y != pos.y)) {
      log.debug("moved externally: now (%d,%d) was (%d,%d) (movedAction %d)",
              pos.x, pos.y, lastPos.x, lastPos.y, movedAction);
      if (movedAction > 0) {
        if (MOVEDHIGHLIGHT) {
          showMousePos(pos.getPoint());
        }
      }
      if (movedAction == MOVEDPAUSE) {
        while (pos.x > 0 && pos.y > 0) {
          SX.pause(0.5);
          pos = at();
          if (MOVEDHIGHLIGHT) {
            showMousePos(pos.getPoint());
          }
        }
        if (pos.x < 1) {
          return;
        }
        SX.terminate(1, "Terminating in MouseMovedResponse = Pause");
      }
      if (movedAction == MOVEDCALLBACK) {
//TODO implement 3
//        if (mouseMovedCallback != null) {
//          mouseMovedCallback.happened(new ObserveEvent(ObserveEvent.Type.GENERIC, lastPos, new Location(pos)));
//          if (shouldTerminate) {
//            shouldTerminate = false;
//            throw new AssertionError("aborted by Sikulix.MouseMovedCallBack");
//          }
//        }
      }
    }
  }

  private void showMousePos(Point pos) {
    //TODO implement showMousePos (Element.show)
  }
  //</editor-fold>

  //<editor-fold desc="*** mouse moved reaction ***">
  public final int MOVEDIGNORE = 0;
  public final int MOVEDSHOW = 1;
  public final int MOVEDPAUSE = 2;
  public final int MOVEDCALLBACK = 3;

  private int movedAction = MOVEDIGNORE;
  private Event movedHandler;
  private boolean MOVEDHIGHLIGHT = true;

  /**
   * current setting what to do if mouse is moved outside Sikuli's mouse protection
   *
   * @return current setting see {@link #setMovedAction(int)}
   */
  public int getMovedAction() {
    return movedAction;
  }

  /**
   * what to do if mouse is moved outside Sikuli's mouse protection

   * - Mouse.MOVEDIGNORE (0) ignore it (default)

   * - Mouse.MOVEDSHOW (1) show and ignore it

   * - Mouse.MOVEDPAUSE (2) show it and pause until user says continue

   * - Mouse.MOVEDCALLBACK (3) visit a given callback {@link #setMovedCallback(Handler)}

   *
   * @param movedAction value
   */
  public int setMovedAction(int movedAction) {
    if (movedAction >= MOVEDIGNORE && movedAction <= MOVEDCALLBACK) {
      this.movedAction = movedAction;
      setCallback(null);
      log.debug("setMovedAction: %d", this.movedAction);
    } else {
      this.movedAction = MOVEDIGNORE;
      log.error("setMovedAction: %d invalid - setting to MOVEDIGNORE", movedAction);
    }
    return this.movedAction;
  }

  /**
   * what to do if mouse is moved outside Sikuli's mouse protection

   * in case of event the user provided callBack.generic is called
   *
   * @param handler Handler
   */
  public void setMovedCallback(Handler handler) {
    movedAction = MOVEDCALLBACK;
    movedHandler = new Event(handler);
  }

  /**
   * @param state
   */
  public void setMovedHighlight(boolean state) {
    MOVEDHIGHLIGHT = state;
  }

  /**
   * check if mouse was moved since last mouse action
   *
   * @return true/false
   */
  public boolean hasMoved() {
    Element pos = at();
    if (lastPos.x != pos.x || lastPos.y != pos.y) {
      return true;
    }
    return false;
  }
  //</editor-fold>

  //<editor-fold desc="*** click ***">
  @Override
  public Element click(Element loc) {
    return click(loc.getTarget(), Action.LEFT);
  }

  @Override
  public Element doubleClick(Element loc) {
    return click(loc, Action.LEFTDOUBLE);
  }

  @Override
  public Element rightClick(Element loc) {
    return click(loc, Action.RIGHT);
  }

  @Override
  public Element click(Action action) {
    return click(null, action);
  }

  @Override
  public Element click(Element loc, Action action) {
    if (SX.isNotNull(loc) && loc.isSpecial()) {
      return null;
    }
    boolean shouldMove = true;
    if (loc == null) {
      shouldMove = false;
      loc = at();
    }
    if (robot == null) {
      return loc;
    }
    lock();
    if (shouldMove) {
      loc = loc.getTarget();
      smoothMove(loc, robot);
    }
    log.trace("click: %s at %s%s", action, loc, (shouldMove ? " with move" : ""));
    int button = (action.toString().startsWith("L") ? LEFT : (action.toString().startsWith("R") ? RIGHT : MIDDLE));
    if (action.toString().contains("DOUBLE")) {
      SX.pause(beforeButton);
      robot.mouseDown(button);
      robot.mouseUp(button);
      robot.mouseDown(button);
      robot.mouseUp(button);
      SX.pause(afterButton);
    } else {
      SX.pause(beforeButton);
      robot.mouseDown(button);
      SX.pause(clickDelay);
      robot.mouseUp(button);
      SX.pause(afterButton);
    }
    unlock();
    return loc;
  }

  @Override
  public Element dragDrop(Element from, Element to, Object... times) {
    if (SX.isNotNull(from) && from.isSpecial()) {
      return null;
    }
    if (SX.isNotNull(to) && to.isSpecial()) {
      return null;
    }
    boolean shouldMove = true;
    if (SX.isNull(from)) {
      shouldMove = false;
      from = at();
    }
    if (SX.isNull(robot)) {
      return from;
    }
    lock();
    if (shouldMove) {
      from = from.getTarget();
      smoothMove(from, robot);
    }
    button(IDevice.Action.LEFTDOWN, false);
    if (SX.isNull(to)) {
      to = at();
    } else {
      to = to.getTarget();
      smoothMove(to, robot);
    }
    button(IDevice.Action.LEFTUP, false);
    log.trace("dragDrop %s from: %s%s to: %s", "LEFT", from, (shouldMove ? " with move" : ""), to);
    unlock();
    return to;
  }
  //</editor-fold>

  //<editor-fold desc="*** move ***">

  @Override
  public Element move(int xoff, int yoff) {
    return move(at().offset(xoff, yoff));
  }

  @Override
  public Element move(Element loc) {
    if (loc != null) {
      if (SX.isNotNull(robot)) {
        lock();
        smoothMove(loc.getTarget(), robot);
        unlock();
      }
    }
    return at();
  }

  private void smoothMove(Element dest, LocalRobot robot) {
    smoothMove(at(), dest, (long) (SX.getOptionNumber("Settings.MoveMouseDelay") * 1000L), robot);
  }

  private void smoothMove(Element src, Element dest, long ms, LocalRobot robot) {
    if (src.x == dest.x && src.y == src.y) {
      return;
    }
    int x = dest.x;
    int y = dest.y;
    log.trace("smoothMove (%.1f): (%d, %d) to (%d, %d)", (0.0 + ms)/1000, src.x, src.y, x, y);
    if (ms == 0) {
      robot.waitForIdle();
      robot.mouseMove(x, y);
      robot.waitForIdle();
    } else {
      Animator aniX = new AnimatorTimeBased(
              new AnimatorOutQuarticEase(src.x, dest.x, ms));
      Animator aniY = new AnimatorTimeBased(
              new AnimatorOutQuarticEase(src.y, dest.y, ms));
      while (aniX.running()) {
        x = (int) aniX.step();
        y = (int) aniY.step();
        robot.waitForIdle();
        robot.mouseMove(x, y);
        robot.waitForIdle();
      }
    }
    checkMouseMoved(dest);
  }

  private void checkMouseMoved(Element loc) {
    //TODO implementation with native hook
    PointerInfo mp = MouseInfo.getPointerInfo();
    Point pCurrent;
    if (mp == null) {
      log.error("checkMouseMoved: MouseInfo invalid after move to (%d, %d)", loc.x, loc.y);
    } else {
      pCurrent = mp.getLocation();
      if (pCurrent.x != loc.x || pCurrent.y != loc.y) {
        log.error("checkMouseMoved: should be (%d, %d)\nbut after move is (%d, %d)"
                        + "\nPossible cause in case you did not touch the mouse while script was running:\n"
                        + " Mouse actions are blocked generally or by the frontmost application."
                        + (SX.isWindows() ? "\nYou might try to run the SikuliX stuff as admin." : ""),
                loc.x, loc.y, pCurrent.x, pCurrent.y);
      }
    }
  }

  @Override
  public Element at() {
    PointerInfo mp = MouseInfo.getPointerInfo();
    if (mp != null) {
      return new Element(mp.getLocation());
    } else {
      log.error("MouseInfo.getPointerInfo(): null");
      return new Element();
    }
  }
  //</editor-fold>

  //<editor-fold desc="*** buttons ***">
  @Override
  public void button(Action action) {
    button(action, true);
  }

  private void button(Action action, boolean withLock) {
    int button = action.toString().startsWith("L") ? LEFT : (action.toString().startsWith("R") ? RIGHT : MIDDLE);
    if (action.toString().contains("DOWN")) {
      if (withLock) {
        lock();
      }
      SX.pause(beforeButtonDown);
      robot.mouseDown(button);
      SX.pause(afterButtonDown);
    } else {
      SX.pause(beforeButtonUp);
      robot.mouseUp(button);
      SX.pause(afterButtonUp);
      if (withLock) {
        unlock();
      }
    }
  }
  //</editor-fold>

  //TODO implement drag, drop, dragDrop

  //<editor-fold desc="*** mouse wheel ***">
  @Override
  public void wheel(Action action, int steps) {
    lock();
    int direction = action.toString().contains("DOWN") ? 1 : -1;
    for (int i = 0; i < steps; i++) {
      robot.mouseWheel(direction);
      SX.pause(stepDelay);
    }
    unlock();
  }
  //</editor-fold>

  //<editor-fold desc="*** keyboard ***">
  @Override
  public void keyStart() {
    lock();
    log.trace("keyStart");
  }

  @Override
  public void keyStop() {
    unlock();
    log.trace("keyStop");
  }

  @Override
  public void key(Action action, Object key) {
    if (key instanceof Character) {
      log.trace("key(char): %s (%s)", key, action);
      robot.typeChar((Character) key, action);
    } else if (key instanceof Integer) {
      robot.typeInt((Integer) key, action);
    } else {
      log.trace("key(???): %s (%s)", key, action);
    }
  }

  public class Modifier {
    public static final int CTRL = InputEvent.CTRL_MASK;
    public static final int SHIFT = InputEvent.SHIFT_MASK;
    public static final int ALT = InputEvent.ALT_MASK;
    public static final int ALTGR = InputEvent.ALT_GRAPH_MASK;
    public static final int META = InputEvent.META_MASK;
    public static final int CMD = InputEvent.META_MASK;
    public static final int WIN = 64;
  }

  public boolean hasModifier(int mods, int mkey) {
    return (mods & mkey) != 0;
  }
  //</editor-fold>

  //<editor-fold desc="monitors, capture, show">
  private GraphicsEnvironment genv = null;
  private GraphicsDevice[] gdevs;
  private Rectangle[] monitorBounds = null;
  private Rectangle rAllMonitors;
  private int mainMonitor = -1;
  private int nMonitors = 0;

  @Override
  public void resetMonitors() {
    initMonitors();
  }

  private int initMonitors() {
    if (!SX.isHeadless()) {
      genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
      gdevs = genv.getScreenDevices();
      nMonitors = gdevs.length;
      if (nMonitors == 0) {
        log.terminate(1, "getMonitors: GraphicsEnvironment has no ScreenDevices");
      }
      monitorBounds = new Rectangle[nMonitors];
      rAllMonitors = null;
      Rectangle currentBounds;
      for (int i = 0; i < nMonitors; i++) {
        currentBounds = new Rectangle(gdevs[i].getDefaultConfiguration().getBounds());
        if (null != rAllMonitors) {
          rAllMonitors = rAllMonitors.union(currentBounds);
        } else {
          rAllMonitors = currentBounds;
        }
        if (currentBounds.contains(new Point())) {
          if (mainMonitor < 0) {
            mainMonitor = i;
            log.trace("getMonitors: 1#ScreenDevice %d has (0,0) --- will be primary Screen(0)", i);
          } else {
            log.trace("getMonitors: 2#ScreenDevice %d too contains (0,0)!", i);
          }
        }
        log.trace("getMonitors: Monitor %d: (%d, %d) %d x %d", i,
                currentBounds.x, currentBounds.y, currentBounds.width, currentBounds.height);
        monitorBounds[i] = currentBounds;
      }
      if (mainMonitor < 0) {
        log.trace("getMonitors: No ScreenDevice has (0,0) --- using 0 as primary: %s", monitorBounds[0]);
        mainMonitor = 0;
      }
    } else {
      log.error("running in headless environment");
    }
    return nMonitors;
  }

  @Override
  public int getNumberOfMonitors() {
    return nMonitors;
  }

  @Override
  public Rectangle getMonitor(int... ids) {
    if (SX.isHeadless() || mainMonitor < 0) {
      return new Rectangle();
    }
    if (ids.length == 1) {
      return new Rectangle(monitorBounds[(ids[0] < 0 || ids[0] >= nMonitors) ? mainMonitor : ids[0]]);
    }
    return monitorBounds[mainMonitor];
  }

  @Override
  public int getMonitorID() {
    return mainMonitor;
  }

  @Override
  public int getMonitorID(int id) {
    if (id > -1 && id < nMonitors) {
      return id;
    }
    return mainMonitor;
  }

  @Override
  public Rectangle getAllMonitors() {
    return rAllMonitors;
  }

  @Override
  public Rectangle[] getMonitors() {
    return monitorBounds;
  }

  @Override
  public int getContainingMonitorID(Element element) {
    int n = 0;
    for (Rectangle monitor : getMonitors()) {
      if (monitor.contains(element.x, element.y)) {
        return n;
      }
      n++;
    }
    return mainMonitor;
  }

  @Override
  public Element getContainingMonitor(Element element) {
    return new Element(monitorBounds[getContainingMonitorID(element)]);
  }

  public Screen getContainingScreen(Element element) {
    return new Screen(getContainingMonitorID(element));
  }

  public GraphicsDevice getGraphicsDevice(int id) {
    return gdevs[id];
  }
  //TODO implement more support for Retina (HiDpi)
  public static boolean isRetina(int id) {
    LocalDevice localDevice = (LocalDevice) new LocalDevice().start();
    if (SX.isMac()) {
      GraphicsDevice screen = localDevice.getGraphicsDevice(0);
//      if (screen instanceof CGraphicsDevice) {
//        return 2 == ((CGraphicsDevice) screen).getScaleFactor();
//      }
    }
    return false;
  }

  //TODO implement capture, show coordination

  public Picture capture(Object... args) {
    Element what = Do.on();
    if (args.length > 0) {
      if (args[0] instanceof Element) {
        what = (Element) args[0];
      }
    }
    Picture img = new Picture(robot.createScreenCapture(what.getRectangle()));
    if (img.hasContent()) {
      what.setContent(img.getContent());
    } else {
      what.setContent();
    }
    return img;
  }
  //</editor-fold>

  //<editor-fold desc="*** Callback">
  private Event callback = null;
  private boolean shouldRunCallback = false;
  private boolean shouldTerminate = false;

  public void setShouldTerminate() {
    shouldTerminate = true;
    log.debug("setShouldTerminate: request issued");
  }

  public boolean isShouldRunCallback() {
    return shouldRunCallback;
  }

  public void setShouldRunCallback(boolean shouldRun) {
    shouldRunCallback = shouldRun;
  }

  private void checkShouldRunCallback() {
    if (shouldRunCallback && callback != null) {
      callback.handle();
      if (shouldTerminate) {
        shouldTerminate = false;
        throw new AssertionError("aborted by Sikulix.GenericDeviceCallBack");
      }
    }
  }

  /**
   * set a callback function for handling Device events

   * in case of event the user provided callBack.happened is called
   *
   * @param handler
   */
  public void setCallback(Handler handler) {
    callback = new Event(handler);
  }
  //</editor-fold>

  private class LocalRobot extends Robot {

    //<editor-fold desc="housekeeping">
    final int MAX_DELAY = 60000;
    private int heldButtons = 0;
    private String heldKeys = "";
    private final ArrayList<Integer> heldKeyCodes = new ArrayList<Integer>();
    public int stdAutoDelay = 0;
    public int stdDelay = 10;

    public LocalRobot() throws AWTException {
      super();
      setAutoDelay(stdAutoDelay);
      setAutoWaitForIdle(false);
    }

    public void pause(int ms) {
      if (ms < 0) {
        return;
      }
      while (ms > MAX_DELAY) {
        super.delay(MAX_DELAY);
        ms -= MAX_DELAY;
      }
      super.delay(ms);
    }
    //</editor-fold>

    //<editor-fold desc="Mouse">
    public void mouseDown(int buttons) {
      if (heldButtons != 0) {
        log.error("mouseDown: buttons still pressed - using all", buttons, heldButtons);
        heldButtons |= buttons;
      } else {
        heldButtons = buttons;
      }
      doMouseDown(heldButtons);
    }

    private void doMouseDown(int buttons) {
      Element.fakeHighlight(true);
      pause(100);
      Element.fakeHighlight(false);
      pause(100);
      waitForIdle();
      mousePress(buttons);
      if (stdAutoDelay == 0) {
        pause(stdDelay);
      }
      waitForIdle();
    }

    public int mouseUp(int buttons) {
      if (buttons == 0) {
        doMouseUp(heldButtons);
        heldButtons = 0;
      } else {
        doMouseUp(buttons);
        heldButtons &= ~buttons;
      }
      return heldButtons;
    }

    private void doMouseUp(int buttons) {
      waitForIdle();
      mouseRelease(buttons);
      if (stdAutoDelay == 0) {
        pause(stdDelay);
      }
      waitForIdle();
    }
    //</editor-fold>

    //<editor-fold desc="Screen">
    public Picture captureScreen(Rectangle rect) {
      waitForIdle();
      BufferedImage bImg = createScreenCapture(rect);
      waitForIdle();
      log.trace("captureScreen: [%d,%d, %dx%d]",
              rect.x, rect.y, rect.width, rect.height);
      return new Picture(bImg);
    }

    public Color getColorAt(int x, int y) {
      return getPixelColor(x, y);
    }
    //</editor-fold>

    //<editor-fold desc="Keys">
    public void pressModifiers(int modifiers) {
      if (hasModifier(modifiers, Modifier.SHIFT)) {
        doKeyPress(KeyEvent.VK_SHIFT);
      }
      if (hasModifier(modifiers, Modifier.CTRL)) {
        doKeyPress(KeyEvent.VK_CONTROL);
      }
      if (hasModifier(modifiers, Modifier.ALT)) {
        doKeyPress(KeyEvent.VK_ALT);
      }
      if (hasModifier(modifiers, Modifier.META)) {
        if (SX.isWindows()) {
          doKeyPress(KeyEvent.VK_WINDOWS);
        } else {
          doKeyPress(KeyEvent.VK_META);
        }
      }
    }

    public void releaseModifiers(int modifiers) {
      if (hasModifier(modifiers, Modifier.SHIFT)) {
        doKeyRelease(KeyEvent.VK_SHIFT);
      }
      if (hasModifier(modifiers, Modifier.CTRL)) {
        doKeyRelease(KeyEvent.VK_CONTROL);
      }
      if (hasModifier(modifiers, Modifier.ALT)) {
        doKeyRelease(KeyEvent.VK_ALT);
      }
      if (hasModifier(modifiers, Modifier.META)) {
        if (SX.isWindows()) {
          doKeyRelease(KeyEvent.VK_WINDOWS);
        } else {
          doKeyRelease(KeyEvent.VK_META);
        }
      }
    }

    public void keyDown(String keys) {
      if (keys != null && !"".equals(keys)) {
        for (int i = 0; i < keys.length(); i++) {
          if (heldKeys.indexOf(keys.charAt(i)) == -1) {
            log.trace("press: " + keys.charAt(i));
            typeChar(keys.charAt(i), Action.DOWN);
            heldKeys += keys.charAt(i);
          }
        }
      }
    }

    public void keyDown(int code) {
      if (!heldKeyCodes.contains(code)) {
        doKeyPress(code);
        heldKeyCodes.add(code);
      }
    }

    private void doKeyPress(int keyCode) {
      waitForIdle();
      keyPress(keyCode);
      if (stdAutoDelay == 0) {
        pause(stdDelay);
      }
      waitForIdle();
    }

    public void keyUp(String keys) {
      if (keys != null && !"".equals(keys)) {
        for (int i = 0; i < keys.length(); i++) {
          int pos;
          if ((pos = heldKeys.indexOf(keys.charAt(i))) != -1) {
            log.trace("release: " + keys.charAt(i));
            typeChar(keys.charAt(i), Action.UP);
            heldKeys = heldKeys.substring(0, pos)
                    + heldKeys.substring(pos + 1);
          }
        }
      }
    }

    public void keyUp(int code) {
      if (heldKeyCodes.contains(code)) {
        doKeyRelease(code);
        heldKeyCodes.remove((Object) code);
      }
    }

    public void keyUp() {
      keyUp(heldKeys);
      for (int code : heldKeyCodes) {
        keyUp(code);
      }
    }

    private void doKeyRelease(int keyCode) {
      waitForIdle();
      keyRelease(keyCode);
      if (stdAutoDelay == 0) {
        pause(stdDelay);
      }
      waitForIdle();
    }

    public void typeChar(char character, IDevice.Action action) {
      log.trace("Robot: typeChar(%s): %s ( %d )", action,
              KeyEvent.getKeyText(Keys.toJavaKeyCode(character)[0]),
              Keys.toJavaKeyCode(character)[0]);
      doType(action, Keys.toJavaKeyCode(character));
    }

    public void typeInt(Integer character, IDevice.Action action) {
      log.trace("Robot: typeInt(%s): %s ( %d )", action,
              KeyEvent.getKeyText(character), character);
      doType(action, character);
    }

    public void typeKey(int key) {
      log.trace("Robot: doType: %s ( %d )", KeyEvent.getKeyText(key), key);
      if (SX.isMac()) {
        if (key == Keys.toJavaKeyCodeFromText("#N.")) {
          doType(Action.DOWN, Keys.toJavaKeyCodeFromText("#C."));
          doType(Action.DOWNUP, key);
          doType(Action.UP, Keys.toJavaKeyCodeFromText("#C."));
          return;
        } else if (key == Keys.toJavaKeyCodeFromText("#T.")) {
          doType(Action.DOWN, Keys.toJavaKeyCodeFromText("#C."));
          doType(Action.DOWN, Keys.toJavaKeyCodeFromText("#A."));
          doType(Action.DOWNUP, key);
          doType(Action.UP, Keys.toJavaKeyCodeFromText("#A."));
          doType(Action.UP, Keys.toJavaKeyCodeFromText("#C."));
          return;
        } else if (key == Keys.toJavaKeyCodeFromText("#X.")) {
          key = Keys.toJavaKeyCodeFromText("#T.");
          doType(Action.DOWN, Keys.toJavaKeyCodeFromText("#A."));
          doType(Action.DOWNUP, key);
          doType(Action.UP, Keys.toJavaKeyCodeFromText("#A."));
          return;
        }
      }
      doType(Action.DOWNUP, key);
    }

    private void doType(IDevice.Action action, int... keyCodes) {
      waitForIdle();
      if (Action.DOWN.equals(action)) {
        for (int i = 0; i < keyCodes.length; i++) {
          doKeyPress(keyCodes[i]);
        }
      } else if (Action.UP.equals(action)) {
        for (int i = 0; i < keyCodes.length; i++) {
          doKeyRelease(keyCodes[i]);
        }
      } else {
        for (int i = 0; i < keyCodes.length; i++) {
          doKeyPress(keyCodes[i]);
        }
        for (int i = 0; i < keyCodes.length; i++) {
          doKeyRelease(keyCodes[i]);
        }
      }
      waitForIdle();
    }
    //</editor-fold>
  }
}

redpajama_set_name: RedPajamaGithub


text:
package microsoft.exchange.webservices.data;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.*;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * HttpClientWebRequest is used for making request to the server through
 * NTLM Authentication by using Apache HttpClient 3.1 and JCIFS Library.
 */
class HttpClientWebRequest extends HttpWebRequest {

  /**
   * The Http Method.
   */
  private HttpPost httpPost = null;
  private HttpResponse response = null;

  private final CloseableHttpClient httpClient;
  private final HttpClientContext httpContext;

  /**
   * Instantiates a new http native web request.
   */
  public HttpClientWebRequest(CloseableHttpClient httpClient, HttpClientContext httpContext) {
    this.httpClient = httpClient;
    this.httpContext = httpContext;
  }

  /**
   * Releases the connection by Closing.
   */
  @Override
  public void close() {
    if (null != httpPost) {
      httpPost.releaseConnection();
      //postMethod.abort();
    }
    httpPost = null;
  }

  /**
   * Prepares the request by setting appropriate headers, authentication, timeouts, etc.
   */
  @Override
  public void prepareConnection() {
    httpPost = new HttpPost(getUrl().toString());

    // Populate headers.
    httpPost.addHeader("Content-type", getContentType());
    httpPost.addHeader("User-Agent", getUserAgent());
    httpPost.addHeader("Accept", getAccept());
    httpPost.addHeader("Keep-Alive", "300");
    httpPost.addHeader("Connection", "Keep-Alive");

    if (isAcceptGzipEncoding()) {
      httpPost.addHeader("Accept-Encoding", "gzip,deflate");
    }

    if (getHeaders() != null) {
      for (Map.Entry<String, String> httpHeader : getHeaders().entrySet()) {
        httpPost.addHeader(httpHeader.getKey(), httpHeader.getValue());
      }
    }

    // Build request configuration.
    // Disable Kerberos in the preferred auth schemes - EWS should usually allow NTLM or Basic auth
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
        .setAuthenticationEnabled(true)
        .setConnectionRequestTimeout(getTimeout())
        .setConnectTimeout(getTimeout())
        .setRedirectsEnabled(isAllowAutoRedirect())
        .setSocketTimeout(getTimeout())
        .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.BASIC))
        .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.BASIC));

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    // Add proxy credentials if necessary.
    WebProxy proxy = getProxy();
    if (proxy != null) {
      HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
      requestConfigBuilder.setProxy(proxyHost);

      if (proxy.hasCredentials()) {
        NTCredentials proxyCredentials = new NTCredentials(proxy.getCredentials().getUsername(),
            proxy.getCredentials().getPassword(), "", proxy.getCredentials().getDomain());

        credentialsProvider.setCredentials(new AuthScope(proxyHost), proxyCredentials);
      }
    }

    // Add web service credentials if necessary.
    if (isAllowAuthentication() && getUsername() != null) {
      NTCredentials webServiceCredentials = new NTCredentials(getUsername(), getPassword(), "", getDomain());
      credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), webServiceCredentials);
    }

    httpContext.setCredentialsProvider(credentialsProvider);

    httpPost.setConfig(requestConfigBuilder.build());
  }

  /**
   * Gets the input stream.
   *
   * @return the input stream
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   * @throws java.io.IOException
   */
  @Override
  public InputStream getInputStream() throws EWSHttpException, IOException {
    throwIfResponseIsNull();
    BufferedInputStream bufferedInputStream = null;
    try {
      bufferedInputStream = new BufferedInputStream(response.getEntity().getContent());
    } catch (IOException e) {
      throw new EWSHttpException("Connection Error " + e);
    }
    return bufferedInputStream;
  }

  /**
   * Gets the error stream.
   *
   * @return the error stream
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public InputStream getErrorStream() throws EWSHttpException {
    throwIfResponseIsNull();
    BufferedInputStream bufferedInputStream = null;
    try {
      bufferedInputStream = new BufferedInputStream(response.getEntity().getContent());
    } catch (Exception e) {
      throw new EWSHttpException("Connection Error " + e);
    }
    return bufferedInputStream;
  }

  /**
   * Gets the output stream.
   *
   * @return the output stream
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public OutputStream getOutputStream() throws EWSHttpException {
    OutputStream os = null;
    throwIfRequestIsNull();
    os = new ByteArrayOutputStream();

    httpPost.setEntity(new ByteArrayOSRequestEntity(os));
    return os;
  }

  /**
   * Gets the response headers.
   *
   * @return the response headers
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public Map<String, String> getResponseHeaders()
      throws EWSHttpException {
    throwIfResponseIsNull();
    Map<String, String> map = new HashMap<String, String>();

    Header[] hM = response.getAllHeaders();
    for (Header header : hM) {
      // RFC2109: Servers may return multiple Set-Cookie headers
      // Need to append the cookies before they are added to the map
      if (header.getName().equals("Set-Cookie")) {
        String cookieValue = "";
        if (map.containsKey("Set-Cookie")) {
          cookieValue += map.get("Set-Cookie");
          cookieValue += ",";
        }
        cookieValue += header.getValue();
        map.put("Set-Cookie", cookieValue);
      } else {
        map.put(header.getName(), header.getValue());
      }
    }

    return map;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * microsoft.exchange.webservices.HttpWebRequest#getResponseHeaderField(
   * java.lang.String)
   */
  @Override
  public String getResponseHeaderField(String headerName)
      throws EWSHttpException {
    throwIfResponseIsNull();
    Header hM = response.getFirstHeader(headerName);
    return hM != null ? hM.getValue() : null;
  }

  /**
   * Gets the content encoding.
   *
   * @return the content encoding
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public String getContentEncoding() throws EWSHttpException {
    throwIfResponseIsNull();
    return response.getFirstHeader("content-encoding") != null ?
        response.getFirstHeader("content-encoding").getValue() :
        null;
  }

  /**
   * Gets the response content type.
   *
   * @return the response content type
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public String getResponseContentType() throws EWSHttpException {
    throwIfResponseIsNull();
    return response.getFirstHeader("Content-type") != null ?
        response.getFirstHeader("Content-type").getValue() :
        null;
  }

  /**
   * Executes Request by sending request xml data to server.
   *
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   * @throws java.io.IOException                                  the IO Exception
   */
  @Override
  public int executeRequest() throws EWSHttpException, IOException {
    throwIfRequestIsNull();
    response = httpClient.execute(httpPost, httpContext);
    return response.getStatusLine().getStatusCode(); // ?? don't know what is wanted in return
  }

  /**
   * Gets the response code.
   *
   * @return the response code
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  @Override
  public int getResponseCode() throws EWSHttpException {
    throwIfResponseIsNull();
    return response.getStatusLine().getStatusCode();
  }

  /**
   * Gets the response message.
   *
   * @return the response message
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  public String getResponseText() throws EWSHttpException {
    throwIfResponseIsNull();
    return response.getStatusLine().getReasonPhrase();
  }

  /**
   * Throw if conn is null.
   *
   * @throws EWSHttpException the eWS http exception
   */
  private void throwIfRequestIsNull() throws EWSHttpException {
    if (null == httpPost) {
      throw new EWSHttpException("Connection not established");
    }
  }

  private void throwIfResponseIsNull() throws EWSHttpException {
    if (null == response) {
      throw new EWSHttpException("Connection not established");
    }
  }

  /**
   * Gets the request properties.
   *
   * @return the request properties
   * @throws microsoft.exchange.webservices.data.EWSHttpException the eWS http exception
   */
  public Map<String, String> getRequestProperty() throws EWSHttpException {
    throwIfRequestIsNull();
    Map<String, String> map = new HashMap<String, String>();

    Header[] hM = httpPost.getAllHeaders();
    for (Header header : hM) {
      map.put(header.getName(), header.getValue());
    }
    return map;
  }
}


text:
package common.zeroquest.entity.ai;

import common.zeroquest.ModItems;
import common.zeroquest.entity.EntityDarkZertum;

import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class EntityCustomDarkZAIBeg extends EntityAIBase
{
    private EntityDarkZertum theWolf;
    private EntityPlayer thePlayer;
    private World worldObject;
    private float minPlayerDistance;
    private int field_75384_e;
    private static final String __OBFID = "CL_00001576";

    public EntityCustomDarkZAIBeg(EntityDarkZertum p_i1617_1_, float p_i1617_2_)
    {
        this.theWolf = p_i1617_1_;
        this.worldObject = p_i1617_1_.worldObj;
        this.minPlayerDistance = p_i1617_2_;
        this.setMutexBits(2);
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
        this.thePlayer = this.worldObject.getClosestPlayerToEntity(this.theWolf, (double)this.minPlayerDistance);
        return this.thePlayer == null ? false : this.hasPlayerGotBoneInHand(this.thePlayer);
    }

    /**
     * Returns whether an in-progress EntityAIBase should continue executing
     */
    public boolean continueExecuting()
    {
        return !this.thePlayer.isEntityAlive() ? false : (this.theWolf.getDistanceSqToEntity(this.thePlayer) > (double)(this.minPlayerDistance * this.minPlayerDistance) ? false : this.field_75384_e > 0 && this.hasPlayerGotBoneInHand(this.thePlayer));
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
        this.theWolf.func_70918_i(true);
        this.field_75384_e = 40 + this.theWolf.getRNG().nextInt(40);
    }

    /**
     * Resets the task
     */
    public void resetTask()
    {
        this.theWolf.func_70918_i(false);
        this.thePlayer = null;
    }

    /**
     * Updates the task
     */
    public void updateTask()
    {
        this.theWolf.getLookHelper().setLookPosition(this.thePlayer.posX, this.thePlayer.posY + (double)this.thePlayer.getEyeHeight(), this.thePlayer.posZ, 10.0F, (float)this.theWolf.getVerticalFaceSpeed());
        --this.field_75384_e;
    }

    /**
     * Gets if the Player has the Bone in the hand.
     */
    private boolean hasPlayerGotBoneInHand(EntityPlayer p_75382_1_)
    {
        ItemStack itemstack = p_75382_1_.inventory.getCurrentItem();
        return itemstack == null ? false : (!this.theWolf.isTamed() && itemstack.getItem() == ModItems.darkNileBone ? true : this.theWolf.isBreedingItem(itemstack));
    }
}


text:
class Base::MonitorsController < ApplicationController
  helper_method :custom_monitors_allowed?, :is_custom_monitor?

  def custom_monitors_allowed?
    @environment.ciAttributes.custom_monitor == 'true'
  end

  def is_custom_monitor?(monitor = @monitor)
    monitor.ciAttributes.custom == 'true'
  end
end


text:
{% extends "travel/base.html" %}
{% block travel_heading %}Advanced Search{% endblock travel_heading %}
{% block travel_content %}
    <form class="form-horizontal" action="" method="get" accept-charset="utf-8">

            <fieldset>

                    Search

                        <textarea
                            class="form-control"
                            id="id_search"
                            name="search"
                            placeholder="Add multiple search criteria, each on its own line"
                            rows="12">{{ search }}</textarea>

                        <button type="submit" class="btn btn-primary">Search</button>

            </fieldset>

    </form>

    {% include "travel/search/search-results.html" %}
{% endblock travel_content %}


text:
		<?php echo Html::img('assets/img/banners/login_banner.png') ?>

		<?php echo Form::open(array()); ?>

			<?php if (isset($_GET['destination'])): ?>
				<?php echo Form::hidden('destination',$_GET['destination']); ?>
			<?php endif; ?>

			<?php if (isset($login_error)): ?>

					<?php /*echo $login_error;*/ ?>
					<?php echo 'Incorrect E-mail or Password!'; ?>

			<?php endif; ?>

			<div class="form-group <?php echo ! $val->error('email') ?: 'has-error' ?>" style="margin-top: 40px;">

				<?php echo Form::input('email', Input::post('email'), array('class' => 'form-control', 'placeholder' => 'Email or Username', 'autofocus')); ?>

				<?php if ($val->error('email')): ?>
					<?php echo $val->error('email')->get_message('You must provide a username or email'); ?></sőan>
				<?php endif; ?>

			<div class="form-group <?php echo ! $val->error('password') ?: 'has-error' ?>">

				<?php echo Form::password('password', null, array('class' => 'form-control', 'placeholder' => 'Password')); ?>

				<?php if ($val->error('password')): ?>
					<?php echo $val->error('password')->get_message(':label cannot be blank'); ?>
				<?php endif; ?>

				<?php echo Form::submit(array('value'=>'Submit', 'name'=>'submit', 'class' => 'btn btn-lg btn-primary btn-block')); ?>

				<a id="custom_anchor" href="<?php echo Config::get('base_url'); ?>user" title="Visitor" style="color: #AAED93; text-decoration: none;">
				Proceed to visitors' page

		<?php echo Form::close(); ?>


text:
scrms
=====

small cloud resource manage


text:
namespace OrdersSystem.Web.Areas.Admin.Controllers
{
    using System.Linq;
    using System.Web.Mvc;

    using Common;
    using Infrastructure.Mapping;
    using Models;
    using OrdersSystem.Web.Controllers;
    using Services.Contracts;
    using ViewModels.Customers;

    [Authorize(Roles = GlobalConstants.AdministratorRoleName)]
    public class CustomersController : BaseController
    {
        private readonly ICustomersServices customers;

        public CustomersController(ICustomersServices customersServices)
        {
            this.customers = customersServices;
        }

        public ActionResult Index()
        {
            var viewModel = this.customers.GetAll().To<CustomerViewModel>();
            return this.View(viewModel);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return this.View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(CustomerInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                var customerName = this.customers
                    .GetAll()
                    .FirstOrDefault(x => x.Name.ToLower() == model.Name.ToLower());
                if (customerName == null)
                {
                    var customer = this.Mapper.Map<Customer>(model);
                    this.customers.Add(customer);
                    TempData["Success"] = GlobalConstants.CustomerAddNotify;
                }
                else
                {
                    TempData["Warning"] = GlobalConstants.CustomerExistsNotify;
                }

                return this.Redirect("/Admin/Customers/Index");
            }

            return this.View(model);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var viewModel = this.customers
                .GetAll()
                .Where(x => x.Id == id)
                .To<CustomerEditModel>()
                .FirstOrDefault();

            return this.View(viewModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CustomerEditModel model)
        {
            if (this.ModelState.IsValid)
            {
                var customer = this.Mapper.Map<Customer>(model);
                this.customers.Update(model.Id, customer);
                TempData["Success"] = GlobalConstants.CustomerUpdateNotify;

                return this.Redirect("/Admin/Customers/Index");
            }

            return this.View(model);
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            this.customers.Delete(id);
            TempData["Success"] = GlobalConstants.CustomerDeletedNotify;

            return this.Redirect("/Admin/Categories/Index");
        }
    }
}