
// MenuTest.java
// Martin Berglund, 2020
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A GUI window showing three menus: "File",
   "Country", "Help". Below the menubar is:
      * a separator line
      * a user-defined component called
        MultiColorComponent
      * a separator line
      * a GUI close button

  The "File" menu contains:
      * "Open" leading to a File Dialog
      * "Exit"
    -- user focus starts on this menu

  The "Country" menu contains:
      * "Germany" submenu which holds a list of names
      * "Japan" submenu which holds a list of names

  The "Help" menu contains:
      * "Homepage"
      * "About"

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   Usage (Windows):
     > compile.bat MenuTest.java
     > run.bat MenuTest

   Usage (Linux):
     > ./compile.sh MenuTest.java
     > ./run.sh MenuTest

*/

import java.io.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.gui2.dialogs.*;
import com.googlecode.lanterna.gui2.menu.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;


public class MenuTest
{

  private static final Runnable DO_NOTHING = () -> { };


  private static final String[] GERMAN_STATES = new String[] {
    "Baden-Wurttemberg", "Bayern", "Berlin",
    "Brandenburg", "Bremen", "Hamburg",
    "Hessen", "Mecklenburg-Vorpommern",
    "Niedersachsen", "Nordrhein-Westfalen",
    "Rheinland-Pfalz", "Saarland",
    "Sachsen", "Sachsen-Anhalt",
    "Schleswig-Holstein", "Thuringen"
  };

  private static final String[] JAPANESE_PRFTS = new String[] {
    "Aichi", "Akita", "Aomori", "Chiba",
    "Ehime", "Fukui", "Fukuoka", "Fukushima",
    "Gifu", "Gunma", "Hiroshima", "Hokkaido",
    "Hyogo", "Ibaraki", "Ishikawa", "Iwate",
    "Kagawa", "Kagoshima", "Kanagawa",
    "Kochi", "Kumamoto", "Kyoto", "Mie",
    "Miyagi", "Miyazaki", "Nagano", "Nagasaki",
    "Nara", "Niigata", "Oita", "Okayama",
    "Okinawa", "Osaka", "Saga", "Saitama",
    "Shiga", "Shimane", "Shizuoka", "Tochigi",
    "Tokushima", "Tokyo", "Tottori",
    "Toyama", "Wakayama", "Yamagata",
    "Yamaguchi", "Yamanashi"
  };


  public static void main(String[] args) 
  {
    // build screen, gui, window
    Screen screen = Utils.createScreen();
    MultiWindowTextGUI gui = 
         new MultiWindowTextGUI(screen);
    BasicWindow window = new BasicWindow();

    // build menu bar
    MenuBar menubar = new MenuBar();
    window.setMenuBar(menubar);

    // "File" menu ------------------------

    Menu menuFile = new Menu("File");
    menubar.add(menuFile);

    // open menu item
    menuFile.add(new MenuItem("Open...", () -> {
      File file = new FileDialogBuilder().build().showDialog(gui);
      if (file != null)
        MessageDialog.showMessageDialog(
          gui, "Open", "Selected file:\n" + file, 
          MessageDialogButton.OK);
    }));

    // exit menu item
    menuFile.add(new MenuItem("Exit", window::close));


    // Country menu ------------------

    Menu countryMenu = new Menu("Country");
    menubar.add(countryMenu);

    // submenu listing German states
    Menu gSubMenu = new Menu("Germany");
    countryMenu.add(gSubMenu);
    for (String state: GERMAN_STATES)
      gSubMenu.add(new MenuItem(state, DO_NOTHING));

    // submenu listing Japanese prefectures
    Menu jSubMenu = new Menu("Japan");
    countryMenu.add(jSubMenu);
    for (String prefecture: JAPANESE_PRFTS)
      jSubMenu.add(new MenuItem(prefecture, DO_NOTHING));


    // "Help" menu -------------------------
    Menu menuHelp = new Menu("Help");
    menubar.add(menuHelp);

    // homepage menu item
    menuHelp.add(new MenuItem("Homepage", () -> 
         MessageDialog.showMessageDialog(gui,
               "Homepage", "https://github.com/mabe02/lanterna",
               MessageDialogButton.OK)));

    // about menu item
    menuHelp.add(new MenuItem("About", () -> 
         MessageDialog.showMessageDialog(gui,
              "About", "Lanterna drop-down menu",
              MessageDialogButton.OK)));


    // ---------- build panel ---------------

    // separator, color component, separator, close box
    Panel p = new Panel(new BorderLayout());
    p.addComponent( Panels.vertical(
        new Separator(Direction.HORIZONTAL).setLayoutData(
          LinearLayout.createLayoutData(LinearLayout.Alignment.Fill)),
        new MultiColorComponent(),
        new Separator(Direction.HORIZONTAL).setLayoutData(
          LinearLayout.createLayoutData(LinearLayout.Alignment.Fill)),
        new Button("Close", window::close)) );


    // link panel, window, and gui
    window.setComponent(p);
    window.setFocusedInteractable(menuFile);
          // start with focus on the "File" menu
    gui.addWindowAndWait(window);

    Utils.close(screen);
  }  // end of main()


  // -----------------------------------------------------------

  private static class MultiColorComponent 
              extends AbstractComponent<MultiColorComponent>
  /* Two columns: the left column is a list of
     color names, the right column shows the colors
     as backgrounds to "TEXT" labels.
  */
  {
    protected ComponentRenderer<MultiColorComponent> createDefaultRenderer()
    {
      return new ComponentRenderer<MultiColorComponent>() {
        public TerminalSize getPreferredSize(MultiColorComponent comp)
        {  return new TerminalSize(40, 
               TextColor.ANSI.values().length+1);  }

        public void drawComponent(TextGUIGraphics tgg,
                                  MultiColorComponent comp)
        /* two columns, consisting of color names and
           labels using those colors as backgrounds */
        {
          tgg.applyThemeStyle(getTheme().getDefaultDefinition().getNormal());
          tgg.fill(' ');  // a blank row
          int row = 1;

          for (TextColor color: TextColor.ANSI.values()) {
            tgg.applyThemeStyle(getTheme().getDefaultDefinition().getNormal());
            tgg.putString(1, row, color.toString() + ": ");
            tgg.setForegroundColor(TextColor.ANSI.BLACK);
            tgg.setBackgroundColor(color);
            tgg.putString(20, row++, "     TEXT     ");
          }
        } /* drawComponent */

      };
    } /* createDefaultRenderer */

  }  // end of MultiColorComponent class


}  // end of MenuTest class
