Skip to content

Commit 8d321ed

Browse files
hansonrhansonr
authored andcommitted
adds support for new JComponent(){}
1 parent f2fbe13 commit 8d321ed

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

sources/net.sf.j2s.java.core/src/java/awt/JSComponent.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ public String getUIClassID() {
262262
return (uiClassID == null ? uiClassID = "ComponentUI" : uiClassID);
263263
}
264264

265+
/**
266+
* for JSToolkit use only
267+
* @param id
268+
*/
269+
public void setUIClassID(String id) {
270+
uiClassID = id;
271+
}
272+
273+
265274
/**
266275
* required by Container, but not actually ever called,
267276
* because all Containers are JComponents in SwingJS

sources/net.sf.j2s.java.core/src/swingjs/JSToolkit.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public static void exit() {
128128
// ////// java.awt.Toolkit /////////
129129

130130
public static void getScreenSize(Dimension d) {
131+
@SuppressWarnings("unused")
131132
JQuery jq = JSUtil.jQuery;
132133
d.width = /** @j2sNative jq.$(window).width() || */0;
133134
d.height = /** @j2sNative jq.$(window).height() || */0;
@@ -285,8 +286,9 @@ public static UIDefaults getLookAndFeelDefaults() {
285286
}
286287

287288
public static JSComponentUI getComponentUI(JComponent target) {
289+
String id = ((JSComponent) target).getUIClassID();
288290
JSComponentUI ui = (JSComponentUI) Interface.getInstance("swingjs.plaf.JS"
289-
+ ((JSComponent) target).getUIClassID(), true);
291+
+ id, true);
290292
if (ui != null)
291293
ui.set(target);
292294
return ui;
@@ -536,7 +538,7 @@ public WindowPeer createWindow(Window target) {
536538

537539
public static JSComponentUI getUI(Component c, boolean isQuiet) {
538540
JSComponentUI ui = /** @2sNative !!c.getUI$ &&*/(JSComponentUI)((JComponent) c).getUI();
539-
if (ui == null && ((JComponent) c).getUIClassID() != "ComponentUI") {
541+
if (ui == null) {
540542
((JComponent) c).updateUI();
541543
ui = (JSComponentUI) ((JComponent) c).getUI();
542544
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.awt.Point;
1616
import java.awt.Rectangle;
1717
import java.awt.Toolkit;
18-
import java.awt.Window;
1918
import java.awt.dnd.DropTarget;
2019
import java.awt.dnd.peer.DropTargetPeer;
2120
import java.awt.event.KeyEvent;
@@ -38,7 +37,6 @@
3837
import javax.swing.JMenuItem;
3938
import javax.swing.JPopupMenu;
4039
import javax.swing.JRootPane;
41-
import javax.swing.JTable;
4240
import javax.swing.JTable.BooleanRenderer;
4341
import javax.swing.KeyStroke;
4442
import javax.swing.SwingConstants;
@@ -552,7 +550,7 @@ public void setDraggable(JSFunction f) {
552550

553551
private DOMNode waitImage;
554552

555-
private final Color colorUNKNOWN = new Color();
553+
protected final Color colorUNKNOWN = new Color();
556554

557555
protected Color inactiveForeground = colorUNKNOWN,
558556
inactiveBackground = colorUNKNOWN;
@@ -643,8 +641,6 @@ public void installJS() {
643641
*/
644642
private void uninstallJS() {
645643

646-
//System.out.println("uninstallJS " + id);
647-
648644
// window closing will fire this with c == null
649645

650646
/**
@@ -1290,7 +1286,6 @@ protected void setMnemonic(int newValue) {
12901286
mnemonic = newValue;
12911287
}
12921288

1293-
private String createMsgs = "";
12941289

12951290
/**
12961291
* set to TRUE by Container.validateTree at the beginning of its laying out and
@@ -1384,16 +1379,13 @@ public DOMNode getDOMNode() {
13841379
* @return the DOM element's node and, if the DOM element already exists,
13851380
*/
13861381
public DOMNode updateDOMNode() {
1387-
if (notImplemented) {
1388-
String msg = "Swingjs WARNING: default JSComponentUI.updateDOMNode() is being used for "
1389-
+ getClass().getName();
1390-
if (debugging && createMsgs.indexOf(msg) < 0) {
1391-
createMsgs += msg;
1392-
JSUtil.alert(msg);
1382+
if (domNode == null) {
1383+
if (notImplemented) {
1384+
String msg = "Swingjs WARNING: default JSComponentUI.updateDOMNode() is being used for "
1385+
+ getClass().getName();
1386+
System.out.println(msg);
13931387
}
1394-
System.out.println(msg);
1395-
if (domNode != null)
1396-
domNode = DOMNode.createElement("div", id);
1388+
domNode = DOMNode.createElement("div", id);
13971389
}
13981390
return domNode;
13991391
}
@@ -2292,7 +2284,6 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
22922284
DOMNode.setVisible(obj, text != null);
22932285
}
22942286
if (obj != null) {
2295-
// System.out.println("JSCUI setText " + id + " " + prop + " " + text);
22962287
setJSText(obj, prop, text);
22972288
}
22982289
if (valueNode != null) {
@@ -2391,6 +2382,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
23912382
int wText = (dimText == null ? 0 : dimText.width);
23922383
int gap = (wText == 0 || wIcon == 0 ? 0 : b.getIconTextGap());
23932384
int w = cellComponent != null ? cellWidth : $(domNode).width();
2385+
if (w < wIcon + wText) {
2386+
// jQuery method can fail that may not have worked.
2387+
w = wIcon + wText;
2388+
}
23942389
boolean alignVCenter = (vAlign == SwingConstants.CENTER);
23952390
Insets margins = (isLabel ? (isAWT ? b.getInsets() : insets) : b.getMargin());
23962391
if (margins == null)
@@ -2500,14 +2495,18 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
25002495
return;
25012496
}
25022497
preferredDim = null;
2503-
String yoff = "-50%";
25042498
DOMNode.setStyles(centeringNode, "position", "absolute", "top", null, "left", null, "transform", null);
25052499
DOMNode.setStyles(centeringNode, "width", wCtr + "px", "height", hCtr + "px");
25062500
if (alignHCenter && alignVCenter && wIcon == 0
25072501
|| wText == 0 && margins.left == margins.right && margins.top == margins.bottom) {
25082502
// simple totally centered label or button
2509-
DOMNode.setStyles(centeringNode, "top", "50%", "left", "50%", "transform",
2510-
"translateX(-50%)translateY("+ yoff + ")");
2503+
// can't have width or height here --- let the browser figure that out
2504+
DOMNode.setStyles(centeringNode, "width",null,"top", "50%", "left", "50%", "transform",
2505+
"translateX(-50%)translateY(-50%)", "position", "absolute");
2506+
DOMNode.setStyles(iconNode, "top", "50%", "left", "50%", "transform",
2507+
"translateX(-50%)translateY(-50%)", "position", "absolute");
2508+
DOMNode.setStyles(textNode, "top", "50%", "left", "50%", "transform",
2509+
"translateX(-50%)translateY(-50%)", "position", "absolute");
25112510
return;
25122511
}
25132512

@@ -2594,11 +2593,14 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
25942593
}
25952594
DOMNode.setStyles(centeringNode, "top", top + "px");
25962595
int itop;
2596+
String yoff = null;
25972597
String iscale = null;
25982598
switch (vTextPos) {
25992599
case SwingConstants.TOP:
26002600
top = itop = 0;
2601-
yoff = null;
2601+
break;
2602+
case SwingConstants.BOTTOM:
2603+
top = itop = 100;
26022604
break;
26032605
default:
26042606
case SwingConstants.CENTER:
@@ -2607,12 +2609,7 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
26072609
itop = 70;
26082610
iscale = "scale(0.8,0.8)";
26092611
}
2610-
// +3 here is a fudge factor for the AWT applets
2611-
yoff = "-50%";//(wIcon == 0 && false ? "-" + ((getFont().getFontMetrics().getAscent()>>1) + (isAWT ? 0 : 0)) + "px" : "-50%");
2612-
break;
2613-
case SwingConstants.BOTTOM:
2614-
top = itop = 100;
2615-
yoff = null;
2612+
yoff = "-50%";
26162613
break;
26172614
}
26182615
DOMNode.setStyles(textNode, "top", top + "%", "transform", "translateY(" + (yoff == null ? "-" + top + "%" : yoff + ")"));
@@ -2689,7 +2686,6 @@ public FontMetrics getFontMetrics(Font font) {
26892686

26902687
@Override
26912688
public void dispose() {
2692-
//System.out.println("JSCUI dispose " + id);
26932689
if (isUIDisabled)
26942690
return;
26952691
if (cellComponent != null) {
@@ -2724,7 +2720,6 @@ protected void undisposeUI(DOMNode node) {
27242720
// cell renderers will set their domNode to null;
27252721
if (outerNode != null && domNode != null && domNode != outerNode) {
27262722
appendChild(outerNode, domNode);
2727-
//System.out.println("JSCUI undispose reattached");
27282723
}
27292724
isDisposed = false;
27302725
}

0 commit comments

Comments
 (0)