/*
A basic extension of the com.sun.java.swing.JApplet class
*/

import com.sun.java.swing.*;
import com.sun.java.swing.text.*;
import java.awt.*;
import java.io.*;

import com.wildcrest.JTextPrinter;
import com.sun.java.swing.JButton;
import com.sun.java.swing.JTextPane;
import com.sun.java.swing.JScrollPane;
public class JTextPrinterSimpleTest extends JApplet
{
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		// symantec.itools.lang.Context.setApplet(this);

		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		getContentPane().setLayout(null);
		setSize(253,377);
		jButton1 = new com.sun.java.swing.JButton();
		jButton1.setText("Print");
		jButton1.setActionCommand("Print");
		jButton1.setBounds(60,300,132,36);
		jButton1.setFont(new Font("Dialog", Font.BOLD, 12));
		jButton1.setForeground(java.awt.Color.black);
		jButton1.setBackground(new java.awt.Color(204,204,204));
		getContentPane().add(jButton1);
		jScrollPane1 = new com.sun.java.swing.JScrollPane();
		jScrollPane1.setBounds(12,12,221,276);
		jScrollPane1.getViewport().add(jTextPane1);
		jTextPane1.setBackground(java.awt.Color.white);
		jTextPane1.setBounds(0,0,218,273);
		jScrollPane1.setFont(new Font("Dialog", Font.PLAIN, 12));
		jScrollPane1.setForeground(java.awt.Color.black);
		jScrollPane1.setBackground(new java.awt.Color(204,204,204));
		getContentPane().add(jScrollPane1);
		jTextPrinter1 = new com.wildcrest.JTextPrinter();
		jTextPrinter1.setLayout(null);
		jTextPrinter1.setBounds(4000,4000,32,40);
		getContentPane().add(jTextPrinter1);
		//}}
	
		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		jButton1.addActionListener(lSymAction);
		//}}
		
        // jTextPane1.requestFocus();  // do this if you don't want to take 2 clicks to get first focus
                        
        SimpleAttributeSet sP12 = makeAttribute("Serif","Plain",12, StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet sB12 = makeAttribute("Serif","Bold",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet sI12 = makeAttribute("SanSerif","Italic",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet sBI12 = makeAttribute("SanSerif","BoldItalic",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet ssP12 = makeAttribute("SanSerif","Plain",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet ssB12 = makeAttribute("SanSerif","Bold",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet ssI12 = makeAttribute("SanSerif","Italic",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet ssBI12 = makeAttribute("SanSerif","BoldItalic",12,StyleConstants.ALIGN_LEFT,Color.black);
        SimpleAttributeSet sP18 = makeAttribute("Serif","Plain",18,StyleConstants.ALIGN_LEFT,Color.black);
                
        // jTextPrinter1.setNumberOfColumns(2); // do this if you want to test multiple column mode
        
        int numberOfParagraphs = 4;
        
        for (int i=0; i<numberOfParagraphs; i++) {
            appendStyledText(jTextPane1, "Paragraph " + i + "\n", sP18);
            appendStyledText(jTextPane1, "Here is a bunch of text.  Now is the time for all good men to come to the "
                + "aid of their party.  Four score and seven years ago, our fathers brought forth upon this continent "
                + "a new nation, conceived in libery and dedicated to proposition that all men are created equal.  ", sP12);
            appendStyledText(jTextPane1, "Now we will insert an icon, that is, an embedded graphics image, and see what "
                + " happens, here it is...", sP12);
	        jTextPane1.setCaretPosition(jTextPane1.getStyledDocument().getLength());       
            jTextPane1.insertIcon(new ImageIcon("JTextPrinterC32.gif"));
            appendStyledText(jTextPane1, "...how was that? ", sP12);
            appendStyledText(jTextPane1, "Now will try some bold text. ", sB12);
            appendStyledText(jTextPane1, "Or perhaps some italic text. ", sI12);
            appendStyledText(jTextPane1, "Or even some bold and italic text. \n\n", sBI12);
                
            appendStyledText(jTextPane1, "Here is some more text that will test out JTextPrinter. ", ssP12);
            appendStyledText(jTextPane1, "But you can do more than enter text in the code. ", ssB12);
            appendStyledText(jTextPane1, "You can also type in the JTextPane and edit this text and add your own. ", ssI12);
            appendStyledText(jTextPane1, "Of course, this simple program doesn't let you control styles. ", ssBI12);
            appendStyledText(jTextPane1, "But it does have the virtue of being a small amount of code.\n\n", ssB12);
        }
        appendStyledText(jTextPane1, "This is the last line, and it ends with a # with no newline:#", ssBI12);
		
	}
	
	private void appendStyledText(JTextPane pane, String string, SimpleAttributeSet attribute) {
	    pane.setCaretPosition(pane.getStyledDocument().getLength());
	    pane.setCharacterAttributes(attribute,true);
	    pane.setParagraphAttributes(attribute,true);
	    pane.replaceSelection(string);
	    return;
	}
   
	private SimpleAttributeSet makeAttribute(String fontName, String style, int size, int align, Color color) {
        SimpleAttributeSet attribute = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attribute, fontName);
        
        StyleConstants.setBold(attribute, false);
        StyleConstants.setItalic(attribute, false);
        StyleConstants.setUnderline(attribute, false);
        if (style.toLowerCase().indexOf("bold")>=0) StyleConstants.setBold(attribute, true);
        if (style.toLowerCase().indexOf("italic")>=0) StyleConstants.setItalic(attribute, true);
        if (style.toLowerCase().indexOf("underline")>=0) StyleConstants.setUnderline(attribute, true);

        StyleConstants.setFontSize(attribute, size);
        StyleConstants.setAlignment(attribute, align);
        StyleConstants.setForeground(attribute, color);
        return attribute;
    }


	//{{DECLARE_CONTROLS
	com.sun.java.swing.JButton jButton1 = new com.sun.java.swing.JButton();
	com.sun.java.swing.JScrollPane jScrollPane1 = new com.sun.java.swing.JScrollPane();
	com.sun.java.swing.JTextPane jTextPane1 = new com.sun.java.swing.JTextPane();
	com.wildcrest.JTextPrinter jTextPrinter1 = new com.wildcrest.JTextPrinter();
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == jButton1)
				jButton1_actionPerformed(event);
		}
	}

	void jButton1_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// print... getText
		{
			jTextPrinter1.print(jTextPane1);
		}
		//}}
	}
	
// main method so can run applet as an application
	static public void main(String args[]) {
		class DriverFrame extends JFrame {
			public DriverFrame() {
				addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent event) {
						dispose();	    // free the system resources
						System.exit(0); // close the application
					}
				});
				this.setTitle("JTextPrinterTest");
				this.getContentPane().setLayout(null);
				this.setSize(250,385);

                JTextPrinterSimpleTest applet = new JTextPrinterSimpleTest();
                applet.init();
				this.getContentPane().add(applet);
			}
		}
		new DriverFrame().show();
	}

}

