use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class FaceBookAccess method getFaceBookObject.
/**
* This method returns immediately and will call the callback when it returns with
* the FaceBook Object data.
*
* @param faceBookId the object id that we would like to query
* @param callback the callback that should be updated when the data arrives
*/
public void getFaceBookObject(String faceBookId, final ActionListener callback) throws IOException {
checkAuthentication();
final FacebookRESTService con = new FacebookRESTService(token, faceBookId, "", false);
con.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!con.isAlive()) {
return;
}
if (callback != null) {
callback.actionPerformed(evt);
}
}
});
if (slider != null) {
SliderBridge.bindProgress(con, slider);
}
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
current = con;
NetworkManager.getInstance().addToQueue(con);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class FaceBookAccess method getAlbumPhotos.
/**
* This method returns a list model of photos that automatically fetches additional images as necessary
* @param targetList required for the image download code
* @param albumId the id of the album
* @param photoCount the number of photos within the album
* @param placeholder a placeholder image that will determine the size of the images requested
* @return the list of the images
*/
private ListModel getAlbumPhotos(final Component targetList, final String albumId, final int photoCount, final Image placeholder) {
if (!isAuthenticated()) {
return null;
}
Hashtable[] h = new Hashtable[photoCount];
int hlen = h.length;
for (int iter = 0; iter < hlen; iter++) {
h[iter] = new Hashtable();
h[iter].put("photo", placeholder);
if (iter < 30) {
h[iter].put("fetching", Boolean.TRUE);
}
}
DefaultListModel dl = new DefaultListModel((Object[]) h) {
public Object getItem(int offset) {
Hashtable hash = (Hashtable) super.getItemAt(offset);
if (!hash.containsKey("fetching")) {
int limit = Math.min(30, photoCount - offset);
for (int iter = 0; iter < limit; iter++) {
Hashtable current = (Hashtable) super.getItemAt(iter + offset);
if (current.containsKey("fetching")) {
break;
}
current.put("fetching", Boolean.TRUE);
}
FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
con.setResponseDestination(this);
con.setResponseOffset(0);
con.addArgument("limit", "" + limit);
con.addArgument("offset", "" + offset);
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
NetworkManager.getInstance().addToQueueAndWait(con);
for (int iter = 0; iter < limit; iter++) {
Hashtable current = (Hashtable) getItemAt(iter + offset);
ImageDownloadService.createImageToStorage((String) current.get("photo"), targetList, iter + offset, "photo", ((String) current.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
}
}
return hash;
}
};
FacebookRESTService con = new FacebookRESTService(token, albumId, FacebookRESTService.PHOTOS, false);
con.setResponseDestination(dl);
con.setResponseOffset(0);
con.addArgument("limit", "30");
con.addArgument("offset", "0");
for (int i = 0; i < responseCodeListeners.size(); i++) {
con.addResponseCodeListener((ActionListener) responseCodeListeners.elementAt(i));
}
NetworkManager.getInstance().addToQueueAndWait(con);
for (int iter = 0; iter < Math.min(30, photoCount); iter++) {
Hashtable hash = (Hashtable) dl.getItemAt(iter);
ImageDownloadService.createImageToStorage((String) hash.get("photo"), targetList, iter, "photo", ((String) hash.get("id")) + placeholder.getHeight(), placeholder, ConnectionRequest.PRIORITY_NORMAL);
}
return dl;
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class ConnectionRequest method downloadImage.
private void downloadImage(final SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail, boolean useCache) {
setReadResponseForErrors(false);
if (useCache) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
if (getDestinationFile() != null) {
String file = getDestinationFile();
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.exists(file)) {
try {
EncodedImage img = EncodedImage.create(fs.openInputStream(file), (int) fs.getLength(file));
if (img == null) {
throw new IOException("Failed to load image at " + file);
}
CallbackDispatcher.dispatchSuccess(onSuccess, img);
} catch (Exception ex) {
CallbackDispatcher.dispatchError(onFail, ex);
}
} else {
downloadImage(onSuccess, onFail, false);
}
} else if (getDestinationStorage() != null) {
String file = getDestinationStorage();
Storage fs = Storage.getInstance();
if (fs.exists(file)) {
try {
EncodedImage img = EncodedImage.create(fs.createInputStream(file), fs.entrySize(file));
if (img == null) {
throw new IOException("Failed to load image at " + file);
}
CallbackDispatcher.dispatchSuccess(onSuccess, img);
} catch (Exception ex) {
CallbackDispatcher.dispatchError(onFail, ex);
}
} else {
downloadImage(onSuccess, onFail, false);
}
}
}
});
} else {
final ActionListener onDownload = new ActionListener<NetworkEvent>() {
public void actionPerformed(NetworkEvent nevt) {
int rc = nevt.getResponseCode();
if (rc == 200 || rc == 201) {
downloadImage(onSuccess, onFail, true);
} else {
if (nevt.getError() == null) {
nevt.setError(new IOException("Failed to get image: Code was " + nevt.getResponseCode()));
}
CallbackDispatcher.dispatchError(onFail, nevt.getError());
}
removeResponseListener(this);
}
};
addResponseListener(onDownload);
NetworkManager.getInstance().addToQueue(this);
}
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class ConnectionRequest method addResponseCodeListener.
/**
* Adds a listener that would be notified on the CodenameOne thread of a response code that
* is not a 200 (OK) or 301/2 (redirect) response code.
*
* @param a listener
*/
public void addResponseCodeListener(ActionListener<NetworkEvent> a) {
if (responseCodeListeners == null) {
responseCodeListeners = new EventDispatcher();
responseCodeListeners.setBlocking(false);
}
responseCodeListeners.addListener(a);
}
use of com.codename1.ui.events.ActionListener in project CodenameOne by codenameone.
the class ClearableTextField method wrap.
/**
* Wraps the given text field with a UI that will allow us to clear it
* @param tf the text field
* @param iconSize size in millimeters for the clear icon, -1 for default size
* @return a Container that should be added to the UI instead of the actual text field
*/
public static ClearableTextField wrap(final TextArea tf, float iconSize) {
ClearableTextField cf = new ClearableTextField();
Button b = new Button("", tf.getUIID());
if (iconSize > 0) {
FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR, iconSize);
} else {
FontImage.setMaterialIcon(b, FontImage.MATERIAL_CLEAR);
}
removeCmpBackground(tf);
removeCmpBackground(b);
cf.setUIID(tf.getUIID());
cf.add(BorderLayout.CENTER, tf);
cf.add(BorderLayout.EAST, b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
tf.stopEditing();
tf.setText("");
tf.startEditingAsync();
}
});
return cf;
}
Aggregations