/*
 *  =================================================================
 *  DemoToolBar.java : Build tool bar with three icons and scrollable
 *  text area window. 
 * 
 *  Written by : Mark Austin                              March, 2001
 *  =================================================================
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class DemoToolBar extends JApplet {

    URL  codeBase;   // used for browser version only....
    URL   leftURL;   // URL for left Icon ...
    URL middleURL;   // URL for middle Icon ...
    URL  rightURL;   // URL for right Icon ...

    protected JTextArea textArea;
    protected String newline = "\n";

    public void init() {

        // 1. Get the handle on the applets' content pane.

        Container container = this.getContentPane();

        // 2. Construct URLs and load images. ....

        codeBase = getCodeBase();
        try {
           leftURL = new URL( codeBase, "left.gif");
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        Icon leftIcon = new ImageIcon( leftURL );

        try {
           middleURL = new URL( codeBase, "middle.gif");
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        Icon middleIcon = new ImageIcon( middleURL );

        try {
           rightURL = new URL( codeBase, "right.gif");
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        Icon rightIcon = new ImageIcon( rightURL );

        // 3. Create a toolbar object.

        JToolBar toolBar = new JToolBar();

        // 4. Set the margin between toolbar border and its comps.

        toolBar.setMargin(new Insets(5,5,5,5));

        // 5. Create some toolbar buttons with tooltip text...

        JButton button1 = new JButton( leftIcon   );
        JButton button2 = new JButton( middleIcon );
        JButton button3 = new JButton( rightIcon  );

        // 6. Add tooltips and action listeners to buttons 

        button1.setToolTipText("This is the left button");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                displayResult("Action for the left button");
            }
        });

        button2.setToolTipText("This is the middle button");
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                displayResult("Action for the middle button");
            }
        });

        button3.setToolTipText("This is the right button");
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                displayResult("Action for the right button");
            }
        });

        // 7. Add the buttons to the toolbar with separators.

        toolBar.add(button1);
        toolBar.add(button2);
        toolBar.addSeparator();
        toolBar.add(button3);

        // 8. Create the text area used for output.

        textArea = new JTextArea(40, 80);
        JScrollPane scrollPane = new JScrollPane(textArea);

        // 9. Add toolbar and textarea components to the applet.

        container.add(   toolBar, BorderLayout.NORTH  );
        container.add(scrollPane, BorderLayout.CENTER );
    }

    //  ======================================
    //  Write action event result to text area 
    //  ======================================

    protected void displayResult(String actionDescription) {
        textArea.append( actionDescription + newline);
    }

}
