Skip to content

Commit 2a5984e

Browse files
authored
Merge pull request #23 from codenameone/master
update
2 parents 02db4b3 + 2d44190 commit 2a5984e

File tree

57 files changed

+5344
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5344
-216
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*.jar
2424
*.class
2525
**/private/*
26+
**/.idea/*
2627
**/build/*
2728
**/dist/*
2829
*.zip
@@ -58,4 +59,4 @@ node_modules
5859
/Ports/CLDC11/build/
5960
/Ports/CLDC11/dist/
6061
/Samples/SampleProjectTemplate/nbproject/private/
61-
/Samples/config
62+
/Samples/config

CodenameOne/src/com/codename1/components/MediaPlayer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ public void run() {
256256
float pos = video.getTime();
257257
int offset = (int)(pos / dur * 100.0f);
258258
if(offset > -1 && offset < 101) {
259-
progress.setProgress(offset);
259+
if (progress != null) {
260+
progress.setProgress(offset);
261+
}
260262
}
261263
}
262264
}
@@ -435,9 +437,11 @@ private void initUI() {
435437
public void actionPerformed(ActionEvent evt) {
436438
float dur = video.getDuration();
437439
if(dur > 0) {
438-
float pos = progress.getProgress();
439-
int t = (int)(pos / 100.0f * dur);
440-
video.setTime(t);
440+
if (progress != null) {
441+
float pos = progress.getProgress();
442+
int t = (int)(pos / 100.0f * dur);
443+
video.setTime(t);
444+
}
441445
}
442446
}
443447
});

CodenameOne/src/com/codename1/components/SpanButton.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.codename1.ui.layouts.BoxLayout;
3434
import com.codename1.ui.layouts.FlowLayout;
3535
import com.codename1.ui.plaf.Style;
36+
import com.codename1.ui.plaf.UIManager;
3637

3738
/**
3839
* <p>
@@ -434,17 +435,25 @@ public void setAutoRelease(boolean autoRelease) {
434435

435436
@Override
436437
protected Dimension calcPreferredSize() {
438+
437439
int w = getWidth();
438440
int h = getHeight();
439-
Dimension dim = super.calcPreferredSize();
440-
setWidth(dim.getWidth());
441-
setHeight(dim.getHeight());
442-
setShouldCalcPreferredSize(true);
443-
444-
dim = super.calcPreferredSize();
441+
Dimension d = getLayout().getPreferredSize(this);
442+
setWidth(d.getWidth());
443+
setHeight(d.getHeight());
444+
d = getLayout().getPreferredSize(this);
445+
Style style = getStyle();
446+
if(style.getBorder() != null && d.getWidth() != 0 && d.getHeight() != 0) {
447+
d.setWidth(Math.max(style.getBorder().getMinimumWidth(), d.getWidth()));
448+
d.setHeight(Math.max(style.getBorder().getMinimumHeight(), d.getHeight()));
449+
}
450+
if(UIManager.getInstance().getLookAndFeel().isBackgroundImageDetermineSize() && style.getBgImage() != null) {
451+
d.setWidth(Math.max(style.getBgImage().getWidth(), d.getWidth()));
452+
d.setHeight(Math.max(style.getBgImage().getHeight(), d.getHeight()));
453+
}
445454
setWidth(w);
446455
setHeight(h);
447-
return dim;
456+
return d;
448457
}
449458

450459

CodenameOne/src/com/codename1/components/SpanLabel.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.codename1.ui.layouts.BoxLayout;
3232
import com.codename1.ui.layouts.FlowLayout;
3333
import com.codename1.ui.plaf.Style;
34+
import com.codename1.ui.plaf.UIManager;
3435

3536
/**
3637
* <p>A multi line label component that can be easily localized, this is simply based
@@ -416,17 +417,25 @@ public boolean isTextSelectionEnabled() {
416417

417418
@Override
418419
protected Dimension calcPreferredSize() {
420+
419421
int w = getWidth();
420422
int h = getHeight();
421-
Dimension dim = super.calcPreferredSize();
422-
setWidth(dim.getWidth());
423-
setHeight(dim.getHeight());
424-
setShouldCalcPreferredSize(true);
425-
426-
dim = super.calcPreferredSize();
423+
Dimension d = getLayout().getPreferredSize(this);
424+
setWidth(d.getWidth());
425+
setHeight(d.getHeight());
426+
d = getLayout().getPreferredSize(this);
427+
Style style = getStyle();
428+
if(style.getBorder() != null && d.getWidth() != 0 && d.getHeight() != 0) {
429+
d.setWidth(Math.max(style.getBorder().getMinimumWidth(), d.getWidth()));
430+
d.setHeight(Math.max(style.getBorder().getMinimumHeight(), d.getHeight()));
431+
}
432+
if(UIManager.getInstance().getLookAndFeel().isBackgroundImageDetermineSize() && style.getBgImage() != null) {
433+
d.setWidth(Math.max(style.getBgImage().getWidth(), d.getWidth()));
434+
d.setHeight(Math.max(style.getBgImage().getHeight(), d.getHeight()));
435+
}
427436
setWidth(w);
428437
setHeight(h);
429-
return dim;
438+
return d;
430439
}
431440

432441

CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ public boolean usesInvokeAndBlockForEditString() {
376376
*/
377377
public abstract void editString(Component cmp, int maxSize, int constraint, String text, int initiatingKeycode);
378378

379+
public boolean nativeEditorPaintsHint() {
380+
return true;
381+
}
382+
379383
/**
380384
* Returns true if we are currently editing a component
381385
* @return whether a component is being edited

CodenameOne/src/com/codename1/io/ConnectionRequest.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.codename1.ui.events.ActionEvent;
3535
import com.codename1.ui.events.ActionListener;
3636
import com.codename1.ui.util.EventDispatcher;
37+
import com.codename1.util.AsyncResource;
3738
import com.codename1.util.Base64;
3839
import com.codename1.util.Callback;
3940
import com.codename1.util.CallbackAdapter;
@@ -2417,6 +2418,48 @@ public static Map<String, Object> fetchJSON(String url) throws IOException {
24172418
return result;
24182419
}
24192420

2421+
/**
2422+
* Fetches JSON asynchronously.
2423+
* @param url The URL to fetch.
2424+
* @return AsyncResource that will resolve with either an exception or the parsed JSON data.
2425+
* @since 7.0
2426+
*/
2427+
public static AsyncResource<Map<String, Object>> fetchJSONAsync(String url) {
2428+
final AsyncResource<Map<String, Object>> out = new AsyncResource<Map<String, Object>>();
2429+
final ConnectionRequest cr = new ConnectionRequest();
2430+
cr.setFailSilently(true);
2431+
cr.setPost(false);
2432+
cr.setUrl(url);
2433+
cr.addResponseListener(new ActionListener<NetworkEvent>() {
2434+
@Override
2435+
public void actionPerformed(NetworkEvent evt) {
2436+
if (out.isDone()) {
2437+
return;
2438+
}
2439+
if(cr.getResponseData() == null) {
2440+
if(cr.failureException != null) {
2441+
out.error(new IOException(cr.failureException.toString()));
2442+
return;
2443+
} else {
2444+
out.error(new IOException("Server returned error code: " + cr.failureErrorCode));
2445+
return;
2446+
}
2447+
}
2448+
JSONParser jp = new JSONParser();
2449+
Map<String,Object> result = null;
2450+
try {
2451+
result = jp.parseJSON(new InputStreamReader(new ByteArrayInputStream(cr.getResponseData()), "UTF-8"));
2452+
} catch (IOException ex) {
2453+
out.error(ex);
2454+
return;
2455+
}
2456+
out.complete(result);
2457+
}
2458+
});
2459+
NetworkManager.getInstance().addToQueue(cr);
2460+
return out;
2461+
}
2462+
24202463
/**
24212464
* Downloads an image to a specified storage file asynchronously and calls the onSuccessCallback with the resulting image.
24222465
* If useCache is true, then this will first try to load the image from Storage if it exists.
@@ -2431,6 +2474,44 @@ public void downloadImageToStorage(String storageFile, final SuccessCallback<Ima
24312474
downloadImage(onSuccess, onFail, useCache);
24322475
}
24332476

2477+
/**
2478+
* Downloads an image to a specified storage file asynchronously returning an AsyncResource that resolves to the resulting image..
2479+
* @param storageFile The storage file where the file should be saved.
2480+
* @return AsyncResource<Image> that will resolve to the loaded image.
2481+
* @since 7.0
2482+
*/
2483+
public AsyncResource<Image> downloadImageToStorage(String storageFile) {
2484+
return downloadImageToStorage(storageFile, true);
2485+
}
2486+
2487+
/**
2488+
* Downloads an image to a specified storage file asynchronously returning an AsyncResource that resolves to the resulting image..
2489+
* If useCache is true, then this will first try to load the image from Storage if it exists.
2490+
* @param storageFile The storage file where the file should be saved.
2491+
* @param useCache If true, then this will first check the storage to see if the image is already downloaded.
2492+
* @return AsyncResource<Image> that will resolve to the loaded image.
2493+
* @since 7.0
2494+
*/
2495+
public AsyncResource<Image> downloadImageToStorage(String storageFile, boolean useCache) {
2496+
final AsyncResource<Image> out = new AsyncResource<Image>();
2497+
downloadImageToStorage(storageFile, new SuccessCallback<Image>() {
2498+
@Override
2499+
public void onSucess(Image value) {
2500+
if (!out.isDone()) {
2501+
out.complete(value);
2502+
}
2503+
}
2504+
}, new FailureCallback<Image>() {
2505+
@Override
2506+
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
2507+
if (!out.isDone()) {
2508+
out.error(err);
2509+
}
2510+
}
2511+
}, useCache);
2512+
return out;
2513+
}
2514+
24342515
/**
24352516
* Downloads an image to a specified storage file asynchronously and calls the onSuccessCallback with the resulting image.
24362517
* If useCache is true, then this will first try to load the image from Storage if it exists.
@@ -2468,6 +2549,53 @@ public void downloadImageToStorage(String storageFile, SuccessCallback<Image> on
24682549
downloadImageToStorage(storageFile, onSuccess, onFail, true);
24692550
}
24702551

2552+
/**
2553+
* Downloads an image to a the file system asynchronously returning an AsyncResource object that resolves to the loaded image..
2554+
* If useCache is true, then this will first try to load the image from Storage if it exists.
2555+
*
2556+
* @param file The storage file where the file should be saved.
2557+
* @param useCache If true, then this will first check the storage to see if the image is already downloaded.
2558+
* @return AsyncResource resolving to the downloaded image.
2559+
* @since 7.0
2560+
*/
2561+
public AsyncResource<Image> downloadImageToFileSystem(String file, boolean useCache) {
2562+
final AsyncResource<Image> out = new AsyncResource<Image>();
2563+
downloadImageToFileSystem(file, new SuccessCallback<Image>() {
2564+
@Override
2565+
public void onSucess(Image value) {
2566+
if (out.isDone()) {
2567+
return;
2568+
}
2569+
out.complete(value);
2570+
}
2571+
}, new FailureCallback<Image>() {
2572+
@Override
2573+
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
2574+
if (out.isDone()) {
2575+
return;
2576+
}
2577+
out.error(err);
2578+
}
2579+
}, useCache);
2580+
return out;
2581+
}
2582+
2583+
2584+
/**
2585+
* Downloads an image to a the file system asynchronously returning an AsyncResource object that resolves to the loaded image..
2586+
* If useCache is true, then this will first try to load the image from Storage if it exists. This is a wrapper around {@link #downloadImageToFileSystem(java.lang.String, boolean) }
2587+
* with {@literal true} as the 2nd parameter.
2588+
*
2589+
* @param file The storage file where the file should be saved.
2590+
2591+
* @return AsyncResource resolving to the downloaded image.
2592+
* @since 7.0
2593+
*/
2594+
public AsyncResource<Image> downloadImageToFileSystem(String file) {
2595+
return downloadImageToFileSystem(file, true);
2596+
}
2597+
2598+
24712599
/**
24722600
* Downloads an image to a the file system asynchronously and calls the onSuccessCallback with the resulting image.
24732601
* If useCache is true, then this will first try to load the image from Storage if it exists.

CodenameOne/src/com/codename1/io/NetworkManager.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.codename1.ui.events.ActionEvent;
3131
import com.codename1.ui.events.ActionListener;
3232
import com.codename1.ui.util.EventDispatcher;
33+
import com.codename1.util.AsyncResource;
3334
import java.io.IOException;
3435
import java.io.InputStream;
3536
import java.util.ArrayList;
@@ -623,6 +624,53 @@ public void addDefaultHeader(String key, String value) {
623624
userHeaders.put(key, value);
624625
}
625626

627+
/**
628+
* Identical to add to queue but returns an AsyncResource object that will resolve to
629+
* the ConnectionRequest.
630+
*
631+
* @param request the request object to add.
632+
* @return AsyncResource resolving to the connection request on complete.
633+
* @since 7.0
634+
*/
635+
public AsyncResource<ConnectionRequest> addToQueueAsync(final ConnectionRequest request) {
636+
final AsyncResource<ConnectionRequest> out = new AsyncResource<ConnectionRequest>();
637+
class WaitingClass implements ActionListener<NetworkEvent> {
638+
639+
640+
public void actionPerformed(NetworkEvent e) {
641+
if(e.getError() != null) {
642+
643+
removeProgressListener(this);
644+
removeErrorListener(this);
645+
if (!out.isDone()) {
646+
out.error(e.getError());
647+
}
648+
return;
649+
}
650+
if(e.getConnectionRequest() == request) {
651+
if(e.getProgressType() == NetworkEvent.PROGRESS_TYPE_COMPLETED) {
652+
if(request.retrying) {
653+
request.retrying = false;
654+
return;
655+
}
656+
657+
removeProgressListener(this);
658+
removeErrorListener(this);
659+
if (!out.isDone()) {
660+
out.complete(request);
661+
}
662+
return;
663+
}
664+
}
665+
}
666+
}
667+
WaitingClass w = new WaitingClass();
668+
addProgressListener(w);
669+
addErrorListener(w);
670+
addToQueue(request);
671+
return out;
672+
}
673+
626674
/**
627675
* Identical to add to queue but waits until the request is processed in the queue,
628676
* this is useful for completely synchronous operations.

CodenameOne/src/com/codename1/io/rest/RequestBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,13 @@ public Response<String> getAsString() {
404404
CN.addToQueueAndWait(request);
405405
Response res = null;
406406
try {
407-
res = new Response(request.getResponseCode(), new String(request.getResponseData(), "UTF-8"), request.getResponseErrorMessage());
407+
byte[] respData = request.getResponseData();
408+
String resp = null;
409+
if(respData != null) {
410+
resp = new String(respData, "UTF-8");
411+
}
412+
res = new Response(request.getResponseCode(), resp,
413+
request.getResponseErrorMessage());
408414
} catch (UnsupportedEncodingException ex) {
409415
ex.printStackTrace();
410416
}

0 commit comments

Comments
 (0)