Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.util.Arrays.format;
 import static org.assertj.core.util.Strings.concat;
 import static org.assertj.core.util.Strings.quote;
 
 
 
 
 import  com.jidesoft.combobox.AbstractComboBox;
 import  com.jidesoft.combobox.ListComboBox;
 import  com.jidesoft.combobox.PopupPanel;
 import  com.jidesoft.converter.ConverterContext;
 import  com.jidesoft.converter.ObjectConverter;

A driver to allow us to interact with a com.jidesoft.combobox.ListComboBox.

Author(s):
Peter Murray
 
 public class ListComboBoxDriver extends AbstractComboBoxDriver {
 
   private final JListDriver _listDriver;
 
 
   public ListComboBoxDriver(Robot robot) {
 	super(robot);
 	 = new JListDriver(robot);
   }
 
   public JList getList(ListComboBox comboBox) {
 	PopupPanel panel = showPopup(comboBox);
 	return .findDropDownList(panel);
   }
 
   public void requireItemCount(final ListComboBox comboBoxint expected) {
 	GuiQuery<Integerquery = new GuiQuery<Integer>() {
 	  @Override
 	  protected Integer executeInEDT() throws Throwable {
 		return comboBox.getModel().getSize();
 	  }
 	};
 	Integer count = GuiActionRunner.execute(query);
 	assertThat(count).as("ItemCount").isEqualTo(expected);
   }

  
Selects the item under the given index in the ListComboBox.

Parameters:
comboBox the target ListComboBox.
index the given index.
Throws:
IllegalStateException if the JComboBox is disabled.
IllegalStateException if the JComboBox is not showing on the screen.
IndexOutOfBoundsException if the given index is negative or greater than the index of the last item in the JComboBox.
 
   public void selectItem(final ListComboBox comboBoxint index) {
 	validateCanSelectItem(comboBoxindex);
 	selectItemAtIndex(comboBoxindex);
 	hidePopupIfVisible(comboBox);
   }
 
   public void requireText(final ListComboBox comboBoxString value) {
 	GuiQuery<Stringquery = new GuiQuery<String>() {
 	  @Override
 	  protected String executeInEDT() throws Throwable {
 		Component editorComp = comboBox.getEditor().getEditorComponent();
 		if (editorComp instanceof JTextComponent) {
 		  return ((JTextComponenteditorComp).getText();
 		}
		return null;
	  }
	};
	String actualValue = GuiActionRunner.execute(query);
	assertThat(actualValue).isEqualTo(value);
  }

  
Selects the first item matching the given text in the ListComboBox.

Parameters:
comboBox the target ListComboBox.
toSelect the text to match
Throws:
LocationUnavailableException if an element matching the given text cannot be found.
  public void selectItem(ListComboBox comboBoxString toSelect) {
	if (areEqual(comboBoxconvertElementToString(comboBoxcomboBox.getSelectedItem()), toSelect)) {
	  return;
	}
	int itemCount = size(comboBox);
	for (int i = 0; i < itemCounti++) {
	  if (areEqual(comboBoxconvertElementToString(comboBoxitemAt(comboBoxi)), toSelect)) {
		selectItem(comboBoxi);
		return;
	  }
	}
	// While actions are supposed to represent real user actions, it's possible that the current environment does not
	// match sufficiently, so we need to throw an appropriate exception that can be used to diagnose the problem.
	throw new LocationUnavailableException(concat("Unable to find item "quote(toSelect),
	                                              " among the ListComboBox contents ("format(contentsOf(comboBox)),
	                                              ")"));
  }

  
Returns the String representation of the element under the given index, using this driver's AbstractComboBoxCellReader.

Parameters:
comboBox the target AbstractComboBox.
index the given index.
Returns:
the value of the element under the given index.
Throws:
org.assertj.swing.exception.LocationUnavailableException if the given index is negative or greater than the index of the last item in the AbstractComboBox.
See also:
cellReader(AbstractComboBoxCellReader)
  public String value(AbstractComboBox comboBoxint index) {
	validateIndex(comboBoxindex);
	return .valueAt(comboBoxindex);
  }

  
Returns an array of Strings that represent the ListComboBox list. Note that the current selection might not be included, since it's possible to have a custom (edited) entry there that is not included in the default contents.

Parameters:
comboBox the target ListComboBox.
Returns:
an array of Strings that represent the ListComboBox list.
  public String[] contentsOf(ListComboBox comboBox) {
	int itemCount = size(comboBox);
	String[] items = new String[itemCount];
	for (int i = 0; i < itemCounti++) {
	  items[i] = itemAt(comboBoxi).toString();
	}
	return items;
  }
  public String[] getContents(ListComboBox target) {
	ComboBoxModel model = target.getModel();
	ConverterContext context = target.getConverterContext();
	ObjectConverter converter = target.getConverter();
	java.util.List<Stringcontents = new ArrayList<String>(model.getSize());
	for (int i = 0; i < model.getSize(); i++) {
	  if (converter == null) {
		contents.add(String.valueOf(model.getElementAt(i)));
	  } else {
		contents.add(converter.toString(model.getElementAt(i), context));
	  }
	}
	return contents.toArray(new String[contents.size()]);
  }
  public String getValueAsString(ListComboBox targetint index) {
	Object o = target.getModel().getElementAt(index);
	ObjectConverter converter = target.getConverter();
	if (converter == null) {
	  return String.valueOf(o);
	}
	return converter.toString(otarget.getConverterContext());
  }
  private void selectItemAtIndex(final ListComboBox comboBoxfinal int index) {
	if (!comboBox.isPopupVisible()) {
	  clickPopupButton(comboBox);
	}
	PopupPanel panel = showPopup(comboBox);
	JList dropDownList = .findDropDownList(panel);
	if (dropDownList != null) {
	  .selectItem(dropDownListindex);
else {
	  ListComboBoxSetSelectedIndexTask.setSelectedIndex(comboBoxindex);
	}
  }
  private static void validateCanSelectItem(final AbstractComboBox comboBoxfinal int index) {
	GuiActionRunner.execute(new GuiTask() {
	  protected void executeInEDT() {
		ComponentPreconditions.checkEnabledAndShowing(comboBox);
		AbstractComboBoxItemIndexValidator.validateIndex(comboBoxindex);
	  }
	});
  }
  private void hidePopupIfVisible(AbstractComboBox comboBox) {
  }
New to GrepCode? Check out our FAQ X