Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adds support for new JComponent(){}
  • Loading branch information
hansonr authored and hansonr committed Mar 22, 2019
commit 8d321ed410ea472f6a01b1fe0a4129f7d5fdc171
9 changes: 9 additions & 0 deletions sources/net.sf.j2s.java.core/src/java/awt/JSComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ public String getUIClassID() {
return (uiClassID == null ? uiClassID = "ComponentUI" : uiClassID);
}

/**
* for JSToolkit use only
* @param id
*/
public void setUIClassID(String id) {
uiClassID = id;
}


/**
* required by Container, but not actually ever called,
* because all Containers are JComponents in SwingJS
Expand Down
6 changes: 4 additions & 2 deletions sources/net.sf.j2s.java.core/src/swingjs/JSToolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static void exit() {
// ////// java.awt.Toolkit /////////

public static void getScreenSize(Dimension d) {
@SuppressWarnings("unused")
JQuery jq = JSUtil.jQuery;
d.width = /** @j2sNative jq.$(window).width() || */0;
d.height = /** @j2sNative jq.$(window).height() || */0;
Expand Down Expand Up @@ -285,8 +286,9 @@ public static UIDefaults getLookAndFeelDefaults() {
}

public static JSComponentUI getComponentUI(JComponent target) {
String id = ((JSComponent) target).getUIClassID();
JSComponentUI ui = (JSComponentUI) Interface.getInstance("swingjs.plaf.JS"
+ ((JSComponent) target).getUIClassID(), true);
+ id, true);
if (ui != null)
ui.set(target);
return ui;
Expand Down Expand Up @@ -536,7 +538,7 @@ public WindowPeer createWindow(Window target) {

public static JSComponentUI getUI(Component c, boolean isQuiet) {
JSComponentUI ui = /** @2sNative !!c.getUI$ &&*/(JSComponentUI)((JComponent) c).getUI();
if (ui == null && ((JComponent) c).getUIClassID() != "ComponentUI") {
if (ui == null) {
((JComponent) c).updateUI();
ui = (JSComponentUI) ((JComponent) c).getUI();
}
Expand Down
51 changes: 23 additions & 28 deletions sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.dnd.DropTarget;
import java.awt.dnd.peer.DropTargetPeer;
import java.awt.event.KeyEvent;
Expand All @@ -38,7 +37,6 @@
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRootPane;
import javax.swing.JTable;
import javax.swing.JTable.BooleanRenderer;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
Expand Down Expand Up @@ -552,7 +550,7 @@ public void setDraggable(JSFunction f) {

private DOMNode waitImage;

private final Color colorUNKNOWN = new Color();
protected final Color colorUNKNOWN = new Color();

protected Color inactiveForeground = colorUNKNOWN,
inactiveBackground = colorUNKNOWN;
Expand Down Expand Up @@ -643,8 +641,6 @@ public void installJS() {
*/
private void uninstallJS() {

//System.out.println("uninstallJS " + id);

// window closing will fire this with c == null

/**
Expand Down Expand Up @@ -1290,7 +1286,6 @@ protected void setMnemonic(int newValue) {
mnemonic = newValue;
}

private String createMsgs = "";

/**
* set to TRUE by Container.validateTree at the beginning of its laying out and
Expand Down Expand Up @@ -1384,16 +1379,13 @@ public DOMNode getDOMNode() {
* @return the DOM element's node and, if the DOM element already exists,
*/
public DOMNode updateDOMNode() {
if (notImplemented) {
String msg = "Swingjs WARNING: default JSComponentUI.updateDOMNode() is being used for "
+ getClass().getName();
if (debugging && createMsgs.indexOf(msg) < 0) {
createMsgs += msg;
JSUtil.alert(msg);
if (domNode == null) {
if (notImplemented) {
String msg = "Swingjs WARNING: default JSComponentUI.updateDOMNode() is being used for "
+ getClass().getName();
System.out.println(msg);
}
System.out.println(msg);
if (domNode != null)
domNode = DOMNode.createElement("div", id);
domNode = DOMNode.createElement("div", id);
}
return domNode;
}
Expand Down Expand Up @@ -2292,7 +2284,6 @@ protected void setIconAndText(String prop, Icon icon, int gap, String text) {
DOMNode.setVisible(obj, text != null);
}
if (obj != null) {
// System.out.println("JSCUI setText " + id + " " + prop + " " + text);
setJSText(obj, prop, text);
}
if (valueNode != null) {
Expand Down Expand Up @@ -2391,6 +2382,10 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
int wText = (dimText == null ? 0 : dimText.width);
int gap = (wText == 0 || wIcon == 0 ? 0 : b.getIconTextGap());
int w = cellComponent != null ? cellWidth : $(domNode).width();
if (w < wIcon + wText) {
// jQuery method can fail that may not have worked.
w = wIcon + wText;
}
boolean alignVCenter = (vAlign == SwingConstants.CENTER);
Insets margins = (isLabel ? (isAWT ? b.getInsets() : insets) : b.getMargin());
if (margins == null)
Expand Down Expand Up @@ -2500,14 +2495,18 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
return;
}
preferredDim = null;
String yoff = "-50%";
DOMNode.setStyles(centeringNode, "position", "absolute", "top", null, "left", null, "transform", null);
DOMNode.setStyles(centeringNode, "width", wCtr + "px", "height", hCtr + "px");
if (alignHCenter && alignVCenter && wIcon == 0
|| wText == 0 && margins.left == margins.right && margins.top == margins.bottom) {
// simple totally centered label or button
DOMNode.setStyles(centeringNode, "top", "50%", "left", "50%", "transform",
"translateX(-50%)translateY("+ yoff + ")");
// can't have width or height here --- let the browser figure that out
DOMNode.setStyles(centeringNode, "width",null,"top", "50%", "left", "50%", "transform",
"translateX(-50%)translateY(-50%)", "position", "absolute");
DOMNode.setStyles(iconNode, "top", "50%", "left", "50%", "transform",
"translateX(-50%)translateY(-50%)", "position", "absolute");
DOMNode.setStyles(textNode, "top", "50%", "left", "50%", "transform",
"translateX(-50%)translateY(-50%)", "position", "absolute");
return;
}

Expand Down Expand Up @@ -2594,11 +2593,14 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
}
DOMNode.setStyles(centeringNode, "top", top + "px");
int itop;
String yoff = null;
String iscale = null;
switch (vTextPos) {
case SwingConstants.TOP:
top = itop = 0;
yoff = null;
break;
case SwingConstants.BOTTOM:
top = itop = 100;
break;
default:
case SwingConstants.CENTER:
Expand All @@ -2607,12 +2609,7 @@ protected void setAlignments(AbstractButton b, boolean justGetPreferred) {
itop = 70;
iscale = "scale(0.8,0.8)";
}
// +3 here is a fudge factor for the AWT applets
yoff = "-50%";//(wIcon == 0 && false ? "-" + ((getFont().getFontMetrics().getAscent()>>1) + (isAWT ? 0 : 0)) + "px" : "-50%");
break;
case SwingConstants.BOTTOM:
top = itop = 100;
yoff = null;
yoff = "-50%";
break;
}
DOMNode.setStyles(textNode, "top", top + "%", "transform", "translateY(" + (yoff == null ? "-" + top + "%" : yoff + ")"));
Expand Down Expand Up @@ -2689,7 +2686,6 @@ public FontMetrics getFontMetrics(Font font) {

@Override
public void dispose() {
//System.out.println("JSCUI dispose " + id);
if (isUIDisabled)
return;
if (cellComponent != null) {
Expand Down Expand Up @@ -2724,7 +2720,6 @@ protected void undisposeUI(DOMNode node) {
// cell renderers will set their domNode to null;
if (outerNode != null && domNode != null && domNode != outerNode) {
appendChild(outerNode, domNode);
//System.out.println("JSCUI undispose reattached");
}
isDisposed = false;
}
Expand Down