1 /*
   2  * Copyright (c) 2007, 2011, 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 /*
  25  * @test
  26  * @key headful
  27  * @bug 6348946
  28  * @summary Tests that JSlider's thumb moves in the right direction
  29  *          when it is used as a JTable cell editor.
  30  * @author Mikhail Lapshin
  31 */
  32 
  33 import java.awt.*;
  34 import java.awt.event.InputEvent;
  35 import javax.swing.*;
  36 import javax.swing.event.*;
  37 import javax.swing.table.*;
  38 
  39 public class bug6348946 {
  40 
  41     private static JFrame frame;
  42 
  43     private static JPanel panel;
  44     private static Robot robot;
  45 
  46     private static volatile boolean passed = false;
  47 
  48     public static void main(String[] args) throws Exception {
  49         robot = new Robot();
  50         robot.setAutoDelay(10);
  51 
  52         String lf = "javax.swing.plaf.metal.MetalLookAndFeel";
  53         UIManager.setLookAndFeel(lf);
  54 
  55         try {
  56             SwingUtilities.invokeAndWait(new Runnable() {
  57                 public void run() {
  58                     setupUI();
  59                 }
  60             });
  61             robot.waitForIdle();
  62             clickOnSlider();
  63             robot.waitForIdle();
  64             checkResult();
  65         } finally {
  66             stopEDT();
  67         }
  68     }
  69 
  70     private static void setupUI() {
  71         frame = new JFrame();
  72 
  73         panel = new JPanel();
  74         panel.setLayout(new BorderLayout());
  75         panel.add(new ParameterTable(), BorderLayout.CENTER);
  76         frame.getContentPane().add(panel);
  77 
  78         frame.pack();
  79         frame.setLocationRelativeTo(null);
  80         frame.setVisible(true);
  81     }
  82 
  83     private static void clickOnSlider() throws Exception {
  84         Rectangle rect = getPanelRectangle();
  85 
  86         double clickX = rect.getX() + rect.getWidth() / 4;
  87         double clickY = rect.getY() + rect.getHeight() / 2;
  88         robot.mouseMove((int) clickX, (int) clickY);
  89 
  90         robot.mousePress(InputEvent.BUTTON1_MASK);
  91         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  92     }
  93 
  94     private static void checkResult(){
  95         if (passed) {
  96             System.out.println("Test passed");
  97         } else {
  98             throw new RuntimeException("The thumb moved " +
  99                     "to the right instead of the left!");
 100         }
 101     }
 102 
 103     private static void stopEDT() {
 104         SwingUtilities.invokeLater(new Runnable() {
 105             public void run() {
 106                 frame.dispose();
 107             }
 108         });
 109     }
 110 
 111     private static class ParameterTable extends JTable {
 112         public ParameterTable() {
 113             super(new Object[][]{{5}}, new String[]{"Value"});
 114             getColumnModel().getColumn(0).setCellRenderer(new Renderer());
 115             getColumnModel().getColumn(0).setCellEditor(new Editor());
 116         }
 117     }
 118 
 119     private static class Renderer implements TableCellRenderer {
 120         private JSlider slider = new JSlider(0, 10);
 121 
 122         public Component getTableCellRendererComponent(JTable table,
 123                                                        Object value,
 124                                                        boolean isSelected,
 125                                                        boolean hasFocus,
 126                                                        int row, int col) {
 127             int val = (Integer) value;
 128             slider.setValue(val);
 129             return slider;
 130         }
 131     }
 132 
 133     private static class Editor extends AbstractCellEditor implements TableCellEditor {
 134         private JSlider slider = new JSlider(0, 10);
 135 
 136         public Component getTableCellEditorComponent(JTable table, Object value,
 137                                                      boolean isSelected,
 138                                                      int row, int col) {
 139             int val = (Integer) value;
 140             slider.setValue(val);
 141             return slider;
 142         }
 143 
 144         public Editor() {
 145             slider.addChangeListener(new ChangeListener() {
 146                 public void stateChanged(ChangeEvent e) {
 147                     if (!slider.getValueIsAdjusting()) {
 148                         passed = slider.getValue() <= 5;
 149                     }
 150                 }
 151             });
 152         }
 153 
 154         public Object getCellEditorValue() {
 155             return slider.getValue();
 156         }
 157     }
 158 
 159     private static Rectangle getPanelRectangle() throws Exception{
 160         final Rectangle[] result = new Rectangle[1];
 161 
 162         SwingUtilities.invokeAndWait(new Runnable() {
 163             @Override
 164             public void run() {
 165                 result[0] = new Rectangle(panel.getLocationOnScreen(), panel.getSize());
 166             }
 167         });
 168 
 169         return result[0];
 170     }
 171 }