1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.event.*;
  26 import javax.swing.*;
  27 /*
  28  * test
  29  * @bug 4666224
  30 */
  31 
  32 public class bug4666224 extends JApplet {
  33     final static int placements[] = { JTabbedPane.LEFT, JTabbedPane.RIGHT, JTabbedPane.TOP, JTabbedPane.BOTTOM };
  34     private JTabbedPane tabPane;
  35     private JPanel mainPanel;
  36 
  37     public bug4666224() throws Exception {
  38        java.awt.EventQueue.invokeAndWait( () -> {
  39            tabPane = new JTabbedPane();
  40            tabPane.addMouseListener(new MouseAdapter() {
  41                 public void mouseClicked(MouseEvent e) {
  42                     Point pt = e.getPoint();
  43                     System.out.println("Index at location: "
  44                         + tabPane.indexAtLocation(pt.x, pt.y));
  45                 }
  46             });
  47             InputMap inputMap = createInputMap();
  48             SwingUtilities.replaceUIInputMap(getRootPane(), JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
  49             ActionMap actionMap = createActionMap();
  50             SwingUtilities.replaceUIActionMap(getRootPane(), actionMap);
  51 
  52             //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53             tabPane.setTabPlacement(JTabbedPane.TOP);
  54             tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  55             JPanel panel = new JPanel();
  56             panel.setPreferredSize(new Dimension(200, 300));
  57             tabPane.addTab("Number Zero", panel);
  58             panel = new JPanel();
  59             //panel.requestFocus();
  60             panel.setPreferredSize(new Dimension(200, 300));
  61             tabPane.addTab("Number One", panel);
  62             panel = new JPanel();
  63             panel.setPreferredSize(new Dimension(200, 300));
  64             tabPane.addTab("Number Two", panel);
  65             panel = new JPanel();
  66             panel.setPreferredSize(new Dimension(200, 300));
  67             tabPane.addTab("Number Three", new JColorChooser());
  68             panel = new JPanel();
  69             panel.setPreferredSize(new Dimension(200, 300));
  70             tabPane.addTab("Number Four", panel);
  71             mainPanel = new JPanel();
  72             mainPanel.add(tabPane);
  73 
  74             getContentPane().add(mainPanel);
  75                     tabPane.requestFocus();
  76 
  77 
  78             //pack();
  79             //setVisible(true);
  80         });
  81     }
  82 
  83     public void init() {
  84                 String[][] instructionsSet =
  85                 {
  86                         {
  87                                 " Note : Incase of Assertion failure,user can enter",
  88                                 " remarks by pressing 'Assertion Fail Remarks ' button",
  89                                 " ",
  90                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
  91                 " ",
  92                     " ON ALL PLATFORMS",
  93                 "1. Click on any of the tabs, focus indicator is visible",
  94                     "2. Lose focus on the window by clicking on some other window ",
  95                     "3. Focus indicator should disappear",
  96                     "4. Regain focus on the window the focus indicator should reappear." ,
  97                     " If focus doesn't behave as above, ",
  98                     " press 'Assertion Fail' else press 'Assertion Pass'",
  99                 },
 100 
 101                         {
 102                                 " Note : Incase of Assertion failure,user can enter",
 103                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 104                                 " ",
 105                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 106                 " ",
 107                     " ON ALL PLATFORMS",
 108                     "1. type 'R' to align the tabs to the right side ",
 109                     "2. Lose focus on the window by clicking on some other window ",
 110                     "3. Focus indicator should disappear",
 111                     "4. Regain focus on the window the focus indicator should reappear." ,
 112                     " If focus doesn't behave as above, ",
 113                     " press 'Assertion Fail' else press 'Assertion Pass'",
 114                 },
 115 
 116                 {
 117                                 " Note : Incase of Assertion failure,user can enter",
 118                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 119                                 " ",
 120                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 121                 " ",
 122                     " ON ALL PLATFORMS",
 123                 "1. type 'B' to align the tabs to the bottom side ",
 124                     "2. Lose focus on the window by clicking on some other window ",
 125                     "3. Focus indicator should disappear",
 126                     "4. Regain focus on the window the focus indicator should reappear." ,
 127                     " If focus doesn't behave as above, ",
 128                     " press 'Assertion Fail' else press 'Assertion Pass'",
 129                 },
 130 
 131                 {
 132                                 " Note : Incase of Assertion failure,user can enter",
 133                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 134                                 " ",
 135                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 136                 " ",
 137                     " ON ALL PLATFORMS",
 138                     "1. type 'L' to align the tabs to the left side ",
 139                     "2. Lose focus on the window by clicking on some other window ",
 140                     "3. Focus indicator should disappear",
 141                     "4. Regain focus on the window the focus indicator should reappear." ,
 142                     " If focus doesn't behave as above, ",
 143                     " press 'Assertion Fail' else press 'Assertion Pass'",
 144                 },
 145 
 146                 {
 147                                 " Note : Incase of Assertion failure,user can enter",
 148                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 149                                 " ",
 150                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 151                 " ",
 152                     " ON ALL PLATFORMS",
 153                     "1. type 'C' to change the tab layout to WRAP_TAB_LAYOUT ",
 154                     "2. Lose focus on the window by clicking on some other window ",
 155                     "3. Focus indicator should disappear",
 156                     "4. Regain focus on the window the focus indicator should reappear." ,
 157                     " If focus doesn't behave as above, ",
 158                     " press 'Assertion Fail' else press 'Assertion Pass'",
 159                 },
 160 
 161                 {
 162                                 " Note : Incase of Assertion failure,user can enter",
 163                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 164                                 " ",
 165                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 166                         " ",
 167                     " ON ALL PLATFORMS",
 168                         "1. type 'T' to align the tabs to the top side ",
 169                         "2. Lose focus on the window by clicking on some other window ",
 170                     "3. Focus indicator should disappear",
 171                     "4. Regain focus on the window the focus indicator should reappear." ,
 172                     " If focus doesn't behave as above, ",
 173                     " press 'Assertion Fail' else press 'Assertion Pass'",
 174                 },
 175 
 176                 {
 177                                 " Note : Incase of Assertion failure,user can enter",
 178                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 179                                 " ",
 180                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 181                 " ",
 182                     " ON ALL PLATFORMS",
 183                         "1. type 'B' to align the tabs to the bottom side ",
 184                         "2. Lose focus on the window by clicking on some other window ",
 185                     "3. Focus indicator should disappear",
 186                     "4. Regain focus on the window the focus indicator should reappear." ,
 187                     " If focus doesn't behave as above, ",
 188                     " press 'Assertion Fail' else press 'Assertion Pass'",
 189                 },
 190 
 191                 {
 192                                 " Note : Incase of Assertion failure,user can enter",
 193                                 " remarks by pressing 'Assertion Fail Remarks ' button",
 194                                 " ",
 195                                 " You would see an applet with JTabbedPane. Keep the size of applet variable.",
 196                         " ",
 197                     " ON ALL PLATFORMS",
 198                     "1. type 'R' to align the tabs to the right side ",
 199                     "2. Lose focus on the window by clicking on some other window ",
 200                     "3. Focus indicator should disappear",
 201                     "4. Regain focus on the window the focus indicator should reappear." ,
 202                     " If focus doesn't behave as above, ",
 203                     " press 'Assertion Fail' else press 'Assertion Pass'",
 204                 }
 205                 };
 206 
 207                 String[] exceptionsSet =
 208                 {
 209                         "Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the TOP of the window",
 210                         "Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the RIGHT of the window",
 211                         "Focus painted incorrectly in tabbed pane(SCROLL_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",
 212                         "Focus painted incorrectly in tabbed pane(SCROLL_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 LEFT of the window",
 214                         "Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the TOP of the window",
 215                         "Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the BOTTOM of the window",
 216                         "Focus painted incorrectly in tabbed pane(WRAP_TAB_LAYOUT) when tabs aligned to the RIGHT of the window"
 217                 };
 218 
 219                 Sysout.setInstructionsWithExceptions(instructionsSet,exceptionsSet);
 220 
 221         }
 222 
 223         public void start (){}
 224 
 225         public void destroy(){
 226             if(Sysout.failStatus())    {
 227                 String failMsg = Sysout.getFailureMessages();
 228                 failMsg = failMsg.replace('\n',' ');
 229                 throw new RuntimeException(failMsg);
 230             }// End destroy
 231     }
 232 
 233     protected InputMap createInputMap() {
 234         return LookAndFeel.makeComponentInputMap(getRootPane(), new Object[] {
 235             "R", "right",
 236             "L", "left",
 237             "T", "top",
 238             "B", "bottom",
 239             "C", "changeLayout",
 240             "D", "dump"
 241         });
 242     }
 243 
 244     protected ActionMap createActionMap() {
 245         ActionMap map = new ActionMap();
 246         map.put("right", new RotateAction(JTabbedPane.RIGHT));
 247         map.put("left", new RotateAction(JTabbedPane.LEFT));
 248         map.put("top", new RotateAction(JTabbedPane.TOP));
 249         map.put("bottom", new RotateAction(JTabbedPane.BOTTOM));
 250         map.put("changeLayout", new ChangeLayoutAction());
 251         map.put("dump", new DumpAction());
 252         return map;
 253     }
 254 
 255     private class RotateAction extends AbstractAction {
 256         private int placement;
 257         public RotateAction(int placement) {
 258             this.placement = placement;
 259         }
 260 
 261         public void actionPerformed(ActionEvent e) {
 262             tabPane.setTabPlacement(placement);
 263         }
 264     }
 265 
 266     private class ChangeLayoutAction extends AbstractAction {
 267         private boolean a = true;
 268         public void actionPerformed(ActionEvent e) {
 269             if (a) {
 270                 tabPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
 271                 a = false;
 272             } else {
 273                 tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 274                 a = true;
 275             }
 276         }
 277     }
 278 
 279     private class DumpAction extends AbstractAction {
 280         public void actionPerformed(ActionEvent e) {
 281             for (int i = 0; i < tabPane.getTabCount(); i++) {
 282                 System.out.println("Tab: " + i + " "
 283                     + tabPane.getUI().getTabBounds(tabPane, i));
 284             }
 285         }
 286     }
 287 }
 288 
 289 
 290 /****************************************************
 291  Standard Test Machinery
 292  DO NOT modify anything below -- it's a standard
 293   chunk of code whose purpose is to make user
 294   interaction uniform, and thereby make it simpler
 295   to read and understand someone else's test.
 296  ****************************************************/
 297 
 298 /**
 299  This is part of the standard test machinery.
 300  It creates a dialog (with the instructions), and is the interface
 301   for sending text messages to the user.
 302  To print the instructions, send an array of strings to Sysout.createDialog
 303   WithInstructions method.  Put one line of instructions per array entry.
 304  To display a message for the tester to see, simply call Sysout.println
 305   with the string to be displayed.
 306  This mimics System.out.println but works within the test harness as well
 307   as standalone.
 308  */
 309 
 310 class Sysout
 311  {
 312    private static TestDialog dialog;
 313 
 314    public static void createDialogWithInstructions( String[] instructions )
 315     {
 316       dialog = new TestDialog( new Frame(), "Instructions" );
 317       dialog.printInstructions( instructions );
 318       dialog.show();
 319       println( "Any messages for the tester will display here." );
 320     }
 321 
 322    public static void createDialog( )
 323     {
 324       dialog = new TestDialog( new Frame(), "Instructions" );
 325       String[] defInstr = { "Instructions will appear here. ", "" } ;
 326       dialog.printInstructions( defInstr );
 327       dialog.show();
 328       println( "Any messages for the tester will display here." );
 329     }
 330 
 331 
 332    public static void printInstructions( String[] instructions )
 333     {
 334       dialog.printInstructions( instructions );
 335     }
 336 
 337 
 338    public static void println( String messageIn )
 339     {
 340       dialog.displayMessage( messageIn );
 341     }
 342 
 343    public static void setInstructionsWithExceptions(String instructionsSet[][],
 344                                                     String exceptionsSet[]) {
 345        createDialogWithInstructions(instructionsSet[0]);
 346        dialog.setInstructions(instructionsSet);
 347        dialog.setExceptionMessages(exceptionsSet);
 348    }
 349 
 350    public static String getFailureMessages()   {
 351        return dialog.failureMessages;
 352    }
 353 
 354    public static boolean failStatus()  {
 355        return dialog.failStatus;
 356    }
 357 
 358  }// Sysout  class
 359 
 360 /**
 361   This is part of the standard test machinery.  It provides a place for the
 362    test instructions to be displayed, and a place for interactive messages
 363    to the user to be displayed.
 364   To have the test instructions displayed, see Sysout.
 365   To have a message to the user be displayed, see Sysout.
 366   Do not call anything in this dialog directly.
 367   */
 368 class TestDialog extends Dialog
 369  {
 370 
 371    TextArea instructionsText;
 372    TextArea messageText;
 373    int maxStringLength = 70;
 374 
 375    Panel assertPanel;
 376    Button assertPass,assertFail,remarks;
 377    HandleAssert handleAssert;
 378    boolean failStatus=false;
 379    int instructionCounter=0;
 380    String instructions[][];
 381    int exceptionCounter=0;
 382    String exceptionMessages[];
 383    String failureMessages="<br>";
 384    String remarksMessage=null;
 385    RemarksDialog remarksDialog;
 386 
 387    //DO NOT call this directly, go through Sysout
 388    public TestDialog( Frame frame, String name )
 389     {
 390       super( frame, name );
 391       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 392       instructionsText = new TextArea( "", 14, maxStringLength, scrollBoth );
 393       add( "North", instructionsText );
 394 
 395       messageText = new TextArea( "", 3, maxStringLength, scrollBoth );
 396       add("Center", messageText);
 397 
 398       assertPanel = new Panel(new FlowLayout());
 399       assertPass=new Button("Assertion Pass");
 400       assertPass.setName("Assertion Pass");
 401       assertFail=new Button("Assertion Fail");
 402       assertFail.setName("Assertion Fail");
 403       remarks = new Button("Assertion Fail Remarks");
 404       remarks.setEnabled(false);
 405       remarks.setName("Assertion Remarks");
 406       assertPanel.add(assertPass);
 407       assertPanel.add(assertFail);
 408       assertPanel.add(remarks);
 409       handleAssert = new HandleAssert();
 410       assertPass.addActionListener(handleAssert);
 411       assertFail.addActionListener(handleAssert);
 412       remarks.addActionListener(handleAssert);
 413       add("South",assertPanel);
 414       pack();
 415 
 416       show();
 417     }// TestDialog()
 418 
 419    //DO NOT call this directly, go through Sysout
 420    public void printInstructions( String[] instructions )
 421     {
 422       //Clear out any current instructions
 423       instructionsText.setText( "" );
 424 
 425       //Go down array of instruction strings
 426 
 427       String printStr, remainingStr;
 428       for( int i=0; i < instructions.length; i++ )
 429        {
 430      //chop up each into pieces maxSringLength long
 431      remainingStr = instructions[ i ];
 432      while( remainingStr.length() > 0 )
 433       {
 434         //if longer than max then chop off first max chars to print
 435         if( remainingStr.length() >= maxStringLength )
 436          {
 437            //Try to chop on a word boundary
 438            int posOfSpace = remainingStr.
 439           lastIndexOf( ' ', maxStringLength - 1 );
 440 
 441            if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 442 
 443            printStr = remainingStr.substring( 0, posOfSpace + 1 );
 444            remainingStr = remainingStr.substring( posOfSpace + 1 );
 445          }
 446         //else just print
 447         else
 448          {
 449            printStr = remainingStr;
 450            remainingStr = "";
 451          }
 452 
 453             instructionsText.append( printStr + "\n" );
 454 
 455       }// while
 456 
 457        }// for
 458 
 459     }//printInstructions()
 460 
 461    //DO NOT call this directly, go through Sysout
 462    public void displayMessage( String messageIn )
 463     {
 464       messageText.append( messageIn + "\n" );
 465     }
 466 
 467    public void emptyMessage()   {
 468        messageText.setText("");
 469    }
 470 
 471    public void setInstructions(String insStr[][])    {
 472        instructions=insStr;
 473    }
 474 
 475    public void setExceptionMessages(String exceptionMessages[])   {
 476        this.exceptionMessages=exceptionMessages;
 477    }
 478 
 479    class HandleAssert implements ActionListener   {
 480         public void actionPerformed(ActionEvent ae)    {
 481            if(ae.getSource()==remarks)  {
 482                remarksDialog = new RemarksDialog(TestDialog.this,
 483                                         "Assertion Remarks Dialog",true);
 484                remarks.setEnabled(false);
 485                if(remarksMessage!=null)
 486                     failureMessages+=". User Remarks : "+remarksMessage;
 487            }
 488            else {
 489                if(instructionCounter<instructions.length-1) {
 490                    emptyMessage();
 491                    instructionCounter++;
 492                    printInstructions(instructions[instructionCounter]);
 493                }
 494                else {
 495                    emptyMessage();
 496                    displayMessage("Testcase Completed");
 497                    displayMessage("Press 'Done' button in the "+
 498                                                     "BaseApplet to close");
 499                    assertPass.setEnabled(false);
 500                    assertFail.setEnabled(false);
 501                }
 502 
 503                if(ae.getSource()==assertPass)    {
 504                    // anything to be done in future
 505                }
 506                else if(ae.getSource()==assertFail)   {
 507                    remarks.setEnabled(true);
 508                    if(!failStatus)
 509                        failStatus=true;
 510                    if(exceptionCounter<exceptionMessages.length)   {
 511                         failureMessages = failureMessages + "<br>"+
 512                                     exceptionMessages[exceptionCounter];
 513                    }
 514                }
 515                exceptionCounter++;
 516            }
 517         }
 518     }
 519 
 520     class RemarksDialog extends Dialog  implements ActionListener{
 521         Panel rootPanel,remarksPanel;
 522         TextArea textarea;
 523         Button addRemarks,cancelRemarks;
 524         public RemarksDialog(Dialog owner,String title,boolean modal)  {
 525             super(owner,title,modal);
 526             rootPanel = new Panel(new BorderLayout());
 527             remarksPanel = new Panel(new FlowLayout());
 528             textarea = new TextArea(5,30);
 529             addRemarks=new Button("Add Remarks");
 530             addRemarks.addActionListener(this);
 531             cancelRemarks = new Button("Cancel Remarks");
 532             cancelRemarks.addActionListener(this);
 533             remarksPanel.add(addRemarks);
 534             remarksPanel.add(cancelRemarks);
 535             rootPanel.add(textarea,"Center");
 536             rootPanel.add(remarksPanel,"South");
 537             add(rootPanel);
 538             setBounds(150,150,400,200);
 539             setVisible(true);
 540         }
 541 
 542         public void actionPerformed(ActionEvent ae) {
 543             remarksMessage=null;
 544             if(ae.getSource()==addRemarks)  {
 545                 String msg = textarea.getText().trim();
 546                 if (msg.length()>0)
 547                     remarksMessage=msg;
 548             }
 549             dispose();
 550         }
 551 
 552     }
 553 
 554 }// TestDialog  class