/*
 * Created on Sep 16, 2005 by Nick
 *
 */
package jmsltestsuite;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * @author Nick Didkovsky, (c) 2005 Nick Didkovsky, nick@didkovsky.com
 *  
 */
public class JScrollPaneTest implements ActionListener {
    JMenuItem switchBig;
    JMenuItem switchSmall;

    private JFrame frame;
    private JPanel smallCanvas;
    private JPanel bigCanvas;
    private JScrollPane scrollpane;

    private int smallCanvasWidth = 600;
    private int smallCanvasHeight = 600;
    private int bigCanvasWidth = 900;
    private int bigCanvasHeight = 900;
    private int frameWidth = 580;
    private int frameHeight = 580;

    public JScrollPaneTest() {
        buildWindow();
        buildScrollPane();
        buildCanvases();
        buildMenu();
        frame.getContentPane().add(scrollpane);
    }

    private void buildMenu() {
        JMenu menu = new JMenu("Switcheroo");
        menu.add(switchBig = new JMenuItem("Use big JPanel"));
        menu.add(switchSmall = new JMenuItem("Use small JPanel"));
        switchBig.addActionListener(this);
        switchSmall.addActionListener(this);
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
    }

    private void buildWindow() {
        String title = "View Factory building components in stright up Swing";
        frame = new JFrame();
        frame.setTitle(title);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private void buildScrollPane() {
        scrollpane = new JScrollPane();
    }

    private void buildCanvases() {
        smallCanvas = new JPanel() {
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.green);
                g.drawLine(0, 0, smallCanvasWidth, smallCanvasHeight);
                g.drawLine(smallCanvasWidth, smallCanvasHeight, smallCanvasWidth - 1, smallCanvasHeight - 10);
                g.drawLine(smallCanvasWidth, smallCanvasHeight, smallCanvasWidth - 10, smallCanvasHeight - 1);

            }
        };
        smallCanvas.setPreferredSize(new Dimension(smallCanvasWidth, smallCanvasHeight));

        bigCanvas = new JPanel() {
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.red);
                g.drawLine(0, 0, bigCanvasWidth, bigCanvasHeight);
                g.drawLine(bigCanvasWidth, bigCanvasHeight, bigCanvasWidth - 1, bigCanvasHeight - 10);
                g.drawLine(bigCanvasWidth, bigCanvasHeight, bigCanvasWidth - 10, bigCanvasHeight - 1);

            }
        };
        bigCanvas.setPreferredSize(new Dimension(bigCanvasWidth, bigCanvasHeight));

        scrollpane.setViewportView(smallCanvas);
    }

    public void show() {
        frame.setSize(new Dimension(frameWidth, frameHeight));
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        JScrollPaneTest test = new JScrollPaneTest();
        test.show();
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == switchSmall) {
            System.out.println("Switching to small content JPanel");
            scrollpane.setViewportView(smallCanvas);
        }
        if (source == switchBig) {
            System.out.println("Switching to big content JPanel");
            scrollpane.setViewportView(bigCanvas);
        }
    }
}