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