import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Label;
import java.awt.List;
import java.awt.TextField;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.StringTokenizer;

import static de.frame4j.util.ComVar.*;
import de.frame4j.util.Prop;
import de.frame4j.graf.GrafHelper;

/** <b>Greek names building kit as Applet</b>.
 *  <br />
 *  A peculiarity of greek names is their almost free combinability from a
 *  limited set of pre and postfixes. This Applet helps you to manufacture
 *  (almost) all those names. Some of them are quite famous and are partly
 *  elucidated by the Applet in English or German language.<br />
 *  Other languages (pre and postfixes and hints to names) may be added by
 *  adding or modifying the Applet's .properties files.<br />
 *  The .properties files are best packed into the Applet's .jar (java 
 *  archive) after being converted to UTF8. See the build script
 *  <a href="./GreekNameApplet.bat">GreekNameApplet.bat</a> and the demo page
 *  <a href="./greek_demo.html">greek_demo.html</a>.br />
 *  <br />
 *  By that way GreekNameApplet demonstrates Frame4J's support also for
 *  Applets by the framework's class 
 *  {@link de.frame4j.util.Prop de.frame4j.util.Prop}. That's its serious
 *  purpose.
 *  <br />
 *  <br />
 *  <h3>German description</h3>
 *  
 *  Basteln Griechischer Namen als Applet. <br />
 *  <br />
 *  Griechische Namen haben die Eigenschaft, aus einigen fast beliebig 
 *  kombinierbaren Pre- und Postfixen zusammengesetzt zu sein. Dieses 
 *  Applet hilft beim Basteln solcher Namen. Und es hält zu einigen 
 *  bekannten Namen eine kurze Erklärung bereit.<br />
 *  <br /> 
 *  Die Liste der Namensteile und die Erklärungen, sowie der Fenstertitel
 *  und ein paar andere Eigenschaften werden mit {@link de.frame4j.util.Prop} 
 *  durch .properties-Dateien eingestellt. Damit ist das Applet ohne 
 *  Neuübersetzung erweiterbar und auch für andere Zwecke als das 
 *  Basteln griechischer Namen einsetzbar.<br />
 *  <br /> 
 *  Die .properties-Dateien können sinnvollerweise dem Java-Archiv
 *  des Applets (.jar-File) zugefügt werden. Sie steuern auch die
 *  automatische Auswertung von &lt;param&gt-tags (siehe
 *  {@link de.frame4j.util.Prop}). Dieses Applet erwartet eine Grund-
 *  Properties-Datei GreekName.properties 
 *  (nicht GreekNameApplet.properties).<br />
 *  Gesteuert durch die Eigenschaften language und region werden 
 *  nach der Auswertung der param-Tags (wie z.B.
 *  &lt;param name=&quot;language&quot; value=&quot;en&quot /&gt;) 
 *  weitere sprach- und regionsspezifische properties-Dateien wie
 *  GreekName_de.properties, GreekName_de_DE.properties oder 
 *  (im Beispiel) GreekName_en.properties geladen.<br />
 *  <br /> 
 *  Copyright 2000-2001, 2003, 2010  &nbsp;  Albrecht Weinert<br />
 *  <br /> 
 *  @author   Albrecht Weinert
 *  @version  5 (22.01.2010)
 *  @see de.a_weinert.graf
 *  @see de.a_weinert.WinApp
 *  @see de.frame4j.util.Prop
 *  @see de.frame4j.util.PropMap
 */
 // Bisher :  V00.00 (06.11.2000 15:10) : new
 //           V00.01 (13.11.2000 17:10) : Prop changes
 //           V00.02 (13.12.2000 09:50) : handle Browser errors
 //           V00.03 (21.08.2001 11:05) : uses Prop >= V00.09 (21.08.01)
 //           V02.00 (23.04.2003 16:52) : CVS Eclipse
 //           V.025+ (20.01.2010 14:36) : SVN, Frame4J, /**
 //           V.001+ (22.01.2010 07:50) : SVN provider crashed, port to new 

public class GreekNameApplet extends Applet  implements ItemListener {  

   public static final String AUTOR     = "Autor:   Albrecht Weinert"; 
   public static final String VERSION   =  
                       " V.5 (20.01.2010, A. Weinert)";
   public static final String COPYRIGHT = 
                     "Copyright (c) 2000-2003, 2010 Albrecht Weinert";

   @Override public Dimension getPreferredSize() {
      return new Dimension(500,152);
   }

   @Override public Dimension getMinimumSize() {
      return new Dimension(420,120);
   }

//----------  ItemListener ---------- (ChexkBox) ----

   @Override public void itemStateChanged(ItemEvent e) {
     String name = preList.getSelectedItem() + postList.getSelectedItem();
     String decl = prop.getString(name);
     if (decl == null)
        ergLabel.setText("     " + name + "     ");
     else {
        ergLabel.setText(name + ", " + decl);
        doLayout(); 
     }
   }

   public void componentResized(ComponentEvent e){
      fontSet(); 
      doLayout(); 
   }

   protected void fontSet() {
     int oldFontSize = actFontSize;
     actFontSize = getSize().width / (360/12); // factor xSize/fontSize
     if (actFontSize < 10) 
        actFontSize = 10;
     else if (actFontSize > 48)
        actFontSize = 48;
      if (oldFontSize != actFontSize) { 
         actFont = new Font ("SansSerif"  , Font.PLAIN , actFontSize); 
         bigFont = new Font ("SansSerif"  , Font.BOLD , actFontSize+2);
      }
      preList.setFont(actFont);
      postList.setFont(actFont);
      ergLabel.setFont(actFont);
      plusLabel.setFont(bigFont);
      istgLabel.setFont(bigFont);
   } //  fontSet()

   int actFontSize = 12;
   protected int oldFontSize = -1;
   Font  actFont; 
   Font  bigFont; 
   List  preList   = new List(); 
   List  postList  = new List();
   TextField ergLabel = new TextField(
        "GreekNameApplet.java  Copyright  Albrecht Weinert");
  //  Label ergLabel  = new Label(
     //    "GreekNameApplet.java  Copyright  Albrecht Weinert",
       //                                       Label.CENTER);
   Label plusLabel = new Label("+", Label.CENTER);
   Label istgLabel = new Label("=", Label.CENTER);

   Prop prop;  

/** Initialising the Applet. */
   @Override public void init() {
      System.out.println ("\nGriechische Namen" 
            + "\n" + AUTOR + "\n" + VERSION);
      fontSet();
      try {
         prop  = new Prop(this, null, "GreekName.properties");
         if (UL_UR_da) 
            System.out.println ("\nSprache: " 
               +  prop.get("language") + " Region: "  
               +  prop.get("region"));
      } catch (Exception e) {
          System.out.println( "\nStartfehler\n" + e);
          ergLabel.setText(e.getMessage()); 
      }

      if (prop != null) {
         ergLabel.setText(prop.getString("startText", 
         "        Wähle  Namensteile   /   Choose  name  parts         "));
         plusLabel.setText(prop.getString("plusLabelText", "+"));
         istgLabel.setText(prop.getString("istglLabelText", "="));


         StringTokenizer stt = new StringTokenizer(
                 prop.getString("namePrefixes", "Diog Aristh"));
         while (stt.hasMoreTokens()) {
            preList.add(stt.nextToken());
         }      
         stt = new StringTokenizer (
                 prop.getString("namePostfixes", "enes oteles"));
         while (stt.hasMoreTokens()) {
            postList.add(stt.nextToken());
         }
         stt = null;
         ergLabel.setBackground(new Color(
                   prop.getInt("labelBgColor", 0xFFFFFF)));
         Color bgColor = new Color(prop.getInt("grafBgColor", 0x0000FF));
         setBackground(bgColor);
         add(preList);
         Color fgColor = GrafHelper.calcFgColor(bgColor);
         plusLabel.setForeground(fgColor);
         istgLabel.setForeground(fgColor);
         add(plusLabel);
         add(postList);
      } else {
         setBackground(Color.yellow);
         ergLabel.setFont(new Font ("SansSerif", Font.PLAIN, actFontSize-5));
         plusLabel       = new Label("Browser Error: use appletviewer");
         add(plusLabel);
         istgLabel= new Label("Browser regards codebase file as local");
      }
      add(istgLabel);
      add(ergLabel);
      preList.addItemListener(this);
      postList.addItemListener(this);
      if (prop != null) {
         postList.select(prop.getInt("postfixSel", 0));
         preList.select(prop.getInt("prefixSel", 0));
      }
   } // init
   
   @Override public void start() {
      System.out.println ("\nGriechische Namen " + VERSION + " start.");
   }
   
   @Override public void stop() {
      System.out.println ("\nGriechische Namen " + VERSION + " stop.");
   }
   
   @Override public void destroy() {
      System.out.println ("\nGriechische Namen " + VERSION + " destroy.");
   }

}  // class GreekNameApplet (21.08.01 , 25.01.2005, 20.01.2010)


