|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractMouseEventData.java | 70.8% | 69.9% | 68.8% | 70.1% |
|
||||||||||||||
| 1 |
package junit.extensions.jfcunit.eventdata;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.jfcunit.xml.JFCXMLConstants;
|
|
| 4 |
|
|
| 5 |
import org.w3c.dom.Element;
|
|
| 6 |
|
|
| 7 |
import java.awt.AWTEvent;
|
|
| 8 |
import java.awt.Component;
|
|
| 9 |
import java.awt.Toolkit;
|
|
| 10 |
import java.awt.event.MouseEvent;
|
|
| 11 |
|
|
| 12 |
import javax.swing.AbstractButton;
|
|
| 13 |
import javax.swing.JComboBox;
|
|
| 14 |
import javax.swing.JComponent;
|
|
| 15 |
import javax.swing.JScrollPane;
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
/**
|
|
| 19 |
* Abstract data container class that holds most of the data necessary for jfcUnit to fire mouse events.
|
|
| 20 |
*
|
|
| 21 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
| 22 |
*/
|
|
| 23 |
public abstract class AbstractMouseEventData extends AbstractEventData { |
|
| 24 |
/**
|
|
| 25 |
* boolean specifying whether this event will show a popup.
|
|
| 26 |
*/
|
|
| 27 |
private boolean m_isPopupTrigger; |
|
| 28 |
|
|
| 29 |
/**
|
|
| 30 |
* Number of clicks in the MouseEvent (1 for single-click, 2 for double clicks).
|
|
| 31 |
*/
|
|
| 32 |
private int m_numberOfClicks = 0; |
|
| 33 |
|
|
| 34 |
/**
|
|
| 35 |
* Get the default modifiers for the popup trigger.
|
|
| 36 |
*
|
|
| 37 |
* @param popupTrigger true if to enable the popup menu.
|
|
| 38 |
* @return int popup modifiers else DEFAULT_MOUSE_MODIFIERS.
|
|
| 39 |
*/
|
|
| 40 | 496 |
public static int getDefaultModifiers(final boolean popupTrigger) { |
| 41 | 496 |
if (popupTrigger) {
|
| 42 | 112 |
return MouseEvent.BUTTON3_MASK;
|
| 43 |
} |
|
| 44 |
|
|
| 45 | 384 |
return DEFAULT_MOUSE_MODIFIERS;
|
| 46 |
} |
|
| 47 |
|
|
| 48 |
/**
|
|
| 49 |
* Get the default popup trigger.
|
|
| 50 |
* @return int popup modifers else DEFAULT_MOUSE_MODIFIERS
|
|
| 51 |
*/
|
|
| 52 | 0 |
public int getDefaultModifiers() { |
| 53 | 0 |
return getDefaultModifiers(getPopupTrigger());
|
| 54 |
} |
|
| 55 |
|
|
| 56 |
/**
|
|
| 57 |
* Get the string representing the current modifiers.
|
|
| 58 |
* @return String version of the modifiers.
|
|
| 59 |
*/
|
|
| 60 | 141 |
public final String getModifierText() {
|
| 61 | 141 |
StringBuffer buf = new StringBuffer();
|
| 62 | 141 |
int modifiers = getModifiers();
|
| 63 |
|
|
| 64 | 141 |
if ((modifiers & MouseEvent.ALT_MASK) != 0) {
|
| 65 | 0 |
buf.append(Toolkit.getProperty("AWT.alt", "Alt")); |
| 66 | 0 |
buf.append("+");
|
| 67 |
} |
|
| 68 |
|
|
| 69 |
// Meta should only be available to Key events.
|
|
| 70 |
// if ((modifiers & MouseEvent.META_MASK) != 0) {
|
|
| 71 |
// buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
|
|
| 72 |
// buf.append("+");
|
|
| 73 |
// }
|
|
| 74 | 141 |
if ((modifiers & MouseEvent.CTRL_MASK) != 0) {
|
| 75 | 0 |
buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); |
| 76 | 0 |
buf.append("+");
|
| 77 |
} |
|
| 78 |
|
|
| 79 | 141 |
if ((modifiers & MouseEvent.SHIFT_MASK) != 0) {
|
| 80 | 0 |
buf.append(Toolkit.getProperty("AWT.shift", "Shift")); |
| 81 | 0 |
buf.append("+");
|
| 82 |
} |
|
| 83 |
|
|
| 84 | 141 |
if ((modifiers & MouseEvent.ALT_GRAPH_MASK) != 0) {
|
| 85 | 0 |
buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); |
| 86 | 0 |
buf.append("+");
|
| 87 |
} |
|
| 88 |
|
|
| 89 | 141 |
if ((modifiers & MouseEvent.BUTTON1_MASK) != 0) {
|
| 90 | 121 |
buf.append(Toolkit.getProperty("AWT.button1", "Button1")); |
| 91 | 121 |
buf.append("+");
|
| 92 |
} |
|
| 93 |
|
|
| 94 | 141 |
if ((modifiers & MouseEvent.BUTTON2_MASK) != 0) {
|
| 95 | 0 |
buf.append(Toolkit.getProperty("AWT.button2", "Button2")); |
| 96 | 0 |
buf.append("+");
|
| 97 |
} |
|
| 98 |
|
|
| 99 | 141 |
if ((modifiers & MouseEvent.BUTTON3_MASK) != 0) {
|
| 100 | 20 |
buf.append(Toolkit.getProperty("AWT.button3", "Button3")); |
| 101 | 20 |
buf.append("+");
|
| 102 |
} |
|
| 103 |
|
|
| 104 | 141 |
if (buf.length() > 0) {
|
| 105 | 141 |
buf.setLength(buf.length() - 1); // remove trailing '+'
|
| 106 |
} |
|
| 107 |
|
|
| 108 | 141 |
return buf.toString();
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* Set the attribute value.
|
|
| 113 |
*
|
|
| 114 |
* @param numberOfClicks The new value of the attribute
|
|
| 115 |
*/
|
|
| 116 | 2791 |
public final void setNumberOfClicks(final int numberOfClicks) { |
| 117 | 2791 |
m_numberOfClicks = numberOfClicks; |
| 118 |
} |
|
| 119 |
|
|
| 120 |
/**
|
|
| 121 |
* Get the attribute value.
|
|
| 122 |
*
|
|
| 123 |
* @return int The value of the attribute
|
|
| 124 |
*/
|
|
| 125 | 1976 |
public final int getNumberOfClicks() { |
| 126 | 1976 |
return m_numberOfClicks;
|
| 127 |
} |
|
| 128 |
|
|
| 129 |
/**
|
|
| 130 |
* Return the default number of clicks.
|
|
| 131 |
* @return defaut number of clicks.
|
|
| 132 |
*/
|
|
| 133 | 0 |
public int getDefaultNumberOfClicks() { |
| 134 | 0 |
return EventDataConstants.DEFAULT_NUMBEROFCLICKS;
|
| 135 |
} |
|
| 136 |
|
|
| 137 |
/**
|
|
| 138 |
* Get the default Popup trigger setting.
|
|
| 139 |
* @return default popup trigger setting.
|
|
| 140 |
*/
|
|
| 141 | 0 |
public boolean getDefaultPopupTrigger() { |
| 142 | 0 |
return EventDataConstants.DEFAULT_ISPOPUPTRIGGER;
|
| 143 |
} |
|
| 144 |
|
|
| 145 |
/**
|
|
| 146 |
* Set the attribute value.
|
|
| 147 |
*
|
|
| 148 |
* @param isPopupTrigger The new value of the attribute
|
|
| 149 |
*/
|
|
| 150 | 2791 |
public void setPopupTrigger(final boolean isPopupTrigger) { |
| 151 | 2791 |
m_isPopupTrigger = isPopupTrigger; |
| 152 |
} |
|
| 153 |
|
|
| 154 |
/**
|
|
| 155 |
* Get the attribute value.
|
|
| 156 |
*
|
|
| 157 |
* @return boolean The value of the attribute
|
|
| 158 |
*/
|
|
| 159 | 1848 |
public boolean getPopupTrigger() { |
| 160 | 1848 |
return m_isPopupTrigger;
|
| 161 |
} |
|
| 162 |
|
|
| 163 |
/**
|
|
| 164 |
* Check if this event can consume the {@link java.awt.AWTEvent}.
|
|
| 165 |
*
|
|
| 166 |
* @param ae {@link java.awt.AWTEvent} to be consumed.
|
|
| 167 |
* @return boolean true if the event can be consumed.
|
|
| 168 |
*/
|
|
| 169 | 1790 |
public boolean canConsume(final AWTEvent ae) { |
| 170 | 1790 |
if (!(ae instanceof MouseEvent)) { |
| 171 | 366 |
return false; |
| 172 |
} |
|
| 173 |
|
|
| 174 | 1424 |
MouseEvent me = (MouseEvent) ae; |
| 175 |
|
|
| 176 | 1424 |
if (me.getID() == MouseEvent.MOUSE_DRAGGED) {
|
| 177 | 27 |
return false; |
| 178 |
} |
|
| 179 |
|
|
| 180 | 1397 |
Object source = ae.getSource(); |
| 181 |
|
|
| 182 | 1397 |
if (source instanceof JScrollPane) { |
| 183 | 84 |
return false; |
| 184 |
} |
|
| 185 |
|
|
| 186 | 1313 |
if (source instanceof JComponent) { |
| 187 | 1259 |
String name = ((JComponent) source).getName(); |
| 188 |
|
|
| 189 | 1259 |
if ((name != null) && name.endsWith(".glassPane")) { |
| 190 | 0 |
return false; |
| 191 |
} |
|
| 192 |
} |
|
| 193 |
|
|
| 194 | 1313 |
if (source instanceof AbstractButton) { |
| 195 | 523 |
if (((AbstractButton) source).getParent() instanceof JComboBox) { |
| 196 | 80 |
return false; |
| 197 |
} |
|
| 198 |
} |
|
| 199 |
|
|
| 200 | 1233 |
if (ae.getSource().equals(getRoot((Component) ae.getSource()))) {
|
| 201 | 54 |
return false; |
| 202 |
} |
|
| 203 |
|
|
| 204 | 1179 |
return true; |
| 205 |
} |
|
| 206 |
|
|
| 207 |
/**
|
|
| 208 |
* Get the attribute value.
|
|
| 209 |
*
|
|
| 210 |
* @param ae {@link java.awt.Event} to be processed.
|
|
| 211 |
* @return boolean The value of the attribute
|
|
| 212 |
*/
|
|
| 213 | 1030 |
public boolean consume(final AWTEvent ae) { |
| 214 | 1030 |
MouseEvent me = (MouseEvent) ae; |
| 215 | 1030 |
int id = me.getID();
|
| 216 |
|
|
| 217 | 1030 |
if ((id == MouseEvent.MOUSE_MOVED)
|
| 218 |
|| (id == MouseEvent.MOUSE_ENTERED) |
|
| 219 |
|| (id == MouseEvent.MOUSE_EXITED) |
|
| 220 |
|| (isValid() && (ae.getSource() == getRoot()))) {
|
|
| 221 |
// Ignore the event.
|
|
| 222 | 199 |
return true; |
| 223 |
} |
|
| 224 |
|
|
| 225 | 831 |
return false; |
| 226 |
} |
|
| 227 |
|
|
| 228 |
/**
|
|
| 229 |
* Compare to event datas and determine if they are equal.
|
|
| 230 |
*
|
|
| 231 |
* @param o Object to be compared.
|
|
| 232 |
* @return true if the events are the same.
|
|
| 233 |
*/
|
|
| 234 | 119 |
public boolean equals(final Object o) { |
| 235 | 119 |
if (!super.equals(o)) { |
| 236 | 72 |
return false; |
| 237 |
} |
|
| 238 |
|
|
| 239 | 47 |
AbstractMouseEventData data = (AbstractMouseEventData) o; |
| 240 |
|
|
| 241 | 47 |
return (data.getNumberOfClicks() == getNumberOfClicks())
|
| 242 |
&& (data.getPopupTrigger() == getPopupTrigger()); |
|
| 243 |
} |
|
| 244 |
|
|
| 245 |
/**
|
|
| 246 |
* hashCode is delegated to the super class.
|
|
| 247 |
* @return hashCode of the super class.
|
|
| 248 |
*/
|
|
| 249 | 0 |
public int hashCode() { |
| 250 | 0 |
return super.hashCode(); |
| 251 |
} |
|
| 252 |
|
|
| 253 |
/**
|
|
| 254 |
* Populate the element given with the attributes of the
|
|
| 255 |
* event.
|
|
| 256 |
*
|
|
| 257 |
* @param e element to be populated.
|
|
| 258 |
*/
|
|
| 259 | 0 |
public void populate(final Element e) { |
| 260 | 0 |
super.populate(e);
|
| 261 |
|
|
| 262 | 0 |
if (getNumberOfClicks() != getDefaultNumberOfClicks()
|
| 263 |
&& getNumberOfClicks() > 0) {
|
|
| 264 | 0 |
e.setAttribute(JFCXMLConstants.CLICKS, "" + getNumberOfClicks());
|
| 265 |
} |
|
| 266 |
|
|
| 267 | 0 |
boolean trigger = getPopupTrigger();
|
| 268 |
|
|
| 269 | 0 |
if (trigger != getDefaultPopupTrigger()) {
|
| 270 | 0 |
String value = "true";
|
| 271 |
|
|
| 272 | 0 |
if (!trigger) {
|
| 273 | 0 |
value = "false";
|
| 274 |
} |
|
| 275 |
|
|
| 276 | 0 |
e.setAttribute(JFCXMLConstants.POPUPTRIGGER, value); |
| 277 |
} |
|
| 278 |
} |
|
| 279 |
|
|
| 280 |
/**
|
|
| 281 |
* Check if the event has the same source as this event data.
|
|
| 282 |
*
|
|
| 283 |
* @param ae {@link java.awt.AWTEvent} to be checked.
|
|
| 284 |
* @return true if the events have the same source.
|
|
| 285 |
*/
|
|
| 286 | 670 |
public boolean sameSource(final AWTEvent ae) { |
| 287 |
// REDTAG: Is this check incorrect?
|
|
| 288 | 670 |
if (isValid()) {
|
| 289 | 516 |
return ((Component) ae.getSource() == getComponent());
|
| 290 |
} else {
|
|
| 291 | 154 |
return true; |
| 292 |
} |
|
| 293 |
} |
|
| 294 |
|
|
| 295 |
/**
|
|
| 296 |
* Return a string representing the eventdata.
|
|
| 297 |
*
|
|
| 298 |
* @return String description of the event data.
|
|
| 299 |
*/
|
|
| 300 | 141 |
public String toString() {
|
| 301 | 141 |
if (!isValid()) {
|
| 302 | 0 |
return super.toString(); |
| 303 |
} |
|
| 304 |
|
|
| 305 | 141 |
StringBuffer buf = new StringBuffer(1000);
|
| 306 | 141 |
buf.append(super.toString());
|
| 307 | 141 |
buf.append(" clicks: " + getNumberOfClicks());
|
| 308 | 141 |
buf.append(" popup: " + getPopupTrigger());
|
| 309 |
|
|
| 310 | 141 |
return buf.toString();
|
| 311 |
} |
|
| 312 |
} |
|
| 313 |
|
|
||||||||||