import java.awt.FlowLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;


public class bug7049024 {

    public static Clipboard clipboard = null;
    public static JTextField textField = null;
    public static JButton button = null;
    public static JFrame frame = null;
    public static DefaultCaret caret= null;
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        frame = new JFrame("Test");
        textField = new JTextField("test selection for textfield");
        button = new JButton("To compete the focus");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String oldselection = null, newselection = null;
                clipboard = textField.getToolkit().getSystemSelection();
                if (null == clipboard){
                    System.exit(0);
                }
                try {
                    textField.requestFocusInWindow();
                    caret = (DefaultCaret) textField.getCaret();
                    caret.setDot(2);
                    caret.moveDot(4);
                    oldselection = (String) clipboard.getData(DataFlavor.stringFlavor);
                    System.out.println("oldSelection is " + oldselection);
                    
                    button.requestFocusInWindow();
                    caret.setDot(4);
                    caret.moveDot(6);
                    newselection = (String) clipboard.getData(DataFlavor.stringFlavor);
                    System.out.println("newSelection is " + newselection);
                    
                    boolean passed = newselection.equals(oldselection);
                    System.out.println("They should be same, testcase " + (passed ? " passed " : "failed") );
                    if (!passed){
                        throw new RuntimeException("The test for bug 7049024 failed");
                    }
                    System.exit(0);
                } catch (UnsupportedFlavorException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        });
        frame.setLayout(new FlowLayout());
        frame.getContentPane().add(textField);
        frame.getContentPane().add(button);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);        

        button.doClick();
    }

}
