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

org.assertj.swing.jide.grids.driver.AbstractComboBoxCellReader Maven / Gradle / Ivy

/**
 * 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-2015 the original author or authors.
 */
package org.assertj.swing.jide.grids.driver;

import com.jidesoft.combobox.AbstractComboBox;
import com.jidesoft.converter.ObjectConverterManager;

/**
 * A CellReader for extracting a String representation of a value in an {@link AbstractComboBox}.
 * 
 * @author Peter Murray
 */
public class AbstractComboBoxCellReader {

  /**
   * Returns the internal value of a cell in a {@link ListComboBox} as expected
   * in a test. This method first tries to get the value from the toString implementation of the object
   * stored in the JComboBox's model at the
   * specified index. If it fails, it returns the value displayed in the ListComboBox's cell renderer.
   * 
   * @param comboBox the given ListComboBox.
   * @param index the index of the cell.
   * @return the internal value of a cell in a ListComboBox as expected in a
   *         test.
   */
  public String valueAt(AbstractComboBox comboBox, int index) {
	final Object item = itemAt(comboBox, index);
	return valueAsText(comboBox, item);
  }

  public String valueAsText(AbstractComboBox comboBox, Object value) {
	if (value == null) {
	  return null;
	}

	if (comboBox.getConverter() != null) {
	  return comboBox.getConverter().toString(value, comboBox.getConverterContext());
	}
	return ObjectConverterManager.toString(value,
	                                       comboBox.getType(),
	                                       comboBox.getConverterContext());
  }

  private Object itemAt(AbstractComboBox comboBox, int index) {
	return comboBox.getModel().getElementAt(index);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy