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.api.Assertions.fail;
 
 
 
 
 import  com.jidesoft.combobox.AbstractComboBox;
 import  com.jidesoft.combobox.PopupPanel;
 import  com.jidesoft.converter.ConverterContext;
 import  com.jidesoft.converter.ObjectConverter;

A driver for an com.jidesoft.combobox.AbstractComboBox. This is loosely based on the org.assertj.swing.driver.JComboBoxDriver class so a familiar usage pattern will exist between Swing combo boxes and Jide combo boxes.

Author(s):
Peter Murray
 
 public class AbstractComboBoxDriver extends JComponentDriver {
 
   private static final String EDITABLE_PROPERTY = "editable";
 
   private static final String SELECTED_INDEX_PROPERTY = "selectedIndex";
 
 
     super(robot);
   }

  
Simulates a user entering the specified text in the com.jidesoft.combobox.AbstractComboBox, replacing any text. This action is executed only if the com.jidesoft.combobox.AbstractComboBox is editable.

Parameters:
comboBox the target AbstractComboBox.
text the text to enter.
Throws:
IllegalStateException if the AbstractComboBox is disabled.
IllegalStateException if the AbstractComboBox is not showing on the screen.
IllegalStateException if the AbstractComboBox is not editable.
 
   public void replaceText(AbstractComboBox comboBoxString text) {
     selectAllText(comboBox);
     enterText(comboBoxtext);
   }

  
Simulates a user selecting the text in the com.jidesoft.combobox.AbstractComboBox. This action is executed only if the com.jidesoft.combobox.AbstractComboBox is editable.

Parameters:
comboBox the target AbstractComboBox.
Throws:
IllegalStateException if the AbstractComboBox is disabled.
IllegalStateException if the AbstractComboBox is not showing on the screen.
IllegalStateException if the AbstractComboBox is not editable.
 
   public void selectAllText(AbstractComboBox comboBox) {
     Component editor = accessibleEditorOf(comboBox);
     if (!(editor instanceof JComponent)) {
       return;
     }
     focus(editor);
   }
 
   private static Component accessibleEditorOf(final AbstractComboBox comboBox) {
    return org.assertj.swing.edt.GuiActionRunner.execute(new GuiQuery<Component>() {
      @Override
      protected Component executeInEDT() {
        AbstractComboBoxAccessibleEditorValidator.validateEditorIsAccessible(comboBox);
        return comboBox.getEditor().getEditorComponent();
      }
    });
  }

  
We need to cater for the editor component if we are editable. When typing into an editable AbstractComboBox, the focus will move to the Editor, not the ComboBox instance which will result in AssertJ-Swing failing on waiting for focus to move to the comboBox.

Parameters:
c the target component.
keyCodes one or more codes of the keys to press.
  public void pressAndReleaseKeys(Component cint... keyCodes) {
    Component interactionComponent = getInteractionComponent(c);
    .moveMouse(interactionComponent);
    .focus(interactionComponent);
    super.pressAndReleaseKeys(interactionComponentkeyCodes);
  }

  
We need to cater for the editor component if we are editable. When typing into an editable AbstractComboBox, the focus will move to the Editor, not the ComboBox instance which will result in AssertJ-Swing failing on waiting for focus to move to the comboBox.

Parameters:
c the target component.
keyPressInfo specifies the key and modifiers to press.
  public void pressAndReleaseKey(Component cKeyPressInfo keyPressInfo) {
    Component interactionComponent = getInteractionComponent(c);
    super.pressAndReleaseKey(interactionComponent,
                             keyPressInfo.keyCode(),
                             keyPressInfo.modifiers());
  }

  
We need to cater for the editor component if we are editable. When typing into an editable AbstractComboBox, the focus will move to the Editor, not the ComboBox instance which will result in AssertJ-Swing failing on waiting for focus to move to the comboBox.

Parameters:
c the target component.
keyCode the code of the key to press.
modifiers the given modifiers.
  public void pressAndReleaseKey(Component cint keyCodeint[] modifiers) {
    Component interactionComponent = getInteractionComponent(c);
    super.pressAndReleaseKey(interactionComponentkeyCodemodifiers);
  }

  
Simulates a user pressing given key on the Component.

Parameters:
c the target component.
keyCode the code of the key to press.
Throws:
IllegalArgumentException if the given code is not a valid key code.
IllegalStateException if the Component is disabled, or is not showing on the screen.
See also:
java.awt.event.KeyEvent
  public void pressKey(Component cint keyCode) {
    Component interactionComponent = getInteractionComponent(c);
    super.pressKey(interactionComponentkeyCode);
  }

  
Simulates a user releasing the given key on the Component.

Parameters:
c the target component.
keyCode the code of the key to release.
Throws:
IllegalArgumentException if the given code is not a valid key code.
IllegalStateException if the Component is disabled, or is not showing on the screen.
See also:
java.awt.event.KeyEvent
  public void releaseKey(Component cint keyCode) {
    Component interactionComponent = getInteractionComponent(c);
    super.releaseKey(interactionComponentkeyCode);
  }
  public void requireSelection(AbstractComboBox comboBoxString value) {
    String selectedItem = .valueAsText(comboBoxcomboBox.getSelectedItem());
    if (selectedItem == null) {
      fail(org.assertj.core.util.Strings.concat("[",
                                                selectedIndexProperty(comboBox),
                                                "] No selection"));
    }
    assertThat(selectedItem).as(selectedIndexProperty(comboBox)).isEqualTo(value);
  }

  
Simulates a user entering the specified text in the com.jidesoft.combobox.AbstractComboBox. This action is executed only if the com.jidesoft.combobox.AbstractComboBox is editable.

Parameters:
comboBox the target AbstractComboBox.
text the text to enter.
Throws:
IllegalStateException if the AbstractComboBox is disabled.
IllegalStateException if the AbstractComboBox is not editable or is not showing on the screen.
  public void enterText(AbstractComboBox comboBoxString text) {
    focus(comboBox);
    .enterText(text);
  }
  private static void inEdtValidateEditorIsAccessible(final AbstractComboBox comboBox) {
    org.assertj.swing.edt.GuiActionRunner.execute(new GuiTask() {
      @Override
      protected void executeInEDT() {
        AbstractComboBoxAccessibleEditorValidator.validateEditorIsAccessible(comboBox);
      }
    });
  }

  
Asserts that the given com.jidesoft.combobox.AbstractComboBox is editable.

Parameters:
comboBox the target AbstractComboBox.
Throws:
AssertionError if the AbstractComboBox is not editable.
  public void requireEditable(final AbstractComboBox comboBox) {
    assertEditable(comboBoxtrue);
  }

  
Asserts that the given com.jidesoft.combobox.AbstractComboBox is not editable.

Parameters:
comboBox the given AbstractComboBox.
Throws:
AssertionError if the AbstractComboBox is editable.
  public void requireNotEditable(AbstractComboBox comboBox) {
    assertEditable(comboBoxfalse);
  }
  public void requireNotEnabled(AbstractComboBox comboBox) {
    assertEnabled(comboBoxfalse);
  }
  public void requireEnabled(AbstractComboBox comboBox) {
    assertEnabled(comboBoxtrue);
  }
  private void assertEditable(AbstractComboBox comboBoxboolean expected) {
    assertThat(AbstractComboBoxEditableQuery.isEditable(comboBox))
                                                                  .as(editableProperty(comboBox)).isEqualTo(expected);
  }
  private void assertEnabled(AbstractComboBox comboBoxboolean expected) {
    assertThat(ComponentEnabledQuery.isEnabled(comboBox))
                                                         .as(editableProperty(comboBox)).isEqualTo(expected);
  }
  private static Description editableProperty(AbstractComboBox comboBox) {
    return propertyName(comboBox);
  }
  public String getStringRepresentation(AbstractComboBox boxObject o) {
    ObjectConverter converter = box.getConverter();
    ConverterContext context = box.getConverterContext();
    if (converter == null) {
      return String.valueOf(o);
    }
    return converter.toString(ocontext);
  }
  AbstractComboBox.EditorComponent getEditor(final AbstractComboBox comboBox) {
    GuiQuery<AbstractComboBox.EditorComponent> query = new GuiQuery<AbstractComboBox.EditorComponent>() {
      @Override
      protected AbstractComboBox.EditorComponent executeInEDT() throws Throwable {
        return (AbstractComboBox.EditorComponent) comboBox.getEditor();
      }
    };
    return GuiActionRunner.execute(query);
  }
  public String getEditorText(final AbstractComboBox comboBox) {
    GuiQuery<Stringquery = new GuiQuery<String>() {
      @Override
      protected String executeInEDT() throws Throwable {
        AbstractComboBox.EditorComponent editorComp =
            (AbstractComboBox.EditorComponent) comboBox.getEditor();
        return editorComp.getText();
      }
    };
    return GuiActionRunner.execute(query);
  }
  public void requireEditorText(final AbstractComboBox comboBoxString requiredText) {
    String currentText = getEditorText(comboBox);
    assertThat(currentText).as("Editor Text Value").isEqualTo(requiredText);
  }
  PopupPanel showPopup(final AbstractComboBox comboBox) {
    return comboBox.getPopupPanel();
  }
  public void requireNoSelection(AbstractComboBox comboBox) {
    Object selectedItem = comboBox.getSelectedItem();
    if (selectedItem != null) {
      fail(Strings.concat("No selection is required, but selection value is, ",
                          selectedItem));
    }
  }
  void dropDownVisibleThroughComponent(AbstractComboBox comboBoxboolean visible) {
    if (visible) {
      if (!comboBox.isPopupVisible()) {
        clickPopupButton(comboBox);
      }
    } else {
      if (comboBox.isPopupVisible()) {
        comboBox.hidePopup();
      }
    }
  }
  int validateIndex(AbstractComboBox comboBoxint index) {
    int itemCount = size(comboBox);
    if (index >= 0 && index < itemCount) {
      return index;
    }
    throw new LocationUnavailableException(Strings.concat("Item index (",
                                                          java.lang.String.valueOf(index),
                                                          ") should be between [0] and [",
                                                          String.valueOf(itemCount - 1),
                                                          "] (inclusive)"));
  }

  
Converts an object that may be present in the ComboBox into a String representation.

Parameters:
comboBox The comboBox to convert the object for.
o The object to convert into a String.
Returns:
The object passed in converted in to a String so that it would appear as it would to a user selecting the value in the ComboBox.
  protected String convertElementToString(AbstractComboBox comboBoxObject o) {
    if (o == null) {
      return null;
    }
    if (o instanceof String) {
      return (Stringo;
    }
    ObjectConverter converter = comboBox.getConverter();
    if (converter != null) {
      return converter.toString(ocomboBox.getConverterContext());
    }
    return o.toString();
  }
  protected boolean isDropDownVisible(AbstractComboBox comboBox) {
    return comboBox.isPopupVisible();
  }
  protected int size(AbstractComboBox comboBox) {
    return comboBox.getModel().getSize();
  }
  protected Object itemAt(AbstractComboBox comboBoxint index) {
    return comboBox.getModel().getElementAt(index);
  }
  protected boolean areEqual(AbstractComboBox comboObject o1Object o2) {
    String s1 = getStringRepresentation(comboo1);
    String s2 = getStringRepresentation(comboo2);
    return s1.equals(s2);
  }
  protected String selectedIndexProperty(AbstractComboBox comboBox) {
    return propertyName(comboBox).toString();
  }

  
Updates the implementation of AbstractComboBoxCellReader to use when comparing internal values of a com.jidesoft.combobox.ListComboBox and the values expected in a test.

Parameters:
newCellReader the new ListComboBoxCellValueReader to use.
Throws:
IllegalArgumentException if newCellReader is null.
  public void cellReader(AbstractComboBoxCellReader newCellReader) {
    // TODO move this back to CommonConditions.validateCellReader()
    // validateCellReader(newCellReader);
    if (newCellReader == null) {
      throw new NullPointerException("Cell reader should not be null");
    }
     = newCellReader;
  }
  public void clickPopupButton(final AbstractComboBox comboBox) {
    AbstractButton button = comboBox.getPopupButton();
    .moveMouse(button);
    .click(button);
  }

  
Obtains the component that the user will interact with, which could be the actual combo box, or the editor component, depending upon the state of the combobox.

Parameters:
c The Component to obtain the interactive component from.
Returns:
The java.awt.Component that the user is to interact with.
    if (c instanceof AbstractComboBox) {
      AbstractComboBox combo = (AbstractComboBox) c;
      if (combo.isEditable()) {
        ComboBoxEditor editor = combo.getEditor();
        return editor.getEditorComponent();
      }
    }
    return c;
  }
  public void requirePopupVisible(final AbstractComboBox targetboolean visible) {
    GuiQuery<Booleanquery = new GuiQuery<Boolean>() {
      @Override
      protected Boolean executeInEDT() throws Throwable {
        return target.isPopupVisible();
      }
    };
    Boolean isVisible = GuiActionRunner.execute(query);
    assertThat(isVisible).as("Popup Visibility").isEqualTo(visible);
  }
New to GrepCode? Check out our FAQ X