import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoScrollPane2 - a program that puts an image in a scroll pane
* and allows the user to change the image using a combo box.<p>
*
* Screen snap...<br>
* <center><img src="DemoScrollPane2-1.gif"></center><p>
*
* @see <a href="DemoScrollPane2.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoScrollPane2
{
   public static void main(String[] args)
   {
      // use Win32 look and feel
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoScrollPane2Frame frame = new DemoScrollPane2Frame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoScrollPane2");
      frame.pack(); // determine required size of window
      frame.show();
   }
}

class DemoScrollPane2Frame extends JFrame implements ActionListener 
{
   JScrollPane scrollPane;
   JComboBox contentCombo;
   JRadioButton hAsNeeded;
   JRadioButton hAlways;
   JRadioButton hNever;
   JRadioButton vAsNeeded;
   JRadioButton vAlways;
   JRadioButton vNever; 

   final JComponent[] CONTENT_ARRAY = {
      new JLabel(new ImageIcon("map.gif")),
      new JLabel(new ImageIcon("hockeyplayer.gif")),
      new JLabel(new ImageIcon("logo.jpg")),
      new JTextArea("Hello Java World"),
      new BigGreeting(),
      new MissionStatement()
   };

   final String[] CHOICES = { "Map", "Hockey player", "York logo",
      "Small greeting",  "Big greeting", "Mission statement" };

   // constructor

   public DemoScrollPane2Frame()
   {
      // -------------------------------
      // create and configure components
      // -------------------------------

      contentCombo = new JComboBox();
      for (int i = 0; i < CHOICES.length; ++i)
         contentCombo.addItem(CHOICES[i]);  
      contentCombo.setSelectedIndex(0);

      hAsNeeded = new JRadioButton("As Needed");
      hAlways = new JRadioButton("Always");
      hNever = new JRadioButton("Never");
      hAsNeeded.setSelected(true);
      ButtonGroup hGroup = new ButtonGroup();
      hGroup.add(hAsNeeded);
      hGroup.add(hAlways);
      hGroup.add(hNever);

      vAsNeeded = new JRadioButton("As Needed");
      vAlways = new JRadioButton("Always");
      vNever = new JRadioButton("Never");
      vAsNeeded.setSelected(true);
      ButtonGroup vGroup = new ButtonGroup();
      vGroup.add(vAsNeeded);
      vGroup.add(vAlways);
      vGroup.add(vNever);

      scrollPane = new JScrollPane(CONTENT_ARRAY[0]);
      scrollPane.setPreferredSize(new Dimension(200, 200));
      scrollPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      scrollPane.setBackground(new Color(222, 222, 222));
      scrollPane.getViewport().setBackground(new Color(234, 234, 255));

      // Note: The following allows the scroll pane to get focus
      scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JButton());

      // -------------
      // add listeners
      // -------------

      contentCombo.addActionListener(this);
      hAsNeeded.addActionListener(this);
      hAlways.addActionListener(this);
      hNever.addActionListener(this);
      vAsNeeded.addActionListener(this);
      vAlways.addActionListener(this);
      vNever.addActionListener(this);

      JLabel message = new JLabel("Note: Press TAB to change focus");
      message.setForeground(new Color(140, 0, 0));
      message.setHorizontalAlignment(SwingConstants.CENTER);

      // ------------------
      // arrange components
      // ------------------

      JPanel p1 = new JPanel();
      p1.add(contentCombo);
      p1.setBorder(new TitledBorder(new EtchedBorder(),
         "Content chooser"));

      JPanel p2 = new JPanel();
      p2.setLayout(new GridLayout(0, 1));
      p2.add(hAsNeeded);
      p2.add(hAlways);
      p2.add(hNever);
      p2.setBorder(new TitledBorder(new EtchedBorder(),
         "Horizontal"));

      JPanel p3 = new JPanel();
      p3.setLayout(new GridLayout(0, 1));
      p3.add(vAsNeeded);
      p3.add(vAlways);
      p3.add(vNever);
      p3.setBorder(new TitledBorder(new EtchedBorder(),
         "Vertical"));

      JPanel p4 = new JPanel();
      p4.setLayout(new GridLayout(0, 1));
      p4.add(p2);
      p4.add(p3);
      p4.setBorder(new TitledBorder(new EtchedBorder(),
         "Scrollbar policy"));   

      JPanel centerPanel = new JPanel();
      centerPanel.setLayout(new BorderLayout());
      centerPanel.add(scrollPane, "Center");
      centerPanel.setBorder(new TitledBorder(new EtchedBorder(),
         "Scroll pane"));

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(centerPanel, "Center");
      contentPane.add(p1, "North");
      contentPane.add(p4, "East");
      contentPane.add(message, "South");
      this.setContentPane(contentPane);
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      Object source = ae.getSource();

      if (source == contentCombo)
      {
         int idx = contentCombo.getSelectedIndex();
         scrollPane.setViewportView(CONTENT_ARRAY[idx]);
      }
      else if (source == hAsNeeded)
         scrollPane.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      else if (source == hAlways)
         scrollPane.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      else if (source == hNever)
         scrollPane.setHorizontalScrollBarPolicy(
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      else if (source == vAsNeeded)
         scrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      else if (source == vAlways)
         scrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      else if (source == vNever)
         scrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_NEVER);

      scrollPane.revalidate(); 
      scrollPane.repaint();    // repaint as soon as possible
   }

   class BigGreeting extends JTextField
   {
      final int SIZE = 100; // 100 pt font size

      BigGreeting()
      {
         super("Hello Java World");
         this.setMargin(new Insets(5, 5, 5, 5));
         this.setFont(new Font("serif", Font.PLAIN, SIZE));
         this.setForeground(new Color(0, 0, 200));
         this.setBackground(new Color(255, 230, 230));
         this.setEditable(false);
      }
   }

   class MissionStatement extends JTextArea
   {
      MissionStatement()
      {
         super("Mission Statement\n\n" +            
            "The mission of York University is the pursuit, preservation, and\n" +
            "dissemination of knowledge. We promise excellence in research\n" +
            "and teaching in pure, applied and professional fields. We test the\n" +
            "boundaries and structures of knowledge. We cultivate the critical\n" +
            "intellect.\n\n" +            
            "York University is part of Toronto: we are dynamic, metropolitan\n" +
            "and multi-cultural. York University is part of Canada: we encourage\n" +
            "bilingual study, we value tolerance and diversity. York University is\n" +
            "open to the world: we explore global concerns.\n\n" +            
            "A community of faculty, students and staff committed to academic\n" +
            "freedom, social justice, accessible education, and collegial\n" +
            "self-governance, York University makes innovation its tradition.\n\n" +            
            "Tentanda Via: the way must be tried."
         );
         this.setMargin(new Insets(5, 5, 5, 5));
         this.setFont(new Font("sansserif", Font.PLAIN, 18));
         this.setForeground(new Color(160, 0, 0));
         this.setBackground(new Color(255, 255, 204));
         this.setEditable(false);
      }
   }
}

