|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JTableHeaderMouseEventData.java | 81.2% | 93.5% | 90.5% | 91.2% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.jfcunit.JFCTestCase;
|
|
| 4 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
| 5 |
|
|
| 6 |
import org.w3c.dom.Element;
|
|
| 7 |
|
|
| 8 |
import java.awt.AWTEvent;
|
|
| 9 |
import java.awt.Component;
|
|
| 10 |
import java.awt.Point;
|
|
| 11 |
import java.awt.Rectangle;
|
|
| 12 |
import java.awt.event.MouseEvent;
|
|
| 13 |
|
|
| 14 |
import javax.swing.table.JTableHeader;
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
/**
|
|
| 18 |
* Data container class that holds all the data necessary for jfcUnit to fire mouse events.
|
|
| 19 |
* This class is specific to events on a JTableHeader.
|
|
| 20 |
*
|
|
| 21 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 22 |
*/
|
|
| 23 |
public class JTableHeaderMouseEventData extends AbstractMouseEventData { |
|
| 24 |
/**
|
|
| 25 |
* The JTableHeader on which to trigger the event.
|
|
| 26 |
*/
|
|
| 27 |
private JTableHeader m_header;
|
|
| 28 |
|
|
| 29 |
/**
|
|
| 30 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 31 |
*/
|
|
| 32 |
private int m_columnIndex; |
|
| 33 |
|
|
| 34 |
/**
|
|
| 35 |
* Constructor.
|
|
| 36 |
*/
|
|
| 37 | 50 |
public JTableHeaderMouseEventData() {
|
| 38 | 50 |
super();
|
| 39 | 50 |
setValid(false);
|
| 40 |
} |
|
| 41 |
|
|
| 42 |
/**
|
|
| 43 |
* Constructor.
|
|
| 44 |
*
|
|
| 45 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 46 |
* @param header The component on which to trigger the event.
|
|
| 47 |
* @param columnIndex
|
|
| 48 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 49 |
* @param numberOfClicks
|
|
| 50 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 51 |
*/
|
|
| 52 | 82 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 53 |
final JTableHeader header, final int columnIndex,
|
|
| 54 |
final int numberOfClicks) {
|
|
| 55 | 82 |
this(testCase, header, columnIndex, numberOfClicks, DEFAULT_SLEEPTIME);
|
| 56 |
} |
|
| 57 |
|
|
| 58 |
/**
|
|
| 59 |
* Constructor.
|
|
| 60 |
*
|
|
| 61 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 62 |
* @param header The component on which to trigger the event.
|
|
| 63 |
* @param columnIndex
|
|
| 64 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 65 |
* @param numberOfClicks
|
|
| 66 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 67 |
* @param sleepTime
|
|
| 68 |
* The wait time in ms between each event.
|
|
| 69 |
*/
|
|
| 70 | 84 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 71 |
final JTableHeader header, final int columnIndex,
|
|
| 72 |
final int numberOfClicks, final long sleepTime) { |
|
| 73 | 84 |
this(testCase, header, columnIndex, numberOfClicks,
|
| 74 |
DEFAULT_ISPOPUPTRIGGER, sleepTime); |
|
| 75 |
} |
|
| 76 |
|
|
| 77 |
/**
|
|
| 78 |
* Constructor.
|
|
| 79 |
*
|
|
| 80 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 81 |
* @param header The component on which to trigger the event.
|
|
| 82 |
* @param columnIndex
|
|
| 83 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 84 |
* @param numberOfClicks
|
|
| 85 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 86 |
* @param isPopupTrigger
|
|
| 87 |
* boolean specifying whether this event will show a popup.
|
|
| 88 |
*/
|
|
| 89 | 2 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 90 |
final JTableHeader header, final int columnIndex,
|
|
| 91 |
final int numberOfClicks, final boolean isPopupTrigger) { |
|
| 92 | 2 |
this(testCase, header, columnIndex, numberOfClicks, isPopupTrigger,
|
| 93 |
DEFAULT_SLEEPTIME); |
|
| 94 |
} |
|
| 95 |
|
|
| 96 |
/**
|
|
| 97 |
* Constructor.
|
|
| 98 |
*
|
|
| 99 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 100 |
* @param header The component on which to trigger the event.
|
|
| 101 |
* @param columnIndex
|
|
| 102 |
* The zero-based column of index the specific cell on which to trigger the event.
|
|
| 103 |
* @param numberOfClicks
|
|
| 104 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 105 |
* @param isPopupTrigger
|
|
| 106 |
* boolean specifying whether this event will show a popup.
|
|
| 107 |
* @param sleepTime
|
|
| 108 |
* The wait time in ms between each event.
|
|
| 109 |
*/
|
|
| 110 | 88 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 111 |
final JTableHeader header, final int columnIndex,
|
|
| 112 |
final int numberOfClicks, final boolean isPopupTrigger, |
|
| 113 |
final long sleepTime) {
|
|
| 114 | 88 |
this(testCase, header, columnIndex, numberOfClicks,
|
| 115 |
getDefaultModifiers(isPopupTrigger), isPopupTrigger, sleepTime); |
|
| 116 |
} |
|
| 117 |
|
|
| 118 |
/**
|
|
| 119 |
* Constructor.
|
|
| 120 |
*
|
|
| 121 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 122 |
* @param header The component on which to trigger the event.
|
|
| 123 |
* @param columnIndex
|
|
| 124 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 125 |
* @param numberOfClicks
|
|
| 126 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 127 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 128 |
* @param isPopupTrigger
|
|
| 129 |
* boolean specifying whether this event will show a popup.
|
|
| 130 |
* @param sleepTime
|
|
| 131 |
* The wait time in ms between each event.
|
|
| 132 |
*/
|
|
| 133 | 120 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 134 |
final JTableHeader header, final int columnIndex,
|
|
| 135 |
final int numberOfClicks, final int modifiers, |
|
| 136 |
final boolean isPopupTrigger, final long sleepTime) { |
|
| 137 | 120 |
this(testCase, header, columnIndex, numberOfClicks, modifiers,
|
| 138 |
isPopupTrigger, sleepTime, DEFAULT_POSITION, null);
|
|
| 139 |
} |
|
| 140 |
|
|
| 141 |
/**
|
|
| 142 |
* Constructor.
|
|
| 143 |
*
|
|
| 144 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code>
|
|
| 145 |
* has to be invoked.
|
|
| 146 |
* @param header The component on which to trigger the event.
|
|
| 147 |
* @param columnIndex
|
|
| 148 |
* The zero-based column index of the specific cell
|
|
| 149 |
* on which to trigger the event.
|
|
| 150 |
* @param numberOfClicks
|
|
| 151 |
* Number of clicks in the MouseEvent (1 for single-click,
|
|
| 152 |
* 2 for double clicks)
|
|
| 153 |
* @param modifiers The modifier key values that need to be passed
|
|
| 154 |
* onto the event.
|
|
| 155 |
* @param isPopupTrigger
|
|
| 156 |
* boolean specifying whether this event will show a popup.
|
|
| 157 |
* @param sleepTime
|
|
| 158 |
* The wait time in ms between each event.
|
|
| 159 |
* @param position The relative mouse position within the cell.
|
|
| 160 |
*/
|
|
| 161 | 92 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 162 |
final JTableHeader header, final int columnIndex,
|
|
| 163 |
final int numberOfClicks, final int modifiers, |
|
| 164 |
final boolean isPopupTrigger, final long sleepTime, final int position) { |
|
| 165 | 92 |
this(testCase, header, columnIndex, numberOfClicks, modifiers,
|
| 166 |
isPopupTrigger, sleepTime, position, null);
|
|
| 167 |
} |
|
| 168 |
|
|
| 169 |
/**
|
|
| 170 |
* Constructor.
|
|
| 171 |
*
|
|
| 172 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 173 |
* @param header The component on which to trigger the event.
|
|
| 174 |
* @param columnIndex
|
|
| 175 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 176 |
* @param numberOfClicks
|
|
| 177 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 178 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 179 |
* @param isPopupTrigger
|
|
| 180 |
* boolean specifying whether this event will show a popup.
|
|
| 181 |
* @param sleepTime
|
|
| 182 |
* The wait time in ms between each event.
|
|
| 183 |
* @param referencePoint
|
|
| 184 |
* The CUSTOM mouse position within the cell.
|
|
| 185 |
*/
|
|
| 186 | 2 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 187 |
final JTableHeader header, final int columnIndex,
|
|
| 188 |
final int numberOfClicks, final int modifiers, |
|
| 189 |
final boolean isPopupTrigger, final long sleepTime, |
|
| 190 |
final Point referencePoint) {
|
|
| 191 | 2 |
this(testCase, header, columnIndex, numberOfClicks, modifiers,
|
| 192 |
isPopupTrigger, sleepTime, CUSTOM, referencePoint); |
|
| 193 |
} |
|
| 194 |
|
|
| 195 |
/**
|
|
| 196 |
* Constructor.
|
|
| 197 |
*
|
|
| 198 |
* @param testCase The JFCTestCase on whose thread <code>awtSleep()</code> has to be invoked.
|
|
| 199 |
* @param header The component on which to trigger the event.
|
|
| 200 |
* @param columnIndex
|
|
| 201 |
* The zero-based column index of the specific cell on which to trigger the event.
|
|
| 202 |
* @param numberOfClicks
|
|
| 203 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks)
|
|
| 204 |
* @param modifiers The modifier key values that need to be passed onto the event.
|
|
| 205 |
* @param isPopupTrigger
|
|
| 206 |
* boolean specifying whether this event will show a popup.
|
|
| 207 |
* @param sleepTime
|
|
| 208 |
* The wait time in ms between each event.
|
|
| 209 |
* @param position The relative mouse position within the cell.
|
|
| 210 |
* @param referencePoint
|
|
| 211 |
* If _position is CUSTOM then the point is a offset from
|
|
| 212 |
* the location of the component. If the _position is PERCENT
|
|
| 213 |
* then the location is a percentage offset of the hight and width.
|
|
| 214 |
* Otherwise, the _referencePoint is unused.
|
|
| 215 |
*/
|
|
| 216 | 260 |
public JTableHeaderMouseEventData(final JFCTestCase testCase,
|
| 217 |
final JTableHeader header, final int columnIndex,
|
|
| 218 |
final int numberOfClicks, final int modifiers, |
|
| 219 |
final boolean isPopupTrigger, final long sleepTime, final int position, |
|
| 220 |
final Point referencePoint) {
|
|
| 221 | 260 |
setTestCase(testCase); |
| 222 | 260 |
setSource(header); |
| 223 | 260 |
setNumberOfClicks(numberOfClicks); |
| 224 | 260 |
setModifiers(modifiers); |
| 225 | 260 |
setPopupTrigger(isPopupTrigger); |
| 226 | 260 |
setColumnIndex(columnIndex); |
| 227 | 260 |
setSleepTime(sleepTime); |
| 228 | 260 |
setPosition(position); |
| 229 | 260 |
setReferencePoint(referencePoint); |
| 230 | 260 |
setValid(true);
|
| 231 |
} |
|
| 232 |
|
|
| 233 |
/**
|
|
| 234 |
* Set the attribute value.
|
|
| 235 |
*
|
|
| 236 |
* @param columnIndex The new value of the attribute.
|
|
| 237 |
*/
|
|
| 238 | 460 |
public final void setColumnIndex(final int columnIndex) { |
| 239 | 460 |
m_columnIndex = columnIndex; |
| 240 |
} |
|
| 241 |
|
|
| 242 |
/**
|
|
| 243 |
* Get the attribute value.
|
|
| 244 |
*
|
|
| 245 |
* @return int The value of the attribute.
|
|
| 246 |
*/
|
|
| 247 | 50 |
public final int getColumnIndex() { |
| 248 | 50 |
return m_columnIndex;
|
| 249 |
} |
|
| 250 |
|
|
| 251 |
/**
|
|
| 252 |
* Set the attribute value.
|
|
| 253 |
*
|
|
| 254 |
* @param header The new value of the attribute.
|
|
| 255 |
*/
|
|
| 256 | 460 |
public final void setSource(final JTableHeader header) { |
| 257 | 460 |
m_header = header; |
| 258 |
} |
|
| 259 |
|
|
| 260 |
/**
|
|
| 261 |
* Get the attribute value.
|
|
| 262 |
*
|
|
| 263 |
* @return JTableHeader The value of the attribute.
|
|
| 264 |
*/
|
|
| 265 | 1063 |
public final JTableHeader getSource() {
|
| 266 | 1063 |
return m_header;
|
| 267 |
} |
|
| 268 |
|
|
| 269 |
/**
|
|
| 270 |
* The component on which the event has to be fired.
|
|
| 271 |
*
|
|
| 272 |
* @return The component.
|
|
| 273 |
*/
|
|
| 274 | 812 |
public Component getComponent() {
|
| 275 |
// by default, the component is the same as the source
|
|
| 276 | 812 |
return getSource();
|
| 277 |
} |
|
| 278 |
|
|
| 279 |
/**
|
|
| 280 |
* Check if this event can consume the event given.
|
|
| 281 |
* @param ae AWTEvent to be consumed.
|
|
| 282 |
* @return true if the event may be consumed.
|
|
| 283 |
*/
|
|
| 284 | 231 |
public boolean canConsume(final AWTEvent ae) { |
| 285 | 231 |
if ((ae.getSource() instanceof JTableHeader) |
| 286 |
&& super.canConsume(ae)
|
|
| 287 |
&& sameSource(ae)) {
|
|
| 288 | 206 |
return true; |
| 289 |
} |
|
| 290 |
|
|
| 291 | 25 |
return false; |
| 292 |
} |
|
| 293 |
|
|
| 294 |
/**
|
|
| 295 |
* Consume the event.
|
|
| 296 |
* @param ae AWTEvent to be consumed.
|
|
| 297 |
* @return boolean true if the event was consumed.
|
|
| 298 |
*/
|
|
| 299 | 206 |
public boolean consume(final AWTEvent ae) { |
| 300 | 206 |
if (super.consume(ae)) { |
| 301 | 6 |
return true; |
| 302 |
} |
|
| 303 |
|
|
| 304 | 200 |
MouseEvent me = (MouseEvent) ae; |
| 305 | 200 |
JTableHeader source = (JTableHeader) me.getSource(); |
| 306 | 200 |
setSource(source); |
| 307 | 200 |
setModifiers(me.getModifiers()); |
| 308 | 200 |
setNumberOfClicks(me.getClickCount()); |
| 309 | 200 |
setPopupTrigger(me.isPopupTrigger()); |
| 310 |
|
|
| 311 | 200 |
Point p = new Point(
|
| 312 |
me.getX(), |
|
| 313 |
me.getY()); |
|
| 314 | 200 |
Point screen = source.getLocationOnScreen(); |
| 315 | 200 |
screen.translate(p.x, p.y); |
| 316 | 200 |
setLocationOnScreen(screen); |
| 317 | 200 |
setSleepTime(getDefaultSleepTime()); |
| 318 |
|
|
| 319 | 200 |
int column = source.columnAtPoint(p);
|
| 320 |
|
|
| 321 | 200 |
setColumnIndex(column); |
| 322 | 200 |
setPosition(CENTER); |
| 323 | 200 |
setReferencePoint(null);
|
| 324 |
|
|
| 325 | 200 |
setValid(true);
|
| 326 |
|
|
| 327 | 200 |
return true; |
| 328 |
} |
|
| 329 |
|
|
| 330 |
/**
|
|
| 331 |
* Compare to event datas and deterime if they are equal.
|
|
| 332 |
*
|
|
| 333 |
* @param o Object to be compared.
|
|
| 334 |
* @return true if the events are the same.
|
|
| 335 |
*/
|
|
| 336 | 11 |
public boolean equals(final Object o) { |
| 337 | 11 |
if (!super.equals(o)) { |
| 338 | 9 |
return false; |
| 339 |
} |
|
| 340 |
|
|
| 341 | 2 |
JTableHeaderMouseEventData data = (JTableHeaderMouseEventData) o; |
| 342 |
|
|
| 343 | 2 |
return (data.getColumnIndex() == getColumnIndex());
|
| 344 |
} |
|
| 345 |
|
|
| 346 |
/**
|
|
| 347 |
* Get the hashCode.
|
|
| 348 |
* @return int hashCode.
|
|
| 349 |
*/
|
|
| 350 | 0 |
public int hashCode() { |
| 351 | 0 |
return super.hashCode() + m_columnIndex; |
| 352 |
} |
|
| 353 |
|
|
| 354 |
/**
|
|
| 355 |
* Populate the XML Element with this objects attributes.
|
|
| 356 |
* @param e element to be populated.
|
|
| 357 |
*/
|
|
| 358 | 0 |
public void populate(final Element e) { |
| 359 | 0 |
super.populate(e);
|
| 360 | 0 |
e.setAttribute(JFCXMLConstants.TYPE, "JTableHeaderMouseEventData");
|
| 361 | 0 |
e.setAttribute(JFCXMLConstants.COLUMN, "" + getColumnIndex());
|
| 362 |
} |
|
| 363 |
|
|
| 364 |
/**
|
|
| 365 |
* Prepare the component to receive the event.
|
|
| 366 |
*
|
|
| 367 |
* @return true if the component is ready to receive the event.
|
|
| 368 |
*/
|
|
| 369 | 235 |
public boolean prepareComponent() { |
| 370 | 235 |
if (!isValidForProcessing(getSource())) {
|
| 371 | 1 |
return false; |
| 372 |
} |
|
| 373 |
|
|
| 374 | 234 |
JFCTestCase testCase = getTestCase(); |
| 375 |
|
|
| 376 | 234 |
if (testCase != null) { |
| 377 |
// try to clear the event queue
|
|
| 378 | 234 |
testCase.flushAWT(); |
| 379 |
} |
|
| 380 |
|
|
| 381 | 234 |
Rectangle headerRect = m_header.getHeaderRect(m_columnIndex); |
| 382 |
|
|
| 383 | 234 |
if (testCase != null) { |
| 384 |
// try to clear the event queue
|
|
| 385 | 234 |
testCase.pauseAWT(); |
| 386 |
} |
|
| 387 |
|
|
| 388 | 234 |
Point p = calculatePoint(headerRect); |
| 389 |
|
|
| 390 | 234 |
if (!m_header.getVisibleRect().contains(p)) {
|
| 391 |
// Center the new point in the visible area.
|
|
| 392 | 17 |
Rectangle tvr = m_header.getTable().getVisibleRect(); |
| 393 | 17 |
Rectangle rect = new Rectangle(p.x - (int) (tvr.getWidth() / 2), |
| 394 |
(int) tvr.getY(), (int) tvr.getWidth(), |
|
| 395 |
(int) tvr.getHeight());
|
|
| 396 | 17 |
m_header.getTable().scrollRectToVisible(rect); |
| 397 |
} |
|
| 398 |
|
|
| 399 | 234 |
Point screen = m_header.getLocationOnScreen(); |
| 400 | 234 |
screen.translate(p.x, p.y); |
| 401 | 234 |
setLocationOnScreen(screen); |
| 402 |
|
|
| 403 | 234 |
return true; |
| 404 |
} |
|
| 405 |
|
|
| 406 |
/**
|
|
| 407 |
* Get string description of event.
|
|
| 408 |
* @return String description of event.
|
|
| 409 |
*/
|
|
| 410 | 30 |
public String toString() {
|
| 411 | 30 |
if (!isValid()) {
|
| 412 | 0 |
return super.toString(); |
| 413 |
} |
|
| 414 |
|
|
| 415 | 30 |
StringBuffer buf = new StringBuffer(1000);
|
| 416 | 30 |
buf.append(super.toString());
|
| 417 | 30 |
buf.append(" column: " + getColumnIndex());
|
| 418 |
|
|
| 419 | 30 |
return buf.toString();
|
| 420 |
} |
|
| 421 |
} |
|
| 422 |
|
|
||||||||||