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