/*
 * Fail FondiParawt.java
 * @author Jaanus Poial
 * @version 0.9
 */

//======================================================
// Fondivaliku dialoog - AWT
//======================================================

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;

public class FondiParawt extends Hashtable <String, Object> {

   //===================================================
   // FondiParawt on paisktabel (vt. java.util.*)
   //  Loomulikult vo~iks see olla ka midagi muud
   //===================================================

   String naiteTekst = "ABCDabcd,.1234";
   Boolean valmis = new Boolean (false); // su"nkro
   Frame raam;

   FondiParawt () { // konstruktor

      //================================================
      // Paisktabeli algva"a"rtustamine
      //================================================

      put ("name", "Dialog");
      put ("style", new Integer (Font.PLAIN));
      put ("size", new Integer (14));

      //================================================
      // Va"lise raami loomine ja juhtimine
      //================================================

      raam = new Frame();
      raam.setSize (350, 300);
      raam.setTitle ("Vali font");
      raam.addWindowListener (new WindowAdapter() {
         public void windowClosing (WindowEvent e) {
            synchronized (valmis) {
               valmis.notifyAll();
            }
         }
      });

      //================================================
      // Tegeliku liidese ehitamine on eraldi meetodina
      //================================================

      looDialoog (raam);
      raam.setVisible (true);
      try {
         synchronized (valmis) {
            valmis.wait();
         }
      } catch (InterruptedException e) {}

      raam.setVisible (false);

   } // konstruktori lopp

   //===================================================
   // Kasutame Graphics-objekti saamiseks u"lekatmist
   //===================================================

   class FondiNaide extends Panel {

      public void paint (Graphics g) {

         String s = naiteTekst;        // naide
         g.setFont (votaFont());       // font

         //=============================================
         // Arvutame teksti asukoha fondimeetrika alusel
         //=============================================

         FontMetrics fm = g.getFontMetrics();
         int tekstikorgus = fm.getHeight();
         int tekstilaius = fm.stringWidth (s);
         int kastikorgus = getSize().height;
         int kastilaius = getSize().width;
         if ((kastilaius  < tekstilaius) ||
             (kastikorgus < tekstikorgus)) {
            // throw new RuntimeException ("Ei mahu!");
         }
         int x = (kastilaius - tekstilaius) / 2;
         int y = (kastikorgus - tekstikorgus) / 2 +
            fm.getAscent(); // alusjoone saamiseks

         g.setColor (Color.white);
         g.fillRect (0, 0, kastilaius, kastikorgus);
         g.setColor (Color.black);
         g.drawString (s, x, y);

      } // paint lopp
      
      private static final long serialVersionUID = -8564370944529496313L;

   } // FondiNaide lopp

   //===================================================
   // Dialoogi loomine etteantud konteinerisse
   //===================================================

   void looDialoog (Container kest) {

      //================================================
      // Aknaelemendid
      //================================================

      FondiNaide fondiNaide = new FondiNaide();
      Label nimeSilt = new Label ("Nimi");

      // String [] fondid = Toolkit.getDefaultToolkit().getFontList(); // JDK 1.1
      String [] fondid = GraphicsEnvironment.
         getLocalGraphicsEnvironment().
         getAvailableFontFamilyNames(); // JDK 1.2

      List nimed = new List();
      for (int i = 0; i < fondid.length; i++)
         nimed.add (fondid[i]);

      Label tekstiSilt = new Label ("Tekst");
      TextField tekstiVali =
         new TextField (naiteTekst, 12);
      Label suuruseSilt = new Label ("Suurus");
      String [] psuurused = { "6", "8", "10", "12",
         "14","16","18","20","24","30","36","40" };
      Choice suurused = new Choice();
      for (int i = 0; i < psuurused.length; i++)
         suurused.addItem (psuurused [i]);
      Label stiiliSilt = new Label ("Stiil");
      Checkbox rasvane = new Checkbox ("Paks kiri");
      Checkbox kaldkiri = new Checkbox ("Kaldkiri");
      Button okNupp = new Button ("Valmis");

      //================================================
      // Ka"itumine
      //================================================

      nimed.addItemListener
         (new ItemListener() {
         public void itemStateChanged (ItemEvent e) {
            String s = (String)((List)e.getSource()).
               getSelectedItem();
            put ("name", s);
            FondiNaide n = (FondiNaide)
               ((Container)((List)e.getSource()).
               getParent()).getComponent (0);
            n.repaint();
         }
      } );

      tekstiVali.addActionListener
         (new ActionListener() {
         public void actionPerformed (ActionEvent e) {
            naiteTekst = ((TextField)e.getSource()).
               getText();
            FondiNaide n = (FondiNaide)
               ((Container)((TextField)e.getSource()).
               getParent()).getComponent (0);
            n.repaint();
         }
      } );

      suurused.addItemListener (new ItemListener() {
         public void itemStateChanged (ItemEvent e) {
            int suurus = Integer.parseInt ((String)
              ((Choice)e.getSource()).
              getSelectedItem());
            put ("size", new Integer (suurus));
            FondiNaide n = (FondiNaide)
               ((Container)((Choice)e.getSource()).
               getParent()).getComponent (0);
            n.repaint();
         }
      } );

      rasvane.addItemListener (new ItemListener() {
         public void itemStateChanged (ItemEvent e) {
            int stiil =
               ((Integer)(get ("style"))).intValue();
            boolean olek = ((Checkbox)e.getSource()).
               getState();
            if (olek)
               stiil = stiil + Font.BOLD;
               else stiil = stiil - Font.BOLD;
            put ("style", new Integer (stiil));
            FondiNaide n = (FondiNaide)
               ((Container)((Checkbox)e.getSource()).
               getParent()).getComponent (0);
            n.repaint();
         }
      } );

      kaldkiri.addItemListener (new ItemListener() {
         public void itemStateChanged (ItemEvent e) {
            int stiil =
               ((Integer)(get ("style"))).intValue();
            boolean olek = ((Checkbox)e.getSource()).
               getState();
            if (olek)
               stiil = stiil + Font.ITALIC;
               else stiil = stiil - Font.ITALIC;
            put ("style", new Integer (stiil));
            FondiNaide n = (FondiNaide)
               ((Container)((Checkbox)e.getSource()).
               getParent()).getComponent (0);
            n.repaint();
         }
      } );

      okNupp.addActionListener (new ActionListener() {
         public void actionPerformed (ActionEvent e) {
            synchronized (valmis) {
               valmis.notifyAll();
            }
         }
      } );

      //================================================
      // Va"ljana"gemine
      //================================================

      GridBagConstraints kitsendused =
         new GridBagConstraints();
      GridBagLayout makett = new GridBagLayout();
      kest.setLayout (makett);

      paneKitsendus (0, 0, 2, 4, kitsendused);
      kitsendused.weightx = 1.0;
      kitsendused.weighty = 1.0;
      kitsendused.fill = GridBagConstraints.BOTH;
      makett.setConstraints (fondiNaide, kitsendused);
      kest.add (fondiNaide);

      paneKitsendus (0, 4, 1, 1, kitsendused);
      kitsendused.weightx = 0.0;
      kitsendused.weighty = 0.0;
      makett.setConstraints (nimeSilt, kitsendused);
      kest.add (nimeSilt);

      paneKitsendus (0, 5, 1, 7, kitsendused);
      kitsendused.weightx = 1.0;
      kitsendused.weighty = 1.0;
      makett.setConstraints (nimed, kitsendused);
      kest.add (nimed);

      paneKitsendus (1, 4, 1, 1, kitsendused);
      kitsendused.weightx = 0.0;
      kitsendused.weighty = 0.0;
      makett.setConstraints (tekstiSilt, kitsendused);
      kest.add (tekstiSilt);

      paneKitsendus (1, 5, 1, 1, kitsendused);
      kitsendused.weightx = 1.0;
      kitsendused.weighty = 0.0;
      makett.setConstraints (tekstiVali, kitsendused);
      kest.add (tekstiVali);

      paneKitsendus (1, 6, 1, 1, kitsendused);
      kitsendused.weightx = 0.0;
      kitsendused.weighty = 0.0;
      makett.setConstraints (suuruseSilt, kitsendused);
      kest.add (suuruseSilt);

      paneKitsendus (1, 7, 1, 1, kitsendused);
      makett.setConstraints (suurused, kitsendused);
      kest.add (suurused);

      paneKitsendus (1, 8, 1, 1, kitsendused);
      makett.setConstraints (stiiliSilt, kitsendused);
      kest.add (stiiliSilt);

      paneKitsendus (1, 9, 1, 1, kitsendused);
      makett.setConstraints (rasvane, kitsendused);
      kest.add (rasvane);

      paneKitsendus (1, 10, 1, 1, kitsendused);
      makett.setConstraints (kaldkiri, kitsendused);
      kest.add (kaldkiri);

      paneKitsendus (1, 11, 1, 1, kitsendused);
      makett.setConstraints (okNupp, kitsendused);
      kest.add (okNupp);

      for (int i = 0; i < nimed.getItemCount(); i++)
         if (nimed.getItem (i).equals (get ("name"))) {
            nimed.select (i);
            nimed.makeVisible (i);
            break;
         }

      suurused.select (get ("size").toString());

   } // looDialoog lopp

   void paneKitsendus (int x, int y, int w, int h,
                       GridBagConstraints k) {
      k.gridx = x;
      k.gridy = y;
      k.gridwidth = w;
      k.gridheight = h;
   }

   //===================================================
   // Po~hiprogramm -- silumiskest
   //===================================================

   public static void main (String[] parameetrid) {
      FondiParawt fp = new FondiParawt();
      Font f = fp.votaFont();
      System.out.println ("Valiti kiri: " + f);
      fp.raam.dispose();
   } // main lopp

   //===================================================
   // Fondi loomine parameetritest
   //===================================================

   public Font votaFont() {
      return new Font ((String)get ("name"), 
         ((Integer)(get ("style"))).intValue(), 
         ((Integer)(get ("size"))).intValue());
   } // votaFont lopp

   private static final long serialVersionUID = -8564370944529496313L;
   
} // FondiParawt lopp
