001 /*
002 * Created on Jun 8, 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 java.awt.Component;
019
020 import javax.swing.*;
021 import javax.swing.text.JTextComponent;
022
023 import org.fest.swing.cell.JTableCellWriter;
024 import org.fest.swing.core.Robot;
025 import org.fest.swing.exception.ActionFailedException;
026
027 /**
028 * Understands the default implementation of <code>{@link JTableCellWriter}</code>.
029 *
030 * @author Yvonne Wang
031 * @author Alex Ruiz
032 */
033 public class BasicJTableCellWriter extends AbstractJTableCellWriter {
034
035 private final JTableCheckBoxEditorCellWriter checkBoxWriter;
036 private final JTableComboBoxEditorCellWriter comboBoxWriter;
037 private final JTableTextComponentEditorCellWriter textComponentWriter;
038
039 public BasicJTableCellWriter(Robot robot) {
040 super(robot);
041 checkBoxWriter = new JTableCheckBoxEditorCellWriter(robot);
042 comboBoxWriter = new JTableComboBoxEditorCellWriter(robot);
043 textComponentWriter = new JTableTextComponentEditorCellWriter(robot);
044 }
045
046 /**
047 * Enters the given value at the given cell of the <code>JTable</code>. This method only supports the following GUI
048 * components as cell editors:
049 * <ul>
050 * <li><code>{@link JCheckBox}</code>: valid values for the property "selected" (a boolean) are "true" and "yes",
051 * other values are considered <code>false</code>.</li>
052 * <li><code>{@link JComboBox}</code>: this writer will select the element which <code>String</code> representation
053 * matches the given value.</li>
054 * <li><code>{@link JTextComponent}</code>: any value will be entered in the cell.</li>
055 * </ul>
056 * @param table the target <code>JTable</code>.
057 * @param row the row index of the cell.
058 * @param column the column index of the cell.
059 * @param value the value to enter.
060 * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
061 */
062 public void enterValue(JTable table, int row, int column, String value) {
063 cellWriterFor(table, row, column).enterValue(table, row, column, value);
064 }
065
066 /**
067 * Starts editing the given cell of the <code>{@link JTable}</code>. This method only supports the following GUI
068 * components as cell editors:
069 * <ul>
070 * <li><code>{@link JCheckBox}</code></li>
071 * <li><code>{@link JComboBox}</code></li>
072 * <li><code>{@link JTextComponent}</code></li>
073 * </ul>
074 * @param row the row index of the cell.
075 * @param column the column index of the cell.
076 * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
077 * @see JTableCellWriter#startCellEditing(JTable, int, int)
078 */
079 public void startCellEditing(JTable table, int row, int column) {
080 cellWriterFor(table, row, column).startCellEditing(table, row, column);
081 }
082
083 /**
084 * Stops editing the given cell of the <code>{@link JTable}</code>. This method only supports the following GUI
085 * components as cell editors:
086 * <ul>
087 * <li><code>{@link JCheckBox}</code></li>
088 * <li><code>{@link JComboBox}</code></li>
089 * <li><code>{@link JTextComponent}</code></li>
090 * </ul>
091 * @param row the row index of the cell.
092 * @param column the column index of the cell.
093 * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
094 * @see JTableCellWriter#stopCellEditing(JTable, int, int)
095 */
096 public void stopCellEditing(JTable table, int row, int column) {
097 cellWriterFor(table, row, column).stopCellEditing(table, row, column);
098 }
099
100 /**
101 * Cancels editing the given cell of the <code>{@link JTable}</code>. This method only supports the following GUI
102 * components as cell editors:
103 * <ul>
104 * <li><code>{@link JCheckBox}</code></li>
105 * <li><code>{@link JComboBox}</code></li>
106 * <li><code>{@link JTextComponent}</code></li>
107 * </ul>
108 * @param row the row index of the cell.
109 * @param column the column index of the cell.
110 * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
111 * @see JTableCellWriter#cancelCellEditing(JTable, int, int)
112 */
113 public void cancelCellEditing(JTable table, int row, int column) {
114 cellWriterFor(table, row, column).cancelCellEditing(table, row, column);
115 }
116
117 private JTableCellWriter cellWriterFor(JTable table, int row, int column) {
118 Component editor = editorForCell(table, row, column);
119 if (editor instanceof JCheckBox) return checkBoxWriter;
120 if (editor instanceof JComboBox) return comboBoxWriter;
121 if (editor instanceof JTextComponent) return textComponentWriter;
122 throw cannotFindOrActivateEditor(row, column);
123 }
124 }