Skip to content

Commit cf340a4

Browse files
committed
JToolTip (still issues with HTML)
1 parent a72bb05 commit cf340a4

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed
234 Bytes
Binary file not shown.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ public void setDraggable(JSFunction f) {
350350
private boolean canAlignText;
351351
private boolean canAlignIcon;
352352

353+
/**
354+
* set false for tool tip or other non-label object that has text
355+
*
356+
*/
357+
protected boolean allowTextAlignment = true;
358+
353359
public JSComponentUI() {
354360
setDoc();
355361
}
@@ -756,13 +762,14 @@ protected void setIconAndText(String prop, ImageIcon icon, int gap,
756762
iconHeight = icon.getIconHeight();
757763
}
758764
}
765+
boolean isHTML = false;
759766
if (text == null || text.length() == 0) {
760767
text = "";
761768
if (icon != null)
762769
canAlignIcon = true;
763770
} else {
764771
if (icon == null) {
765-
canAlignText = true;
772+
canAlignText = allowTextAlignment;
766773
} else {
767774
//vCenter(imageNode, 10); // perhaps? Not sure if this is a good idea
768775
if (gap == Integer.MAX_VALUE)
@@ -771,15 +778,20 @@ protected void setIconAndText(String prop, ImageIcon icon, int gap,
771778
DOMNode.addHorizontalGap(iconNode, gap);
772779
}
773780
if (text.indexOf("<html>") == 0) {
781+
isHTML = true;
774782
// PhET uses <html> in labels and uses </br>
775783
text = PT.rep(text.substring(6, text.length() - 7), "</br>", "");
784+
text = PT.rep(text, "</html>", "");
785+
text = PT.rep(text, "href=", "target=_blank href=");
786+
System.out.println(text);
776787
}
777788
}
778789
DOMNode obj = null;
779790
if (textNode != null) {
780791
prop = "innerHTML";
781792
obj = textNode;
782-
text = PT.rep(text, "<", "&lt;");
793+
if (!isHTML)
794+
text = PT.rep(text, "<", "&lt;");
783795
} else if (valueNode != null) {
784796
prop = "value";
785797
obj = valueNode;

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
import swingjs.api.js.DOMNode;
99

1010
/**
11-
* A JavaScript equivalent for a label.
11+
* A JavaScript equivalent for a label.
12+
*
13+
* Also used for ToolTip
1214
*
1315
* @author Bob Hanson
1416
*
1517
*/
1618
public class JSLabelUI extends JSLightweightUI {
1719
private JLabel label;
20+
protected ImageIcon icon;
21+
protected int gap;
22+
protected String text;
1823

1924
public JSLabelUI() {
2025
setDoc();
@@ -33,8 +38,8 @@ protected DOMNode updateDOMNode() {
3338
centeringNode.appendChild(textNode);
3439
domNode.appendChild(centeringNode);
3540
}
36-
setIconAndText("label", (ImageIcon) label.getIcon(),
37-
label.getIconTextGap(), label.getText());
41+
getIconAndText(); // could be ToolTip
42+
setIconAndText("label", icon, gap, text);
3843
DOMNode.setStyles(domNode, "position", "absolute", "width", c.getWidth()
3944
+ "px", "height", c.getHeight() + "px");
4045
if (actualHeight > 0)
@@ -50,6 +55,13 @@ protected DOMNode updateDOMNode() {
5055

5156
}
5257

58+
protected void getIconAndText() {
59+
icon = (ImageIcon) label.getIcon();
60+
gap = label.getIconTextGap();
61+
text = label.getText();
62+
}
63+
64+
5365
/**
5466
* adding in outer styles for text alignment of a label
5567
*/
Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
11
package swingjs.plaf;
22

3-
4-
import java.awt.Dimension;
5-
63
import javax.swing.JComponent;
7-
import javax.swing.JRootPane;
4+
import javax.swing.JToolTip;
85
import javax.swing.LookAndFeel;
9-
import swingjs.api.js.DOMNode;
106

11-
public class JSToolTipUI extends JSLightweightUI {
7+
public class JSToolTipUI extends JSLabelUI {
128

13-
int frameZ = 10000;
9+
// TODO: allow generic tool tip, not just text
10+
1411
public JSToolTipUI() {
15-
isContainer = true;
16-
setDoc();
12+
super();
13+
allowTextAlignment = false;
1714
}
1815

19-
@Override
20-
protected DOMNode updateDOMNode() {
21-
if (domNode == null) {
22-
JRootPane root = jc.getRootPane();
23-
//isContentPane = (root != null && jc == root.getContentPane());
24-
domNode = newDOMObject("div", id);
25-
DOMNode.setVisible(domNode, false);
26-
}
27-
return domNode;
28-
}
16+
protected JToolTip toolTip;
2917

3018
@Override
31-
protected Dimension setHTMLSize(DOMNode obj, boolean addCSS) {
32-
// SwingJS for now: just designated container width/height
33-
return new Dimension(c.getWidth(), c.getHeight());
19+
protected void getIconAndText() {
20+
icon = null;
21+
gap = 0;
22+
text = toolTip.getTipText();
3423
}
35-
3624

3725
@Override
3826
public void installUI(JComponent jc) {
39-
LookAndFeel.installColorsAndFont(jc,
40-
"Panel.background",
41-
"Panel.foreground",
42-
"Panel.font");
27+
toolTip = (JToolTip) jc;
28+
LookAndFeel.installColorsAndFont(jc, "ToolTip.background", "ToolTip.foreground",
29+
"Tooltip.font");
4330
}
4431

32+
4533
@Override
4634
public void uninstallUI(JComponent jc) {
35+
System.out.println("Uninstalling ToolTipUI");
36+
// should remove dom node?
4737
// TODO Auto-generated method stub
4838

4939
}
5040

51-
@Override
52-
public Dimension getPreferredSize() {
53-
return null;
54-
}
55-
5641

5742
}

0 commit comments

Comments
 (0)