/*
 * @(#)FontSampler.java  2.5.0  31 August 2006
 *
 * Copyright 2006
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import java.awt.*;
import java.awt.font.*;

/**
 * <p>Class <code>FontSampler</code> creates a GUI to sample
 * the fonts installed on the system.  The characters shown
 * are those with codes from 32 to 126.</p>
 * 
 * @author Richard Rasala
 * @version 2.5.0
 */
public class FontSampler
    extends DisplayPanel
{
    /** The minimum font size. */
    protected int minimum = 8;
    
    /** The table gap. */
    protected int gap = 12;
    
    /** The width delta. */
    protected int deltaw = 40;
    
    /** The height delta. */
    protected int deltah = 60;
    
    /** The BufferedPanel width. */
    protected int width  = 17 * deltaw;
    
    /** The BufferedPanel height. */
    protected int height =  7 * deltah;
    
    /** The BufferedPanel */
    protected BufferedPanel window =
        new BufferedPanel(width, height);
    
    /** The graphics environment. */
    protected GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    
    /** The font family list as an array. */
    protected String[] familyList =
        ge.getAvailableFontFamilyNames();
    
    /** The show font action. */
    protected SimpleAction showFont =
        new SimpleAction("Show Font") {
            public void perform() {
                showFont();
            }
        };
    
    /** The font family dropdown list. */
    protected Dropdown fontDropdown =
        new Dropdown(familyList);
        
    /** The font size text field view. */
    protected TextFieldView fontSizeTFV =
        new TextFieldView("24", 60);
    
    /** The bold check box. */
    protected BooleanView boldBox =
        new BooleanView("Bold", showFont, false);
    
    /** The italic check box. */
    protected BooleanView italicBox =
        new BooleanView("Italic", showFont, false);
    
    /** The font family stuff. */
    protected Object[] fontStuff =
        { "Font Name", fontDropdown };
    
    /** The font family panel. */
    protected HTable fontPanel =
        new HTable(fontStuff, gap, gap, CENTER);
    
    /** The font params stuff. */
    protected Object[] paramsStuff =
        { "Font Size", fontSizeTFV, boldBox, italicBox };
    
    /** The font params table */
    protected HTable paramsTable =
        new HTable(paramsStuff, gap, gap, CENTER);
    
    /** The main stuff. */
    protected Object[] mainStuff =
        { window, fontPanel, paramsTable };
    
    /** The main table. */
    protected VTable mainPanel =
        new VTable(mainStuff, gap, gap, CENTER);
    
    
    /** The constructor. */
    public FontSampler() {
        fontDropdown.setMaximumRowCount(16);
        
        fontDropdown.addActionListener(showFont);
        fontSizeTFV. addActionListener(showFont);
        
        window.lineBorder(Color.black, 2);
        window.emptyBorder(4);
        
        add(mainPanel);
        
        showFont();
    }
    
    
    /**
     * <p>This methods displays the characters with codes
     * from 32 to 126 in the graphics window using the
     * current font.</p> 
     */
    protected void showFont() {
        window.clearPanel();
        
        Graphics2D graphics = window.getBufferGraphics();
        
        Font font = getCurrentFont();
        
        TextPaintable paintable =
            new TextPaintable
                (null, font, null, null, TextAnchor.CENTER_BASELINE, 0, 0);
        
        for (int row = 2; row < 8; row++) {
            int y = deltah * (row - 1);
            int next = 16 * row;
            
            for (int col = 0; col <= 15; col++) {
                int x = deltaw * (col + 1);
                int code = next + col;
                
                if (code == 127)
                    break;
                
                String s = "" + (char) (code);
                
                paintable.setString(s);
                paintable.setAnchorPosition(x, y);
                paintable.paint(graphics);
            }
        }
        
        // repaint window
        window.repaint();
    }
    
    
    /**
     * <p>Returns the current font based on the user
     * selections in the GUI of this panel.</p>
     */
    public Font getCurrentFont() {
        String name = fontDropdown.getViewState();
        
        int size = fontSizeTFV.demandInt();
        
        if (size < minimum) {
            size = minimum;
            fontSizeTFV.setViewState("" + size);
        }
        
        int style =
            (boldBox.  getBooleanValue() ? Font.BOLD   : 0) |
            (italicBox.getBooleanValue() ? Font.ITALIC : 0);
            
        return new Font(name, style, size);
    }
    
    
    /**
     * <p>The main program that launches a font sampler
     * in its own GUI frame.</p>
     * 
     * <p>Use the call:</p>
     * 
     * <pre>  FontSampler.main(null)</pre>
     * 
     * @param args ignored and may be <code>null</code>
     */
    public static void main(String[] args) {
        new FontSampler().frame("Font Sampler", NORTH);
    }
    
}
