All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.assertj.swing.driver.JTableComboBoxEditorCellWriter Maven / Gradle / Ivy

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2018 the original author or authors.
 */
package org.assertj.swing.driver;

import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.swing.driver.JComboBoxEditableQuery.isEditable;
import static org.assertj.swing.driver.JTableStopCellEditingTask.stopEditing;

import java.awt.Point;

import org.jetbrains.annotations.NotNull;
import javax.swing.JComboBox;
import javax.swing.JTable;

import org.assertj.swing.annotation.RunsInEDT;
import org.assertj.swing.core.Robot;

/**
 * {@link org.assertj.swing.cell.JTableCellWriter} that knows how to use {@code JComboBox}es as cell editors.
 *
 * @author Alex Ruiz
 * @author Yvonne Wang
 */
public class JTableComboBoxEditorCellWriter extends AbstractJTableCellWriter {
  private final JComboBoxDriver driver;

  public JTableComboBoxEditorCellWriter(@NotNull Robot robot) {
    super(robot);
    driver = new JComboBoxDriver(robot);
  }

  @RunsInEDT
  @Override
  public void enterValue(@NotNull JTable table, int row, int column, @NotNull String value) {
    JComboBox editor = doStartCellEditing(table, row, column);
    selectOrType(editor, value);
    stopEditing(table, row, column);
  }

  private void selectOrType(@NotNull JComboBox editor, @NotNull String value) {
    boolean selectValue = !isEditable(editor);
    if (!selectValue) {
      selectValue = newArrayList(driver.contentsOf(editor)).contains(value);
    }
    if (selectValue) {
      driver.selectItem(editor, value);
      return;
    }
    driver.replaceText(editor, value);
  }

  @Override
  @RunsInEDT
  public void startCellEditing(@NotNull JTable table, int row, int column) {
    doStartCellEditing(table, row, column);
  }

  @RunsInEDT
  private JComboBox doStartCellEditing(@NotNull JTable table, int row, int column) {
    Point cellLocation = cellLocation(table, row, column, location());
    robot.click(table, cellLocation); // activate JComboBox editor
    JComboBox comboBox = waitForEditorActivation(table, row, column);
    cellEditor(cellEditor(table, row, column));
    return comboBox;
  }

  @RunsInEDT
  private JComboBox waitForEditorActivation(@NotNull JTable table, int row, int column) {
    return waitForEditorActivation(table, row, column, JComboBox.class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy