use of com.codename1.ui.PeerComponent in project CodenameOne by codenameone.
the class CodenameOneView method onTouchEvent.
// private boolean nativePeerGrabbedPointer = false;
public boolean onTouchEvent(MotionEvent event) {
if (this.implementation.getCurrentForm() == null) {
/**
* make sure a form has been set before we can send events to the
* EDT. if we send events before the form has been set we might
* deadlock!
*/
return true;
}
int[] x = null;
int[] y = null;
int size = event.getPointerCount();
if (size > 1) {
x = new int[size];
y = new int[size];
for (int i = 0; i < size; i++) {
x[i] = (int) event.getX(i);
y[i] = (int) event.getY(i);
}
}
/*
if (!cn1GrabbedPointer) {
if (x == null) {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)event.getX(), (int)event.getY());
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//nativePeerGrabbedPointer = false;
}
return false;
}
} else {
Component componentAt = this.implementation.getCurrentForm().getComponentAt((int)x[0], (int)y[0]);
if (componentAt != null && (componentAt instanceof PeerComponent)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
nativePeerGrabbedPointer = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
nativePeerGrabbedPointer = false;
}
return false;
}
}
}
*/
// if (nativePeerGrabbedPointer) {
// return false;
// }
Component componentAt;
if (x == null) {
componentAt = this.implementation.getCurrentForm().getComponentAt((int) event.getX(), (int) event.getY());
} else {
componentAt = this.implementation.getCurrentForm().getComponentAt((int) x[0], (int) y[0]);
}
boolean isPeer = (componentAt instanceof PeerComponent);
boolean consumeEvent = !isPeer || cn1GrabbedPointer;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x == null) {
this.implementation.pointerPressed((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerPressed(x, y);
}
if (!isPeer)
cn1GrabbedPointer = true;
break;
case MotionEvent.ACTION_UP:
if (x == null) {
this.implementation.pointerReleased((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerReleased(x, y);
}
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_CANCEL:
cn1GrabbedPointer = false;
break;
case MotionEvent.ACTION_MOVE:
if (x == null) {
this.implementation.pointerDragged((int) event.getX(), (int) event.getY());
} else {
this.implementation.pointerDragged(x, y);
}
break;
}
return consumeEvent;
}
use of com.codename1.ui.PeerComponent in project CodenameOne by codenameone.
the class BlackBerryImplementation method createNativePeer.
/**
* @inheritDoc
*/
public PeerComponent createNativePeer(Object nativeComponent) {
if (nativeComponent instanceof Field) {
if (nullFld == null) {
nullFld = new NullField();
nullFld.setFocusListener(new FinishEditFocus());
synchronized (UiApplication.getEventLock()) {
canvas.add(nullFld);
}
}
final Field fld = (Field) nativeComponent;
final PeerComponent peer = new PeerComponent(fld) {
public boolean isFocusable() {
if (fld != null) {
return fld.isFocusable();
}
return super.isFocusable();
}
public void setFocus(boolean b) {
if (hasFocus() == b) {
return;
}
if (b) {
canvas.eventTarget = fld;
fld.setFocusListener(new PeerFocus(fld, this));
} else {
fld.setFocusListener(null);
if (canvas.eventTarget == fld) {
canvas.eventTarget = null;
}
}
if (isInitialized()) {
InvokeLaterWrapper i = new InvokeLaterWrapper(INVOKE_AND_WAIT_setFocus);
i.val = b;
i.fld = fld;
app.invokeAndWait(i);
} else {
super.setFocus(b);
}
}
public boolean animate() {
if (fld.isDirty()) {
repaint();
}
return super.animate();
}
protected Dimension calcPreferredSize() {
InvokeLaterWrapper i = new InvokeLaterWrapper(INVOKE_AND_WAIT_calcPreferredSize);
i.dim = new Dimension();
i.fld = fld;
app.invokeAndWait(i);
return i.dim;
}
protected void onPositionSizeChange() {
InvokeLaterWrapper i = new InvokeLaterWrapper(INVOKE_LATER_dirty);
i.fld = fld;
app.invokeLater(i);
}
protected void initComponent() {
fieldComponentMap.put(fld, this);
InvokeLaterWrapper i = new InvokeLaterWrapper(INVOKE_LATER_initComponent);
i.fld = fld;
app.invokeLater(i);
setFocus(super.hasFocus());
if (hasFocus()) {
canvas.eventTarget = fld;
}
getComponentForm().registerAnimated(this);
}
protected void deinitialize() {
getComponentForm().deregisterAnimated(this);
canvas.eventTarget = null;
InvokeLaterWrapper i = new InvokeLaterWrapper(INVOKE_LATER_deinitialize);
i.fld = fld;
app.invokeLater(i);
fieldComponentMap.remove(fld);
}
};
fieldComponentMap.put(fld, peer);
return peer;
}
throw new IllegalArgumentException(nativeComponent.getClass().getName());
}
use of com.codename1.ui.PeerComponent in project CodenameOne by codenameone.
the class BlackBerryOS5Implementation method createBrowserComponent.
public PeerComponent createBrowserComponent(Object browserComponent) {
synchronized (UiApplication.getEventLock()) {
BrowserField bff = new BrowserField();
final BrowserComponent cmp = (BrowserComponent) browserComponent;
bff.addListener(new BrowserFieldListener() {
public void documentError(BrowserField browserField, Document document) throws Exception {
cmp.fireWebEvent("onError", new ActionEvent(document.getDocumentURI()));
super.documentError(browserField, document);
}
public void documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) throws Exception {
cmp.fireWebEvent("onStart", new ActionEvent(document.getDocumentURI()));
super.documentCreated(browserField, scriptEngine, document);
}
public void documentLoaded(BrowserField browserField, Document document) throws Exception {
cmp.fireWebEvent("onLoad", new ActionEvent(document.getDocumentURI()));
super.documentLoaded(browserField, document);
}
});
return PeerComponent.create(bff);
}
}
use of com.codename1.ui.PeerComponent in project CodenameOne by codenameone.
the class BlackBerryTouchSupport method touchEvent.
protected boolean touchEvent(TouchEvent e) {
super.touchEvent(e);
lastNavigationCharged = false;
int x1 = e.getGlobalX(1);
int y1 = e.getGlobalY(1);
int x2 = e.getGlobalX(2);
int y2 = e.getGlobalY(2);
int evt = e.getEvent();
Form f = Display.getInstance().getCurrent();
if (f != null) {
Component cmp = f.getComponentAt(x1, y1);
if (cmp != null) {
if (cmp instanceof PeerComponent) {
return false;
}
if (cmp instanceof TextArea && cmp.hasFocus() && impl.nativeEdit != null) {
return false;
}
if (impl.nativeEdit != null) {
impl.finishEdit(true);
}
}
}
if (!isClickTouchScreen()) {
if (evt == TouchEvent.DOWN) {
evt = TouchEvent.CLICK;
} else if (evt == TouchEvent.UP) {
evt = TouchEvent.UNCLICK;
}
}
switch(evt) {
case TouchEvent.UNCLICK:
clicked = false;
drawHover = true;
if (x1 < 0) {
impl.pointerReleased(x2, y2);
} else {
if (x2 < 0) {
impl.pointerReleased(x1, y1);
} else {
impl.pointerReleased(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
break;
case TouchEvent.CLICK:
clicked = true;
drawHover = false;
if (x1 < 0) {
impl.pointerPressed(x2, y2);
} else {
if (x2 < 0) {
impl.pointerPressed(x1, y1);
} else {
impl.pointerPressed(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
break;
case TouchEvent.MOVE:
if (clicked) {
drawHover = false;
if (x1 < 0) {
impl.pointerDragged(x2, y2);
} else {
if (x2 < 0) {
impl.pointerDragged(x1, y1);
} else {
impl.pointerDragged(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
} else {
drawHover = true;
if (x1 < 0) {
impl.pointerHover(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHover(x1, y1);
} else {
impl.pointerHover(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
case TouchEvent.DOWN:
if (!clicked) {
drawHover = true;
if (x1 < 0) {
impl.pointerHoverPressed(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHoverPressed(x1, y1);
} else {
impl.pointerHoverPressed(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
case TouchEvent.UP:
if (!clicked) {
drawHover = false;
if (x1 < 0) {
impl.pointerHoverReleased(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHoverReleased(x1, y1);
} else {
impl.pointerHoverReleased(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
}
hoverY = y1;
hoverX = x1;
return true;
}
Aggregations