001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.event;
009:
010: import net.mygwt.ui.client.MyGWT;
011: import net.mygwt.ui.client.widget.Component;
012:
013: import com.google.gwt.user.client.DOM;
014: import com.google.gwt.user.client.Element;
015: import com.google.gwt.user.client.Event;
016: import com.google.gwt.user.client.ui.Widget;
017:
018: /**
019: * Instances of this class provide a description of a particular MyGWT event.
020: *
021: * <p>
022: * Note: For a given event, only the fields which are appropriate will be filled
023: * in. The appropriate fields for each event are documented by the event source.
024: * </p>
025: */
026: public class BaseEvent {
027:
028: /**
029: * The event type.
030: */
031: public int type;
032:
033: /**
034: * The source object.
035: */
036: public Object source;
037:
038: /**
039: * Depending on the event, a flag indicating whether the operation should be
040: * allowed.
041: */
042: public boolean doit = true;
043:
044: /**
045: * The widget that issued the event.
046: */
047: public Widget widget;
048:
049: /**
050: * The item that the event occurred in.
051: */
052: public Widget item;
053:
054: /**
055: * List of widgets.
056: */
057: public Component[] items;
058:
059: /**
060: * The dom event.
061: */
062: public Event event;
063:
064: /**
065: * The index.
066: */
067: public int index;
068:
069: /**
070: * The row index.
071: */
072: public int rowIndex;
073:
074: /**
075: * The width.
076: */
077: public int width;
078:
079: /**
080: * The height.
081: */
082: public int height;
083:
084: /**
085: * X coordinate
086: */
087: public int x;
088:
089: /**
090: * Y coordinate
091: */
092: public int y;
093:
094: /**
095: * The size.
096: */
097: public int size;
098:
099: /**
100: * The name.
101: */
102: public String name;
103:
104: /**
105: * The value.
106: */
107: public Object value;
108:
109: /**
110: * The old value.
111: */
112: public Object oldValue;
113:
114: /**
115: * Creates a new base event.
116: */
117: public BaseEvent() {
118:
119: }
120:
121: /**
122: * Creates a new base event.
123: *
124: * @param widget the source widget
125: */
126: public BaseEvent(Widget widget) {
127: this .widget = widget;
128: }
129:
130: /**
131: * Creates a new base event.
132: *
133: * @param widget the source widget
134: * @param item the item widget
135: */
136: public BaseEvent(Widget widget, Widget item) {
137: this .widget = widget;
138: this .item = item;
139: }
140:
141: /**
142: * Cancels bubbling for the given event. This will stop the event from being
143: * propagated to parent elements.
144: */
145: public void cancelBubble() {
146: if (event != null) {
147: DOM.eventCancelBubble(event, true);
148: }
149: }
150:
151: /**
152: * Returns the event's x coordinate.
153: *
154: * @return the x coordinate or -1 if no dom event.
155: */
156: public int getClientX() {
157: if (event != null) {
158: return DOM.eventGetClientX(event);
159: }
160: return -1;
161: }
162:
163: /**
164: * Returns the event's y coordinate.
165: *
166: * @return the y coordinate or -1 if no dom event.
167: */
168: public int getClientY() {
169: if (event != null) {
170: return DOM.eventGetClientY(event);
171: }
172: return -1;
173: }
174:
175: /**
176: * Returns the dom event type.
177: *
178: * @return the event type
179: */
180: public int getEventType() {
181: return DOM.eventGetType(event);
182: }
183:
184: /**
185: * Returns the key code associated with this event.
186: *
187: * @return the key code
188: */
189: public int getKeyCode() {
190: return DOM.eventGetKeyCode(event);
191: }
192:
193: /**
194: * Returns the event's target element.
195: *
196: * @return the target element or <code>nulol</code> if no dom event.
197: */
198: public Element getTarget() {
199: if (event != null) {
200: return DOM.eventGetTarget(event);
201: }
202: return null;
203: }
204:
205: /**
206: * Returns <code>true</code> if the control, alt, shift, or meta key is
207: * pressed.
208: *
209: * @return the modifier state
210: */
211: public boolean hasModifier() {
212: if (event != null) {
213: if (DOM.eventGetAltKey(event) || DOM.eventGetCtrlKey(event)
214: || DOM.eventGetShiftKey(event)
215: || DOM.eventGetMetaKey(event)) {
216: return true;
217: }
218: }
219: return false;
220: }
221:
222: /**
223: * Returns <code>true</code> if the control key (or meta key) is pressed.
224: *
225: * @return the control key state
226: */
227: public boolean isControlKey() {
228: return event == null ? false
229: : (DOM.eventGetCtrlKey(event) || DOM
230: .eventGetMetaKey(event));
231: }
232:
233: /**
234: * Returns <code>true</code> if the event is a right click.
235: *
236: * @return the right click state
237: */
238: public boolean isRightClick() {
239: if (event != null) {
240: if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT
241: || (MyGWT.isMac && DOM.eventGetCtrlKey(event))) {
242: return true;
243: }
244: }
245:
246: return false;
247: }
248:
249: /**
250: * Returns <code>true</code> if the shift key is pressed.
251: *
252: * @return the shift key state
253: */
254: public boolean isShiftKey() {
255: return event == null ? false : DOM.eventGetShiftKey(event);
256: }
257:
258: /**
259: * Prevents the browser from taking its default action for the given event.
260: */
261: public void preventDefault() {
262: DOM.eventPreventDefault(event);
263: }
264:
265: /**
266: * Stops the event (preventDefault and cancelBubble).
267: */
268: public void stopEvent() {
269: cancelBubble();
270: preventDefault();
271: }
272:
273: /**
274: * Returns <code>true</code> if the target of this event equals or is a
275: * child of the given element.
276: *
277: * @param element the element
278: * @return the within state
279: */
280: public boolean within(Element element) {
281: if (event != null) {
282: return DOM.isOrHasChild(element, getTarget());
283: }
284: return false;
285: }
286:
287: }
|