
// Comps2.java  (was MiscComponentTest.java)
// Martin Berglund, 2020
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A GUI window using a panel containing two nested
   panels for the left and right sides. Illustrates
   various GUI components and the Panels class. 
   Also see Comps1.java.

   The left panel contains:
     * four checkboxes in their own panel
     * two textfields in their own panel
     * a button in its own panel that affects the
       spacing of the left panel

   The right panel contains:
     * a read-only text area in its own panel so that
       scrollbars appear on the right and bottom;
     * a progress bar that is moved using a
       repeating timer

and
     * a separator line
     * a GUI "Ok" close button

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   Usage (Windows):
     > compile.bat Comps2.java
     > run.bat Comps2

   Usage (Linux):
     > ./compile.sh Comps2.java
     > ./run.sh Comps2

*/

import java.io.*;
import java.util.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;


public class Comps2
{
  private static String HAMLET = 
     "To be, or not to be, that is the question:\n" +
     "Whether 'tis nobler in the mind to suffer\n" +
     "The slings and arrows of outrageous fortune,\n" +
     "Or to take arms against a sea of troubles\n" +
     "And by opposing end themo die -- to sleep,\n" +
     "No more; and by a sleep to say we end\n" +
     "The heart-ache and the thousand natural shocks\n" +
     "That flesh is heir to: 'tis a consummation\n" +
     "Devoutly to be wish'd. To die, to sleep;\n" +
     "To sleep, perchance to dream -- ay, there's the rub:\n" +
     "For in that sleep of death what dreams may come.\n";


  public static void main(String[] args) 
   //  throws IOException, InterruptedException 
  {
    // build screen, gui, window
    Screen screen = Utils.createScreen();

    MultiWindowTextGUI gui = 
         new MultiWindowTextGUI(screen);

    BasicWindow window = new BasicWindow("Grid layout test");


    // ----------------- left side ---------------
    Panel leftP = new Panel();

    // four checkboxes in their own panel
    Panel checkBoxP = new Panel();
    for(int i = 0; i < 4; i++) {
      CheckBox checkBox = new CheckBox("Checkbox #" + (i+1));
      checkBoxP.addComponent(checkBox);
    }

    // two textfields in their own panel
    Panel textBoxP = new Panel();
    textBoxP.addComponent(
      Panels.horizontal( new Label("Normal:   "),
                         new TextBox(new TerminalSize(12, 1),
                         "Text")));

    textBoxP.addComponent(
      Panels.horizontal( new Label("Password: "),
                         new TextBox(new TerminalSize(12, 1),
                         "Text").setMask('*')));

    // a button in its own panel that affects the
    // spacing of the left panel
    Panel buttonP = new Panel();
    buttonP.addComponent(new Button("Enable spacing", 
       () -> { LinearLayout lm = 
                  (LinearLayout) leftP.getLayoutManager();
               lm.setSpacing( (lm.getSpacing() == 0) ? 1 : 0);
             }
    ));

    leftP.addComponent(
      checkBoxP.withBorder(Borders.singleLine("CheckBoxes")));

    leftP.addComponent(
      textBoxP.withBorder(Borders.singleLine("TextBoxes")));

    leftP.addComponent(
      buttonP.withBorder(Borders.singleLine("Buttons")));


    // ----------------- right side ---------------

    Panel rightP = new Panel();

    // a read-only text area in its own panel
    textBoxP = new Panel();
    TextBox roTA = new TextBox(new TerminalSize(16, 8));
    roTA.setReadOnly(true);
    roTA.setText(HAMLET);
    textBoxP.addComponent(roTA);

    rightP.addComponent(
      textBoxP.withBorder(Borders.singleLine("Read-only")));

    // a progress bar
    ProgressBar prog = new ProgressBar(0, 100, 16);
    prog.setRenderer(new ProgressBar.LargeProgressBarRenderer());
    prog.setLabelFormat("%2.0f%%");

    rightP.addComponent(
      prog.withBorder(Borders.singleLine("ProgressBar")));

    rightP.setLayoutData( LinearLayout.createLayoutData(
                             LinearLayout.Alignment.Fill));

    // set up a timer to drive the progress bar
    Timer timer = new Timer("ProgressBar-timer", true);
    timer.schedule(new TimerTask() {
      public void run() 
      {
        if (prog.getValue() == prog.getMax())
          prog.setValue(0);
        else
          prog.setValue(prog.getValue() + 1);
      }
    }, 50, 50);    // delay, period
				

    // ------------ combine the two sides ---------------

    Panel p = new Panel();
    p.setLayoutManager( new LinearLayout(Direction.VERTICAL));

    p.addComponent( Panels.horizontal(leftP, rightP));

    p.addComponent(
      new Separator(Direction.HORIZONTAL).setLayoutData(
        LinearLayout.createLayoutData(LinearLayout.Alignment.Fill)));

    Button okButton = new Button("OK", 
      () -> { window.close();
              timer.cancel();
            }).
      setLayoutData( LinearLayout.createLayoutData(
                       LinearLayout.Alignment.Center));

    p.addComponent(okButton);

    // link panel, window, and gui
    window.setComponent(p);
    gui.addWindowAndWait(window);

    Utils.close(screen);
  }  // end of main()


} // end of Comps2 class
