
// Comps1.java    (was Tutorial04.java)
// Martin Berglund
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A GUI window using a grid with two columns, 
   with spacing between the columns. Illustrates
   various GUI components. Also see Comps2.java.

   The rows of the grid are:
     * a label;
     * a textbox
     * a password field textbox
     * a read-only combobox
     * an editable combobox
     * a button with a MessageDialog
     * an empty row
     * a separator line
     * a GUI close button

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   More info at:
 https://github.com/mabe02/lanterna/blob/master/docs/using-gui.md
   and
 https://github.com/mabe02/lanterna/blob/master/docs/GUIGuideWindows.md

   Usage (Windows):
     > compile.bat Comps1.java
     > run.bat Comps1

   Usage (Linux):
     > ./compile.sh Comps1.java
     > ./run.sh Comps1

*/

import java.io.*;
import java.util.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.gui2.dialogs.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;


public class Comps1
{
  public static void main(String[] args)
                           //     throws IOException
  {
    // build screen, gui, window
    Screen screen = Utils.createScreen();
    WindowBasedTextGUI gui = new MultiWindowTextGUI(screen);
    Window window = new BasicWindow("My Root Window");

    Panel p = new Panel(new GridLayout(2));

    // a grid with two columns, with spacing between the columns
    GridLayout gridLayout = (GridLayout) p.getLayoutManager();
    gridLayout.setHorizontalSpacing(3);

    Label title = new Label("This is a label that spans two columns");
    title.setLayoutData(
       GridLayout.createLayoutData(
            GridLayout.Alignment.BEGINNING, 
            GridLayout.Alignment.BEGINNING, 
            true,     // Give extra horizontal space if available
            false,    // Give extra vertical space if available
            2, 1));   // Horizontal and vertical span

    p.addComponent(title);

    p.addComponent(new Label("Text Box (aligned)"));

    p.addComponent(new TextBox().
      setLayoutData(GridLayout.createLayoutData(
                      GridLayout.Alignment.BEGINNING,
                      GridLayout.Alignment.CENTER)));

    // text box masks the content for password input
    p.addComponent(new Label("Password"));

    p.addComponent( new TextBox().setMask('*').
      setLayoutData(GridLayout.createLayoutData(
                      GridLayout.Alignment.END,
                      GridLayout.Alignment.CENTER)));

    /* combo-boxes: one that is
       read-only and one that is editable.
     */
    p.addComponent(new Label("Read-only Combo"));

    List<String> zones = new ArrayList<>(
                   Arrays.asList(TimeZone.getAvailableIDs()));
    ComboBox<String> roCB = new ComboBox<>(zones);
    roCB.setReadOnly(true);
    roCB.setPreferredSize(new TerminalSize(20, 1));
    p.addComponent(roCB);

    p.addComponent(new Label("Editable Combo"));

    p.addComponent(new ComboBox<>("Item #1", "Item #2", 
                                   "Item #3", "Item #4").
      setReadOnly(false).setLayoutData(
           GridLayout.createHorizontallyFilledLayoutData(1)));

    // a button with a callback that invokes a MessageDialog
    p.addComponent(new Label("Button (centered)"));

    p.addComponent(new Button("Button",
         () -> MessageDialog.showMessageDialog(
                  gui, "MessageBox", "This is a message box",
                  MessageDialogButton .OK)).
                     setLayoutData( GridLayout.createLayoutData(
                          GridLayout.Alignment.CENTER,
                          GridLayout.Alignment.CENTER)) );

    /* an empty row, a separator, then a button to
       close the window
     */
    p.addComponent( new EmptySpace().setLayoutData(
        GridLayout.createHorizontallyFilledLayoutData(2)));

    p.addComponent( new Separator(
      com.googlecode.lanterna.gui2.Direction.HORIZONTAL).
      setLayoutData(
        GridLayout.createHorizontallyFilledLayoutData(2)));

    p.addComponent( new Button("Close", window::close).
      setLayoutData(
        GridLayout.createHorizontallyEndAlignedLayoutData(2)));

    // link panel, window, and gui
    window.setComponent(p);
    gui.addWindowAndWait(window);

    Utils.close(screen);
  } /* main */


}  // end of Comps1 class
