use of com.googlecode.lanterna.gui2.Panel in project jbang-catalog by quintesse.
the class lanterna method main.
public static void main(String[] args) {
DefaultTerminalFactory terminalFactory = new DefaultTerminalFactory().setMouseCaptureMode(MouseCaptureMode.CLICK_RELEASE);
try (Screen screen = terminalFactory.createScreen()) {
screen.startScreen();
List<String> timezonesAsStrings = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
final Window window = new BasicWindow("My Root Window");
Panel contentPanel = new Panel().setLayoutManager(new GridLayout(2).setHorizontalSpacing(3)).addComponent(new Label("This is a label that spans two columns").setLayoutData(GridLayout.createLayoutData(// Horizontal alignment in the grid cell if the cell
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell
GridLayout.Alignment.BEGINNING, // Give the component extra horizontal space if available
true, // Give the component extra vertical space if available
false, // Horizontal span
2, // Vertical span
1))).addComponent(new Label("Text Box (aligned)")).addComponent(new TextBox().setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.BEGINNING, GridLayout.Alignment.CENTER))).addComponent(new Label("Password Box (right aligned)")).addComponent(new TextBox().setMask('*').setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER))).addComponent(new Label("Read-only Combo Box (forced size)")).addComponent(new ComboBox<>(timezonesAsStrings).setReadOnly(true).setPreferredSize(new TerminalSize(20, 1))).addComponent(new Label("Editable Combo Box (filled)")).addComponent(new ComboBox<>("Item #1", "Item #2", "Item #3", "Item #4").setReadOnly(false).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(1))).addComponent(new Label("Button (centered)")).addComponent(new Button("Button", () -> MessageDialog.showMessageDialog(textGUI, "MessageBox", "This is a message box", MessageDialogButton.OK)).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER))).addComponent(new EmptySpace().setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2))).addComponent(new Separator(Direction.HORIZONTAL).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2))).addComponent(new Button("Close", window::close).setLayoutData(GridLayout.createHorizontallyEndAlignedLayoutData(2)));
window.setComponent(contentPanel);
window.addWindowListener(new WindowListenerAdapter() {
public void onUnhandledInput(Window basePane, KeyStroke keyStroke, AtomicBoolean hasBeenHandled) {
if (keyStroke.getKeyType() == KeyType.Escape) {
window.close();
}
}
});
textGUI.addWindowAndWait(window);
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.googlecode.lanterna.gui2.Panel in project security-lib by ncsa.
the class GUIDemo method helloWorld.
public static void helloWorld(Screen screen) {
// Create panel to hold components
Panel panel = new Panel();
panel.setLayoutManager(new GridLayout(2));
panel.addComponent(new Label("Forename"));
panel.addComponent(new TextBox());
panel.addComponent(new Label("Surname"));
panel.addComponent(new TextBox());
// Empty space underneath labels
panel.addComponent(new EmptySpace(new TerminalSize(0, 0)));
panel.addComponent(new Button("Submit"));
// Create window to hold the panel
BasicWindow window = new BasicWindow();
window.setComponent(panel);
// Create gui and start gui
MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
gui.addWindowAndWait(window);
}
use of com.googlecode.lanterna.gui2.Panel in project security-lib by ncsa.
the class GUIDemo method testBox.
/**
* Next is taken verbatim from
* <a href="https://github.com/mabe02/lanterna/blob/master/src/test/java/com/googlecode/lanterna/tutorial/Tutorial04.java">this laterna tutorial.</a>
* It will create a groovy floating text window with border. The contents have a few labels and a text box.
* <h3>Notes</h3>
* <ul>
* <li>Tab between items.</li>
* <li>A return activates item</li>
* <li>Looks like accelerators are enabled (first character highlighted) but no code to do that, To close, tab to "Close"
* button and hit enter.</li>
* </ul>
*
* @param screen
* @throws IOException
*/
protected static void testBox(Screen screen) throws IOException {
// ------------------
// Note that this puts windows on a screen but altering the screen might give strange results,
// because these GUI components are not aware of it. Think of the screen as working like
// the Swing JDesktop pane and being a container.
// All the components (e.g.Panel) are Lanterna NOT Java!
// ------------------
final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
/*
Creating a new window is relatively uncomplicated, you can optionally supply a title for the window
*/
final Window window = new BasicWindow("My Root Window");
/*
The window has no content initially, you need to call setComponent to populate it with something. In this
case, and quite often in fact, you'll want to use more than one component so we'll create a composite
'Panel' component that can hold multiple sub-components. This is where we decide what the layout manager
should be.
*/
Panel contentPanel = new Panel(new GridLayout(2));
/*
* Lanterna contains a number of built-in layout managers, the simplest one being LinearLayout that simply
* arranges components in either a horizontal or a vertical line. In this tutorial, we'll use the GridLayout
* which is based on the layout manager with the same name in SWT. In the constructor above we have
* specified that we want to have a grid with two columns, below we customize the layout further by adding
* some spacing between the columns.
*/
GridLayout gridLayout = (GridLayout) contentPanel.getLayoutManager();
gridLayout.setHorizontalSpacing(3);
/*
One of the most basic components is the Label, which simply displays a static text. In the example below,
we use the layout data field attached to each component to give the layout manager extra hints about how it
should be placed. Obviously the layout data has to be created from the same layout manager as the container
is using, otherwise it will be ignored.
*/
Label title = new Label("This is a label that spans two columns");
title.setLayoutData(GridLayout.createLayoutData(// Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
GridLayout.Alignment.BEGINNING, // Give the component extra horizontal space if available
true, // Give the component extra vertical space if available
false, // Horizontal span
2, // Vertical span
1));
contentPanel.addComponent(title);
/*
Since the grid has two columns, we can do something like this to add components when we don't need to
customize them any further.
*/
contentPanel.addComponent(new Label("Text Box (aligned)"));
contentPanel.addComponent(new TextBox().setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.BEGINNING, GridLayout.Alignment.CENTER)));
/*
Here is an example of customizing the regular text box component so it masks the content and can work for
password input.
*/
contentPanel.addComponent(new Label("Password Box (right aligned)"));
contentPanel.addComponent(new TextBox().setMask('*').setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER)));
/*
While we are not going to demonstrate all components here, here is an example of combo-boxes, one that is
read-only and one that is editable.
*/
contentPanel.addComponent(new Label("Read-only Combo Box (forced size)"));
List<String> timezonesAsStrings = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
ComboBox<String> readOnlyComboBox = new ComboBox<>(timezonesAsStrings);
readOnlyComboBox.setReadOnly(true);
readOnlyComboBox.setPreferredSize(new TerminalSize(20, 1));
contentPanel.addComponent(readOnlyComboBox);
contentPanel.addComponent(new Label("Editable Combo Box (filled)"));
contentPanel.addComponent(new ComboBox<>("Item #1", "Item #2", "Item #3", "Item #4").setReadOnly(false).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(1)));
/*
Some user interactions, like buttons, work by registering callback methods. In this example here, we're
using one of the pre-defined dialogs when the button is triggered.
*/
contentPanel.addComponent(new Label("Button (centered)"));
contentPanel.addComponent(new Button("Button", () -> MessageDialog.showMessageDialog(textGUI, "MessageBox", "This is a message box", MessageDialogButton.OK)).setLayoutData(GridLayout.createLayoutData(GridLayout.Alignment.CENTER, GridLayout.Alignment.CENTER)));
/*
Close off with an empty row and a separator, then a button to close the window
*/
contentPanel.addComponent(new EmptySpace().setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2)));
contentPanel.addComponent(new Separator(Direction.HORIZONTAL).setLayoutData(GridLayout.createHorizontallyFilledLayoutData(2)));
contentPanel.addComponent(new Button("Close", window::close).setLayoutData(GridLayout.createHorizontallyEndAlignedLayoutData(2)));
/*
We now have the content panel fully populated with components. A common mistake is to forget to attach it to
the window, so let's make sure to do that.
*/
window.setComponent(contentPanel);
/*
Now the window is created and fully populated. As discussed above regarding the threading model, we have the
option to fire off the GUI here and then later on decide when we want to stop it. In order for this to work,
you need a dedicated UI thread to run all the GUI operations, usually done by passing in a
SeparateTextGUIThread object when you create the TextGUI. In this tutorial, we are using the conceptually
simpler SameTextGUIThread, which essentially hijacks the caller thread and uses it as the GUI thread until
some stop condition is met. The absolutely simplest way to do this is to simply ask lanterna to display the
window and wait for it to be closed. This will initiate the event loop and make the GUI functional. In the
"Close" button above, we tied a call to the close() method on the Window object when the button is
triggered, this will then break the even loop and our call finally returns.
*/
textGUI.addWindowAndWait(window);
/*
When our call has returned, the window is closed and no longer visible. The screen still contains the last
state the TextGUI left it in, so we can easily add and display another window without any flickering. In
this case, we want to shut down the whole thing and return to the ordinary prompt. We just need to stop the
underlying Screen for this, the TextGUI system does not require any additional disassembly.
*/
}
use of com.googlecode.lanterna.gui2.Panel in project security-lib by ncsa.
the class GUIDemo method componentTest.
public static void componentTest(Screen screen, TextGraphics textGraphics) {
final WindowBasedTextGUI textGUI = new MultiWindowTextGUI(screen);
final Window window = new MyWindow();
Panel contentPanel = new Panel(new GridLayout(2));
contentPanel.addComponent(new Button("open file", new Runnable() {
@Override
public void run() {
input = new FileDialogBuilder().setTitle("Open File").setDescription("Choose a file").setActionLabel("Open").build().showDialog(textGUI);
}
}));
window.setComponent(contentPanel);
textGUI.addWindowAndWait(window);
window.close();
}
Aggregations