|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JComboBoxMouseEventData.java | 73.8% | 82.9% | 82.8% | 80.9% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import java.util.EventListener;
|
|
| 4 |
import javax.accessibility.Accessible;
|
|
| 5 |
|
|
| 6 |
import java.awt.AWTEvent;
|
|
| 7 |
import java.awt.Component;
|
|
| 8 |
import java.awt.Point;
|
|
| 9 |
import java.awt.Rectangle;
|
|
| 10 |
import java.awt.event.MouseEvent;
|
|
| 11 |
import javax.swing.AbstractListModel;
|
|
| 12 |
import javax.swing.JButton;
|
|
| 13 |
import javax.swing.JComboBox;
|
|
| 14 |
import javax.swing.JList;
|
|
| 15 |
import javax.swing.ListModel;
|
|
| 16 |
import javax.swing.event.ListDataListener;
|
|
| 17 |
import javax.swing.plaf.ComboBoxUI;
|
|
| 18 |
import javax.swing.plaf.basic.BasicComboPopup;
|
|
| 19 |
|
|
| 20 |
import org.w3c.dom.Element;
|
|
| 21 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
| 22 |
import junit.extensions.jfcunit.finder.ComponentFinder;
|
|
| 23 |
import junit.extensions.jfcunit.finder.Finder;
|
|
| 24 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
/**
|
|
| 28 |
* Data container class that holds all the data necessary for jfcUnit to fire mouse events.
|
|
| 29 |
* This class is specific to events on a {@link javax.swing.JComboBox}.
|
|
| 30 |
*
|
|
| 31 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 32 |
*/
|
|
| 33 |
public class JComboBoxMouseEventData extends AbstractMouseEventData { |
|
| 34 |
/**
|
|
| 35 |
* The {@link JComboBox} on which to trigger the event.
|
|
| 36 |
*/
|
|
| 37 |
private JComboBox m_comboBox;
|
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* The list view associated with the {@link JComboBox}.
|
|
| 41 |
*/
|
|
| 42 |
private JList m_listView;
|
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 46 |
*/
|
|
| 47 |
private int m_elementIndex; |
|
| 48 |
|
|
| 49 |
/**
|
|
| 50 |
* Constructor.
|
|
| 51 |
*/
|
|
| 52 | 14 |
public JComboBoxMouseEventData() {
|
| 53 | 14 |
super();
|
| 54 | 14 |
setValid(false);
|
| 55 |
} |
|
| 56 |
|
|
| 57 |
/**
|
|
| 58 |
* Constructor.
|
|
| 59 |
*
|
|
| 60 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 61 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 62 |
* @param element The specific element on which to trigger the event.
|
|
| 63 |
* @param numberOfClicks
|
|
| 64 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 65 |
*/
|
|
| 66 | 12 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 67 |
final JComboBox comboBox, final Object element, final int numberOfClicks) {
|
|
| 68 | 12 |
this(testCase, comboBox, element, numberOfClicks, DEFAULT_SLEEPTIME);
|
| 69 |
} |
|
| 70 |
|
|
| 71 |
/**
|
|
| 72 |
* Constructor.
|
|
| 73 |
*
|
|
| 74 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 75 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 76 |
* @param element The specific element on which to trigger the event.
|
|
| 77 |
* @param numberOfClicks
|
|
| 78 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 79 |
* @param sleepTime
|
|
| 80 |
* The wait time in ms between each event.
|
|
| 81 |
*/
|
|
| 82 | 12 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 83 |
final JComboBox comboBox, final Object element, |
|
| 84 |
final int numberOfClicks, final long sleepTime) { |
|
| 85 | 12 |
this(testCase, comboBox, element, numberOfClicks,
|
| 86 |
DEFAULT_MOUSE_MODIFIERS, DEFAULT_ISPOPUPTRIGGER, sleepTime); |
|
| 87 |
} |
|
| 88 |
|
|
| 89 |
/**
|
|
| 90 |
* Constructor.
|
|
| 91 |
*
|
|
| 92 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 93 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 94 |
* @param element The specific element on which to trigger the event.
|
|
| 95 |
* @param numberOfClicks
|
|
| 96 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 97 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 98 |
* @param isPopupTrigger
|
|
| 99 |
* boolean specifying whether this event will show a popup.
|
|
| 100 |
* @param sleepTime
|
|
| 101 |
* The wait time in ms between each event.
|
|
| 102 |
*/
|
|
| 103 | 12 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 104 |
final JComboBox comboBox, final Object element, |
|
| 105 |
final int numberOfClicks, final int modifiers, |
|
| 106 |
final boolean isPopupTrigger, final long sleepTime) { |
|
| 107 | 12 |
this(testCase, comboBox, element, numberOfClicks, modifiers,
|
| 108 |
isPopupTrigger, sleepTime, DEFAULT_POSITION, null);
|
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* Constructor.
|
|
| 113 |
*
|
|
| 114 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 115 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 116 |
* @param element The specific element on which to trigger the event.
|
|
| 117 |
* @param numberOfClicks
|
|
| 118 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 119 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 120 |
* @param isPopupTrigger
|
|
| 121 |
* boolean specifying whether this event will show a popup.
|
|
| 122 |
* @param sleepTime
|
|
| 123 |
* The wait time in ms between each event.
|
|
| 124 |
* @param referencePoint
|
|
| 125 |
* The CUSTOM mouse position within the cell.
|
|
| 126 |
*/
|
|
| 127 | 0 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 128 |
final JComboBox comboBox, final Object element, |
|
| 129 |
final int numberOfClicks, final int modifiers, |
|
| 130 |
final boolean isPopupTrigger, final long sleepTime, |
|
| 131 |
final Point referencePoint) {
|
|
| 132 | 0 |
this(testCase, comboBox, element, numberOfClicks, modifiers,
|
| 133 |
isPopupTrigger, sleepTime, CUSTOM, referencePoint); |
|
| 134 |
} |
|
| 135 |
|
|
| 136 |
/**
|
|
| 137 |
* Constructor.
|
|
| 138 |
*
|
|
| 139 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 140 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 141 |
* @param element The specific element on which to trigger the event.
|
|
| 142 |
* @param numberOfClicks
|
|
| 143 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 144 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 145 |
* @param isPopupTrigger
|
|
| 146 |
* boolean specifying whether this event will show a popup.
|
|
| 147 |
* @param sleepTime
|
|
| 148 |
* The wait time in ms between each event.
|
|
| 149 |
* @param position The relative mouse position within the cell.
|
|
| 150 |
*/
|
|
| 151 | 0 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 152 |
final JComboBox comboBox, final Object element, |
|
| 153 |
final int numberOfClicks, final int modifiers, |
|
| 154 |
final boolean isPopupTrigger, final long sleepTime, final int position) { |
|
| 155 | 0 |
this(testCase, comboBox, element, numberOfClicks, modifiers,
|
| 156 |
isPopupTrigger, sleepTime, position, null);
|
|
| 157 |
} |
|
| 158 |
|
|
| 159 |
/**
|
|
| 160 |
* Constructor.
|
|
| 161 |
*
|
|
| 162 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 163 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 164 |
* @param element The specific element on which to trigger the event.
|
|
| 165 |
* @param numberOfClicks
|
|
| 166 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 167 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 168 |
* @param isPopupTrigger
|
|
| 169 |
* boolean specifying whether this event will show a popup.
|
|
| 170 |
* @param sleepTime
|
|
| 171 |
* The wait time in ms between each event.
|
|
| 172 |
* @param position The relative mouse position within the cell.
|
|
| 173 |
* @param referencePoint
|
|
| 174 |
* If position is CUSTOM then the point is a offset from
|
|
| 175 |
* the location of the {@link Component}. If the position is PERCENT
|
|
| 176 |
* then the location is a percentage offset of the hight and width.
|
|
| 177 |
* Otherwise, the referencePoint is unused.
|
|
| 178 |
*/
|
|
| 179 | 12 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 180 |
final JComboBox comboBox, final Object element, |
|
| 181 |
final int numberOfClicks, final int modifiers, |
|
| 182 |
final boolean isPopupTrigger, final long sleepTime, final int position, |
|
| 183 |
final Point referencePoint) {
|
|
| 184 | 12 |
this(testCase, comboBox,
|
| 185 |
getIndexOf(comboBox, element), numberOfClicks, modifiers, |
|
| 186 |
isPopupTrigger, sleepTime, position, referencePoint); |
|
| 187 |
} |
|
| 188 |
|
|
| 189 |
/**
|
|
| 190 |
* Constructor.
|
|
| 191 |
*
|
|
| 192 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 193 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 194 |
* @param elementIndex
|
|
| 195 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 196 |
* @param numberOfClicks
|
|
| 197 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 198 |
*/
|
|
| 199 | 22 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 200 |
final JComboBox comboBox, final int elementIndex,
|
|
| 201 |
final int numberOfClicks) {
|
|
| 202 | 22 |
this(testCase, comboBox, elementIndex, numberOfClicks, DEFAULT_SLEEPTIME);
|
| 203 |
} |
|
| 204 |
|
|
| 205 |
/**
|
|
| 206 |
* Constructor.
|
|
| 207 |
*
|
|
| 208 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 209 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 210 |
* @param elementIndex
|
|
| 211 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 212 |
* @param numberOfClicks
|
|
| 213 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 214 |
* @param sleepTime
|
|
| 215 |
* The wait time in ms between each event.
|
|
| 216 |
*/
|
|
| 217 | 24 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 218 |
final JComboBox comboBox, final int elementIndex,
|
|
| 219 |
final int numberOfClicks, final long sleepTime) { |
|
| 220 | 24 |
this(testCase, comboBox, elementIndex, numberOfClicks,
|
| 221 |
DEFAULT_MOUSE_MODIFIERS, DEFAULT_ISPOPUPTRIGGER, sleepTime); |
|
| 222 |
} |
|
| 223 |
|
|
| 224 |
/**
|
|
| 225 |
* Constructor.
|
|
| 226 |
*
|
|
| 227 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 228 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 229 |
* @param elementIndex
|
|
| 230 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 231 |
* @param numberOfClicks
|
|
| 232 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 233 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 234 |
* @param isPopupTrigger
|
|
| 235 |
* boolean specifying whether this event will show a popup.
|
|
| 236 |
* @param sleepTime
|
|
| 237 |
* The wait time in ms between each event.
|
|
| 238 |
*/
|
|
| 239 | 26 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 240 |
final JComboBox comboBox, final int elementIndex,
|
|
| 241 |
final int numberOfClicks, final int modifiers, |
|
| 242 |
final boolean isPopupTrigger, final long sleepTime) { |
|
| 243 | 26 |
this(testCase, comboBox, elementIndex, numberOfClicks, modifiers,
|
| 244 |
isPopupTrigger, sleepTime, DEFAULT_POSITION, null);
|
|
| 245 |
} |
|
| 246 |
|
|
| 247 |
/**
|
|
| 248 |
* Constructor.
|
|
| 249 |
*
|
|
| 250 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 251 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 252 |
* @param elementIndex
|
|
| 253 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 254 |
* @param numberOfClicks
|
|
| 255 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 256 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 257 |
* @param isPopupTrigger
|
|
| 258 |
* boolean specifying whether this event will show a popup.
|
|
| 259 |
* @param sleepTime
|
|
| 260 |
* The wait time in ms between each event.
|
|
| 261 |
* @param referencePoint
|
|
| 262 |
* The CUSTOM mouse position within the cell.
|
|
| 263 |
*/
|
|
| 264 | 2 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 265 |
final JComboBox comboBox, final int elementIndex,
|
|
| 266 |
final int numberOfClicks, final int modifiers, |
|
| 267 |
final boolean isPopupTrigger, final long sleepTime, |
|
| 268 |
final Point referencePoint) {
|
|
| 269 | 2 |
this(testCase, comboBox, elementIndex, numberOfClicks, modifiers,
|
| 270 |
isPopupTrigger, sleepTime, CUSTOM, referencePoint); |
|
| 271 |
} |
|
| 272 |
|
|
| 273 |
/**
|
|
| 274 |
* Constructor.
|
|
| 275 |
*
|
|
| 276 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 277 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 278 |
* @param elementIndex
|
|
| 279 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 280 |
* @param numberOfClicks
|
|
| 281 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 282 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 283 |
* @param isPopupTrigger
|
|
| 284 |
* boolean specifying whether this event will show a popup.
|
|
| 285 |
* @param sleepTime
|
|
| 286 |
* The wait time in ms between each event.
|
|
| 287 |
* @param position The relative mouse position within the cell.
|
|
| 288 |
*/
|
|
| 289 | 2 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 290 |
final JComboBox comboBox, final int elementIndex,
|
|
| 291 |
final int numberOfClicks, final int modifiers, |
|
| 292 |
final boolean isPopupTrigger, final long sleepTime, final int position) { |
|
| 293 | 2 |
this(testCase, comboBox, elementIndex, numberOfClicks, modifiers,
|
| 294 |
isPopupTrigger, sleepTime, position, null);
|
|
| 295 |
} |
|
| 296 |
|
|
| 297 |
/**
|
|
| 298 |
* Constructor.
|
|
| 299 |
*
|
|
| 300 |
* @param testCase The {@link JFCTestCase} on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 301 |
* @param comboBox The {@link JComboBox} on which to trigger the event.
|
|
| 302 |
* @param elementIndex
|
|
| 303 |
* The zero-based index of the specific element on which to trigger the event.
|
|
| 304 |
* @param numberOfClicks
|
|
| 305 |
* Number of clicks in the {@link MouseEvent} (1 for single-click, 2 for double clicks)
|
|
| 306 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 307 |
* @param isPopupTrigger
|
|
| 308 |
* boolean specifying whether this event will show a popup.
|
|
| 309 |
* @param sleepTime
|
|
| 310 |
* The wait time in ms between each event.
|
|
| 311 |
* @param position The relative mouse position within the cell.
|
|
| 312 |
* @param referencePoint
|
|
| 313 |
* If position is CUSTOM then the point is a offset from
|
|
| 314 |
* the location of the component. If the position is PERCENT
|
|
| 315 |
* then the location is a percentage offset of the hight and width.
|
|
| 316 |
* Otherwise, the referencePoint is unused.
|
|
| 317 |
*/
|
|
| 318 | 134 |
public JComboBoxMouseEventData(final JFCTestCase testCase,
|
| 319 |
final JComboBox comboBox, final int elementIndex,
|
|
| 320 |
final int numberOfClicks, final int modifiers, |
|
| 321 |
final boolean isPopupTrigger, final long sleepTime, final int position, |
|
| 322 |
final Point referencePoint) {
|
|
| 323 | 134 |
setTestCase(testCase); |
| 324 | 134 |
setSource(comboBox); |
| 325 | 134 |
setNumberOfClicks(numberOfClicks); |
| 326 | 134 |
setModifiers(modifiers); |
| 327 | 134 |
setPopupTrigger(isPopupTrigger); |
| 328 | 134 |
setElementIndex(elementIndex); |
| 329 | 134 |
setSleepTime(sleepTime); |
| 330 | 134 |
setPosition(position); |
| 331 | 134 |
setReferencePoint(referencePoint); |
| 332 | 134 |
setValid(true);
|
| 333 |
} |
|
| 334 |
|
|
| 335 |
/**
|
|
| 336 |
* Set the attribute value.
|
|
| 337 |
*
|
|
| 338 |
* @param comboBox The new value of the attribute.
|
|
| 339 |
*/
|
|
| 340 | 150 |
public final void setSource(final JComboBox comboBox) { |
| 341 | 150 |
m_comboBox = comboBox; |
| 342 |
} |
|
| 343 |
|
|
| 344 |
/**
|
|
| 345 |
* Get the attribute value.
|
|
| 346 |
*
|
|
| 347 |
* @return JComboBox The value of the attribute.
|
|
| 348 |
*/
|
|
| 349 | 175 |
public final JComboBox getSource() {
|
| 350 | 175 |
return m_comboBox;
|
| 351 |
} |
|
| 352 |
|
|
| 353 |
/**
|
|
| 354 |
* The component on which the event has to be fired.
|
|
| 355 |
*
|
|
| 356 |
* @return The component.
|
|
| 357 |
*/
|
|
| 358 | 46 |
public Component getComponent() {
|
| 359 |
// by default, the component is the same as the source
|
|
| 360 |
|
|
| 361 |
/** @todo this is a hack
|
|
| 362 |
* but should work for now to get the
|
|
| 363 |
* correct component to the recording. Should probably use a
|
|
| 364 |
* new interface.
|
|
| 365 |
*/
|
|
| 366 | 46 |
if (m_listView == null) { |
| 367 | 46 |
return getSource();
|
| 368 |
} |
|
| 369 |
|
|
| 370 | 0 |
return m_listView;
|
| 371 |
} |
|
| 372 |
|
|
| 373 |
/**
|
|
| 374 |
* Set the attribute value.
|
|
| 375 |
*
|
|
| 376 |
* @param elementIndex The new value of the attribute.
|
|
| 377 |
*/
|
|
| 378 | 150 |
public final void setElementIndex(final int elementIndex) { |
| 379 | 150 |
m_elementIndex = elementIndex; |
| 380 |
} |
|
| 381 |
|
|
| 382 |
/**
|
|
| 383 |
* Get the attribute value.
|
|
| 384 |
*
|
|
| 385 |
* @return int The value of the attribute.
|
|
| 386 |
*/
|
|
| 387 | 32 |
public int getElementIndex() { |
| 388 | 32 |
return m_elementIndex;
|
| 389 |
} |
|
| 390 |
|
|
| 391 |
/**
|
|
| 392 |
* Returns true if the event can be consumed by
|
|
| 393 |
* this instance of event data.
|
|
| 394 |
*
|
|
| 395 |
* @param ae Event to be consumed.
|
|
| 396 |
* @return true if the event was consumed.
|
|
| 397 |
*/
|
|
| 398 | 37 |
public boolean canConsume(final AWTEvent ae) { |
| 399 | 37 |
if (!super.canConsume(ae) || !(ae.getSource() instanceof JList)) { |
| 400 | 8 |
return false; |
| 401 |
} |
|
| 402 |
|
|
| 403 | 29 |
if (isValid()) {
|
| 404 | 8 |
JList source = (JList) ae.getSource(); |
| 405 | 8 |
int index = source.locationToIndex(
|
| 406 |
new Point(
|
|
| 407 |
((MouseEvent) ae).getX(), |
|
| 408 |
((MouseEvent) ae).getY())); |
|
| 409 |
|
|
| 410 | 8 |
if (index != getElementIndex()) {
|
| 411 | 0 |
return false; |
| 412 |
} |
|
| 413 |
} |
|
| 414 |
|
|
| 415 | 29 |
return isComboBox((JList) ae.getSource());
|
| 416 |
} |
|
| 417 |
|
|
| 418 |
/**
|
|
| 419 |
* Consume the event.
|
|
| 420 |
*
|
|
| 421 |
* @param ae AWTEvent to be consumed.
|
|
| 422 |
* @return boolean true if the event was consumed.
|
|
| 423 |
*/
|
|
| 424 | 24 |
public boolean consume(final AWTEvent ae) { |
| 425 | 24 |
if (super.consume(ae)) { |
| 426 | 8 |
return true; |
| 427 |
} |
|
| 428 |
|
|
| 429 | 16 |
MouseEvent me = (MouseEvent) ae; |
| 430 | 16 |
JList source = (JList) me.getSource(); |
| 431 | 16 |
setSource(getParentComboBox(source)); |
| 432 | 16 |
setModifiers(me.getModifiers()); |
| 433 | 16 |
setNumberOfClicks(me.getClickCount()); |
| 434 | 16 |
setPopupTrigger(me.isPopupTrigger()); |
| 435 |
|
|
| 436 | 16 |
Point p = new Point(
|
| 437 |
me.getX(), |
|
| 438 |
me.getY()); |
|
| 439 | 16 |
Point screen = source.getLocationOnScreen(); |
| 440 | 16 |
screen.translate(p.x, p.y); |
| 441 | 16 |
setLocationOnScreen(screen); |
| 442 | 16 |
setSleepTime(getDefaultSleepTime()); |
| 443 |
|
|
| 444 | 16 |
int index = source.locationToIndex(new Point( |
| 445 |
me.getX(), |
|
| 446 |
me.getY())); |
|
| 447 | 16 |
setElementIndex(index); |
| 448 |
|
|
| 449 | 16 |
setPosition(CENTER); |
| 450 | 16 |
setReferencePoint(null);
|
| 451 |
|
|
| 452 | 16 |
setValid(true);
|
| 453 |
|
|
| 454 | 16 |
return true; |
| 455 |
} |
|
| 456 |
|
|
| 457 |
/**
|
|
| 458 |
* Compare to event datas and determine if they are equal.
|
|
| 459 |
*
|
|
| 460 |
* @param o Object to be compared.
|
|
| 461 |
* @return true if the events are the same.
|
|
| 462 |
*/
|
|
| 463 | 11 |
public boolean equals(final Object o) { |
| 464 | 11 |
if (!super.equals(o)) { |
| 465 | 9 |
return false; |
| 466 |
} |
|
| 467 |
|
|
| 468 | 2 |
JComboBoxMouseEventData data = (JComboBoxMouseEventData) o; |
| 469 |
|
|
| 470 | 2 |
return (data.getSource() == getSource())
|
| 471 |
&& (data.getElementIndex() == getElementIndex()); |
|
| 472 |
} |
|
| 473 |
|
|
| 474 |
/**
|
|
| 475 |
* Return a hashCode for this object.
|
|
| 476 |
* @return int hashCode.
|
|
| 477 |
*/
|
|
| 478 | 0 |
public int hashCode() { |
| 479 | 0 |
return super.hashCode() + m_elementIndex; |
| 480 |
} |
|
| 481 |
|
|
| 482 |
/**
|
|
| 483 |
* Populate the JComboBoxMouseEventData XML element.
|
|
| 484 |
* @param e element to be populated.
|
|
| 485 |
*/
|
|
| 486 | 0 |
public void populate(final Element e) { |
| 487 | 0 |
super.populate(e);
|
| 488 | 0 |
e.setAttribute(JFCXMLConstants.TYPE, "JComboBoxMouseEventData");
|
| 489 | 0 |
e.setAttribute(JFCXMLConstants.INDEX, "" + getElementIndex());
|
| 490 |
} |
|
| 491 |
|
|
| 492 |
/**
|
|
| 493 |
* Prepare the {@link Component} to receive the event.
|
|
| 494 |
*
|
|
| 495 |
* @return true if the component is ready to receive the event.
|
|
| 496 |
*/
|
|
| 497 | 113 |
public boolean prepareComponent() { |
| 498 | 113 |
if (!isValidForProcessing(getSource())) {
|
| 499 | 1 |
return false; |
| 500 |
} |
|
| 501 |
|
|
| 502 | 112 |
JFCTestCase testCase = getTestCase(); |
| 503 |
|
|
| 504 | 112 |
if (testCase != null) { |
| 505 |
// try to clear the event queue
|
|
| 506 | 112 |
testCase.flushAWT(); |
| 507 |
} |
|
| 508 |
|
|
| 509 | 112 |
Component toClick = m_comboBox; |
| 510 | 112 |
int clicks = 1;
|
| 511 |
// If the combo box isEditable,
|
|
| 512 |
// Then we must click in the button to
|
|
| 513 |
// show the menu.
|
|
| 514 | 112 |
if (m_comboBox.isEditable()) {
|
| 515 | 23 |
Finder f = new ComponentFinder(JButton.class); |
| 516 | 23 |
f.setWait(0); |
| 517 | 23 |
Component comp = f.find(m_comboBox, 0); |
| 518 |
// If we have a button then use the button.
|
|
| 519 | 23 |
if (comp != null) { |
| 520 | 23 |
MouseEventData data = new MouseEventData(testCase, comp);
|
| 521 | 23 |
testCase.getHelper().enterClickAndLeave(data); |
| 522 |
} else {
|
|
| 523 |
// We will try to assume that the
|
|
| 524 |
// location to click is between the
|
|
| 525 |
// editor and the combobox.
|
|
| 526 | 0 |
Rectangle e = m_comboBox.getEditor().getEditorComponent(). |
| 527 |
getBounds(); |
|
| 528 | 0 |
Rectangle cb = m_comboBox.getBounds(); |
| 529 | 0 |
Rectangle button = calcButtonArea(e, cb); |
| 530 | 0 |
Point p = new Point((int) button.getCenterX(), |
| 531 |
(int) button.getCenterY());
|
|
| 532 | 0 |
MouseEventData data = new MouseEventData(testCase, toClick);
|
| 533 | 0 |
data.setPosition(MouseEventData.CUSTOM); |
| 534 | 0 |
data.setReferencePoint(p); |
| 535 | 0 |
testCase.getHelper().enterClickAndLeave(data); |
| 536 |
} |
|
| 537 |
} else {
|
|
| 538 |
// Click on the combo box to open the menu.
|
|
| 539 | 89 |
testCase.getHelper().enterClickAndLeave( |
| 540 |
new MouseEventData(testCase, toClick, clicks));
|
|
| 541 |
} |
|
| 542 |
|
|
| 543 |
// If the popup is not open, then we are done.
|
|
| 544 |
// Popup may not open if the combo box is not
|
|
| 545 |
// enabled.
|
|
| 546 | 112 |
if (!m_comboBox.isPopupVisible()) {
|
| 547 | 0 |
return false; |
| 548 |
} |
|
| 549 |
|
|
| 550 |
// m_comboBox.showPopup();
|
|
| 551 |
|
|
| 552 | 112 |
if (testCase != null) { |
| 553 |
// try to clear the event queue
|
|
| 554 | 112 |
testCase.flushAWT(); |
| 555 |
} |
|
| 556 |
|
|
| 557 | 112 |
BasicComboPopup bcp = (BasicComboPopup) m_comboBox.getAccessibleContext() |
| 558 |
.getAccessibleChild(0 /*popup*/);
|
|
| 559 |
|
|
| 560 | 112 |
if (testCase != null) { |
| 561 |
// try to clear the event queue
|
|
| 562 | 112 |
testCase.flushAWT(); |
| 563 |
} |
|
| 564 |
|
|
| 565 | 112 |
m_listView = bcp.getList(); |
| 566 |
|
|
| 567 | 112 |
testCase.getHelper().enterClickAndLeave( |
| 568 |
new JListMouseEventData(testCase, m_listView, m_elementIndex, 1));
|
|
| 569 |
|
|
| 570 |
|
|
| 571 |
|
|
| 572 | 112 |
return false; |
| 573 |
} |
|
| 574 |
|
|
| 575 |
/**
|
|
| 576 |
* Calculate the difference between the two
|
|
| 577 |
* rectangles. And use this as the button
|
|
| 578 |
* location.
|
|
| 579 |
*
|
|
| 580 |
* @param comboBox Rectangle from combo box.
|
|
| 581 |
* @param editor Rectangle from editor.
|
|
| 582 |
* @return Rectangle Remaining rectangle.
|
|
| 583 |
*/
|
|
| 584 | 0 |
private Rectangle calcButtonArea(final Rectangle comboBox, final Rectangle editor) {
|
| 585 | 0 |
return new Rectangle( |
| 586 |
editor.x + editor.width, editor.y, |
|
| 587 |
comboBox.width - editor.width, |
|
| 588 |
editor.height |
|
| 589 |
); |
|
| 590 |
} |
|
| 591 |
|
|
| 592 |
/**
|
|
| 593 |
* Get string description of event.
|
|
| 594 |
*
|
|
| 595 |
* @return String description of event.
|
|
| 596 |
*/
|
|
| 597 | 8 |
public String toString() {
|
| 598 | 8 |
if (!isValid()) {
|
| 599 | 0 |
return super.toString(); |
| 600 |
} |
|
| 601 |
|
|
| 602 | 8 |
StringBuffer buf = new StringBuffer(1000);
|
| 603 | 8 |
buf.append(super.toString());
|
| 604 | 8 |
buf.append(" index: " + getElementIndex());
|
| 605 |
|
|
| 606 | 8 |
return buf.toString();
|
| 607 |
} |
|
| 608 |
|
|
| 609 |
/**
|
|
| 610 |
* Get the parent combo box of the list.
|
|
| 611 |
*
|
|
| 612 |
* @param list List to check.
|
|
| 613 |
* @return true if combo box.
|
|
| 614 |
*/
|
|
| 615 | 29 |
private boolean isComboBox(final JList list) { |
| 616 | 29 |
Accessible ap = list.getAccessibleContext().getAccessibleParent(); |
| 617 |
|
|
| 618 | 29 |
while (ap != null) { |
| 619 | 92 |
if ((ap instanceof ComboBoxUI) |
| 620 |
|| (ap instanceof JComboBox)
|
|
| 621 |
|| (ap.getClass().getName().indexOf("ComboBoxUI") != -1)) {
|
|
| 622 | 24 |
return true; |
| 623 |
} |
|
| 624 |
|
|
| 625 | 68 |
ap = ap.getAccessibleContext().getAccessibleParent(); |
| 626 |
} |
|
| 627 |
|
|
| 628 | 5 |
return false; |
| 629 |
} |
|
| 630 |
|
|
| 631 |
/**
|
|
| 632 |
* Finds the index of the element passed in from the {@link JComboBox}.
|
|
| 633 |
*
|
|
| 634 |
* @param comboBox The JComboBox which holds the elements.
|
|
| 635 |
* @param element The element whose index has to be found out.
|
|
| 636 |
* @return int The zero-based index where the element occurs
|
|
| 637 |
* (-1 is returned if not found).
|
|
| 638 |
*/
|
|
| 639 | 12 |
private static int getIndexOf(final JComboBox comboBox, final Object element) { |
| 640 | 30 |
for (int i = 0; i < comboBox.getItemCount(); i++) { |
| 641 | 30 |
if ((comboBox.getItemAt(i) != null) |
| 642 |
&& comboBox.getItemAt(i).equals(element)) {
|
|
| 643 | 12 |
return i;
|
| 644 |
} |
|
| 645 |
} |
|
| 646 |
|
|
| 647 | 0 |
return -1;
|
| 648 |
} |
|
| 649 |
|
|
| 650 |
/**
|
|
| 651 |
* Get the parent combo box of the list.
|
|
| 652 |
* This is difficult since the JList is normally a
|
|
| 653 |
* inner class, and there is not a linkage to traverse
|
|
| 654 |
* outside the class.
|
|
| 655 |
*
|
|
| 656 |
* Here we will traverse the Listener list to get to the
|
|
| 657 |
* JComboBox as it should be listening to the list as well.
|
|
| 658 |
*
|
|
| 659 |
* @param list List to check
|
|
| 660 |
* @return JComboBox of parent.
|
|
| 661 |
*/
|
|
| 662 | 16 |
private JComboBox getParentComboBox(final JList list) {
|
| 663 | 16 |
ListModel ldm = list.getModel(); |
| 664 |
|
|
| 665 | 16 |
if (ldm instanceof AbstractListModel) { |
| 666 | 16 |
AbstractListModel cbm = (AbstractListModel) ldm; |
| 667 | 16 |
EventListener[] listeners = cbm.getListeners(ListDataListener.class);
|
| 668 |
|
|
| 669 | 64 |
for (int i = 0; i < listeners.length; i++) { |
| 670 | 64 |
if (listeners[i] instanceof JComboBox) { |
| 671 | 16 |
return (JComboBox) listeners[i];
|
| 672 |
} |
|
| 673 |
} |
|
| 674 |
} |
|
| 675 |
|
|
| 676 | 0 |
return null; |
| 677 |
} |
|
| 678 |
} |
|
| 679 |
|
|
||||||||||