|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 BREDEX GmbH. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* BREDEX GmbH - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jubula.rc.swing.swing.uiadapter; |
| 12 |
|
| 13 |
import java.awt.Component; |
| 14 |
import java.awt.Container; |
| 15 |
import java.awt.Dimension; |
| 16 |
import java.awt.Rectangle; |
| 17 |
import java.util.Arrays; |
| 18 |
|
| 19 |
import javax.accessibility.Accessible; |
| 20 |
import javax.accessibility.AccessibleContext; |
| 21 |
import javax.swing.ComboBoxEditor; |
| 22 |
import javax.swing.JButton; |
| 23 |
import javax.swing.JComboBox; |
| 24 |
import javax.swing.JList; |
| 25 |
import javax.swing.JPopupMenu; |
| 26 |
import javax.swing.SwingUtilities; |
| 27 |
|
| 28 |
import org.apache.commons.lang.Validate; |
| 29 |
import org.eclipse.jubula.rc.common.driver.ClickOptions; |
| 30 |
import org.eclipse.jubula.rc.common.driver.IRunnable; |
| 31 |
import org.eclipse.jubula.rc.common.exception.StepExecutionException; |
| 32 |
import org.eclipse.jubula.rc.common.implclasses.MatchUtil; |
| 33 |
import org.eclipse.jubula.rc.common.uiadapter.interfaces.IComboBoxAdapter; |
| 34 |
import org.eclipse.jubula.rc.swing.swing.caps.CapUtil; |
| 35 |
import org.eclipse.jubula.rc.swing.swing.implclasses.JComboBoxHelper; |
| 36 |
import org.eclipse.jubula.rc.swing.swing.implclasses.JComboBoxImplClass; |
| 37 |
import org.eclipse.jubula.tools.objects.event.EventFactory; |
| 38 |
import org.eclipse.jubula.tools.objects.event.TestErrorEvent; |
| 39 |
/** |
| 40 |
* Implementation of the Interface <code>IComboBoxAdapter</code> as a |
| 41 |
* adapter for the <code>JComboBox</code> component. |
| 42 |
* @author BREDEX GmbH |
| 43 |
* |
| 44 |
*/ |
| 45 |
public class JComboBoxAdapter extends WidgetAdapter implements |
| 46 |
IComboBoxAdapter { |
| 47 |
/** |
| 48 |
* <code>INVALID_MAX_WIDTH</code> |
| 49 |
*/ |
| 50 |
public static final int NO_MAX_WIDTH = -1; |
| 51 |
|
| 52 |
/** */ |
| 53 |
private JComboBox m_comboBox; |
| 54 |
/** |
| 55 |
* |
| 56 |
* @param objectToAdapt |
| 57 |
*/ |
| 58 |
public JComboBoxAdapter(Object objectToAdapt) { |
| 59 |
super(objectToAdapt); |
| 60 |
m_comboBox = (JComboBox) objectToAdapt; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritDoc} |
| 65 |
*/ |
| 66 |
public String getText() { |
| 67 |
String comboBoxText; |
| 68 |
if (m_comboBox.isEditable()) { |
| 69 |
comboBoxText = CapUtil.getRenderedText( |
| 70 |
getComboBoxEditorComponent(m_comboBox), true); |
| 71 |
} else { |
| 72 |
final int selIndex = m_comboBox.getSelectedIndex(); |
| 73 |
if (selIndex == -1) { |
| 74 |
comboBoxText = String.valueOf( |
| 75 |
m_comboBox.getSelectedItem()); |
| 76 |
} else { |
| 77 |
final JList jlist = new JList(m_comboBox.getModel()); |
| 78 |
Object o = getEventThreadQueuer().invokeAndWait( |
| 79 |
"getText", new IRunnable() { //$NON-NLS-1$ |
| 80 |
public Object run() { |
| 81 |
Component disp = m_comboBox.getRenderer() |
| 82 |
.getListCellRendererComponent(jlist, |
| 83 |
jlist.getModel().getElementAt(selIndex), |
| 84 |
selIndex, true, m_comboBox.hasFocus()); |
| 85 |
return CapUtil.getRenderedText(disp, false); |
| 86 |
} |
| 87 |
}); |
| 88 |
comboBoxText = String.valueOf(o); |
| 89 |
} |
| 90 |
} |
| 91 |
return comboBoxText; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritDoc} |
| 97 |
*/ |
| 98 |
public boolean isEditable() { |
| 99 |
Boolean editable = (Boolean)getEventThreadQueuer().invokeAndWait("isEditable", //$NON-NLS-1$ |
| 100 |
new IRunnable() { |
| 101 |
public Object run() { |
| 102 |
// see findBugs |
| 103 |
return m_comboBox.isEditable() |
| 104 |
? Boolean.TRUE : Boolean.FALSE; |
| 105 |
} |
| 106 |
}); |
| 107 |
return editable.booleanValue(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* {@inheritDoc} |
| 112 |
*/ |
| 113 |
public boolean containsValue(String value, String operator) { |
| 114 |
JListAdapter list = new JListAdapter(findJList()); |
| 115 |
return list.containsValue(value, operator); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* {@inheritDoc} |
| 120 |
*/ |
| 121 |
public boolean containsValue(String value) { |
| 122 |
return containsValue(value, MatchUtil.EQUALS); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* select the whole text of the textfield by clicking three times. |
| 127 |
*/ |
| 128 |
public void selectAll() { |
| 129 |
click(new Integer(1)); |
| 130 |
getRobot().keyStroke(getRobot().getSystemModifierSpec() + " A"); //$NON-NLS-1$ |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* {@inheritDoc} |
| 135 |
*/ |
| 136 |
public int getSelectedIndex() { |
| 137 |
Integer actual = (Integer)getEventThreadQueuer() |
| 138 |
.invokeAndWait( |
| 139 |
JComboBoxImplClass.class.getName() + ".getSelectedIndex", //$NON-NLS-1$ |
| 140 |
new IRunnable() { |
| 141 |
public Object run() { |
| 142 |
return new Integer(m_comboBox.getSelectedIndex()); |
| 143 |
} |
| 144 |
}); |
| 145 |
return actual.intValue(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* {@inheritDoc} |
| 150 |
*/ |
| 151 |
public void select(int index) { |
| 152 |
JListAdapter list = new JListAdapter(findJList()); |
| 153 |
list.clickOnIndex(new Integer(index), ClickOptions |
| 154 |
.create().setClickCount(1), getMaxWidth()); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Selects the specified item in the combobox. |
| 159 |
* @param values the values which should be (not) selected |
| 160 |
* @param operator if regular expressions are used |
| 161 |
* @param searchType Determines where the search begins ("relative" or "absolute") |
| 162 |
* @throws StepExecutionException if an error occurs during selecting the item |
| 163 |
* @throws IllegalArgumentException if <code>component</code> or <code>text</code> are null |
| 164 |
*/ |
| 165 |
public void select(final String[] values, String operator, |
| 166 |
String searchType) |
| 167 |
throws StepExecutionException, IllegalArgumentException { |
| 168 |
try { |
| 169 |
for (int i = 0; i < values.length; i++) { |
| 170 |
String text = values[i]; |
| 171 |
Validate.notNull(text, "text must not be null"); //$NON-NLS-1$ |
| 172 |
} |
| 173 |
JListAdapter list = new JListAdapter(findJList()); |
| 174 |
Integer[] indices = list.findIndicesOfValues(values, |
| 175 |
operator, searchType); |
| 176 |
Arrays.sort(indices); |
| 177 |
if (indices.length == 0) { |
| 178 |
throw new StepExecutionException("Text '" + Arrays.asList(values).toString() //$NON-NLS-1$ |
| 179 |
+ "' not found", //$NON-NLS-1$ |
| 180 |
EventFactory.createActionError(TestErrorEvent.NOT_FOUND)); |
| 181 |
} |
| 182 |
list.clickOnIndex(indices[0], ClickOptions |
| 183 |
.create().setClickCount(1), getMaxWidth()); |
| 184 |
} catch (StepExecutionException e) { |
| 185 |
m_comboBox.hidePopup(); |
| 186 |
throw e; |
| 187 |
} catch (IllegalArgumentException e) { |
| 188 |
m_comboBox.hidePopup(); |
| 189 |
throw e; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Inputs <code>text</code> to <code>component</code>.<br> |
| 195 |
* @param text the text to type in |
| 196 |
* @param replace whether to rplace the text or not |
| 197 |
* @throws StepExecutionException if an error occurs during typing <code>text</code> |
| 198 |
* @throws IllegalArgumentException if <code>component</code> or <code>text</code> are null |
| 199 |
*/ |
| 200 |
public void input(String text, boolean replace) |
| 201 |
throws StepExecutionException, IllegalArgumentException { |
| 202 |
|
| 203 |
Validate.notNull(text, "text must not be null"); //$NON-NLS-1$ |
| 204 |
Component editor = getComboBoxEditorComponent(m_comboBox); |
| 205 |
if (editor == null) { |
| 206 |
throw new StepExecutionException("could not find editor", //$NON-NLS-1$ |
| 207 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 208 |
} |
| 209 |
if (replace) { |
| 210 |
selectAll(); |
| 211 |
} |
| 212 |
getRobot().type(editor, text); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* performs a <code>count</code> -click on the textfield. |
| 217 |
* @param count the number of clicks |
| 218 |
*/ |
| 219 |
public void click(Integer count) { |
| 220 |
Component editor = getComboBoxEditorComponent(m_comboBox); |
| 221 |
if (editor == null) { |
| 222 |
throw new StepExecutionException("no editor found", //$NON-NLS-1$ |
| 223 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 224 |
} |
| 225 |
getRobot().click(editor, null, ClickOptions.create().setClickCount( |
| 226 |
count.intValue())); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @param component |
| 231 |
* the combobox |
| 232 |
* @return the editor used to render and edit the selected item in the |
| 233 |
* JComboBox field. |
| 234 |
* @throws StepExecutionException |
| 235 |
* if the editor comonent could not be found |
| 236 |
*/ |
| 237 |
private Component getComboBoxEditorComponent(JComboBox component) |
| 238 |
throws StepExecutionException { |
| 239 |
|
| 240 |
ComboBoxEditor cbe = component.getEditor(); |
| 241 |
if (cbe == null) { |
| 242 |
throw new StepExecutionException("no ComboBoxEditor found", //$NON-NLS-1$ |
| 243 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 244 |
} |
| 245 |
Component c = cbe.getEditorComponent(); |
| 246 |
if (c == null) { |
| 247 |
throw new StepExecutionException("no EditorComponent found", //$NON-NLS-1$ |
| 248 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 249 |
} |
| 250 |
return c; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Finds the <code>JList</code> of the combobox. |
| 255 |
* @return The list |
| 256 |
*/ |
| 257 |
private JList findJList() { |
| 258 |
JList list = (JList)getComponentViaHierarchy(openPopupMenu(), |
| 259 |
JList.class); |
| 260 |
if (list == null) { |
| 261 |
throw new StepExecutionException("list component not found", //$NON-NLS-1$ |
| 262 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 263 |
} |
| 264 |
return list; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Opens the combobox popup menu and returns the popup instance. May also be |
| 269 |
* called if the popup is already visible |
| 270 |
* @return The popup menu |
| 271 |
*/ |
| 272 |
private JPopupMenu openPopupMenu() { |
| 273 |
if (!isPopupVisible()) { |
| 274 |
Component c = getComponentViaHierarchy(m_comboBox, JButton.class); |
| 275 |
Rectangle r = null; |
| 276 |
if ((c == null) && (!m_comboBox.isEditable())) { |
| 277 |
c = m_comboBox; |
| 278 |
} else if ((c == null) && (m_comboBox.isEditable())) { |
| 279 |
c = m_comboBox; |
| 280 |
r = findArrowIconArea(); |
| 281 |
} |
| 282 |
// if (log.isDebugEnabled()) { |
| 283 |
// log.debug("Opening popup by clicking on: " + c); //$NON-NLS-1$ |
| 284 |
// } |
| 285 |
getRobot().click(c, r); |
| 286 |
} |
| 287 |
if (!isPopupVisible()) { |
| 288 |
// log.debug("Dropdown list still not visible, must be an error"); //$NON-NLS-1$ |
| 289 |
throw new StepExecutionException("dropdown list not visible", //$NON-NLS-1$ |
| 290 |
EventFactory.createActionError( |
| 291 |
TestErrorEvent.DROPDOWN_LIST_NOT_FOUND)); |
| 292 |
} |
| 293 |
return getPopupMenu(m_comboBox); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Tries to find the popup menu from the combobox |
| 298 |
* @param component the combobox |
| 299 |
* @return the popup of the combobox |
| 300 |
* @throws StepExecutionException if the popup could not be found |
| 301 |
*/ |
| 302 |
private JPopupMenu getPopupMenu(JComboBox component) |
| 303 |
throws StepExecutionException { |
| 304 |
|
| 305 |
AccessibleContext ac = component.getAccessibleContext(); |
| 306 |
for (int i = 0; i < ac.getAccessibleChildrenCount(); i++) { |
| 307 |
Accessible a = ac.getAccessibleChild(i); |
| 308 |
if (a instanceof JPopupMenu) { |
| 309 |
return (JPopupMenu)a; |
| 310 |
} |
| 311 |
} |
| 312 |
throw new StepExecutionException("cannot find dropdown list", //$NON-NLS-1$ |
| 313 |
EventFactory.createActionError( |
| 314 |
TestErrorEvent.DROPDOWN_LIST_NOT_FOUND)); |
| 315 |
} |
| 316 |
/** |
| 317 |
* Tries to find the component in the component hierarchy |
| 318 |
* @param component where to search |
| 319 |
* @param c type of the component which should be found |
| 320 |
* @return the desired component |
| 321 |
*/ |
| 322 |
private Component getComponentViaHierarchy(Container component, Class c) { |
| 323 |
Component[] comps = component.getComponents(); |
| 324 |
for (int i = 0; i < comps.length; i++) { |
| 325 |
if (c.isInstance(comps[i])) { |
| 326 |
return comps[i]; |
| 327 |
} |
| 328 |
} |
| 329 |
for (int i = 0; i < comps.length; i++) { |
| 330 |
if (comps[i] instanceof Container) { |
| 331 |
Component ct = getComponentViaHierarchy((Container)comps[i], c); |
| 332 |
if (ct != null) { |
| 333 |
return ct; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
return null; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* @return true, if the popup of the combobox is visible |
| 342 |
*/ |
| 343 |
private boolean isPopupVisible() { |
| 344 |
Boolean visible = (Boolean)getEventThreadQueuer().invokeAndWait( |
| 345 |
JComboBoxHelper.class.getName() |
| 346 |
+ "isPopupVisible", new IRunnable() { //$NON-NLS-1$ |
| 347 |
public Object run() throws StepExecutionException { |
| 348 |
return m_comboBox.isPopupVisible() |
| 349 |
? Boolean.TRUE : Boolean.FALSE; |
| 350 |
} |
| 351 |
}); |
| 352 |
return visible.booleanValue(); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* @return a rectangle, where the arrow icon is expected. |
| 357 |
*/ |
| 358 |
private Rectangle findArrowIconArea() { |
| 359 |
JComboBox comboBox = m_comboBox; |
| 360 |
Component editor = getComboBoxEditorComponent(comboBox); |
| 361 |
Rectangle r = null; |
| 362 |
if (editor == null) { |
| 363 |
throw new StepExecutionException("could not find editor", //$NON-NLS-1$ |
| 364 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 365 |
} |
| 366 |
Rectangle ra[] = |
| 367 |
SwingUtilities.computeDifference(comboBox.getBounds(), |
| 368 |
editor.getBounds()); |
| 369 |
if ((ra == null) || (ra.length < 1)) { |
| 370 |
throw new StepExecutionException("could not arrow icon", //$NON-NLS-1$ |
| 371 |
EventFactory.createActionError(TestErrorEvent.COMP_NOT_FOUND)); |
| 372 |
} |
| 373 |
r = ra[0]; |
| 374 |
// find the largest area of the returned rectangles. |
| 375 |
double bestAreaIndex = Double.MAX_VALUE; |
| 376 |
for (int i = 0; i < ra.length; i++) { |
| 377 |
if ((ra[i].height > 0) && (ra[i].width > 0)) { |
| 378 |
double areaIndex = ((double)ra[i].width) / ra[i].height - 1.0; |
| 379 |
if (areaIndex < 0) { |
| 380 |
areaIndex *= (-1); |
| 381 |
} |
| 382 |
if (areaIndex < bestAreaIndex) { |
| 383 |
bestAreaIndex = areaIndex; |
| 384 |
r = ra[i]; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
return r; |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* @return the maximal width for the selection; -1 if none available |
| 393 |
* e.g. the preferred width of the combo box itself is 100 pixel although |
| 394 |
* the preferred size of the embedded items is more than two times bigger |
| 395 |
* --> click outside of component (JList) #3013 |
| 396 |
*/ |
| 397 |
private double getMaxWidth() { |
| 398 |
double maxWidth = NO_MAX_WIDTH; |
| 399 |
Dimension d = m_comboBox.getPreferredSize(); |
| 400 |
if (d != null) { |
| 401 |
maxWidth = d.getWidth(); |
| 402 |
} |
| 403 |
return maxWidth; |
| 404 |
} |
| 405 |
} |