001 /* 002 * Created on Jun 10, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2008-2010 the original author or authors. 015 */ 016 package org.fest.swing.driver; 017 018 import static java.lang.Boolean.parseBoolean; 019 import static org.fest.swing.edt.GuiActionRunner.execute; 020 021 import java.awt.Point; 022 023 import javax.swing.JCheckBox; 024 import javax.swing.JTable; 025 import javax.swing.text.JTextComponent; 026 027 import org.fest.swing.annotation.RunsInEDT; 028 import org.fest.swing.cell.JTableCellWriter; 029 import org.fest.swing.core.Robot; 030 import org.fest.swing.edt.GuiQuery; 031 import org.fest.swing.util.Pair; 032 033 /** 034 * Understands an implementation of <code>{@link JTableCellWriter}</code> that knows how to use 035 * <code>{@link JTextComponent}</code>s as cell editors. 036 * 037 * @author Alex Ruiz 038 * @author Yvonne Wang 039 */ 040 public class JTableCheckBoxEditorCellWriter extends AbstractJTableCellWriter { 041 042 public JTableCheckBoxEditorCellWriter(Robot robot) { 043 super(robot); 044 } 045 046 /** {@inheritDoc} */ 047 @RunsInEDT 048 public void enterValue(JTable table, int row, int column, String value) { 049 boolean realValue = parseBoolean(value); 050 Pair<Boolean, Point> editingInfo = doStartCellEditing(table, row, column, location); 051 if (editingInfo.i == realValue) return; // JCheckBox already has value to set. 052 robot.click(table, editingInfo.ii); 053 } 054 055 /** {@inheritDoc} */ 056 @RunsInEDT 057 public void startCellEditing(JTable table, int row, int column) { 058 doStartCellEditing(table, row, column, location); 059 } 060 061 @RunsInEDT 062 private static Pair<Boolean, Point> doStartCellEditing(final JTable table, final int row, final int column, 063 final JTableLocation location) { 064 return execute(new GuiQuery<Pair<Boolean, Point>>() { 065 protected Pair<Boolean, Point> executeInEDT() { 066 JCheckBox editor = editor(table, row, column, JCheckBox.class); 067 scrollToCell(table, row, column, location); 068 return new Pair<Boolean, Point>(editor.isSelected(), location.pointAt(table, row, column)); 069 } 070 }); 071 } 072 }