X Tutup
Skip to content

Commit a341393

Browse files
author
jossonsmith
committed
Implement Menu and MenuItem;
Refactor widgets' CSS class related codes
1 parent 1ce2e77 commit a341393

File tree

16 files changed

+468
-294
lines changed

16 files changed

+468
-294
lines changed

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

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static int getContainerHeight(Object container) {
158158
return max;
159159
}
160160

161-
public static void insertText(Object el, String text) {
161+
public static String insertText(Object el, String text) {
162162
String[] lines = null;
163163
Element handle = (Element) el;
164164
// el.appendChild (document.createTextNode (text));
@@ -167,7 +167,7 @@ public static void insertText(Object el, String text) {
167167
if (!((/[\r\n\t&]/).test (text))) {
168168
handle.style.display = "inline";
169169
handle.appendChild(document.createTextNode(text));
170-
return ;
170+
return text;
171171
}
172172
var c160 = String.fromCharCode (160);
173173
var c160x8 = c160 + c160 + c160 + c160 + c160 + c160 + c160 + c160;
@@ -185,7 +185,7 @@ public static void insertText(Object el, String text) {
185185
if (!((/[\r\n\t&]/).test (b))) {
186186
d.style.display = "inline";
187187
d.appendChild(document.createTextNode(b));
188-
return ;
188+
return b;
189189
}
190190
var z = String.fromCharCode (160);
191191
var w = z + z + z + z + z + z + z + z;
@@ -235,6 +235,7 @@ public static void insertText(Object el, String text) {
235235
lineEl.appendChild (span);
236236
//span.style.whiteSpace = "nowrap";
237237
span.appendChild (document.createTextNode ("" + c));
238+
text = "" + c;
238239
lastIndex = idx + 2;
239240
idx = line.indexOf ('&', lastIndex);
240241
}
@@ -259,6 +260,7 @@ public static void insertText(Object el, String text) {
259260
*/ {}
260261
lineEl.appendChild (document.createTextNode (s));
261262
}
263+
return text;
262264
}
263265

264266
private static String wrapCSS(String a) {
@@ -531,4 +533,146 @@ public static void updateArrowSize(Object el, int style, int cx, int cy) {
531533
}
532534
}
533535

536+
public static boolean existedCSSClass(Object el, String cssClazz) {
537+
Element e = (Element) el;
538+
String className = e.className;
539+
if (className == null || className.length() == 0) {
540+
return false;
541+
}
542+
String[] clazz = className.split("\\s");
543+
for (int i = 0; i < clazz.length; i++) {
544+
if (clazz[i] == cssClazz) {
545+
return true;
546+
}
547+
}
548+
return false;
549+
}
550+
551+
public static boolean removeCSSClass(Object el, String cssClazz) {
552+
Element e = (Element) el;
553+
String className = e.className;
554+
if (className == null || className.length() == 0) {
555+
return false;
556+
}
557+
String[] clazz = className.split("\\s");
558+
boolean existed = false;
559+
for (int i = 0; i < clazz.length; i++) {
560+
if (clazz[i] == cssClazz) {
561+
existed = true;
562+
for (int j = i; j < clazz.length - 1; j++) {
563+
clazz[j] = clazz[j + 1];
564+
}
565+
/**
566+
* @j2sNative
567+
* clazz.length--;
568+
*/ {}
569+
break;
570+
}
571+
}
572+
if (existed)
573+
/**
574+
* @j2sNative
575+
* e.className = clazz.join (" ");
576+
*/ {}
577+
return existed;
578+
}
579+
580+
public static boolean addCSSClass(Object el, String cssClazz) {
581+
Element e = (Element) el;
582+
String className = e.className;
583+
if (className == null || className.length() == 0) {
584+
e.className = cssClazz;
585+
return true;
586+
}
587+
String[] clazz = className.split("\\s");
588+
for (int i = 0; i < clazz.length; i++) {
589+
if (clazz[i] == cssClazz) {
590+
return false;
591+
}
592+
}
593+
clazz[clazz.length] = cssClazz;
594+
/**
595+
* @j2sNative
596+
* e.className = clazz.join (" ");
597+
*/ {}
598+
return true;
599+
}
600+
601+
public static void toggleCSSClass(Object el, String cssClazz) {
602+
Element e = (Element) el;
603+
String className = e.className;
604+
if (className == null || className.length() == 0) {
605+
e.className = cssClazz;
606+
return;
607+
}
608+
String[] clazz = className.split("\\s");
609+
for (int i = 0; i < clazz.length; i++) {
610+
if (clazz[i] == cssClazz) {
611+
for (int j = i; j < clazz.length - 1; j++) {
612+
clazz[j] = clazz[j + 1];
613+
}
614+
/**
615+
* @j2sNative
616+
* clazz.length--;
617+
* e.className = clazz.join (" ");
618+
*/ {}
619+
return;
620+
}
621+
}
622+
clazz[clazz.length] = cssClazz;
623+
/**
624+
* @j2sNative
625+
* e.className = clazz.join (" ");
626+
*/ {}
627+
}
628+
629+
/**
630+
* Keep or remove the given CSS class for the given element.
631+
* It's similar to the following code:
632+
* <pre>
633+
* if (kept) {
634+
* OS.addCSSClass(el, cssClazz);
635+
* } else {
636+
* OS.removeCSSClass(el, cssClazz);
637+
* }
638+
* </pre>
639+
*
640+
* @param el
641+
* @param cssClazz
642+
* @param kept kept or remove CSS class
643+
*/
644+
public static void updateCSSClass(Object el, String cssClazz, boolean kept) {
645+
Element e = (Element) el;
646+
String className = e.className;
647+
if (className == null || className.length() == 0) {
648+
if (kept) {
649+
e.className = cssClazz;
650+
}
651+
return;
652+
}
653+
String[] clazz = className.split("\\s");
654+
for (int i = 0; i < clazz.length; i++) {
655+
if (clazz[i] == cssClazz) {
656+
if (kept) {
657+
return;
658+
}
659+
for (int j = i; j < clazz.length - 1; j++) {
660+
clazz[j] = clazz[j + 1];
661+
}
662+
/**
663+
* @j2sNative
664+
* clazz.length--;
665+
* e.className = clazz.join (" ");
666+
*/ {}
667+
return;
668+
}
669+
}
670+
if (kept) {
671+
clazz[clazz.length] = cssClazz;
672+
/**
673+
* @j2sNative
674+
* e.className = clazz.join (" ");
675+
*/ {}
676+
}
677+
}
534678
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static Rectangle popupMenu(Rectangle bounds, Rectangle rect, int width, i
145145
}
146146
}
147147
} else {
148-
x = rect.x + rect.width;
148+
x = rect.x;
149149
}
150150
}
151151
}

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,7 @@ void enableWidget (boolean enabled) {
551551
}
552552
*/
553553
btnHandle.disabled = !enabled;
554-
String cssName = handle.className;
555-
if (cssName == null) cssName = "";
556-
String key = "button-disabled";
557-
int idx = cssName.indexOf(key);
558-
if (!enabled) {
559-
if (idx == -1) {
560-
handle.className += " " + key;
561-
}
562-
} else {
563-
if (idx != -1) {
564-
handle.className = cssName.substring(0, idx) + cssName.substring(idx + key.length());
565-
}
566-
}
554+
OS.updateCSSClass(btnHandle, "button-disabled", !enabled);
567555
}
568556

569557
/**
@@ -658,7 +646,7 @@ public boolean getSelection () {
658646
*/
659647
if ((style & SWT.TOGGLE) != 0) {
660648
//System.out.println(btnHandle.className);
661-
return (btnHandle.className != null && btnHandle.className.indexOf("button-selected") != -1);
649+
return OS.existedCSSClass(btnHandle, "button-selected");
662650
} else if ((style & (SWT.RADIO | SWT.CHECK)) != 0) {
663651
return btnHandle.checked;
664652
}
@@ -1031,20 +1019,8 @@ boolean setSavedFocus () {
10311019
public void setSelection (boolean selected) {
10321020
checkWidget ();
10331021
if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return;
1034-
if ((style & SWT.TOGGLE) != 0) {
1035-
String cssName = btnHandle.className;
1036-
if (cssName == null) cssName = "";
1037-
String key = "button-selected";
1038-
int idx = cssName.indexOf(key);
1039-
if (selected) {
1040-
if (idx == -1) {
1041-
btnHandle.className += " " + key;
1042-
}
1043-
} else {
1044-
if (idx != -1) {
1045-
btnHandle.className = cssName.substring(0, idx) + cssName.substring(idx + key.length());
1046-
}
1047-
}
1022+
if ((style & SWT.TOGGLE) != 0) {
1023+
OS.updateCSSClass(btnHandle, "button-selected", selected);
10481024
} else if ((style & (SWT.RADIO | SWT.CHECK)) != 0) {
10491025
btnHandle.checked = selected;
10501026
}

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

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -611,20 +611,15 @@ public void run() {
611611

612612
void createSelect() {
613613
selectInput = document.createElement("SELECT");
614+
selectInput.style.top = height + "px";
615+
selectInput.style.left = textInput.style.left;
616+
selectInput.size = visibleCount;
617+
handle.appendChild(selectInput);
614618
if(isSimple){
615-
selectInput.style.top = height + "px";
616-
selectInput.style.left = textInput.style.left;
617619
selectInput.className = "combo-select-box-visible";
618-
selectInput.size = visibleCount;
619-
handle.appendChild(selectInput);
620620
}else{
621621
selectInput.style.position = "absolute";
622-
selectInput.style.top = height + "px" ;
623-
selectInput.style.left = textInput.style.left;
624622
selectInput.className = "combo-select-box-invisible combo-select-box-notsimple";
625-
selectInput.size = visibleCount;
626-
handle.appendChild(selectInput);
627-
628623
ResizeSystem.register(getShell(), SWT.NONE);
629624
}
630625
}
@@ -2528,19 +2523,7 @@ void enableWidget(boolean enabled) {
25282523
this.textInput.disabled = !enabled;
25292524
this.dropDownButton.disabled = !enabled;
25302525

2531-
String cssName = handle.className;
2532-
if (cssName == null) cssName = "";
2533-
String key = "combo-disabled";
2534-
int idx = cssName.indexOf(key);
2535-
if (!enabled) {
2536-
if (idx == -1) {
2537-
handle.className += " " + key;
2538-
}
2539-
} else {
2540-
if (idx != -1) {
2541-
handle.className = cssName.substring(0, idx) + cssName.substring(idx + key.length());
2542-
}
2543-
}
2526+
OS.updateCSSClass(handle, "combo-disabled", !enabled);
25442527
}
25452528
protected boolean SetWindowPos(Object hWnd, Object hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags) {
25462529
if (OS.isIE) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,6 @@ public void run() {
25752575
}
25762576
el.oncontextmenu = new RunnableCompatibility() {
25772577
public void run() {
2578-
System.out.println("..fsfd.s");
25792578
Object evt = getEvent();
25802579
if (evt != null) {
25812580
HTMLEventWrapper evtHTML = new HTMLEventWrapper(evt);
@@ -2595,7 +2594,6 @@ public void run() {
25952594
evtHTML.preventDefault();
25962595
evtHTML.stopPropagation();
25972596
}
2598-
System.out.println("to return false...");
25992597
toReturn(false);
26002598
}
26012599
};

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public class Decorations extends Canvas {
127127
private Element shellMax;
128128
private Element shellIcon;
129129
private Element titleBar;
130-
private Element shellClose;
130+
private Element shellClose;
131+
Element shellMenuBar;
132+
Element shellToolBar;
131133

132134
/**
133135
* Prevents uninitialized instances from being created outside the package.
@@ -877,6 +879,9 @@ public Rectangle getClientArea () {
877879
w -= 4;
878880
h -= 4;
879881
}
882+
if (OS.existedCSSClass(handle, "shell-menu-bar")) {
883+
h -= 21;
884+
}
880885
}
881886
return new Rectangle(0, 0, w, h);
882887
}
@@ -1159,6 +1164,14 @@ protected void releaseHandle() {
11591164
OS.destroyHandle(titleBar);
11601165
titleBar = null;
11611166
}
1167+
if (shellMenuBar != null) {
1168+
OS.destroyHandle(shellMenuBar);
1169+
shellMenuBar = null;
1170+
}
1171+
if (shellToolBar != null) {
1172+
OS.destroyHandle(shellToolBar);
1173+
shellToolBar = null;
1174+
}
11621175
if (contentHandle != null) {
11631176
OS.destroyHandle(contentHandle);
11641177
contentHandle = null;
@@ -1582,22 +1595,12 @@ void toggleMaximize() {
15821595
String key = "shell-maximized";
15831596
if (oldBounds != null) {
15841597
setBounds(oldBounds);
1585-
String cssName = titleBar.className;
1586-
if (cssName == null) cssName = "";
1587-
int idx = cssName.indexOf(key);
1588-
if (idx != -1) {
1589-
titleBar.className = cssName.substring(0, idx) + cssName.substring(idx + key.length());
1590-
}
1598+
OS.removeCSSClass(titleBar, key);
15911599
oldBounds = null;
15921600
ResizeSystem.unregister(this, SWT.MAX);
15931601
} else {
1594-
setMaximized(true);
1595-
String cssName = titleBar.className;
1596-
if (cssName == null) cssName = "";
1597-
int idx = cssName.indexOf(key);
1598-
if (idx == -1) {
1599-
titleBar.className += " " + key;
1600-
}
1602+
setMaximized(true);
1603+
OS.addCSSClass(titleBar, key);
16011604
}
16021605
}
16031606

@@ -2098,6 +2101,9 @@ protected boolean SetWindowPos(Object hWnd, Object hWndInsertAfter, int X, int Y
20982101
dh += 3;
20992102
dww += 2;
21002103
}
2104+
if (OS.existedCSSClass(handle, "shell-menu-bar")) {
2105+
dh += 21;
2106+
}
21012107
contentHandle.style.height = ((height - dh >= 0) ? height - dh : 0) + "px";
21022108
contentHandle.style.width = ((width - dw) > 0 ? width - dw : 0) + "px";
21032109
titleBar.style.width = ((width - dww) > 0 ? width - dww : 0) + "px";

0 commit comments

Comments
 (0)
X Tutup