use of com.codename1.ui.util.UITimer in project CodenameOne by codenameone.
the class LayerWithZoomLevels method pointerReleased.
/**
* {@inheritDoc}
*/
public void pointerReleased(int x, int y) {
super.pointerReleased(x, y);
final long currTime = System.currentTimeMillis();
if (currTime - lastPressed < singleTapThreshold) {
tapCount++;
final int tapX = x;
final int tapY = y;
final int currTapCount = tapCount;
UITimer timer = new UITimer(new Runnable() {
public void run() {
if (currTapCount == tapCount) {
pointerTapped(tapX, tapY, tapCount);
tapCount = 0;
}
}
});
timer.schedule(doubleTapThreshold, false, this.getComponentForm());
} else {
tapCount = 0;
}
if (oldDistance != -1) {
double scale = (double) scaleX / (double) getWidth();
Coord refCoord = this.getCoordFromPosition(zoomCenterX + getAbsoluteX(), zoomCenterY + getAbsoluteY());
int oldZoom = _zoom;
if (scale > 1) {
if (scale < 1.2) {
// do nothing
} else if (scale < 1.6) {
zoomIn();
} else if (scale < 2.0) {
zoomIn();
zoomIn();
} else if (scale < 2.4) {
zoomIn();
zoomIn();
zoomIn();
} else {
zoomIn();
zoomIn();
zoomIn();
zoomIn();
}
} else {
if (scale > 0.8) {
// do nothing
} else if (scale > 0.5) {
zoomOut();
} else if (scale > 0.2) {
zoomOut();
zoomOut();
} else {
zoomOut();
zoomOut();
zoomOut();
}
}
if (oldZoom != _zoom) {
Coord c1 = this.getCoordFromPosition(0, 0);
Coord c2 = this.getCoordFromPosition(getWidth(), getHeight());
Coord pixelToCoord = new Coord((c2.getLatitude() - c1.getLatitude()) / (float) getHeight(), (c2.getLongitude() - c1.getLongitude()) / (float) getWidth());
float offX = (getWidth() / 2) - zoomCenterX;
float offY = (getHeight() / 2) - zoomCenterY;
_center = _map.projection().fromWGS84(refCoord.translate(offY * pixelToCoord.getLatitude(), offX * pixelToCoord.getLongitude()));
}
translateX = 0;
translateY = 0;
scaleX = 0;
scaleY = 0;
oldDistance = -1;
if (buffer != null) {
// buffer.dispose();
buffer = null;
refreshLayers = true;
}
if (Display.getInstance().areMutableImagesFast()) {
super.repaint();
} else {
// workaround for rounding error in scale/clipping
getComponentForm().repaint();
}
fireMapListenerEvent();
return;
}
Coord scale = _map.scale(_zoom);
_center = _center.translate(translateY * -scale.getLatitude(), translateX * scale.getLongitude());
_needTiles = true;
translateX = 0;
translateY = 0;
x = x - getAbsoluteX();
y = y - getAbsoluteY();
Tile t = screenTile();
Coord southWest = t.position(x - 20, t.dimension().getHeight() - y - 20);
Coord c = Mercator.inverseMercator(southWest.getLatitude(), southWest.getLongitude());
Coord northEast = t.position(x + 20, t.dimension().getHeight() - y + 20);
c = Mercator.inverseMercator(northEast.getLatitude(), northEast.getLongitude());
BoundingBox bbox = new BoundingBox(southWest, northEast);
Enumeration e = _layers.elements();
while (e.hasMoreElements()) {
LayerWithZoomLevels layer = (LayerWithZoomLevels) e.nextElement();
if (layer.layer instanceof PointsLayer) {
((PointsLayer) layer.layer).fireActionEvent(bbox);
}
}
super.repaint();
fireMapListenerEvent();
}
use of com.codename1.ui.util.UITimer 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.util.UITimer in project CodenameOne by codenameone.
the class JavaSEPort method editStringLegacy.
public void editStringLegacy(final Component cmp, int maxSize, int constraint, String text, int keyCode) {
checkEDT();
java.awt.Component awtTf;
if (cmp instanceof com.codename1.ui.TextField) {
java.awt.TextField t = new java.awt.TextField();
awtTf = t;
t.setSelectionEnd(0);
t.setSelectionStart(0);
} else {
java.awt.TextArea t = new java.awt.TextArea("", 0, 0, java.awt.TextArea.SCROLLBARS_NONE);
;
awtTf = t;
t.setSelectionEnd(0);
t.setSelectionStart(0);
}
final java.awt.Component tf = awtTf;
if (keyCode > 0) {
text += ((char) keyCode);
setText(tf, text);
setCaretPosition(tf, text.length());
((com.codename1.ui.TextField) cmp).setText(getText(tf));
} else {
setText(tf, text);
}
canvas.add(tf);
if (getSkin() != null) {
tf.setBounds((int) ((cmp.getAbsoluteX() + getScreenCoordinates().x + canvas.x) * zoomLevel), (int) ((cmp.getAbsoluteY() + getScreenCoordinates().y + canvas.y) * zoomLevel), (int) (cmp.getWidth() * zoomLevel), (int) (cmp.getHeight() * zoomLevel));
java.awt.Font f = font(cmp.getStyle().getFont().getNativeFont());
tf.setFont(f.deriveFont(f.getSize2D() * zoomLevel));
} else {
tf.setBounds(cmp.getAbsoluteX(), cmp.getAbsoluteY(), cmp.getWidth(), cmp.getHeight());
tf.setFont(font(cmp.getStyle().getFont().getNativeFont()));
}
setCaretPosition(tf, getText(tf).length());
tf.requestFocus();
class Listener implements ActionListener, FocusListener, KeyListener, TextListener, Runnable {
public synchronized void run() {
while (tf.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 (tf instanceof java.awt.TextField) {
((java.awt.TextField) tf).removeActionListener(this);
}
((TextComponent) tf).removeTextListener(this);
tf.removeFocusListener(this);
canvas.remove(tf);
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 java.awt.TextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) >= getText(tf).length() - 1) {
actionPerformed(null);
}
}
return;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (tf instanceof java.awt.TextField) {
actionPerformed(null);
} else {
if (getCaretPosition(tf) <= 2) {
actionPerformed(null);
}
}
return;
}
}
public void textValueChanged(TextEvent e) {
if (cmp instanceof com.codename1.ui.TextField) {
((com.codename1.ui.TextField) cmp).setText(getText(tf));
}
}
}
;
final Listener l = new Listener();
if (tf instanceof java.awt.TextField) {
((java.awt.TextField) tf).addActionListener(l);
}
((TextComponent) tf).addTextListener(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);
}
Aggregations