/*
 * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
 * Published By SunSoft Press/Prentice-Hall
 * Copyright (C) 1996 Sun Microsystems Inc.
 * All Rights Reserved. ISBN 0-13-565755-5
 *
 * Permission to use, copy, modify, and distribute this 
 * software and its documentation for NON-COMMERCIAL purposes
 * and without fee is hereby granted provided that this 
 * copyright notice appears in all copies. 
 * 
 * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR 
 * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
 * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
/**
 * @version 1.01 07 May 1996 
 * @author Cay Horstmann
 */

import java.awt.*;
import corejava.*;

public class FontDialog extends Frame
{  private void add(Component c, GridBagLayout gbl,
      GridBagConstraints gbc,
      int x, int y, int w, int h)
   {  gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;
      gbl.setConstraints(c, gbc);
      add(c);
   }
   public FontDialog()
   {  setTitle("FontDialog");
      GridBagLayout gbl = new GridBagLayout();
      setLayout(gbl);
      
      style = new List(4, false);
      style.addItem("Times Roman");
      style.addItem("Helvetica");
      style.addItem("Courier");
      style.addItem("Zapf Dingbats");
      style.addItem("Dialog");
      style.addItem("DialogInput");
      style.select(0);
                  
      bold = new Checkbox("Bold");
      italic = new Checkbox("Italic");
      Label label = new Label("Size: ");
      size = new IntTextField(10,1,100,4);
      sample = new TextField();

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.weightx = 20;
      gbc.weighty = 100;
      add(style, gbl, gbc, 0, 0, 1, 3);
      gbc.weightx = 100;
      gbc.fill = GridBagConstraints.NONE;
      gbc.anchor = GridBagConstraints.CENTER;
      add(bold, gbl, gbc, 1, 0, 2, 1);
      add(italic, gbl, gbc, 1, 1, 2, 1);
      add(label, gbl, gbc, 1, 2, 1, 1);
      gbc.fill = GridBagConstraints.HORIZONTAL;
      add(size, gbl, gbc, 2, 2, 1, 1);
      gbc.anchor = GridBagConstraints.SOUTH;
      gbc.weighty = 0;
      add(sample, gbl, gbc, 0, 3, 4, 1);
      sample.setText("The quick brown fox");
   }
   
   public boolean handleEvent(Event evt)
   {  if ((evt.target.equals(bold) || evt.target.equals(italic) ||
         evt.target.equals(style) || evt.target.equals(size))
            && size.isValid())
      {  int m = (bold.getState() ? Font.BOLD : 0)
         + (italic.getState() ?
            Font.ITALIC : 0);
         sample.setFont(new Font(style.getSelectedItem(), m, size.getValue()));
         sample.show();
      }
      else if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
      return super.handleEvent(evt);
   }
 
   public static void main(String[] args)
   {  Frame f = new FontDialog();
      f.resize(250, 150);
      f.show();  
   }
   
   private List style;
   private Checkbox bold;
   private Checkbox italic;
   private IntTextField size;
   private TextField sample;
}


