use of com.codename1.ui.util.xml.Val in project CodenameOne by codenameone.
the class JavaSEPort method getInAppPurchase.
public Purchase getInAppPurchase() {
return new Purchase() {
private Vector purchases;
@Override
public Product[] getProducts(String[] skus) {
return null;
}
@Override
public boolean isItemListingSupported() {
return false;
}
@Override
public boolean isManagedPaymentSupported() {
return managedPurchaseSupported;
}
@Override
public boolean isManualPaymentSupported() {
return manualPurchaseSupported;
}
@Override
public boolean isRefundable(String sku) {
if (!refundSupported) {
return false;
}
int val = JOptionPane.showConfirmDialog(window, "Is " + sku + " refundable?", "Purchase", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
return val == JOptionPane.YES_OPTION;
}
private Vector getPurchases() {
if (purchases == null) {
purchases = (Vector) Storage.getInstance().readObject("CN1InAppPurchases");
if (purchases == null) {
purchases = new Vector();
}
}
return purchases;
}
private void savePurchases() {
if (purchases != null) {
Storage.getInstance().writeObject("CN1InAppPurchases", purchases);
}
}
@Override
public boolean isSubscriptionSupported() {
return subscriptionSupported;
}
@Override
public boolean isUnsubscribeSupported() {
return subscriptionSupported;
}
@Override
public String pay(final double amount, final String currency) {
try {
if (Display.getInstance().isEdt()) {
final String[] response = new String[1];
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
response[0] = pay(amount, currency);
}
});
return response[0];
}
if (!manualPurchaseSupported) {
throw new RuntimeException("Manual payment isn't supported check the isManualPaymentSupported() method!");
}
final String[] result = new String[1];
final boolean[] completed = new boolean[] { false };
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int res = JOptionPane.showConfirmDialog(window, "A payment of " + amount + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (res == JOptionPane.YES_OPTION) {
result[0] = UUID.randomUUID().toString();
}
completed[0] = true;
}
});
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (!completed[0]) {
try {
Thread.sleep(20);
} catch (InterruptedException err) {
}
}
}
});
if (getPurchaseCallback() != null) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (result[0] != null) {
getPurchaseCallback().paymentSucceeded(result[0], amount, currency);
} else {
getPurchaseCallback().paymentFailed(UUID.randomUUID().toString(), null);
}
}
});
}
return result[0];
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
public void purchase(final String sku) {
if (managedPurchaseSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "An in-app purchase of " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_SIMULATOR, sku, "cn1-iap-sim-" + UUID.randomUUID().toString(), System.currentTimeMillis(), "");
getPurchaseCallback().itemPurchased(sku);
getPurchases().addElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Purchase failed");
}
}
});
}
});
} else {
throw new RuntimeException("In app purchase isn't supported on this platform!");
}
}
@Override
public void refund(final String sku) {
if (refundSupported) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
getPurchaseCallback().itemRefunded(sku);
getPurchases().removeElement(sku);
savePurchases();
}
});
}
}
@Override
public void subscribe(final String sku) {
if (subscriptionSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "An in-app subscription to " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
getPurchaseCallback().subscriptionStarted(sku);
getPurchases().addElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Subscription failed");
}
}
});
}
});
}
}
@Override
public void unsubscribe(final String sku) {
if (subscriptionSupported) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final int res = JOptionPane.showConfirmDialog(window, "In-app unsubscription request for " + sku + " was made\nDo you wish to accept it?", "Payment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (res == JOptionPane.YES_OPTION) {
getPurchaseCallback().subscriptionCanceled(sku);
getPurchases().removeElement(sku);
savePurchases();
} else {
getPurchaseCallback().itemPurchaseError(sku, "Error in unsubscribe");
}
}
});
}
});
}
}
@Override
public boolean wasPurchased(String sku) {
return getPurchases().contains(sku);
}
};
}
use of com.codename1.ui.util.xml.Val in project CodenameOne by codenameone.
the class List method animate.
/**
* {@inheritDoc}
*/
public boolean animate() {
// parent is performing the animation we shouldn't do anything in this case
// this is the scrolling animation which we don't want to interfear with
boolean parentFinished = super.animate();
if ((animationPosition != 0) && listMotion != null && !isDragActivated()) {
if (animationPosition < 0) {
animationPosition = Math.min(listMotion.getValue() - destination, 0);
} else {
animationPosition = Math.max(destination - listMotion.getValue(), 0);
}
if (animationPosition == 0) {
listMotion = null;
deregisterAnimatedInternal();
}
return true;
}
if (fixedDraggedMotion != null) {
int val = -fixedDraggedMotion.getValue();
fixedDraggedAnimationPosition = fixedDraggedAnimationPosition - (fixedDraggedPosition - val);
fixedDraggedPosition = val;
Dimension size = getElementSize(false, true);
int s;
if (orientation == VERTICAL) {
s = size.getHeight();
} else {
s = size.getWidth();
}
if (fixedDraggedMotion.isFinished()) {
deregisterAnimatedInternal();
// is the closest and animate to it.
if (fixedDraggedAnimationPosition <= -s / 2) {
fixedDraggedSelection++;
if (fixedDraggedSelection >= model.getSize()) {
fixedDraggedSelection = 0;
}
} else if (fixedDraggedAnimationPosition >= s / 2) {
fixedDraggedSelection--;
if (fixedDraggedSelection < 0) {
fixedDraggedSelection = model.getSize() - 1;
}
}
if (fixedDraggedAnimationPosition != 0) {
if (fixedDraggedAnimationPosition < 0) {
if (fixedDraggedAnimationPosition < -s / 2) {
destination = s + fixedDraggedAnimationPosition;
animationPosition = destination;
} else {
destination = -fixedDraggedAnimationPosition;
animationPosition = fixedDraggedAnimationPosition;
}
} else {
if (fixedDraggedAnimationPosition > s / 2) {
destination = (s - fixedDraggedAnimationPosition);
animationPosition = -destination;
} else {
destination = fixedDraggedAnimationPosition;
animationPosition = fixedDraggedAnimationPosition;
}
}
initListMotion();
fixedDraggedAnimationPosition = 0;
}
// this happens when dragging an empty list causing an exception on a negative selection
if (fixedDraggedSelection >= 0 && fixedDraggedSelection < getModel().getSize()) {
setSelectedIndex(fixedDraggedSelection, false);
}
setDragActivated(false);
fixedDraggedMotion = null;
return false;
} else {
if (fixedDraggedAnimationPosition <= -s) {
fixedDraggedSelection++;
if (fixedDraggedSelection >= model.getSize()) {
fixedDraggedSelection = 0;
}
fixedDraggedPosition = val;
} else if (fixedDraggedAnimationPosition >= s) {
fixedDraggedSelection--;
if (fixedDraggedSelection < 0) {
fixedDraggedSelection = model.getSize() - 1;
}
fixedDraggedPosition = val;
}
fixedDraggedAnimationPosition = fixedDraggedAnimationPosition % s;
}
return true;
}
return parentFinished;
}
use of com.codename1.ui.util.xml.Val in project CodenameOne by codenameone.
the class List method setShouldCalcPreferredSize.
/**
* {@inheritDoc}
*/
public void setShouldCalcPreferredSize(boolean shouldCalcPreferredSize) {
super.setShouldCalcPreferredSize(shouldCalcPreferredSize);
elemSize = null;
selectedElemSize = null;
// we should try passing the should calcPreferredSize to the renderer so it can revalidate too
if (shouldCalcPreferredSize) {
ListCellRenderer r = getRenderer();
Object val;
if (renderingPrototype != null) {
val = renderingPrototype;
} else {
if (getModel().getSize() > 0) {
val = getModel().getItemAt(0);
} else {
return;
}
}
Component c = r.getListCellRendererComponent(this, val, 0, false);
c.setShouldCalcPreferredSize(shouldCalcPreferredSize);
c = r.getListCellRendererComponent(this, val, 0, true);
c.setShouldCalcPreferredSize(shouldCalcPreferredSize);
}
}
use of com.codename1.ui.util.xml.Val in project CodenameOne by codenameone.
the class InputComponent method errorMessage.
/**
* Sets the text of the error label
* @param errorMessage the text
* @return this for chaining calls E.g. {@code TextComponent tc = new TextComponent().text("Text").label("Label"); }
*/
public InputComponent errorMessage(String errorMessage) {
String col = getUIManager().getThemeConstant("textComponentErrorColor", null);
if (errorMessage == null || errorMessage.length() == 0) {
// no need for double showing of error
if (this.errorMessage.getText().length() == 0) {
return this;
}
// clear the error mode
this.errorMessage.setText("");
if (col != null) {
lbl.setUIID(lbl.getUIID());
getEditor().setUIID(getEditor().getUIID());
}
} else {
this.errorMessage.setText(errorMessage);
if (col != null) {
int val = Integer.parseInt(col, 16);
lbl.getAllStyles().setFgColor(val);
Border b = Border.createUnderlineBorder(2, val);
getEditor().getAllStyles().setBorder(b);
}
}
refreshForGuiBuilder();
return this;
}
use of com.codename1.ui.util.xml.Val in project CodenameOne by codenameone.
the class SwipeableContainer method animate.
@Override
public boolean animate() {
if (openCloseMotion != null) {
int val = openCloseMotion.getValue();
if (openedToRight) {
topWrapper.setX(val);
} else {
topWrapper.setX(-val);
}
repaint();
boolean finished = openCloseMotion.isFinished();
if (finished) {
// getComponentForm().deregisterAnimated(this);
openCloseMotion = null;
if (!open) {
bottomRightWrapper.setVisible(false);
bottomLeftWrapper.setVisible(false);
openedToLeft = false;
openedToRight = false;
} else {
dispatcher.fireActionEvent(new ActionEvent(this, ActionEvent.Type.Swipe));
}
}
return !finished;
}
return false;
}
Aggregations