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