Esempio n. 1
0
 /**
  * Reads a line of text edited by the user, terminated by <code>ENTER</code>. Editing is performed
  * at the current cursor position.
  *
  * @param field Maximum length of the text
  * @return The text read. <code>null</code> if it was pressed the <code>Esc</code> key.
  */
 public static String nextLine(int field) {
   boolean oldCursorOn = frame.isCursorOn();
   boolean oldEcho = frame.isEcho();
   cursor(false);
   int lin = frame.getLin(), col = frame.getCol();
   for (int i = 0; i < field; ++i) print(' ');
   cursor(lin, col);
   cursor(true);
   echo(false);
   StringBuffer res = new StringBuffer(field);
   char c;
   while ((c = waitChar(0)) != '\n') {
     if (c == KeyEvent.VK_ESCAPE) {
       res = null;
       break;
     } // Esc (27)
     else if (c == KeyEvent.VK_BACK_SPACE) { // Backspace (8)
       if (res.length() == 0) continue;
       res.deleteCharAt(res.length() - 1);
       cursor(false);
       cursor(frame.getLin(), frame.getCol() - 1);
       print(' ');
       cursor(frame.getLin(), frame.getCol() - 1);
       cursor(true);
     } else if (res.length() < field) {
       print(c);
       res.append(c);
     }
   }
   cursor(oldCursorOn);
   echo(oldEcho);
   return res == null ? null : res.toString();
 }
Esempio n. 2
0
 /**
  * Wait until any key is pressed within the time indicated This method also checks action keys
  * (Cursor arrows, Home, etc.).
  *
  * @param timeout Maximum time to wait in milliseconds. If is 0 wait forever
  * @return The key pressed or a negative value if no key was pressed (NO_KEY or MOUSE_CLICK).
  * @see #getKeyPressedUntil(long)
  * @see #getKeyPressed()
  * @see #getMouseEvent()
  */
 public static int waitKeyPressed(long timeout) {
   if (timeout < 0) timeout = 0;
   if (timeout > 0) timeout += System.currentTimeMillis();
   int k;
   while ((k = frame.getKeyPressed()) == NO_KEY
       && (timeout <= 0 || System.currentTimeMillis() < timeout)) ;
   return k;
 }
Esempio n. 3
0
 /**
  * Reads a key which has been pressed. This method reads only keys that are used to edit text
  * (letters, digits, space, enter, etc.).
  *
  * @return The key pressed or NO_CHAR ((char) 0) if no key was pressed
  * @see #echo(boolean)
  * @see #waitChar(long)
  */
 public static char getChar() {
   try {
     char c = frame.getChar();
     return c == 0 ? NO_CHAR : c;
   } catch (InterruptedException e) {
     return NO_CHAR;
   }
 }
Esempio n. 4
0
 /**
  * Turn on the mechanism to read mouse events.
  *
  * <p>This mechanism is normally turned off when the console is open.
  *
  * <p>To read mouse events should be called the getMouseEvent method.
  *
  * @param drag True to enable also the mouse drags
  * @see #getMouseEvent()
  * @see #disableMouseEvents()
  * @see #waitKeyPressed(long)
  * @see #getKeyPressed()
  */
 public static void enableMouseEvents(boolean drag) {
   frame.enableMouseEvents(drag);
 }
Esempio n. 5
0
 /**
  * Returns the code of a key that is pressed, probably the last pressed. It may be more than one
  * key pressed at the same time. This method also checks action keys (Cursor arrows, Home, etc.).
  *
  * @return The code of the key pressed or a negative value if no key was pressed.
  *     <p>- NO_KEY (-1) if no key was pressed
  *     <p>- If <code>mouseClick(true)</code> and there was a mouse click and no key was pressed
  *     return MOUSE_CLICK (-2)
  * @see #isKeyPressed(int)
  * @see #waitKeyPressed(long)
  * @see #enableMouseEvents(boolean))
  * @see #getMouseEvent()
  */
 public static int getKeyPressed() {
   return frame.getKeyPressed();
 }
Esempio n. 6
0
 /**
  * Checks if any key is pressed. This method also checks action keys (Cursor arrows, Home, etc.).
  *
  * @return True if any key is pressed
  */
 public static boolean isKeyPressed() {
   return frame.anyKeyPressed();
 }
Esempio n. 7
0
 /**
  * Checks whether a specific key is pressed. It may be more than one key pressed at the same time.
  * This method also checks action keys (Cursor arrows, Home, etc.).
  *
  * @param code Code of key checked
  * @return True if key is pressed
  * @see #getKeyPressed()
  */
 public static boolean isKeyPressed(int code) {
   return frame.keyPressed(code);
 }
Esempio n. 8
0
 /**
  * Write a char into the console at the current position of the cursor and the current colors.
  *
  * @param c Char to write
  * @see #color(int, int)
  * @see #cursor(int, int)
  */
 public static void print(char c) {
   check();
   frame.put(c);
 }
Esempio n. 9
0
 /**
  * Close the console
  *
  * @see #open(int lines, int cols)
  * @see #open(String, int, int)
  */
 public static void close() {
   frame.dispose();
   frame = null;
 }
Esempio n. 10
0
 /** Clears the content of the console, writing spaces with current colors. */
 public static void clear() {
   frame.cursor(0, 0);
   for (int i = 0; i < lines * cols; ++i) frame.put(' ');
 }
Esempio n. 11
0
 /**
  * Change the colors of text for next writes
  *
  * @param color Foreground color
  * @see #color(int, int)
  */
 public static void setForeground(int color) {
   frame.setForeColor(colors[color % colors.length]);
 }
Esempio n. 12
0
 /**
  * Change the colors of background for next writes
  *
  * @param color Background color
  * @see #color(int, int)
  */
 public static void setBackground(int color) {
   frame.setBackColor(colors[color % colors.length]);
 }
Esempio n. 13
0
 /**
  * Enables or disables the cursor. When the console opens the cursor is disabled.
  *
  * @param on true to enable or false to disable
  */
 public static void cursor(boolean on) {
   frame.setCursorOn(on);
 }
Esempio n. 14
0
 /**
  * Changes the current cursor position for next writes.
  *
  * @param lin Line where the cursor changes [0..lines-1]
  * @param col Column where the cursor change [0..cols-1]
  * @see #open(int lines, int cols)
  */
 public static void cursor(int lin, int col) {
   if (lin < 0 || lin >= lines || col < 0 || col >= cols) throw new InvalidParameterException();
   frame.cursor(lin, col);
 }
Esempio n. 15
0
 /**
  * Turn off the mechanism to read mouse events.
  *
  * <p>
  *
  * @see #enableMouseEvents(boolean)
  * @see #getMouseEvent()
  */
 public static void disableMouseEvents() {
   frame.disableMouseEvents();
 }
Esempio n. 16
0
 /**
  * Returns the event information of the last mouse event.
  *
  * <p>Always returns null if the mouse event mechanism is off.
  *
  * <p>The <code>waitKeyPressed()</code> and <code>getKeyPressed()</code> return <code>MOUSE_EVENT
  * </code> (-3) when there is no key but there is a mouse event.
  *
  * @return The event information of the last mouse event or <code>null</code> if no events.
  * @see #enableMouseEvents(boolean)
  * @see #disableMouseEvents()
  * @see #getMouseEvent(int)
  * @see #waitKeyPressed(long)
  * @see #getKeyPressed()
  */
 public static MouseEvent getMouseEvent() {
   return frame.getMouseEvent();
 }
Esempio n. 17
0
 /**
  * Turns on or off the echo of next keys read from the keyboard with getKey() or waitKey(). When
  * the console opens the echo is on.
  *
  * @param on true to on; false to off
  * @see #getChar()
  * @see #waitChar(long)
  */
 public static void echo(boolean on) {
   frame.setEcho(on);
 }
Esempio n. 18
0
 /**
  * Defines the behavior of the close button of the window in console. This method must be called
  * after open()
  *
  * @param on
  *     <p><code>true</code> to terminates the application if the window is closed.
  *     <p><code>false</code> to ignore the close button.
  */
 public static void exit(boolean on) {
   frame.setDefaultCloseOperation(on ? JFrame.EXIT_ON_CLOSE : JFrame.DO_NOTHING_ON_CLOSE);
 }
Esempio n. 19
0
 /**
  * Write a string into the console at the current position of the cursor and the current colors.
  *
  * @param s Text to write
  * @see #color(int, int)
  * @see #cursor(int, int)
  */
 public static void print(String s) {
   check();
   frame.put(s);
 }