Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions curses/src/main/java/org/jline/curses/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.EnumSet;

import org.jline.terminal.KeyEvent;
import org.jline.terminal.MouseEvent;

public interface Component {
Expand Down Expand Up @@ -51,7 +52,32 @@ enum Behavior {
Popup
}

void handleMouse(MouseEvent event);

void handleInput(String input);
boolean handleMouse(MouseEvent event);

/**
* Handle a key event.
* @param event the key event to handle
* @return true if the event was handled, false otherwise
*/
boolean handleKey(KeyEvent event);

/**
* Marks this component as needing to be repainted.
* This will trigger a redraw on the next render cycle.
*/
void invalidate();

/**
* Returns true if this component needs to be repainted.
* @return true if the component is invalid and needs repainting
*/
boolean isInvalid();

/**
* Gets the shortcut key for this component, if any.
* @return the shortcut key, or null if none
*/
default String getShortcutKey() {
return null;
}
}
44 changes: 41 additions & 3 deletions curses/src/main/java/org/jline/curses/Curses.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public static Box box(String title, Border border, Component component) {
return new Box(title, border, component);
}

public static BoxBuilder box(String title, Border border) {
return new BoxBuilder(title, border);
}

public interface ComponentBuilder<C extends Component> {

C build();
Expand Down Expand Up @@ -127,7 +131,7 @@ public Container build() {
public static class SubMenuBuilder {
private String name;
private String key;
List<MenuItem> contents = new ArrayList<>();
java.util.List<MenuItem> contents = new ArrayList<>();

public SubMenuBuilder name(String name) {
this.name = name;
Expand Down Expand Up @@ -205,9 +209,9 @@ public SubMenuBuilder add() {

public static class MenuBuilder implements ComponentBuilder<Menu> {

List<SubMenu> contents = new ArrayList<>();
java.util.List<SubMenu> contents = new ArrayList<>();

public MenuBuilder submenu(String name, String key, List<MenuItem> menu) {
public MenuBuilder submenu(String name, String key, java.util.List<MenuItem> menu) {
return submenu(new SubMenu(name, key, menu));
}

Expand Down Expand Up @@ -259,4 +263,38 @@ public Window build() {
return w;
}
}

public static class BoxBuilder implements ComponentBuilder<Box> {
private final String title;
private final Border border;
private Component component;
private String shortcutKey;

BoxBuilder(String title, Border border) {
this.title = title;
this.border = border;
}

public BoxBuilder component(Component component) {
this.component = component;
return this;
}

public BoxBuilder component(ComponentBuilder<?> component) {
return component(component.build());
}

public BoxBuilder key(String shortcutKey) {
this.shortcutKey = shortcutKey;
return this;
}

@Override
public Box build() {
if (component == null) {
throw new IllegalStateException("Component must be set");
}
return new Box(title, border, component, shortcutKey);
}
}
}
70 changes: 70 additions & 0 deletions curses/src/main/java/org/jline/curses/InputEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2002-2018, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.curses;

import org.jline.terminal.KeyEvent;

/**
* Represents an input event that can be either a KeyEvent or a Mouse event indicator.
* This is used in the KeyMap to distinguish between keyboard and mouse input.
*/
public class InputEvent {

/**
* Special singleton instance to indicate mouse events.
*/
public static final InputEvent MOUSE = new InputEvent();

private final KeyEvent keyEvent;
private final boolean isMouse;

// Private constructor for mouse indicator
private InputEvent() {
this.keyEvent = null;
this.isMouse = true;
}

/**
* Creates an InputEvent for a KeyEvent.
*/
public InputEvent(KeyEvent keyEvent) {
this.keyEvent = keyEvent;
this.isMouse = false;
}

/**
* Returns true if this is a mouse event indicator.
*/
public boolean isMouse() {
return isMouse;
}

/**
* Returns true if this is a key event.
*/
public boolean isKey() {
return !isMouse;
}

/**
* Returns the KeyEvent if this is a key event, null otherwise.
*/
public KeyEvent getKeyEvent() {
return keyEvent;
}

@Override
public String toString() {
if (isMouse) {
return "InputEvent[MOUSE]";
} else {
return "InputEvent[" + keyEvent + "]";
}
}
}
Loading
Loading