/*
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. getBoundProperties(
 *
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sun.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
  /* This test case brings up a TextArea in a scrollpane. For a particular width
   * and height(22, 51) setting of the ScrollPane, the application flickers viz. it toggles
   * between requiring to have a scrollbar and not requiring to have a scrollbar.
   *
   * this has been caused because, the width set for the TextArea does not exactly match
   * with size required for displaying some letters. calculateBreakPosition method returns
   * break position equal to the start position. It causes that number of lines necessary for
   * text displaying are calculated uncorrectly.
   */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class TextFieldFlicker {

                public TextFieldFlicker(){

        System.out.println("annotate TEST test_name="+"bug4410243");
                System.out.println("annotate TEST type="+"Manual");
                System.out.println("annotate TEST Proj="+"Mantis");


        final JFrame frame = new JFrame("Test");
        final JFrame frame2 = new JFrame("Instructions");
        JTextArea text = new JTextArea();
        text.setWrapStyleWord(true);
        text.setColumns(4);
        text.setText("this is a test. this is a test. test is test");
        text.setLineWrap(true);

        Container parent = frame.getContentPane();
        parent.setLayout(null);
        final JScrollPane sp = new JScrollPane(text);

         String info = " \nClick on the Text Field and enter 22 51, the user chosen \n"+
                                                "width and height of the Text Area. If the Text Area flickers\n"+
                                                "toggling display of the Scrollbar on the TextArea, Press Fail Button\n"+
                                                "else if there is no flicker Press the Pass button\n";


                                                JPanel panel=new JPanel(new BorderLayout());
                                                JButton button1 = new JButton("Pass");
                                                JButton button2 = new JButton("Fail");
                                                button1.setActionCommand("Pass");
                                                button2.setActionCommand("Fail");
                                                ButtonListener lst = new ButtonListener();
                                                button1.addActionListener(lst);
                                                button2.addActionListener(lst);
                                                JPanel btnpanel1 = new JPanel();
                                                btnpanel1.add(button1);
                                                btnpanel1.add(button2);
                                                panel.add(addInfo(info),BorderLayout.CENTER);
                                                panel.add(btnpanel1, BorderLayout.SOUTH);



        parent.add(sp);
        sp.setBounds(50, 50, 100, 100);
                frame2.getContentPane().add(panel);


        JTextField tf = new JTextField("100 100");
        parent.add(tf);
        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                StringTokenizer tok = new StringTokenizer(((JTextField)ae.
                                                getSource()).getText());

                try {
                    int width = Integer.parseInt(tok.nextToken());
                    int height = Integer.parseInt(tok.nextToken());

                    sp.setSize(width, height);
                    sp.revalidate();
                    sp.getParent().repaint();
                } catch (NumberFormatException nfe) {
                    System.out.println("NFE: " + nfe);
                }
            }
        });
        tf.setBounds(0, 0, 100, 25);
        frame.setBounds(0, 0, 400, 400);
        frame.show();
                frame2.setBounds(500,300,400,200);
                frame2.show();
    }

 public static void main(String[] args)
{

new TextFieldFlicker();

}

public JScrollPane addInfo(String info) {
   JTextArea jta = new JTextArea(info,8,20);
   jta.setEditable(false);
   jta.setLineWrap(true);
   JScrollPane sp = new JScrollPane(jta);
   return sp;

}

}

class ButtonListener implements ActionListener {
       public void actionPerformed(ActionEvent ae) {
            JButton button = (JButton)ae.getSource();
            if(button.getActionCommand() == "Pass") {

               System.out.println("PRN_LOG TESTCASE desc="+" Text Area does not Flicker");
                   System.out.println("PRN_LOG TESTCASE status="+ true);
               System.out.println("User Evaluated Test as Passed");
                System.exit(0);
             } else {
                           System.out.println("PRN_LOG TESTCASE desc="+" Text Area does Flicker");
                   System.out.println("PRN_LOG TESTCASE status="+ false);
               System.out.println("User Evaluated Test as Failed");
               System.exit(1);
             }

      }
   }  // End of ButtonListener class
