Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTabbedPane/4666224/bug4666224.java
38855 views
/*1* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;25import javax.swing.*;26/*27* test28* @bug 466622429*/3031public class bug4666224 extends JApplet {32final static int placements[] = { JTabbedPane.LEFT, JTabbedPane.RIGHT, JTabbedPane.TOP, JTabbedPane.BOTTOM };33private JTabbedPane tabPane;34private JPanel mainPanel;3536public bug4666224() throws Exception {37java.awt.EventQueue.invokeAndWait( () -> {38tabPane = new JTabbedPane();39tabPane.addMouseListener(new MouseAdapter() {40public void mouseClicked(MouseEvent e) {41Point pt = e.getPoint();42System.out.println("Index at location: "43+ tabPane.indexAtLocation(pt.x, pt.y));44}45});46InputMap inputMap = createInputMap();47SwingUtilities.replaceUIInputMap(getRootPane(), JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);48ActionMap actionMap = createActionMap();49SwingUtilities.replaceUIActionMap(getRootPane(), actionMap);5051//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);52tabPane.setTabPlacement(JTabbedPane.TOP);53tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);54JPanel panel = new JPanel();55panel.setPreferredSize(new Dimension(200, 300));56tabPane.addTab("Number Zero", panel);57panel = new JPanel();58//panel.requestFocus();59panel.setPreferredSize(new Dimension(200, 300));60tabPane.addTab("Number One", panel);61panel = new JPanel();62panel.setPreferredSize(new Dimension(200, 300));63tabPane.addTab("Number Two", panel);64panel = new JPanel();65panel.setPreferredSize(new Dimension(200, 300));66tabPane.addTab("Number Three", new JColorChooser());67panel = new JPanel();68panel.setPreferredSize(new Dimension(200, 300));69tabPane.addTab("Number Four", panel);70mainPanel = new JPanel();71mainPanel.add(tabPane);7273getContentPane().add(mainPanel);74tabPane.requestFocus();757677//pack();78//setVisible(true);79});80}8182public void init() {83String[][] instructionsSet =84{85{86" Note : Incase of Assertion failure,user can enter",87" remarks by pressing 'Assertion Fail Remarks ' button",88" ",89" You would see an applet with JTabbedPane. Keep the size of applet variable.",90" ",91" ON ALL PLATFORMS",92"1. Click on any of the tabs, focus indicator is visible",93"2. Lose focus on the window by clicking on some other window ",94"3. Focus indicator should disappear",95"4. Regain focus on the window the focus indicator should reappear." ,96" If focus doesn't behave as above, ",97" press 'Assertion Fail' else press 'Assertion Pass'",98},99100{101" Note : Incase of Assertion failure,user can enter",102" remarks by pressing 'Assertion Fail Remarks ' button",103" ",104" You would see an applet with JTabbedPane. Keep the size of applet variable.",105" ",106" ON ALL PLATFORMS",107"1. type 'R' to align the tabs to the right side ",108"2. Lose focus on the window by clicking on some other window ",109"3. Focus indicator should disappear",110"4. Regain focus on the window the focus indicator should reappear." ,111" If focus doesn't behave as above, ",112" press 'Assertion Fail' else press 'Assertion Pass'",113},114115{116" Note : Incase of Assertion failure,user can enter",117" remarks by pressing 'Assertion Fail Remarks ' button",118" ",119" You would see an applet with JTabbedPane. Keep the size of applet variable.",120" ",121" ON ALL PLATFORMS",122"1. type 'B' to align the tabs to the bottom side ",123"2. Lose focus on the window by clicking on some other window ",124"3. Focus indicator should disappear",125"4. Regain focus on the window the focus indicator should reappear." ,126" If focus doesn't behave as above, ",127" press 'Assertion Fail' else press 'Assertion Pass'",128},129130{131" Note : Incase of Assertion failure,user can enter",132" remarks by pressing 'Assertion Fail Remarks ' button",133" ",134" You would see an applet with JTabbedPane. Keep the size of applet variable.",135" ",136" ON ALL PLATFORMS",137"1. type 'L' to align the tabs to the left side ",138"2. Lose focus on the window by clicking on some other window ",139"3. Focus indicator should disappear",140"4. Regain focus on the window the focus indicator should reappear." ,141" If focus doesn't behave as above, ",142" press 'Assertion Fail' else press 'Assertion Pass'",143},144145{146" Note : Incase of Assertion failure,user can enter",147" remarks by pressing 'Assertion Fail Remarks ' button",148" ",149" You would see an applet with JTabbedPane. Keep the size of applet variable.",150" ",151" ON ALL PLATFORMS",152"1. type 'C' to change the tab layout to WRAP_TAB_LAYOUT ",153"2. Lose focus on the window by clicking on some other window ",154"3. Focus indicator should disappear",155"4. Regain focus on the window the focus indicator should reappear." ,156" If focus doesn't behave as above, ",157" press 'Assertion Fail' else press 'Assertion Pass'",158},159160{161" Note : Incase of Assertion failure,user can enter",162" remarks by pressing 'Assertion Fail Remarks ' button",163" ",164" You would see an applet with JTabbedPane. Keep the size of applet variable.",165" ",166" ON ALL PLATFORMS",167"1. type 'T' to align the tabs to the top side ",168"2. Lose focus on the window by clicking on some other window ",169"3. Focus indicator should disappear",170"4. Regain focus on the window the focus indicator should reappear." ,171" If focus doesn't behave as above, ",172" press 'Assertion Fail' else press 'Assertion Pass'",173},174175{176" Note : Incase of Assertion failure,user can enter",177" remarks by pressing 'Assertion Fail Remarks ' button",178" ",179" You would see an applet with JTabbedPane. Keep the size of applet variable.",180" ",181" ON ALL PLATFORMS",182"1. type 'B' to align the tabs to the bottom side ",183"2. Lose focus on the window by clicking on some other window ",184"3. Focus indicator should disappear",185"4. Regain focus on the window the focus indicator should reappear." ,186" If focus doesn't behave as above, ",187" press 'Assertion Fail' else press 'Assertion Pass'",188},189190{191" Note : Incase of Assertion failure,user can enter",192" remarks by pressing 'Assertion Fail Remarks ' button",193" ",194" You would see an applet with JTabbedPane. Keep the size of applet variable.",195" ",196" ON ALL PLATFORMS",197"1. type 'R' to align the tabs to the right side ",198"2. Lose focus on the window by clicking on some other window ",199"3. Focus indicator should disappear",200"4. Regain focus on the window the focus indicator should reappear." ,201" If focus doesn't behave as above, ",202" press 'Assertion Fail' else press 'Assertion Pass'",203}204};205206String[] exceptionsSet =207{208"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the TOP of the window",209"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the RIGHT of the window",210"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",211"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the LEFT of the window",212"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the LEFT of the window",213"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the TOP of the window",214"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",215"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the RIGHT of the window"216};217218Sysout.setInstructionsWithExceptions(instructionsSet,exceptionsSet);219220}221222public void start (){}223224public void destroy(){225if(Sysout.failStatus()) {226String failMsg = Sysout.getFailureMessages();227failMsg = failMsg.replace('\n',' ');228throw new RuntimeException(failMsg);229}// End destroy230}231232protected InputMap createInputMap() {233return LookAndFeel.makeComponentInputMap(getRootPane(), new Object[] {234"R", "right",235"L", "left",236"T", "top",237"B", "bottom",238"C", "changeLayout",239"D", "dump"240});241}242243protected ActionMap createActionMap() {244ActionMap map = new ActionMap();245map.put("right", new RotateAction(JTabbedPane.RIGHT));246map.put("left", new RotateAction(JTabbedPane.LEFT));247map.put("top", new RotateAction(JTabbedPane.TOP));248map.put("bottom", new RotateAction(JTabbedPane.BOTTOM));249map.put("changeLayout", new ChangeLayoutAction());250map.put("dump", new DumpAction());251return map;252}253254private class RotateAction extends AbstractAction {255private int placement;256public RotateAction(int placement) {257this.placement = placement;258}259260public void actionPerformed(ActionEvent e) {261tabPane.setTabPlacement(placement);262}263}264265private class ChangeLayoutAction extends AbstractAction {266private boolean a = true;267public void actionPerformed(ActionEvent e) {268if (a) {269tabPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);270a = false;271} else {272tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);273a = true;274}275}276}277278private class DumpAction extends AbstractAction {279public void actionPerformed(ActionEvent e) {280for (int i = 0; i < tabPane.getTabCount(); i++) {281System.out.println("Tab: " + i + " "282+ tabPane.getUI().getTabBounds(tabPane, i));283}284}285}286}287288289/****************************************************290Standard Test Machinery291DO NOT modify anything below -- it's a standard292chunk of code whose purpose is to make user293interaction uniform, and thereby make it simpler294to read and understand someone else's test.295****************************************************/296297/**298This is part of the standard test machinery.299It creates a dialog (with the instructions), and is the interface300for sending text messages to the user.301To print the instructions, send an array of strings to Sysout.createDialog302WithInstructions method. Put one line of instructions per array entry.303To display a message for the tester to see, simply call Sysout.println304with the string to be displayed.305This mimics System.out.println but works within the test harness as well306as standalone.307*/308309class Sysout310{311private static TestDialog dialog;312313public static void createDialogWithInstructions( String[] instructions )314{315dialog = new TestDialog( new Frame(), "Instructions" );316dialog.printInstructions( instructions );317dialog.show();318println( "Any messages for the tester will display here." );319}320321public static void createDialog( )322{323dialog = new TestDialog( new Frame(), "Instructions" );324String[] defInstr = { "Instructions will appear here. ", "" } ;325dialog.printInstructions( defInstr );326dialog.show();327println( "Any messages for the tester will display here." );328}329330331public static void printInstructions( String[] instructions )332{333dialog.printInstructions( instructions );334}335336337public static void println( String messageIn )338{339dialog.displayMessage( messageIn );340}341342public static void setInstructionsWithExceptions(String instructionsSet[][],343String exceptionsSet[]) {344createDialogWithInstructions(instructionsSet[0]);345dialog.setInstructions(instructionsSet);346dialog.setExceptionMessages(exceptionsSet);347}348349public static String getFailureMessages() {350return dialog.failureMessages;351}352353public static boolean failStatus() {354return dialog.failStatus;355}356357}// Sysout class358359/**360This is part of the standard test machinery. It provides a place for the361test instructions to be displayed, and a place for interactive messages362to the user to be displayed.363To have the test instructions displayed, see Sysout.364To have a message to the user be displayed, see Sysout.365Do not call anything in this dialog directly.366*/367class TestDialog extends Dialog368{369370TextArea instructionsText;371TextArea messageText;372int maxStringLength = 70;373374Panel assertPanel;375Button assertPass,assertFail,remarks;376HandleAssert handleAssert;377boolean failStatus=false;378int instructionCounter=0;379String instructions[][];380int exceptionCounter=0;381String exceptionMessages[];382String failureMessages="<br>";383String remarksMessage=null;384RemarksDialog remarksDialog;385386//DO NOT call this directly, go through Sysout387public TestDialog( Frame frame, String name )388{389super( frame, name );390int scrollBoth = TextArea.SCROLLBARS_BOTH;391instructionsText = new TextArea( "", 14, maxStringLength, scrollBoth );392add( "North", instructionsText );393394messageText = new TextArea( "", 3, maxStringLength, scrollBoth );395add("Center", messageText);396397assertPanel = new Panel(new FlowLayout());398assertPass=new Button("Assertion Pass");399assertPass.setName("Assertion Pass");400assertFail=new Button("Assertion Fail");401assertFail.setName("Assertion Fail");402remarks = new Button("Assertion Fail Remarks");403remarks.setEnabled(false);404remarks.setName("Assertion Remarks");405assertPanel.add(assertPass);406assertPanel.add(assertFail);407assertPanel.add(remarks);408handleAssert = new HandleAssert();409assertPass.addActionListener(handleAssert);410assertFail.addActionListener(handleAssert);411remarks.addActionListener(handleAssert);412add("South",assertPanel);413pack();414415show();416}// TestDialog()417418//DO NOT call this directly, go through Sysout419public void printInstructions( String[] instructions )420{421//Clear out any current instructions422instructionsText.setText( "" );423424//Go down array of instruction strings425426String printStr, remainingStr;427for( int i=0; i < instructions.length; i++ )428{429//chop up each into pieces maxSringLength long430remainingStr = instructions[ i ];431while( remainingStr.length() > 0 )432{433//if longer than max then chop off first max chars to print434if( remainingStr.length() >= maxStringLength )435{436//Try to chop on a word boundary437int posOfSpace = remainingStr.438lastIndexOf( ' ', maxStringLength - 1 );439440if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;441442printStr = remainingStr.substring( 0, posOfSpace + 1 );443remainingStr = remainingStr.substring( posOfSpace + 1 );444}445//else just print446else447{448printStr = remainingStr;449remainingStr = "";450}451452instructionsText.append( printStr + "\n" );453454}// while455456}// for457458}//printInstructions()459460//DO NOT call this directly, go through Sysout461public void displayMessage( String messageIn )462{463messageText.append( messageIn + "\n" );464}465466public void emptyMessage() {467messageText.setText("");468}469470public void setInstructions(String insStr[][]) {471instructions=insStr;472}473474public void setExceptionMessages(String exceptionMessages[]) {475this.exceptionMessages=exceptionMessages;476}477478class HandleAssert implements ActionListener {479public void actionPerformed(ActionEvent ae) {480if(ae.getSource()==remarks) {481remarksDialog = new RemarksDialog(TestDialog.this,482"Assertion Remarks Dialog",true);483remarks.setEnabled(false);484if(remarksMessage!=null)485failureMessages+=". User Remarks : "+remarksMessage;486}487else {488if(instructionCounter<instructions.length-1) {489emptyMessage();490instructionCounter++;491printInstructions(instructions[instructionCounter]);492}493else {494emptyMessage();495displayMessage("Testcase Completed");496displayMessage("Press 'Done' button in the "+497"BaseApplet to close");498assertPass.setEnabled(false);499assertFail.setEnabled(false);500}501502if(ae.getSource()==assertPass) {503// anything to be done in future504}505else if(ae.getSource()==assertFail) {506remarks.setEnabled(true);507if(!failStatus)508failStatus=true;509if(exceptionCounter<exceptionMessages.length) {510failureMessages = failureMessages + "<br>"+511exceptionMessages[exceptionCounter];512}513}514exceptionCounter++;515}516}517}518519class RemarksDialog extends Dialog implements ActionListener{520Panel rootPanel,remarksPanel;521TextArea textarea;522Button addRemarks,cancelRemarks;523public RemarksDialog(Dialog owner,String title,boolean modal) {524super(owner,title,modal);525rootPanel = new Panel(new BorderLayout());526remarksPanel = new Panel(new FlowLayout());527textarea = new TextArea(5,30);528addRemarks=new Button("Add Remarks");529addRemarks.addActionListener(this);530cancelRemarks = new Button("Cancel Remarks");531cancelRemarks.addActionListener(this);532remarksPanel.add(addRemarks);533remarksPanel.add(cancelRemarks);534rootPanel.add(textarea,"Center");535rootPanel.add(remarksPanel,"South");536add(rootPanel);537setBounds(150,150,400,200);538setVisible(true);539}540541public void actionPerformed(ActionEvent ae) {542remarksMessage=null;543if(ae.getSource()==addRemarks) {544String msg = textarea.getText().trim();545if (msg.length()>0)546remarksMessage=msg;547}548dispose();549}550551}552553}// TestDialog class554555556