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