use of com.codename1.ui.Container in project CodenameOne by codenameone.
the class Component method scrollRectToVisible.
/**
* Makes sure the component is visible in the scroll if this container
* is scrollable
*
* @param x
* @param y
* @param width
* @param height
* @param coordinateSpace the component according to whose coordinates
* rect is defined. Rect's x/y are relative to that component
* (they are not absolute).
*/
public void scrollRectToVisible(int x, int y, int width, int height, Component coordinateSpace) {
if (isScrollable()) {
int scrollPosition = getScrollY();
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
if (isSmoothScrolling() && destScrollY > -1) {
view = new Rectangle(getScrollX(), destScrollY, w, h - invisibleAreaUnderVKB);
} else {
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
}
int relativeX = x;
int relativeY = y;
// component needs to be in absolute coordinates...
Container parent = null;
if (coordinateSpace != null) {
parent = coordinateSpace.getParent();
}
if (parent == this) {
if (view.contains(x, y, width, height)) {
return;
}
} else {
while (parent != this) {
// mostly a special case for list
if (parent == null) {
relativeX = x;
relativeY = y;
break;
}
relativeX += parent.getX();
relativeY += parent.getY();
parent = parent.getParent();
}
if (view.contains(relativeX, relativeY, width, height)) {
return;
}
}
if (isScrollableX()) {
if (getScrollX() > relativeX) {
setScrollX(relativeX);
}
int rightX = relativeX + width - s.getHorizontalPadding();
if (getScrollX() + w < rightX) {
setScrollX(getScrollX() + (rightX - (getScrollX() + w)));
} else {
if (getScrollX() > relativeX) {
setScrollX(relativeX);
}
}
}
if (isScrollableY()) {
if (getScrollY() > relativeY) {
scrollPosition = relativeY;
}
int bottomY = relativeY + height - s.getVerticalPadding();
if (getScrollY() + h < bottomY + invisibleAreaUnderVKB) {
scrollPosition = getScrollY() + (bottomY - (getScrollY() + h)) + invisibleAreaUnderVKB;
} else {
if (getScrollY() > relativeY) {
scrollPosition = relativeY;
}
}
if (isSmoothScrolling() && isInitialized()) {
initialScrollY = getScrollY();
destScrollY = scrollPosition;
initScrollMotion();
} else {
setScrollY(scrollPosition);
}
}
repaint();
} else {
// try to move parent scroll if you are not scrollable
Container parent = getParent();
if (parent != null) {
parent.scrollRectToVisible(getAbsoluteX() - parent.getAbsoluteX() + x, getAbsoluteY() - parent.getAbsoluteY() + y, width, height, parent);
}
}
}
use of com.codename1.ui.Container in project CodenameOne by codenameone.
the class Component method pointerDragged.
/**
* If this Component is focused, the pointer dragged event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerDragged(final int x, final int y) {
Form p = getComponentForm();
if (p == null) {
return;
}
if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
pointerDraggedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y));
}
if (dragAndDropInitialized) {
// keep call to pointerDragged to move the parent scroll if needed
if (dragCallbacks < 2) {
dragCallbacks++;
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (dragActivated) {
pointerDragged(x, y);
}
dragCallbacks--;
}
});
}
if (!dragActivated) {
dragActivated = true;
setVisible(false);
p.setDraggedComponent(this);
oldx = x;
oldy = y;
draggedx = getAbsoluteX();
draggedy = getAbsoluteY();
}
Component dropTo = findDropTarget(this, x, y);
if (dropTo != null && dragOverListener != null) {
ActionEvent ev = new ActionEvent(this, dropTo, x, y);
dragOverListener.fireActionEvent(ev);
if (ev.isConsumed()) {
return;
}
}
if (dropTargetComponent != dropTo) {
if (dropTargetComponent != null) {
dropTargetComponent.dragExit(this);
}
dropTargetComponent = dropTo;
if (dropTargetComponent != null) {
dropTargetComponent.dragEnter(this);
}
}
// we repaint twice to create an intersection of the old and new position
p.repaint(draggedx, draggedy, getWidth(), getHeight());
draggedx = draggedx + (x - oldx);
draggedy = draggedy + (y - oldy);
oldx = x;
oldy = y;
p.repaint(draggedx, draggedy, getWidth(), getHeight());
Container scrollParent = getParent();
while (scrollParent != null && !scrollParent.isScrollable()) {
scrollParent = scrollParent.getParent();
}
if (scrollParent != null) {
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
// if the dragging component is out of bounds move the scrollable parent
if (!view.contains(draggedx - scrollParent.getAbsoluteX(), draggedy - scrollParent.getAbsoluteY(), getWidth(), getHeight())) {
if ((scrollParent.isScrollableY() && scrollParent.getScrollY() >= 0 && scrollParent.getScrollY() + (draggedy + getHeight()) < scrollParent.getScrollDimension().getHeight()) || (scrollParent.isScrollableX() && scrollParent.getScrollX() >= 0 && scrollParent.getScrollX() + (draggedx + getWidth()) < scrollParent.getScrollDimension().getWidth())) {
int yposition = draggedy - scrollParent.getAbsoluteY() - 40;
if (yposition < 0) {
yposition = 0;
}
int xposition = draggedx - scrollParent.getAbsoluteX() - 40;
if (xposition < 0) {
xposition = 0;
}
int height = getHeight() + 80;
if (scrollParent.getScrollY() + draggedy + height >= scrollParent.getScrollDimension().getHeight()) {
yposition = draggedy - scrollParent.getAbsoluteY();
height = scrollParent.getScrollDimension().getHeight() - yposition;
}
int width = getWidth() + 80;
if (scrollParent.getScrollX() + draggedx + width >= scrollParent.getScrollDimension().getWidth()) {
xposition = draggedx - scrollParent.getAbsoluteX();
width = scrollParent.getScrollDimension().getWidth() - xposition;
}
scrollParent.scrollRectToVisible(xposition, yposition, width, height, scrollParent);
}
}
}
return;
}
if (dragActivated && p.getDraggedComponent() == null) {
dragActivated = false;
}
if (!dragActivated) {
boolean draggedOnX = Math.abs(p.initialPressX - x) > Math.abs(p.initialPressY - y);
shouldGrabScrollEvents = (isScrollableX() && draggedOnX) || isScrollableY() && !draggedOnX;
}
if (isScrollable() && isSmoothScrolling() && shouldGrabScrollEvents) {
if (!dragActivated) {
dragActivated = true;
lastScrollY = y;
lastScrollX = x;
p.setDraggedComponent(this);
p.registerAnimatedInternal(this);
Component fc = p.getFocused();
if (fc != null && fc != this) {
fc.dragInitiated();
}
}
// and pulling it in the reverse direction of the drag
if (isScrollableY()) {
int tl;
if (getTensileLength() > -1 && refreshTask == null) {
tl = getTensileLength();
} else {
tl = getHeight() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollY() + (lastScrollY - y);
if (isAlwaysTensile() && getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() <= getHeight()) {
if (scroll >= -tl && scroll < getHeight() + tl) {
setScrollY(scroll);
}
} else {
if (scroll >= -tl && scroll < getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() - getHeight() + tl) {
setScrollY(scroll);
}
}
updateTensileHighlightIntensity(lastScrollY, y, false);
}
if (isScrollableX()) {
int tl;
if (getTensileLength() > -1) {
tl = getTensileLength();
} else {
tl = getWidth() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollX() + (lastScrollX - x);
if (scroll >= -tl && scroll < getScrollDimension().getWidth() - getWidth() + tl) {
setScrollX(scroll);
}
}
lastScrollY = y;
lastScrollX = x;
} else {
// try to find a scrollable element until you reach the Form
Component parent = getParent();
if (!(parent instanceof Form)) {
parent.pointerDragged(x, y);
}
}
}
use of com.codename1.ui.Container in project CodenameOne by codenameone.
the class Component method setScrollY.
/**
* Indicates the X position of the scrolling, this number is relative to the
* component position and so a position of 0 would indicate the x position
* of the component.
*
* @param scrollY the Y position of the scrolling
*/
protected void setScrollY(int scrollY) {
if (this.scrollY != scrollY) {
CodenameOneImplementation ci = Display.impl;
if (ci.isAsyncEditMode() && ci.isEditingText()) {
Component editingText = ci.getEditingText();
if (editingText != null && this instanceof Container && ((Container) this).contains(editingText)) {
ci.hideTextEditor();
}
}
}
// the setter must always update the value regardless...
int scrollYtmp = scrollY;
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
Form parentForm = getComponentForm();
int v = getInvisibleAreaUnderVKB();
int h = getScrollDimension().getHeight() - getHeight() + v;
scrollYtmp = Math.min(scrollYtmp, h);
scrollYtmp = Math.max(scrollYtmp, 0);
}
if (isScrollableY()) {
if (Form.activePeerCount > 0) {
onParentPositionChange();
}
repaint();
}
if (scrollListeners != null) {
scrollListeners.fireScrollEvent(this.scrollX, scrollYtmp, this.scrollX, this.scrollY);
}
this.scrollY = scrollYtmp;
onScrollY(this.scrollY);
}
use of com.codename1.ui.Container in project CodenameOne by codenameone.
the class ComponentSelector method fadeOut.
/**
* Fades out components in this set.
* @param duration Duration of animation.
* @param callback Callback to run when animation completes.
* @return Self for chaining.
*/
public ComponentSelector fadeOut(int duration, final SuccessCallback<ComponentSelector> callback) {
final String placeholderProperty = "com.codename1.ui.ComponentSelector#fadeOutPlaceholder";
AnimationManager mgr = null;
ArrayList<ComponentAnimation> animations = new ArrayList<ComponentAnimation>();
final ArrayList<Component> animatingComponents = new ArrayList<Component>();
for (Component c : this) {
Container parent = c.getParent();
if (parent != null) {
AnimationManager cmgr = c.getAnimationManager();
if (cmgr != null) {
mgr = cmgr;
Container placeholder = new Container();
// placeholder.setShowEvenIfBlank(true);
c.putClientProperty(placeholderProperty, placeholder);
Component.setSameHeight(placeholder, c);
Component.setSameWidth(placeholder, c);
$(placeholder).setMargin(c.getStyle().getMarginTop(), c.getStyle().getMarginRight(false), c.getStyle().getMarginBottom(), c.getStyle().getMarginLeft(false)).setPadding(c.getStyle().getPaddingTop(), c.getStyle().getPaddingRight(false), c.getStyle().getPaddingBottom(), c.getStyle().getPaddingLeft(false));
ComponentAnimation a = parent.createReplaceTransition(c, placeholder, CommonTransitions.createFade(duration));
animations.add(a);
animatingComponents.add(c);
}
// centerBackground.add(BorderLayout.CENTER, boxy);
}
}
if (mgr != null) {
mgr.addAnimation(ComponentAnimation.compoundAnimation(animations.toArray(new ComponentAnimation[animations.size()])), new Runnable() {
public void run() {
for (final Component c : animatingComponents) {
// c.setHidden(true);
c.setVisible(false);
final Container placeholder = (Container) c.getClientProperty(placeholderProperty);
c.putClientProperty(placeholderProperty, null);
if (placeholder != null) {
Container parent = placeholder.getParent();
/*
if (parent == null) {
System.out.println("Deferring replace back");
$(new Runnable() {
public void run() {
Container parent = placeholder.getParent();
if (parent != null) {
System.out.println("Found parent after deferral");
parent.replace(placeholder, c, CommonTransitions.createEmpty());
}
}
});
} else {
*/
if (parent != null) {
parent.replace(placeholder, c, CommonTransitions.createEmpty());
}
// }
}
}
if (callback != null) {
callback.onSucess(ComponentSelector.this);
}
}
});
}
return this;
}
use of com.codename1.ui.Container in project CodenameOne by codenameone.
the class Container method wrapInLayeredPane.
/**
* An atomic operation that wraps the current component in a Container with
* a layered layout. This prevents us from having to initialize and deinitialize
* all of the components in a sub-tree because we want to re-root it. In particular
* Form.getLayeredPane() re-roots the entire content pane the first time it is
* called on a form. If the form contains native peers there is a flicker which
* is quite annoying. Providing a way to do this atomically results in a better
* user experience.
* @return The Container that is the new parent of this component.
*/
Container wrapInLayeredPane() {
final Container oldParent = getParent();
final Container newParent = new Container(new LayeredLayout());
final Layout parentLayout = oldParent != null && oldParent.layout != null ? oldParent.layout : null;
final Object constraint = parentLayout != null ? parentLayout.getComponentConstraint(this) : null;
newParent.setParent(oldParent);
newParent.components.add(this);
final Runnable r = new Runnable() {
public void run() {
if (parentLayout != null) {
parentLayout.removeLayoutComponent(Container.this);
parentLayout.addLayoutComponent(constraint, newParent, oldParent);
}
newParent.initComponentImpl();
if (oldParent != null) {
int cmpIndex = -1;
for (int i = 0; i < oldParent.getComponentCount(); i++) {
Component c = oldParent.getComponentAt(i);
if (c.equals(Container.this)) {
cmpIndex = i;
break;
}
}
// int cmpIndex = oldParent.getComponentIndex(Container.this); <--- WTF... this always returns -1!!
if (cmpIndex == -1) {
throw new RuntimeException("WTF we have parent but no index!!!!");
}
oldParent.components.set(cmpIndex, newParent);
}
Container.this.setParent(newParent);
newParent.revalidate();
}
};
AnimationManager a = getAnimationManager();
if (a != null && a.isAnimating()) {
a.addAnimation(new ComponentAnimation() {
@Override
public boolean isInProgress() {
return false;
}
@Override
protected void updateState() {
r.run();
}
});
return newParent;
} else {
r.run();
return newParent;
}
}
Aggregations