use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class JavaSEPort method editString.
/**
* @inheritDoc
*/
public void editString(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
if (scrollWheeling) {
return;
}
if (System.getProperty("TextCompatMode") != null) {
editStringLegacy(cmp, maxSize, constraint, text, keyCode);
return;
}
// a workaround to fix an issue where the previous Text Component wasn't removed properly.
java.awt.Component[] cmps = canvas.getComponents();
for (int i = 0; i < cmps.length; i++) {
java.awt.Component cmp1 = cmps[i];
if (cmp1 instanceof JScrollPane || cmp1 instanceof javax.swing.text.JTextComponent) {
canvas.remove(cmp1);
}
}
checkEDT();
javax.swing.text.JTextComponent swingT;
if (((com.codename1.ui.TextArea) cmp).isSingleLineTextArea()) {
JTextComponent t;
if (isDesktop() && (constraint & TextArea.PASSWORD) == TextArea.PASSWORD) {
t = new JPasswordField() {
public void repaint(long tm, int x, int y, int width, int height) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
cmp.repaint();
}
});
}
};
} else {
t = new JTextField() {
public void repaint(long tm, int x, int y, int width, int height) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
cmp.repaint();
}
});
}
};
/*
((JTextField)t).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (cmp instanceof com.codename1.ui.TextField) {
final com.codename1.ui.TextField tf = (com.codename1.ui.TextField)cmp;
if (tf.getDoneListener() != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (tf.getDoneListener() != null) {
tf.fireDoneEvent();
}
}
});
}
}
}
});
*/
}
swingT = t;
textCmp = swingT;
} else {
final com.codename1.ui.TextArea ta = (com.codename1.ui.TextArea) cmp;
JTextArea t = new JTextArea(ta.getLines(), ta.getColumns()) {
public void repaint(long tm, int x, int y, int width, int height) {
int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
Rectangle bounds;
if (getSkin() != null) {
bounds = new Rectangle((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
} else {
bounds = new Rectangle(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
}
if (textCmp != null && !textCmp.getBounds().equals(bounds)) {
textCmp.setBounds(bounds);
}
Display.getInstance().callSerially(new Runnable() {
public void run() {
cmp.repaint();
}
});
}
};
t.setWrapStyleWord(true);
t.setLineWrap(true);
swingT = t;
JScrollPane pane = new JScrollPane(swingT);
pane.setBorder(null);
pane.setOpaque(false);
pane.getViewport().setOpaque(false);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textCmp = pane;
}
DefaultCaret caret = (DefaultCaret) swingT.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
swingT.setFocusTraversalKeysEnabled(false);
TextEditUtil.setCurrentEditComponent(cmp);
final javax.swing.text.JTextComponent txt = swingT;
txt.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB) {
TextEditUtil.editNextTextArea();
}
}
});
swingT.setBorder(null);
swingT.setOpaque(false);
swingT.setForeground(new Color(cmp.getUnselectedStyle().getFgColor()));
final javax.swing.text.JTextComponent tf = swingT;
if (keyCode > 0) {
text += ((char) keyCode);
setText(tf, text);
setCaretPosition(tf, text.length());
if (cmp instanceof com.codename1.ui.TextField) {
((com.codename1.ui.TextField) cmp).setText(getText(tf));
}
} else {
setText(tf, text);
}
textCmp.setBorder(null);
textCmp.setOpaque(false);
canvas.add(textCmp);
int marginTop = cmp.getSelectedStyle().getPadding(Component.TOP);
int marginLeft = cmp.getSelectedStyle().getPadding(Component.LEFT);
int marginRight = cmp.getSelectedStyle().getPadding(Component.RIGHT);
int marginBottom = cmp.getSelectedStyle().getPadding(Component.BOTTOM);
if (getSkin() != null) {
textCmp.setBounds((int) ((cmp.getAbsoluteX() + cmp.getScrollX() + getScreenCoordinates().x + canvas.x + marginLeft) * zoomLevel), (int) ((cmp.getAbsoluteY() + cmp.getScrollY() + getScreenCoordinates().y + canvas.y + marginTop) * zoomLevel), (int) ((cmp.getWidth() - marginLeft - marginRight) * zoomLevel), (int) ((cmp.getHeight() - marginTop - marginBottom) * zoomLevel));
java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
} else {
textCmp.setBounds(cmp.getAbsoluteX() + cmp.getScrollX() + marginLeft, cmp.getAbsoluteY() + cmp.getScrollY() + marginTop, cmp.getWidth() - marginRight - marginLeft, cmp.getHeight() - marginTop - marginBottom);
tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
}
setCaretPosition(tf, getText(tf).length());
tf.requestFocus();
tf.setSelectionStart(0);
tf.setSelectionEnd(0);
class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable, DocumentListener {
public synchronized void run() {
while (textCmp.getParent() != null) {
try {
wait(20);
} catch (InterruptedException ex) {
}
}
}
public void actionPerformed(ActionEvent e) {
String txt = getText(tf);
if (testRecorder != null) {
testRecorder.editTextFieldCompleted(cmp, txt);
}
Display.getInstance().onEditingComplete(cmp, txt);
if (e != null && cmp instanceof com.codename1.ui.TextField) {
final com.codename1.ui.TextField cn1Tf = (com.codename1.ui.TextField) cmp;
if (cmp != null && cn1Tf.getDoneListener() != null) {
cn1Tf.fireDoneEvent();
}
}
if (tf instanceof JTextField) {
((JTextField) tf).removeActionListener(this);
}
((JTextComponent) tf).getDocument().removeDocumentListener(this);
tf.removeFocusListener(this);
canvas.remove(textCmp);
synchronized (this) {
notify();
}
canvas.repaint();
}
public void focusGained(FocusEvent e) {
setCaretPosition(tf, getText(tf).length());
}
public void focusLost(FocusEvent e) {
actionPerformed(null);
}
public void keyTyped(KeyEvent e) {
String t = getText(tf);
if (t.length() >= ((TextArea) cmp).getMaxSize()) {
e.consume();
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (tf instanceof JTextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) >= getText(tf).length() - 1) {
actionPerformed(null);
}
}
return;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (tf instanceof JTextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) <= 2) {
actionPerformed(null);
}
}
return;
}
}
public void textValueChanged(TextEvent e) {
if (cmp instanceof com.codename1.ui.TextField) {
updateText();
}
}
private void updateText() {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (cmp instanceof com.codename1.ui.TextField) {
((com.codename1.ui.TextField) cmp).setText(getText(tf));
}
}
});
}
public void insertUpdate(DocumentEvent e) {
updateText();
}
public void removeUpdate(DocumentEvent e) {
updateText();
}
public void changedUpdate(DocumentEvent e) {
updateText();
}
}
;
final Listener l = new Listener();
if (tf instanceof JTextField) {
((JTextField) tf).addActionListener(l);
}
((JTextComponent) tf).getDocument().addDocumentListener(l);
tf.addKeyListener(l);
tf.addFocusListener(l);
if (simulateAndroidKeyboard) {
java.util.Timer t = new java.util.Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
if (!Display.getInstance().isEdt()) {
Display.getInstance().callSerially(this);
return;
}
if (tf.getParent() != null) {
final int height = getScreenCoordinates().height;
JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height / 2);
new UITimer(new Runnable() {
public void run() {
if (tf.getParent() != null) {
new UITimer(this).schedule(100, false, Display.getInstance().getCurrent());
} else {
JavaSEPort.this.sizeChanged(getScreenCoordinates().width, height);
}
}
}).schedule(100, false, Display.getInstance().getCurrent());
}
}
};
t.schedule(tt, 300);
}
Display.getInstance().invokeAndBlock(l);
}
use of com.codename1.ui.geom.Rectangle 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.geom.Rectangle 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.geom.Rectangle in project CodenameOne by codenameone.
the class Container method scrollComponentToVisible.
/**
* Makes sure the component is visible in the scroll if this container is
* scrollable
*
* @param c the component that will be scrolling for visibility
*/
public void scrollComponentToVisible(Component c) {
if (isScrollable()) {
if (c != null) {
Rectangle r = c.getVisibleBounds();
if (c.getParent() != null) {
// special case for the first component to allow the user to scroll all the
// way to the top
Form f = getComponentForm();
if (f != null && f.getInvisibleAreaUnderVKB() == 0 && f.findFirstFocusable() == c) {
// support this use case only if the component doesn't explicitly declare visible bounds
if (r == c.getBounds() && !Display.getInstance().isTouchScreenDevice()) {
scrollRectToVisible(new Rectangle(0, 0, c.getX() + Math.min(c.getWidth(), getWidth()), c.getY() + Math.min(c.getHeight(), getHeight())), this);
return;
}
}
}
boolean moveToVisible = true;
boolean large = c.getVisibleBounds().getSize().getHeight() > getHeight() || c.getVisibleBounds().getSize().getWidth() > getWidth();
if (large) {
int x = getScrollX();
int y = getScrollY();
int w = getWidth();
int h = getHeight();
boolean visible = contains(c) && Rectangle.intersects(c.getAbsoluteX(), c.getAbsoluteY(), c.getWidth(), c.getHeight(), getAbsoluteX() + x, getAbsoluteY() + y, w, h);
// if this is a big component no need to scroll to the begining if it's
// partially visible
moveToVisible = !visible;
}
if (moveToVisible) {
scrollRectToVisible(r.getX(), r.getY(), Math.min(r.getSize().getWidth(), getWidth()), Math.min(r.getSize().getHeight(), getHeight()), c);
}
}
}
}
use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class Container method calculateLastPaintableOffset.
/**
* Gets the index of the "last" child component that intersects the given rectangle. This is
* only helpful if the components are sorted (e.g. with BoxLayout.Y_AXIS). If they aren't
* sorted then this will just return components.size()-1.
* @param pos The starting position to search. It is assumed that this starting
* position is in the visible region.
* @param clipX1 The left bounds of the region to search. (0,0) is the top left corner of the container.
* @param clipY1 The top bounds of the region to search. (0,0) is the top left corner of the container.
* @param clipX2 The right bounds of the region to search. (0,0) is the top left corner of the container.
* @param clipY2 The bottom bounds of the region to search. (0,0) is the top left corner of the container.
* @return The index of the last visible component in this container - or components.size()-1
*/
private int calculateLastPaintableOffset(int pos, int clipX1, int clipY1, int clipX2, int clipY2) {
final int len = components.size();
if (pos >= len - 1) {
// Let's return one less than pos to indicate this
return len - 1;
}
final Layout l = getLayout();
if (l.getClass() == BoxLayout.class) {
if (((BoxLayout) l).getAxis() == BoxLayout.Y_AXIS) {
// Use a binary search to find the first visible
// Component c = components.get(++pos);
Component c = null;
int cy1 = -1;
final int end = len - 1;
// This should still be a valid index because
pos++;
// we previously checked to see if it was >= len-1
do {
c = components.get(pos);
cy1 = c.getBounds().getY();
} while (++pos <= end && cy1 <= clipY2);
return pos - 1;
}
}
return len - 1;
}
Aggregations