Path: blob/master/test/jdk/javax/swing/JTabbedPane/4666224/bug4666224.java
41155 views
/*1* Copyright (c) 2001, 2020, 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);70panel = new JPanel();71panel.setPreferredSize(new Dimension(200, 300));72tabPane.addTab("Number Five", panel);73panel = new JPanel();74panel.setPreferredSize(new Dimension(200, 300));75tabPane.addTab("Number Six", panel);76panel = new JPanel();77panel.setPreferredSize(new Dimension(200, 300));78tabPane.addTab("Number Seven", panel);79panel = new JPanel();80panel.setPreferredSize(new Dimension(200, 300));81tabPane.addTab("Number Eight", panel);82panel = new JPanel();83panel.setPreferredSize(new Dimension(200, 300));84tabPane.addTab("Number Nine", panel);85panel = new JPanel();86panel.setPreferredSize(new Dimension(200, 300));87tabPane.addTab("Number Ten", panel);88mainPanel = new JPanel();89mainPanel.add(tabPane);9091getContentPane().add(mainPanel);92tabPane.requestFocus();939495//pack();96//setVisible(true);97});98}99100public void init() {101String[][] instructionsSet =102{103{104" Note : Incase of Assertion failure,user can enter",105" remarks by pressing 'Assertion Fail Remarks ' button",106" ",107" You would see an applet with JTabbedPane. Keep the size of applet variable.",108" ",109" ON ALL PLATFORMS",110"1. Click on any of the tabs, focus indicator is visible",111"2. Lose focus on the window by clicking on some other window ",112"3. Focus indicator should disappear",113"4. Regain focus on the window the focus indicator should reappear." ,114" If focus doesn't behave as above, ",115" press 'Assertion Fail' else press 'Assertion Pass'",116},117{118" Note : Incase of Assertion failure,user can enter",119" remarks by pressing 'Assertion Fail Remarks ' button",120" ",121" You would see an applet with JTabbedPane. Keep the size of applet variable.",122" ",123" ON ALL PLATFORMS",124"1. type 'C' to change the tab layout to WRAP_TAB_LAYOUT ",125"2. Lose focus on the window by clicking on some other window ",126"3. Focus indicator should disappear",127"4. Regain focus on the window the focus indicator should reappear." ,128" If focus doesn't behave as above, ",129" press 'Assertion Fail' else press 'Assertion Pass'",130},131132{133" Note : Incase of Assertion failure,user can enter",134" remarks by pressing 'Assertion Fail Remarks ' button",135" ",136" You would see an applet with JTabbedPane. Keep the size of applet variable.",137" ",138" ON ALL PLATFORMS",139"1. type 'R' to align the tabs to the right side ",140"2. Lose focus on the window by clicking on some other window ",141"3. Focus indicator should disappear",142"4. Regain focus on the window the focus indicator should reappear." ,143" If focus doesn't behave as above, ",144" press 'Assertion Fail' else press 'Assertion Pass'",145},146147{148" Note : Incase of Assertion failure,user can enter",149" remarks by pressing 'Assertion Fail Remarks ' button",150" ",151" You would see an applet with JTabbedPane. Keep the size of applet variable.",152" ",153" ON ALL PLATFORMS",154"1. type 'B' to align the tabs to the bottom side ",155"2. Lose focus on the window by clicking on some other window ",156"3. Focus indicator should disappear",157"4. Regain focus on the window the focus indicator should reappear." ,158" If focus doesn't behave as above, ",159" press 'Assertion Fail' else press 'Assertion Pass'",160},161162{163" Note : Incase of Assertion failure,user can enter",164" remarks by pressing 'Assertion Fail Remarks ' button",165" ",166" You would see an applet with JTabbedPane. Keep the size of applet variable.",167" ",168" ON ALL PLATFORMS",169"1. type 'L' to align the tabs to the left side ",170"2. Lose focus on the window by clicking on some other window ",171"3. Focus indicator should disappear",172"4. Regain focus on the window the focus indicator should reappear." ,173" If focus doesn't behave as above, ",174" press 'Assertion Fail' else press 'Assertion Pass'",175},176177178{179" Note : Incase of Assertion failure,user can enter",180" remarks by pressing 'Assertion Fail Remarks ' button",181" ",182" You would see an applet with JTabbedPane. Keep the size of applet variable.",183" ",184" ON ALL PLATFORMS",185"1. type 'T' to align the tabs to the top side ",186"2. Lose focus on the window by clicking on some other window ",187"3. Focus indicator should disappear",188"4. Regain focus on the window the focus indicator should reappear." ,189" If focus doesn't behave as above, ",190" press 'Assertion Fail' else press 'Assertion Pass'",191},192193{194" Note : Incase of Assertion failure,user can enter",195" remarks by pressing 'Assertion Fail Remarks ' button",196" ",197" You would see an applet with JTabbedPane. Keep the size of applet variable.",198" ",199" ON ALL PLATFORMS",200"1. type 'B' to align the tabs to the bottom side ",201"2. Lose focus on the window by clicking on some other window ",202"3. Focus indicator should disappear",203"4. Regain focus on the window the focus indicator should reappear." ,204" If focus doesn't behave as above, ",205" press 'Assertion Fail' else press 'Assertion Pass'",206},207208{209" Note : Incase of Assertion failure,user can enter",210" remarks by pressing 'Assertion Fail Remarks ' button",211" ",212" You would see an applet with JTabbedPane. Keep the size of applet variable.",213" ",214" ON ALL PLATFORMS",215"1. type 'R' to align the tabs to the right side ",216"2. Lose focus on the window by clicking on some other window ",217"3. Focus indicator should disappear",218"4. Regain focus on the window the focus indicator should reappear." ,219" If focus doesn't behave as above, ",220" press 'Assertion Fail' else press 'Assertion Pass'",221}222};223224String[] exceptionsSet =225{226"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the TOP of the window",227"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the RIGHT of the window",228"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",229"Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the LEFT of the window",230"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the LEFT of the window",231"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the TOP of the window",232"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",233"Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the RIGHT of the window"234};235236Sysout.setInstructionsWithExceptions(instructionsSet,exceptionsSet);237238}239240public void start (){}241242public void destroy(){243if(Sysout.failStatus()) {244String failMsg = Sysout.getFailureMessages();245failMsg = failMsg.replace('\n',' ');246throw new RuntimeException(failMsg);247}// End destroy248}249250protected InputMap createInputMap() {251return LookAndFeel.makeComponentInputMap(getRootPane(), new Object[] {252"R", "right",253"L", "left",254"T", "top",255"B", "bottom",256"C", "changeLayout",257"D", "dump"258});259}260261protected ActionMap createActionMap() {262ActionMap map = new ActionMap();263map.put("right", new RotateAction(JTabbedPane.RIGHT));264map.put("left", new RotateAction(JTabbedPane.LEFT));265map.put("top", new RotateAction(JTabbedPane.TOP));266map.put("bottom", new RotateAction(JTabbedPane.BOTTOM));267map.put("changeLayout", new ChangeLayoutAction());268map.put("dump", new DumpAction());269return map;270}271272private class RotateAction extends AbstractAction {273private int placement;274public RotateAction(int placement) {275this.placement = placement;276}277278public void actionPerformed(ActionEvent e) {279tabPane.setTabPlacement(placement);280}281}282283private class ChangeLayoutAction extends AbstractAction {284private boolean a = true;285public void actionPerformed(ActionEvent e) {286if (a) {287tabPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);288a = false;289} else {290tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);291a = true;292}293}294}295296private class DumpAction extends AbstractAction {297public void actionPerformed(ActionEvent e) {298for (int i = 0; i < tabPane.getTabCount(); i++) {299System.out.println("Tab: " + i + " "300+ tabPane.getUI().getTabBounds(tabPane, i));301}302}303}304}305306307/****************************************************308Standard Test Machinery309DO NOT modify anything below -- it's a standard310chunk of code whose purpose is to make user311interaction uniform, and thereby make it simpler312to read and understand someone else's test.313****************************************************/314315/**316This is part of the standard test machinery.317It creates a dialog (with the instructions), and is the interface318for sending text messages to the user.319To print the instructions, send an array of strings to Sysout.createDialog320WithInstructions method. Put one line of instructions per array entry.321To display a message for the tester to see, simply call Sysout.println322with the string to be displayed.323This mimics System.out.println but works within the test harness as well324as standalone.325*/326327class Sysout328{329private static TestDialog dialog;330331public static void createDialogWithInstructions( String[] instructions )332{333dialog = new TestDialog( new Frame(), "Instructions" );334dialog.printInstructions( instructions );335dialog.show();336println( "Any messages for the tester will display here." );337}338339public static void createDialog( )340{341dialog = new TestDialog( new Frame(), "Instructions" );342String[] defInstr = { "Instructions will appear here. ", "" } ;343dialog.printInstructions( defInstr );344dialog.show();345println( "Any messages for the tester will display here." );346}347348349public static void printInstructions( String[] instructions )350{351dialog.printInstructions( instructions );352}353354355public static void println( String messageIn )356{357dialog.displayMessage( messageIn );358}359360public static void setInstructionsWithExceptions(String instructionsSet[][],361String exceptionsSet[]) {362createDialogWithInstructions(instructionsSet[0]);363dialog.setInstructions(instructionsSet);364dialog.setExceptionMessages(exceptionsSet);365}366367public static String getFailureMessages() {368return dialog.failureMessages;369}370371public static boolean failStatus() {372return dialog.failStatus;373}374375}// Sysout class376377/**378This is part of the standard test machinery. It provides a place for the379test instructions to be displayed, and a place for interactive messages380to the user to be displayed.381To have the test instructions displayed, see Sysout.382To have a message to the user be displayed, see Sysout.383Do not call anything in this dialog directly.384*/385class TestDialog extends Dialog386{387388TextArea instructionsText;389TextArea messageText;390int maxStringLength = 70;391392Panel assertPanel;393Button assertPass,assertFail,remarks;394HandleAssert handleAssert;395boolean failStatus=false;396int instructionCounter=0;397String instructions[][];398int exceptionCounter=0;399String exceptionMessages[];400String failureMessages="<br>";401String remarksMessage=null;402RemarksDialog remarksDialog;403404//DO NOT call this directly, go through Sysout405public TestDialog( Frame frame, String name )406{407super( frame, name );408int scrollBoth = TextArea.SCROLLBARS_BOTH;409instructionsText = new TextArea( "", 14, maxStringLength, scrollBoth );410add( "North", instructionsText );411412messageText = new TextArea( "", 3, maxStringLength, scrollBoth );413add("Center", messageText);414415assertPanel = new Panel(new FlowLayout());416assertPass=new Button("Assertion Pass");417assertPass.setName("Assertion Pass");418assertFail=new Button("Assertion Fail");419assertFail.setName("Assertion Fail");420remarks = new Button("Assertion Fail Remarks");421remarks.setEnabled(false);422remarks.setName("Assertion Remarks");423assertPanel.add(assertPass);424assertPanel.add(assertFail);425assertPanel.add(remarks);426handleAssert = new HandleAssert();427assertPass.addActionListener(handleAssert);428assertFail.addActionListener(handleAssert);429remarks.addActionListener(handleAssert);430add("South",assertPanel);431pack();432433show();434}// TestDialog()435436//DO NOT call this directly, go through Sysout437public void printInstructions( String[] instructions )438{439//Clear out any current instructions440instructionsText.setText( "" );441442//Go down array of instruction strings443444String printStr, remainingStr;445for( int i=0; i < instructions.length; i++ )446{447//chop up each into pieces maxSringLength long448remainingStr = instructions[ i ];449while( remainingStr.length() > 0 )450{451//if longer than max then chop off first max chars to print452if( remainingStr.length() >= maxStringLength )453{454//Try to chop on a word boundary455int posOfSpace = remainingStr.456lastIndexOf( ' ', maxStringLength - 1 );457458if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;459460printStr = remainingStr.substring( 0, posOfSpace + 1 );461remainingStr = remainingStr.substring( posOfSpace + 1 );462}463//else just print464else465{466printStr = remainingStr;467remainingStr = "";468}469470instructionsText.append( printStr + "\n" );471472}// while473474}// for475476}//printInstructions()477478//DO NOT call this directly, go through Sysout479public void displayMessage( String messageIn )480{481messageText.append( messageIn + "\n" );482}483484public void emptyMessage() {485messageText.setText("");486}487488public void setInstructions(String insStr[][]) {489instructions=insStr;490}491492public void setExceptionMessages(String exceptionMessages[]) {493this.exceptionMessages=exceptionMessages;494}495496class HandleAssert implements ActionListener {497public void actionPerformed(ActionEvent ae) {498if(ae.getSource()==remarks) {499remarksDialog = new RemarksDialog(TestDialog.this,500"Assertion Remarks Dialog",true);501remarks.setEnabled(false);502if(remarksMessage!=null)503failureMessages+=". User Remarks : "+remarksMessage;504}505else {506if(instructionCounter<instructions.length-1) {507emptyMessage();508instructionCounter++;509printInstructions(instructions[instructionCounter]);510}511else {512emptyMessage();513displayMessage("Testcase Completed");514displayMessage("Press 'Done' button in the "+515"BaseApplet to close");516assertPass.setEnabled(false);517assertFail.setEnabled(false);518}519520if(ae.getSource()==assertPass) {521// anything to be done in future522}523else if(ae.getSource()==assertFail) {524remarks.setEnabled(true);525if(!failStatus)526failStatus=true;527if(exceptionCounter<exceptionMessages.length) {528failureMessages = failureMessages + "<br>"+529exceptionMessages[exceptionCounter];530}531}532exceptionCounter++;533}534}535}536537class RemarksDialog extends Dialog implements ActionListener{538Panel rootPanel,remarksPanel;539TextArea textarea;540Button addRemarks,cancelRemarks;541public RemarksDialog(Dialog owner,String title,boolean modal) {542super(owner,title,modal);543rootPanel = new Panel(new BorderLayout());544remarksPanel = new Panel(new FlowLayout());545textarea = new TextArea(5,30);546addRemarks=new Button("Add Remarks");547addRemarks.addActionListener(this);548cancelRemarks = new Button("Cancel Remarks");549cancelRemarks.addActionListener(this);550remarksPanel.add(addRemarks);551remarksPanel.add(cancelRemarks);552rootPanel.add(textarea,"Center");553rootPanel.add(remarksPanel,"South");554add(rootPanel);555setBounds(150,150,400,200);556setVisible(true);557}558559public void actionPerformed(ActionEvent ae) {560remarksMessage=null;561if(ae.getSource()==addRemarks) {562String msg = textarea.getText().trim();563if (msg.length()>0)564remarksMessage=msg;565}566dispose();567}568569}570571}// TestDialog class572573574