--- /dev/null 2014-07-31 16:13:19.122792434 +0400 +++ new/test/java/awt/reliability/GUIMerlinFocus.java 2014-07-31 17:27:48.114615322 +0400 @@ -0,0 +1,811 @@ +/* + * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.*; +import java.awt.event.*; + +/* + * @author Aruna Samji + */ + +public class GUIMerlinFocus extends Frame { + //GUI for KeyboardFocusManagerEvent + Button button; + List list; + + volatile boolean buttonFocusGained, listFocusGained, listFocusLost, + buttonFocusLost, currentkeyboardFocusManager, buttonClicked, + KeyPressed; + + String buttonClickedStr; + + CustomDefaultKeyBoardFocusManager1 cdk; + + //GUI for WindowFocusEvents + Frame frame1; + Dialog dialog1; + + Frame frame2; + Dialog dialog2; + + Frame frame3; + TextField textfield; + Button button1; + TextArea textarea; + Panel rootPanel; + + volatile boolean frame1Activated, frame2Deactivated, + dialog1Activated, dialog2Deactivated, + button1FocusGained, textareaFocusGained, textfieldFocusLost; + + //GUI for OppositeComponentTest + Frame frame4; + Button button2; + List list2; + TextField textfield2; + + Component focusLostComp; + Component focusGainedComp; + + //GUI for FocusTraversalPolicyTest + Frame testFrame; + Button button5; + List list5; + Panel rootPanel1; + Button button6; + Choice choice5; + TextField textfield5; + Checkbox checkbox5; + Component focusLost5; + Component focusGained5; + + Frame testFrame1; + Button button7; + List list6; + Panel rootPanel2; + + FocusTraversalPolicy cp; + + Frame frame5; + Panel firstpanel; + Panel secondpanel; + Button firstbutton; + Button secondbutton; + Choice firstchoice; + Choice secondchoice; + TextField firsttextfield; + TextField secondtextfield; + + Component complost; + Component compgain; + + DefaultKeyboardFocusManager manager; + + //GUI for ProgrammaticTraversalTest + Frame testFrame2; + Button button8; + List list8; + Panel rootPanel8; + Button button9; + Choice choice8; + TextField textfield8; + Checkbox checkbox8; + Component focusLost2; + Component focusGained2; + + KeyboardFocusManager manager2; + + //GUI for RequestFocusOwnerTest + Frame testframe3; + TextField textfield14; + Button button14; + TextArea textarea14; + Checkbox checkbox14; + + //GUI for GlobalFocusOwnerTest + Frame testframe4; + TextField textfield15; + + volatile boolean tempfocus, + keypressed15, keyreleased15, + button1flag, focusgain; + + DefaultKeyboardFocusManager de; + + public void setBooleans() { + + buttonFocusGained = false; + listFocusGained = false; + listFocusLost = false; + buttonFocusLost = false; + currentkeyboardFocusManager = false; + buttonClicked = false; + KeyPressed = false; + + frame1Activated = false; + frame2Deactivated = false; + dialog1Activated = false; + dialog2Deactivated = false; + + button1FocusGained = false; + textareaFocusGained = false; + textfieldFocusLost = false; + + keypressed15 = false; + keyreleased15 = false; + button1flag = false; + focusgain = false ; + } + + public void framesSetLocationAndSize() { + frame1.setSize(500, 100); + frame1.setLocation(100, 100); + frame2.setSize(500, 100); + frame2.setLocation(100, 200); + } + + public void dialogsSetLocationAndSize() { + dialog1.setSize(500, 100); + dialog1.setLocation(100, 300); + dialog2.setSize(500, 100); + dialog2.setLocation(100, 400); + } + + public GUIMerlinFocus() { + setBooleans(); + this.setSize(500, 270); + this.setLayout(new FlowLayout()); + this.setBackground(Color.blue); + cdk = new CustomDefaultKeyBoardFocusManager1(this); + button = new Button("Button"); + + list = new java.awt.List(); + list.add("one"); + list.add("two"); + + button.addFocusListener(new FocusListener() { + public void focusGained(FocusEvent fe) { buttonFocusGained = true; } + public void focusLost(FocusEvent fe) { buttonFocusLost = true; } + }); + + list.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent ke) { KeyPressed = true; } + }); + + list.addFocusListener(new FocusListener() { + public void focusGained(FocusEvent fe) { listFocusGained = true; } + public void focusLost(FocusEvent fe) { listFocusLost = true; } + }); + + button.addActionListener(e -> { + if (e.getSource() == button) { + buttonClickedStr = "AWTEventDispatcher " + + "dispatches the event by itself:" + + "After ButtonClick"; + buttonClicked = true; + } + }); + + this.add(button); + this.add(list); + //End of GUI for KeyboardFocusManagerEvent + + //GUI for WindowFocusEvents + frame1 = new Frame("Frame 1"); + dialog1 = new Dialog(frame1, "Dialog 1"); + + frame2 = new Frame("Frame 2"); + dialog2 = new Dialog(frame2, "Dialog 2"); + + frame1.setBackground(Color.red); + frame1.addWindowListener(new WindowAdapter() { + public void windowActivated(WindowEvent we) { + if (we.getSource() == frame1) frame1Activated = true; + } + }); + + frame2.setBackground(Color.blue); + frame2.addWindowListener(new WindowAdapter() { + public void windowDeactivated(WindowEvent we) { + if (we.getSource() == frame2) frame2Deactivated = true; + } + }); + + dialog1.setBackground(Color.green); + dialog1.addWindowListener(new WindowAdapter() { + public void windowActivated(WindowEvent we) { + if (we.getSource() == dialog1) dialog1Activated = true; + } + }); + + dialog2.setBackground(Color.yellow); + dialog2.addWindowListener(new WindowAdapter() { + public void windowDeactivated(WindowEvent we) { + if (we.getSource() == dialog2) dialog2Deactivated = true; + } + }); + + frame3 = new Frame("WindowFocusEvents"); + frame3.setLayout(new FlowLayout()); + frame3.setBackground(Color.red); + + rootPanel = new Panel(); + rootPanel.setBackground(Color.yellow); + + textfield = new TextField("TextField"); + textfield.addFocusListener(new FocusAdapter() { + public void focusLost(FocusEvent fe) { + textfieldFocusLost = true; + } + }); + + button1 = new Button("Button"); + button1.setEnabled(false); + button1.addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent fe) { + button1FocusGained = true; + } + }); + + textarea = new TextArea("TextArea", 3, 10); + textarea.addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent fe) { + textareaFocusGained = true; + } + }); + + rootPanel.add(textfield, 0); + rootPanel.add(button1, 1); + rootPanel.add(textarea, 2); + rootPanel.setBounds(20, 20, 350, 200); + + frame3.setBounds(0, 0, 500, 270); + frame3.add(rootPanel); + //End of GUI for WindowFocusEvents + + //GUI for OppositeComponentTest + //Component + frame4 = new Frame("OppositeComponentTest"); + button2 = new Button("Button"); + button2.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusGainedComp = fe.getOppositeComponent(); } + public void focusGained(FocusEvent fe) { focusLostComp = fe.getOppositeComponent(); } + }); + + textfield2 = new TextField("TextField"); + textfield2.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusGainedComp = fe.getOppositeComponent(); } + public void focusGained(FocusEvent fe) { focusLostComp = fe.getOppositeComponent(); } + }); + + list2 = new java.awt.List(2); + list2.add("Item 1"); + list2.add("Item 2"); + list2.add("Item 3"); + list2.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusGainedComp = fe.getOppositeComponent(); } + public void focusGained(FocusEvent fe) { focusLostComp = fe.getOppositeComponent(); } + }); + + frame4.setLayout(new FlowLayout()); + frame4.add(button2); + frame4.add(textfield2); + frame4.add(list2); + frame4.setBackground(Color.green); + frame4.setSize(500, 255); + //End of GUI for OppositeComponentTest + + //GUI for FocusTraversalPolicyTest + testFrame = new Frame("FocusTraversalPolicy"); + testFrame.setLayout(new FlowLayout()); + testFrame.setBackground(Color.blue); + + testFrame1 = new Frame("FocusTraversalPolicy"); + testFrame1.setLayout(new FlowLayout()); + testFrame1.setBackground(Color.blue); + testFrame1.setSize(500, 270); + + rootPanel1 = new Panel(); + rootPanel1.setBackground(Color.yellow); + + rootPanel2 = new Panel(); + rootPanel2.setBackground(Color.yellow); + + button5 = new Button("Button1"); + button5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + button7 = new Button("Button3"); + button7.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + list5 = new List(2); + list5.add("Item 1"); + list5.add("Item 2"); + list5.add("Item 3"); + list5.add("Item 4"); + list5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + list6 = new List(2); + list6.add("Item 1"); + list6.add("Item 2"); + list6.add("Item 3"); + list6.add("Item 4"); + list6.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + button6 = new Button("Button2"); + button6.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + choice5 = new Choice(); + choice5.add("item 1"); + choice5.add("item 2"); + choice5.add("item 3"); + choice5.add("item 4"); + choice5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + textfield5 = new TextField("TextField"); + textfield5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + checkbox5 = new Checkbox("CheckBox"); + checkbox5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + focusLost5 = fe.getComponent(); + focusLost5 = fe.getComponent(); + } + + public void focusGained(FocusEvent fe) { + focusGained5 = fe.getComponent(); + focusGained5 = fe.getComponent(); + } + }); + + testFrame.add(button5); + testFrame.add(list5); + testFrame.add(button6); + testFrame.add(choice5); + testFrame.add(checkbox5); + testFrame.add(textfield5, 4); + + rootPanel1.add(button7); + rootPanel1.add(list6); + + cp = new CustomPolicy(button5, list5, button6, choice5, textfield5, checkbox5); + + testFrame.setFocusTraversalPolicy(cp); + + testFrame1.add(rootPanel1); + + testFrame.setSize(500, 270); + //Forward and backward traversal + manager = new DefaultKeyboardFocusManager(); + frame5 = new Frame("FocusTraversalPolicy"); + frame5.setBackground(Color.yellow); + + firstpanel = new Panel(); + firstpanel.setBackground(Color.red); + + secondpanel = new Panel(); + secondpanel.setBackground(Color.green); + + firstbutton = new Button("First"); + firstbutton.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + secondbutton = new Button("Second"); + secondbutton.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + firstchoice = new Choice(); + firstchoice.add("First"); + firstchoice.add("First"); + firstchoice.add("First"); + firstchoice.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + secondchoice = new Choice(); + secondchoice.add("Second"); + secondchoice.add("Second"); + secondchoice.add("Second"); + secondchoice.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + firsttextfield = new TextField("First"); + firsttextfield.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + secondtextfield = new TextField("Second"); + secondtextfield.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + firstpanel.add(firstbutton); + firstpanel.add(firstchoice); + firstpanel.add(firsttextfield); + firstpanel.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + secondpanel.add(secondbutton); + secondpanel.add(secondchoice); + secondpanel.add(secondtextfield); + secondpanel.setFocusCycleRoot(true); + secondpanel.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + + frame5.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + complost = (Component) fe.getSource(); + } + + public void focusGained(FocusEvent fe) { + compgain = null; + compgain = (Component) fe.getSource(); + } + }); + frame5.add(firstpanel, BorderLayout.NORTH); + frame5.add(secondpanel, BorderLayout.SOUTH); + + //End of GUI for FocusTraversalPolicyTest + + //GUI for ProgrammaticTraversalTest + testFrame2 = new Frame("ProgrammaticTraversal"); + testFrame2.setLayout(new FlowLayout()); + + rootPanel8 = new Panel(); + rootPanel8.setLayout(new FlowLayout()); + rootPanel8.setBackground(Color.yellow); + + button8 = new Button("Button1"); + button8.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + list8 = new List(2); + list8.add("Item 1"); + list8.add("Item 2"); + list8.add("Item 3"); + list8.add("Item 4"); + list8.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + button9 = new Button("Button2"); + button9.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + choice8 = new Choice(); + choice8.add("Item 1"); + choice8.add("Item 2"); + choice8.add("Item 3"); + choice8.add("Item 4"); + choice8.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + textfield8 = new TextField("Text Field"); + textfield8.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + checkbox8 = new Checkbox("Check Box"); + checkbox8.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { focusLost2 = fe.getComponent(); } + public void focusGained(FocusEvent fe) { focusGained2 = fe.getComponent(); } + }); + + rootPanel8.add(button8); + rootPanel8.add(list8); + rootPanel8.add(button9); + rootPanel8.add(choice8); + rootPanel8.add(textfield8, 4); + rootPanel8.add(checkbox8); + + testFrame2.add(rootPanel8); + testFrame2.setBackground(Color.red); + testFrame2.pack(); + testFrame2.setSize(500, 270); + + manager2 = KeyboardFocusManager.getCurrentKeyboardFocusManager(); + + //End of GUI for ProgrammaticTraversalTest + + //GUI for RequestFocusOwnerTest + testframe3 = new Frame("RequestFocusOwner"); + testframe3.setLayout(new FlowLayout()); + testframe3.setBackground(Color.yellow); + + textfield14 = new TextField("TextField"); + textarea14 = new TextArea("TextArea", 3, 10); + checkbox14 = new Checkbox("Click Me"); + button14 = new Button("Press me"); + + testframe3.add(button14); + testframe3.add(checkbox14); + testframe3.add(textarea14); + testframe3.add(textfield14); + testframe3.setSize(500, 270); + + //End of GUI for RequestFocusOwnerTest + + //GUI for GlobalFocusOwnerTest + de = new DefaultKeyboardFocusManager(); + testframe4 = new Frame("testFrame"); + testframe4.setLayout(new FlowLayout()); + testframe4.setBackground(Color.green); + + textfield15 = new TextField("TextField"); + textfield15.addFocusListener(new FocusListener() { + public void focusLost(FocusEvent fe) { + tempfocus = fe.isTemporary(); + } + + public void focusGained(FocusEvent fe) { + if (fe.getSource().equals(textfield15)) + focusgain = true; + } + }); + + textfield15.addKeyListener(new KeyListener() { + public void keyPressed(KeyEvent ke) { keypressed15 = true; } + public void keyReleased(KeyEvent ke) { keyreleased15 = true; } + public void keyTyped(KeyEvent ke) { } + }); + + testframe4.add(textfield15); + testframe4.setSize(500, 270); + } + + class CustomPolicy extends FocusTraversalPolicy { + Button button5; + List list5; + Button button6; + Choice choice5; + TextField textfield5; + Checkbox checkbox5; + + public CustomPolicy(Button button5, List list5, Button button6, + Choice choice5, TextField textfield5, Checkbox checkbox5) { + this.button5 = button5; + this.list5 = list5; + this.button6 = button6; + this.choice5 = choice5; + this.textfield5 = textfield5; + this.checkbox5 = checkbox5; + } + + public Component getComponentAfter(Container fr,Component comp) { + if (comp == button5) + return choice5; + else if (comp == list5) + return textfield5; + else if (comp == button6) + return button5; + else if (comp == choice5) + return list5; + else + return button6; + } + + public Component getComponentBefore(Container fr,Component comp) { + if (comp == button5) + return button6; + else if (comp == list5) + return choice5; + else if (comp == button6) + return textfield5; + else if (comp == choice5) + return button5; + else + return list5; + } + + public Component getDefaultComponent(Container fr) { + return choice5; + } + + public Component getFirstComponent(Container fr) { + return choice5; + } + + public Component getLastComponent(Container fr) { + return checkbox5; + } + } + + class CustomDefaultKeyBoardFocusManager1 extends DefaultKeyboardFocusManager { + + public Component comp; + boolean cdkdispatchKeyEvent = false; + boolean cdkprocessKeyEvent = false; + + public CustomDefaultKeyBoardFocusManager1(Component ce) { + this.comp = ce; + } + + public boolean dispatchKeyEvent(KeyEvent e) { + super.dispatchKeyEvent(e); + cdkdispatchKeyEvent=true; + return false; + } + + public boolean dispatchEvent(java.awt.AWTEvent ae) { + super.dispatchEvent(ae); + return false; + } + + public void processKeyEvent(Component comp,KeyEvent ke) { + cdkprocessKeyEvent = true; + super.processKeyEvent(comp,ke); + } + + protected Container getGlobalCurrentFocusCycleRoot() { + return super.getGlobalCurrentFocusCycleRoot(); + } + + protected Component getGlobalFocusOwner() throws SecurityException { + return super.getGlobalFocusOwner(); + } + + protected Window getGlobalFocusedWindow() throws SecurityException { + return super.getGlobalFocusedWindow(); + } + } +} + +