Skip to content

Commit 1bad590

Browse files
author
zhourenjian
committed
Add image cache for image size
Refactor Browser to avoid unexpected exceptions
1 parent 0419d7e commit 1bad590

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/browser/Browser.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.eclipse.swt.SWTError;
1515
import org.eclipse.swt.SWTException;
1616
import org.eclipse.swt.internal.browser.OS;
17-
import org.eclipse.swt.internal.xhtml.ContentWindow;
1817
import org.eclipse.swt.internal.xhtml.Element;
1918
import org.eclipse.swt.internal.xhtml.document;
2019
import org.eclipse.swt.widgets.Composite;
@@ -984,12 +983,21 @@ public void refresh() {
984983
checkWidget();
985984
// int[] rgdispid = auto.getIDsOfNames(new String[] { "Refresh" }); //$NON-NLS-1$
986985
// auto.invoke(rgdispid[0]);
987-
if (browserHandle != null) {
988-
if (browserHandle.contentWindow != null) {
989-
browserHandle.contentWindow.reload();
990-
} else { // Opera
991-
browserHandle.src = url; //?
992-
}
986+
if (browserHandle != null)
987+
/**
988+
* @j2sNative
989+
* try {
990+
* var win = this.browserHandle.contentWindow;
991+
* if (win != null) {
992+
* win.reload();
993+
* } else { // Opera ?
994+
* this.browserHandle.src = this.url;
995+
* }
996+
* } catch (e) {
997+
* this.browserHandle.src = this.url;
998+
* }
999+
*/ {
1000+
browserHandle.src = url;
9931001
}
9941002
}
9951003

@@ -1476,12 +1484,21 @@ public boolean setUrl(String url) {
14761484
*/
14771485
this.url = url;
14781486

1479-
if (browserHandle != null) {
1480-
if (browserHandle.contentWindow != null) {
1481-
browserHandle.contentWindow.location = url;
1482-
} else { // Opera
1483-
browserHandle.src = url;
1484-
}
1487+
if (browserHandle != null)
1488+
/**
1489+
* @j2sNative
1490+
* try {
1491+
* var win = this.browserHandle.contentWindow;
1492+
* if (win != null) {
1493+
* win.location = this.url;
1494+
* } else {
1495+
* this.browserHandle.src = this.url;
1496+
* }
1497+
* } catch (e) {
1498+
* this.browserHandle.src = this.url;
1499+
* }
1500+
*/ {
1501+
browserHandle.src = url;
14851502
}
14861503
back = true;
14871504
return true;

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/internal/browser/OS.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,29 @@ public static int getFixedBodyOffsetLeft() {
893893
return bcScrollLeft;
894894
}
895895

896+
private static Object imageCaches = new Object();
897+
896898
public static Point getImageSize(Image image) {
897899
int w = 16, h = 16; // Default to 16x16 for common
898900
if (image.width == 0 && image.height == 0) {
899901
if (image.url != null && image.url.length() != 0) {
900-
org.eclipse.swt.internal.xhtml.Image img = new org.eclipse.swt.internal.xhtml.Image ();
901-
img.src = image.url;
902+
org.eclipse.swt.internal.xhtml.Image img = null;
903+
/**
904+
* @j2sNative
905+
* img = O$.imageCaches[image.url];
906+
*/ {}
907+
if (img == null) {
908+
img = new org.eclipse.swt.internal.xhtml.Image ();
909+
img.src = image.url;
910+
}
902911
image.width = img.width;
903912
image.height = img.height;
904913
w = img.width;
905914
h = img.height;
915+
/**
916+
* @j2sNative
917+
* O$.imageCaches[image.url] = img;
918+
*/ {}
906919
// TODO: The above method to find out width & height is unsafe!
907920
// TODO: Maybe the image may fail to be loaded!
908921
} // else default 16x16

sources/net.sf.j2s.java.org.eclipse.swt/src/org/eclipse/swt/widgets/Button.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,7 @@ public void setImage (Image image) {
999999

10001000
CSSStyle handleStyle = null;
10011001
handleStyle = btnText.style;
1002-
org.eclipse.swt.internal.xhtml.Image img = new org.eclipse.swt.internal.xhtml.Image ();
1003-
img.src = this.image.url;
1004-
if (image.width == 0) {
1005-
this.image.width = img.width;
1006-
}
1007-
if (image.height == 0) {
1008-
this.image.height = img.height;
1009-
}
1002+
OS.getImageSize(this.image);
10101003
if ((style & (SWT.RADIO | SWT.CHECK)) != 0) {
10111004
//// handleStyle.fontSize = this.image.height + "px";
10121005
// handleStyle.display = "block";

0 commit comments

Comments
 (0)