Search in sources :

Example 6 with Button

use of com.googlecode.lanterna.gui2.Button 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.
                   */
}
Also used : Window(com.googlecode.lanterna.gui2.Window) Label(com.googlecode.lanterna.gui2.Label) ArrayList(java.util.ArrayList) Panel(com.googlecode.lanterna.gui2.Panel) GridLayout(com.googlecode.lanterna.gui2.GridLayout) Button(com.googlecode.lanterna.gui2.Button) MessageDialogButton(com.googlecode.lanterna.gui2.dialogs.MessageDialogButton)

Example 7 with Button

use of com.googlecode.lanterna.gui2.Button 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();
}
Also used : Window(com.googlecode.lanterna.gui2.Window) FileDialogBuilder(com.googlecode.lanterna.gui2.dialogs.FileDialogBuilder) Panel(com.googlecode.lanterna.gui2.Panel) GridLayout(com.googlecode.lanterna.gui2.GridLayout) Button(com.googlecode.lanterna.gui2.Button) MessageDialogButton(com.googlecode.lanterna.gui2.dialogs.MessageDialogButton)

Aggregations

MessageDialogButton (com.googlecode.lanterna.gui2.dialogs.MessageDialogButton)4 Button (com.googlecode.lanterna.gui2.Button)3 GridLayout (com.googlecode.lanterna.gui2.GridLayout)3 Panel (com.googlecode.lanterna.gui2.Panel)3 PageEditorPage (com.adobe.cq.testing.selenium.pageobject.PageEditorPage)2 Button (com.adobe.cq.wcm.core.components.it.seljup.util.components.button.v1.Button)2 Label (com.googlecode.lanterna.gui2.Label)2 Window (com.googlecode.lanterna.gui2.Window)2 ArrayList (java.util.ArrayList)2 TerminalSize (com.googlecode.lanterna.TerminalSize)1 FileDialogBuilder (com.googlecode.lanterna.gui2.dialogs.FileDialogBuilder)1 KeyStroke (com.googlecode.lanterna.input.KeyStroke)1 Screen (com.googlecode.lanterna.screen.Screen)1 DefaultTerminalFactory (com.googlecode.lanterna.terminal.DefaultTerminalFactory)1 Button (controlP5.Button)1 Toggle (controlP5.Toggle)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1