
// HelloGUI.java
// Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A GUI window using a Lanterna theme, which has a label
   centered in its middle, filling the window, and a 
   close box at the bottom left.

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   Usage (Windows):
     > compile.bat HelloGUI.java
     > run.bat HelloGUI

   Usage (Linux):
     > ./compile.sh HelloGUI.java
     > ./run.sh HelloGUI

*/



import java.io.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;

import com.googlecode.lanterna.bundle.LanternaThemes;


public class HelloGUI 
{

  public static void main(String[] args)
                  //    throws IOException
  {
    // build screen, gui, window
    Screen screen = Utils.createScreen();

    TerminalSize termSz = screen.getTerminalSize();
    System.err.println("Terminal size: " + termSz);
/*
    MultiWindowTextGUI gui = 
         new MultiWindowTextGUI(screen);
*/
    MultiWindowTextGUI gui = 
       new MultiWindowTextGUI(screen, 
          new DefaultWindowManager(),
               new EmptySpace(TextColor.ANSI.BLACK) );
        // make the background of the gui all black

    System.err.println("Available themes:");
    for(String theme: LanternaThemes.getRegisteredThemes())
      System.err.println("  " + theme);

    gui.setTheme(LanternaThemes.getRegisteredTheme(
                                   // "default"));
                                     "defrost"));
                                   //  "businessmachine"));

    BasicWindow window = new BasicWindow();

    // the panel is a 1-column grid
    Panel p = new Panel(new GridLayout(1));
    p.setPreferredSize(termSz.withRelative(-4,-4)); 
           // make panel a bit smaller than the terminal

    // center a label in the panel
    Label label = new Label("Hello World");
    label.setLayoutData(
       GridLayout.createLayoutData(
            GridLayout.Alignment.CENTER, 
            GridLayout.Alignment.CENTER, 
            true, true));  // use extra horizontal and vertical space

    p.addComponent(label);
    p.addComponent( new Button("Exit", window::close)); 

    // link panel, window, and gui
    window.setComponent(p);
    gui.addWindowAndWait(window);

    Utils.close(screen);
  }  // end of main()

}
