Skip to content

Commit de9be48

Browse files
author
zhourenjian
committed
Fix bug that setting focus on invisible element in IE will trigger an exception.
1 parent 5278ffd commit de9be48

File tree

9 files changed

+27
-33
lines changed

9 files changed

+27
-33
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,14 @@ public static Point getImageSize(Image image) {
908908
return new Point(w, h);
909909
}
910910

911+
/**
912+
* @j2sNative
913+
* try {
914+
* handle.focus();
915+
* } catch (e) {}
916+
*/
911917
public static void SetFocus(Element handle) {
912-
handle.focus();
918+
// handle.focus();
913919
}
914920

915921

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,13 +835,16 @@ public void setAlignment (int alignment) {
835835
if ((style & SWT.LEFT) != 0) {
836836
btnText.style.textAlign = "left";
837837
handleStyle.backgroundPosition = "left center";
838-
} if ((style & SWT.CENTER) != 0) {
838+
} else if ((style & SWT.CENTER) != 0) {
839839
btnText.style.textAlign = "center";
840840
handleStyle.backgroundPosition = "center center";
841841
} else if ((style & SWT.RIGHT) != 0) {
842842
btnText.style.textAlign = "right";
843843
handleStyle.backgroundPosition = "right center";
844844
}
845+
if (text != null && text.trim().length() != 0) {
846+
handleStyle.backgroundPosition = "left center";
847+
}
845848
}
846849
/*
847850
int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
@@ -879,9 +882,7 @@ void setDefault (boolean value) {
879882
OS.SendMessage (handle, OS.BM_SETSTYLE, bits, 1);
880883
*/
881884
if (value) {
882-
try {
883-
handle.focus();
884-
} catch (Error e) { }
885+
OS.SetFocus(handle); // handle.focus();
885886
}
886887
}
887888

@@ -1048,6 +1049,9 @@ public void setImage (Image image) {
10481049
bgXPos = "right";
10491050
} else if ((style & SWT.CENTER) != 0) {
10501051
bgXPos = "center";
1052+
if (text != null && text.trim().length() != 0) {
1053+
bgXPos = "left";
1054+
}
10511055
} else {
10521056
bgXPos = "left";
10531057
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public void run() {
546546
OS.addCSSClass(containerHandle.childNodes[0], "color-dialog-custom");
547547
// parent.display.asyncExec(new Runnable() {
548548
// public void run() {
549-
// hText.focus();
549+
// OS.SetFocus(hText); //hText.focus();
550550
// }
551551
// });
552552
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ void hide(){
728728

729729
}
730730
selectInput.className = "combo-select-box-invisible" + (isSimple ? "" : " combo-select-box-notsimple");
731-
dropDownButton.focus();
731+
//dropDownButton.focus();
732+
OS.SetFocus(dropDownButton);
732733
}
733734

734735
void show(){
@@ -783,11 +784,7 @@ void show(){
783784
selectInput.style.height = bounds.height + "px";
784785
}
785786
selectInput.style.width = bounds.width +"px";
786-
try {
787-
selectInput.focus();
788-
} catch (Throwable e) {
789-
// TODO: handle exception
790-
}
787+
OS.SetFocus(selectInput); // selectInput.focus();
791788
}
792789

793790
void updateSelection(){

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public boolean updateShellBounds(int x, int y, int w, int h) {
746746
}
747747
// contentHandle.onclick = new RunnableCompatibility(){
748748
// public void run(){
749-
// contentHandle.focus();
749+
// OS.SetFocus(contentHandle); //contentHandle.focus();
750750
// }
751751
// };
752752
contentHandle.onkeydown = new RunnableCompatibility() {
@@ -2263,7 +2263,7 @@ public void run() {
22632263
public void run() {
22642264
bringToTop();
22652265
if(contentHandle != null){
2266-
contentHandle.focus();
2266+
OS.SetFocus(contentHandle); //contentHandle.focus();
22672267
}
22682268
toReturn(true);
22692269
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ void _setVisible (boolean visible) {
262262
}
263263
style.left = left + "px";
264264
style.top = top + "px";
265-
try {
266-
btnFocus.focus();
267-
} catch (Error err) {}
265+
OS.SetFocus(btnFocus); //btnFocus.focus();
268266
if (hooks(SWT.Show)) sendEvent(SWT.Show);
269267
} else {
270268
style.display = "none";

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ public void run() {
303303
if (!isEnabled()) {
304304
display.timerExec(40, new Runnable() {
305305
public void run() {
306-
try {
307-
parent.btnFocus.focus();
308-
} catch (Error e) { }
306+
OS.SetFocus(parent.btnFocus); //parent.btnFocus.focus();
309307
}
310308
});
311309
return;
@@ -1388,9 +1386,7 @@ void showSubMenu() {
13881386
menu.handle.style.top = rect.y + "px";
13891387
menu.handle.style.zIndex = "1" + window.currentTopZIndex;
13901388
menu.handle.style.display = "block";
1391-
try {
1392-
menu.btnFocus.focus();
1393-
} catch (Error err) {}
1389+
OS.SetFocus(menu.btnFocus); // menu.btnFocus.focus();
13941390
}
13951391

13961392
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void run() {
167167
return;
168168
}
169169
// handle.focus();
170-
parent.handle.focus();
170+
OS.SetFocus(parent.handle); //parent.handle.focus();
171171
// Element element = handle.childNodes[0].childNodes[0].childNodes[1];
172172
// element.className = "tree-item-text-selected";
173173
HTMLEvent evt = (HTMLEvent) getEvent();

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void createHandle () {
240240
}
241241
handle.onfocus = new RunnableCompatibility() {
242242
public void run() {
243-
textHandle.focus();
243+
OS.SetFocus(textHandle); //textHandle.focus();
244244
}
245245
};
246246
Element wrapper = document.createElement("DIV");
@@ -2860,15 +2860,8 @@ void enableWidget (boolean enabled) {
28602860

28612861
public boolean forceFocus() {
28622862
boolean ret = super.forceFocus();
2863-
//this.textHandle.focus();
2864-
/**
2865-
* @j2sNative
2866-
* try {
2867-
* this.textHandle.focus();
2868-
* } catch (e) {}
2869-
*/ {}
2863+
OS.SetFocus(textHandle); //this.textHandle.focus();
28702864
return ret;
28712865
}
28722866

2873-
28742867
}

0 commit comments

Comments
 (0)