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.awt.event.KeyEvent.VK_F2;
019 import static org.fest.swing.core.MouseButton.LEFT_BUTTON;
020
021 import java.awt.Point;
022
023 import javax.swing.JTable;
024 import javax.swing.text.JTextComponent;
025
026 import org.fest.swing.annotation.RunsInEDT;
027 import org.fest.swing.cell.JTableCellWriter;
028 import org.fest.swing.core.Robot;
029 import org.fest.swing.exception.ActionFailedException;
030
031 /**
032 * Understands an implementation of <code>{@link JTableCellWriter}</code> that knows how to use
033 * <code>{@link JTextComponent}</code>s as cell editors.
034 *
035 * @author Alex Ruiz
036 * @author Yvonne Wang
037 */
038 public class JTableTextComponentEditorCellWriter extends AbstractJTableCellWriter {
039
040 protected final JTextComponentDriver driver;
041
042 public JTableTextComponentEditorCellWriter(Robot robot) {
043 super(robot);
044 driver = new JTextComponentDriver(robot);
045 }
046
047 /** {@inheritDoc} */
048 @RunsInEDT
049 public void enterValue(JTable table, int row, int column, String value) {
050 JTextComponent editor = doStartCellEditing(table, row, column);
051 driver.replaceText(editor, value);
052 stopCellEditing(table, row, column);
053 }
054
055 /** {@inheritDoc} */
056 @RunsInEDT
057 public void startCellEditing(JTable table, int row, int column) {
058 doStartCellEditing(table, row, column);
059 }
060
061 @RunsInEDT
062 private JTextComponent doStartCellEditing(JTable table, int row, int column) {
063 Point cellLocation = cellLocation(table, row, column, location);
064 JTextComponent textComponent = null;
065 try {
066 textComponent = activateEditorWithF2Key(table, row, column, cellLocation);
067 } catch (ActionFailedException e) {
068 textComponent = activateEditorWithDoubleClick(table, row, column, cellLocation);
069 }
070 cellEditor(cellEditor(table, row, column));
071 return textComponent;
072 }
073
074 @RunsInEDT
075 private JTextComponent activateEditorWithF2Key(JTable table, int row, int column, Point cellLocation) {
076 robot.click(table, cellLocation);
077 robot.pressAndReleaseKeys(VK_F2);
078 return waitForEditorActivation(table, row, column);
079 }
080
081 @RunsInEDT
082 private JTextComponent activateEditorWithDoubleClick(JTable table, int row, int column, Point cellLocation) {
083 robot.click(table, cellLocation, LEFT_BUTTON, 2);
084 return waitForEditorActivation(table, row, column);
085 }
086
087 @RunsInEDT
088 private JTextComponent waitForEditorActivation(JTable table, int row, int column) {
089 return waitForEditorActivation(table, row, column, JTextComponent.class);
090 }
091 }