use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class NetworkManager method addErrorListener.
/**
* Adds a generic listener to a network error that is invoked before the exception is propagated.
* Notice that this doesn't apply to server error codes!
* Consume the event in order to prevent it from propagating further.
*
* @param e callback will be invoked with the Exception as the source object
*/
public void addErrorListener(ActionListener<NetworkEvent> e) {
if (errorListeners == null) {
errorListeners = new EventDispatcher();
errorListeners.setBlocking(true);
}
errorListeners.addListener(e);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class Oauth2 method handleURL.
private void handleURL(String url, WebBrowser web, final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {
if ((url.startsWith(redirectURI))) {
if (Display.getInstance().getCurrent() == progress) {
progress.dispose();
}
web.stop();
// remove the browser component.
if (login != null) {
login.removeAll();
login.revalidate();
}
if (url.indexOf("code=") > -1) {
Hashtable params = getParamsFromURL(url);
ConnectionRequest req = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
byte[] tok = Util.readInputStream(input);
String t = new String(tok);
if (t.startsWith("{")) {
JSONParser p = new JSONParser();
Map map = p.parseJSON(new StringReader(t));
token = (String) map.get("access_token");
Object ex = map.get("expires_in");
if (ex == null) {
ex = map.get("expires");
}
if (ex != null) {
expires = ex.toString();
}
} else {
token = t.substring(t.indexOf("=") + 1, t.indexOf("&"));
int off = t.indexOf("expires=");
int start = 8;
if (off == -1) {
off = t.indexOf("expires_in=");
start = 11;
}
if (off > -1) {
int end = t.indexOf('&', off);
if (end < 0 || end < off) {
end = t.length();
}
expires = t.substring(off + start, end);
}
}
if (login != null) {
login.dispose();
}
}
protected void handleException(Exception err) {
if (backToForm != null) {
backToForm.showBack();
}
if (al != null) {
al.actionPerformed(new ActionEvent(err, ActionEvent.Type.Exception));
}
}
protected void postResponse() {
if (backToParent && backToForm != null) {
backToForm.showBack();
}
if (al != null) {
al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
}
}
};
req.setUrl(tokenRequestURL);
req.setPost(true);
req.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.addArgument("client_id", clientId);
req.addArgument("redirect_uri", redirectURI);
req.addArgument("client_secret", clientSecret);
req.addArgument("code", (String) params.get("code"));
req.addArgument("grant_type", "authorization_code");
NetworkManager.getInstance().addToQueue(req);
} else if (url.indexOf("error_reason=") > -1) {
Hashtable table = getParamsFromURL(url);
String error = (String) table.get("error_reason");
if (login != null) {
login.dispose();
}
if (backToForm != null) {
backToForm.showBack();
}
if (al != null) {
al.actionPerformed(new ActionEvent(new IOException(error), ActionEvent.Type.Exception));
}
} else {
boolean success = url.indexOf("#") > -1;
if (success) {
String accessToken = url.substring(url.indexOf("#") + 1);
if (accessToken.indexOf("&") > 0) {
token = accessToken.substring(accessToken.indexOf("=") + 1, accessToken.indexOf("&"));
} else {
token = accessToken.substring(accessToken.indexOf("=") + 1);
}
if (login != null) {
login.dispose();
}
if (backToParent && backToForm != null) {
backToForm.showBack();
}
if (al != null) {
al.actionPerformed(new ActionEvent(new AccessToken(token, expires), ActionEvent.Type.Response));
}
}
}
} else {
if (frm != null && Display.getInstance().getCurrent() != frm) {
progress.dispose();
frm.show();
}
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class LayerWithZoomLevels method getTiles.
private void getTiles() throws RuntimeException {
_tiles = new Vector();
Dimension tileSize = _map.tileSize();
int posY = 0;
_delta = null;
while (posY - tileSize.getHeight() < getHeight()) {
int posX = 0;
while (posX - tileSize.getWidth() < getWidth()) {
Tile tile;
Coord cur = _map.translate(_center, _zoom, posX - getWidth() / 2, getHeight() / 2 - posY);
if (_map.projection().extent().contains(cur)) {
tile = _map.tileFor(_map.bboxFor(cur, _zoom));
if (_delta == null) {
_delta = tile.pointPosition(cur);
}
tile.setsTileReadyListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
refreshLayers = true;
repaint();
}
});
_tiles.addElement(new PositionedTile(new Point(posX, posY), tile));
}
posX += tileSize.getWidth();
}
posY += tileSize.getHeight();
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class Util method downloadUrlTo.
private static boolean downloadUrlTo(String url, String fileName, boolean showProgress, boolean background, boolean storage, ActionListener callback) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setReadResponseForErrors(false);
cr.setDuplicateSupported(true);
cr.setUrl(url);
if (callback != null) {
cr.addResponseListener(callback);
}
if (storage) {
cr.setDestinationStorage(fileName);
} else {
cr.setDestinationFile(fileName);
}
if (background) {
NetworkManager.getInstance().addToQueue(cr);
return true;
}
if (showProgress) {
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(cr);
d.dispose();
} else {
NetworkManager.getInstance().addToQueueAndWait(cr);
}
int rc = cr.getResponseCode();
return rc == 200 || rc == 201;
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class Toolbar method constructOnTopSideMenu.
private void constructOnTopSideMenu() {
if (sidemenuDialog == null) {
permanentSideMenuContainer = constructSideNavigationComponent();
final Form parent = getComponentForm();
parent.addPointerPressedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (Display.getInstance().getImplementation().isScrollWheeling()) {
return;
}
if (sidemenuDialog.isShowing()) {
if (evt.getX() > sidemenuDialog.getWidth()) {
parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
closeSideMenu();
evt.consume();
} else {
if (evt.getX() + Display.getInstance().convertToPixels(8) > sidemenuDialog.getWidth()) {
parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
} else {
parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
}
if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
evt.consume();
}
}
} else {
int displayWidth = Display.getInstance().getDisplayWidth();
final int sensitiveSection = displayWidth / getUIManager().getThemeConstant("sideSwipeSensitiveInt", 10);
if (evt.getX() < sensitiveSection) {
parent.putClientProperty("cn1$sidemenuCharged", Boolean.TRUE);
evt.consume();
} else {
parent.putClientProperty("cn1$sidemenuCharged", Boolean.FALSE);
permanentSideMenuContainer.pointerPressed(evt.getX(), evt.getY());
}
}
}
});
parent.addPointerDraggedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (Display.getInstance().getImplementation().isScrollWheeling()) {
return;
}
Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuCharged");
if (b != null && b.booleanValue()) {
parent.putClientProperty("cn1$sidemenuActivated", Boolean.TRUE);
showOnTopSidemenu(evt.getX(), false);
evt.consume();
} else {
if (sidemenuDialog.isShowing()) {
permanentSideMenuContainer.pointerDragged(evt.getX(), evt.getY());
evt.consume();
}
}
}
});
parent.addPointerReleasedListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (Display.getInstance().getImplementation().isScrollWheeling()) {
return;
}
Boolean b = (Boolean) parent.getClientProperty("cn1$sidemenuActivated");
if (b != null && b.booleanValue()) {
parent.putClientProperty("cn1$sidemenuActivated", null);
if (evt.getX() < parent.getWidth() / 4) {
closeSideMenu();
} else {
showOnTopSidemenu(-1, true);
}
evt.consume();
} else {
if (sidemenuDialog.isShowing()) {
if (!isComponentInOnTopSidemenu(parent.getComponentAt(evt.getX(), evt.getY()))) {
evt.consume();
}
permanentSideMenuContainer.pointerReleased(evt.getX(), evt.getY());
}
}
}
});
sidemenuDialog = new InteractionDialog(new BorderLayout());
sidemenuDialog.setFormMode(true);
sidemenuDialog.setUIID("Container");
sidemenuDialog.setDialogUIID("Container");
sidemenuDialog.getTitleComponent().remove();
sidemenuDialog.add(BorderLayout.CENTER, permanentSideMenuContainer);
if (sidemenuSouthComponent != null) {
sidemenuDialog.add(BorderLayout.SOUTH, sidemenuSouthComponent);
}
float size = 4.5f;
try {
size = Float.parseFloat(getUIManager().getThemeConstant("menuImageSize", "4.5"));
} catch (Throwable t) {
Log.e(t);
}
if (!parent.getUIManager().isThemeConstant("hideLeftSideMenuBool", false)) {
Image i = (Image) parent.getUIManager().getThemeImageConstant("sideMenuImage");
if (i != null) {
Command cmd = addCommandToLeftBar("", i, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (sidemenuDialog.isShowing()) {
closeSideMenu();
return;
}
showOnTopSidemenu(-1, false);
}
});
Image p = (Image) parent.getUIManager().getThemeImageConstant("sideMenuPressImage");
if (p != null) {
findCommandComponent(cmd).setPressedIcon(p);
}
} else {
addMaterialCommandToLeftBar("", FontImage.MATERIAL_MENU, size, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (sidemenuDialog.isShowing()) {
closeSideMenu();
return;
}
showOnTopSidemenu(-1, false);
}
});
}
}
}
}
Aggregations