Skip to content

Commit d48ef6d

Browse files
committed
adds LineMetrics, fixes missing tooltip
LineMetrics are not high precision and not tested. ToolTip issue was that clientProperties was defined both in JSComponent and JComponent
1 parent 3b77bf8 commit d48ef6d

File tree

6 files changed

+182
-23
lines changed

6 files changed

+182
-23
lines changed
7.32 KB
Binary file not shown.

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@
3030

3131
import javajs.util.SB;
3232
import swingjs.JSFontMetrics;
33+
import swingjs.JSLineMetrics;
3334
import swingjs.JSToolkit;
3435
import java.text.AttributedCharacterIterator.Attribute;
3536
//import java.util.Hashtable;
3637
//import java.util.Locale;
37-
38+
import java.text.CharacterIterator;
39+
import java.util.Hashtable;
40+
import java.util.Map;
3841
import java.awt.font.FontRenderContext;
42+
import java.awt.font.LineMetrics;
3943
import java.awt.font.TextAttribute;
4044
import java.awt.geom.AffineTransform;
4145
//import sun.font.AttributeValues;
@@ -2655,4 +2659,32 @@ protected void finalize() throws Throwable {
26552659
* trigger object finalization.
26562660
*/
26572661
}
2662+
2663+
private Map<String, LineMetrics> mapLineMetrics;
2664+
private LineMetrics getLineMetrics(String s) {
2665+
if (mapLineMetrics == null)
2666+
mapLineMetrics = new Hashtable<String, LineMetrics>();
2667+
LineMetrics lm = mapLineMetrics.get(s);
2668+
return (lm == null ? new JSLineMetrics(this, s) : lm);
2669+
}
2670+
2671+
public LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, Object object) {
2672+
// TODO Auto-generated method stub
2673+
return null;
2674+
}
2675+
2676+
public LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, Object object) {
2677+
// TODO Auto-generated method stub
2678+
return null;
2679+
}
2680+
2681+
public LineMetrics getLineMetrics(String str, int beginIndex, int limit, Object object) {
2682+
// TODO Auto-generated method stub
2683+
return null;
2684+
}
2685+
2686+
public LineMetrics getLineMetrics(String str, Object object) {
2687+
// TODO Auto-generated method stub
2688+
return null;
2689+
}
26582690
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,4 @@ public boolean isBackgroundSet() {
283283
// !isBackgroundPainted);
284284
}
285285

286-
protected transient ArrayTable clientProperties;
287-
288-
public Object getClientProperty(Object key) {
289-
if (clientProperties == null) {
290-
return null;
291-
} else {
292-
synchronized (clientProperties) {
293-
return clientProperties.get(key);
294-
}
295-
}
296-
}
297-
298286
}

sources/net.sf.j2s.java.core/src/javax/swing/JComponent.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public abstract class JComponent extends Container {
217217
protected EventListenerList listenerList = new EventListenerList();
218218

219219
protected transient ArrayTable clientProperties;
220+
220221
// private VetoableChangeSupport vetoableChangeSupport;
221222
/**
222223
* Whether or not autoscroll has been enabled.
@@ -3331,23 +3332,16 @@ private ArrayTable getClientProperties() {
33313332
* @return the value of this property or <code>null</code>
33323333
* @see #putClientProperty
33333334
*/
3334-
public /* SwingJS final*/ Object getClientProperty(Object key) {
3335-
// moved to java.awt.JSComponent
3336-
return super.getClientProperty(key);
3335+
public Object getClientProperty(Object key) {
33373336
// if (key == SwingUtilities2.AA_TEXT_PROPERTY_KEY) {
33383337
// return aaTextInfo;
33393338
// } else if (key == SwingUtilities2.COMPONENT_UI_PROPERTY_KEY) {
33403339
// return ui;
33413340
// }
3342-
// if (clientProperties == null) {
3343-
// return null;
3344-
// } else {
3345-
// synchronized (clientProperties) {
3346-
// return clientProperties.get(key);
3347-
// }
3348-
// }
3341+
return (clientProperties == null ? null : clientProperties.get(key));
33493342
}
33503343

3344+
33513345
/**
33523346
* Adds an arbitrary key/value "client property" to this component.
33533347
* <p>

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.awt.Font;
44
import java.awt.FontMetrics;
5+
import java.awt.Graphics;
6+
import java.awt.font.LineMetrics;
7+
import java.text.CharacterIterator;
58

69
public class JSFontMetrics extends FontMetrics {
710

@@ -113,4 +116,72 @@ public float[] getWidthsFloat() {
113116
return fwidths;
114117
}
115118

119+
120+
/**
121+
* Returns the {@link LineMetrics} object for the specified
122+
* <code>String</code> in the specified {@link Graphics} context.
123+
* @param str the specified <code>String</code>
124+
* @param context the specified <code>Graphics</code> context
125+
* @return a <code>LineMetrics</code> object created with the
126+
* specified <code>String</code> and <code>Graphics</code> context.
127+
* @see java.awt.Font#getLineMetrics(String, FontRenderContext)
128+
*/
129+
public LineMetrics getLineMetrics( String str, Graphics context) {
130+
return font.getLineMetrics(str, null);
131+
}
132+
133+
/**
134+
* Returns the {@link LineMetrics} object for the specified
135+
* <code>String</code> in the specified {@link Graphics} context.
136+
* @param str the specified <code>String</code>
137+
* @param beginIndex the initial offset of <code>str</code>
138+
* @param limit the end offset of <code>str</code>
139+
* @param context the specified <code>Graphics</code> context
140+
* @return a <code>LineMetrics</code> object created with the
141+
* specified <code>String</code> and <code>Graphics</code> context.
142+
* @see java.awt.Font#getLineMetrics(String, int, int, FontRenderContext)
143+
*/
144+
public LineMetrics getLineMetrics( String str,
145+
int beginIndex, int limit,
146+
Graphics context) {
147+
return font.getLineMetrics(str, beginIndex, limit, null);
148+
}
149+
150+
/**
151+
* Returns the {@link LineMetrics} object for the specified
152+
* character array in the specified {@link Graphics} context.
153+
* @param chars the specified character array
154+
* @param beginIndex the initial offset of <code>chars</code>
155+
* @param limit the end offset of <code>chars</code>
156+
* @param context the specified <code>Graphics</code> context
157+
* @return a <code>LineMetrics</code> object created with the
158+
* specified character array and <code>Graphics</code> context.
159+
* @see java.awt.Font#getLineMetrics(char[], int, int, FontRenderContext)
160+
*/
161+
public LineMetrics getLineMetrics(char [] chars,
162+
int beginIndex, int limit,
163+
Graphics context) {
164+
return font.getLineMetrics(
165+
chars, beginIndex, limit, null);
166+
}
167+
168+
/**
169+
* Returns the {@link LineMetrics} object for the specified
170+
* {@link CharacterIterator} in the specified {@link Graphics}
171+
* context.
172+
* @param ci the specified <code>CharacterIterator</code>
173+
* @param beginIndex the initial offset in <code>ci</code>
174+
* @param limit the end index of <code>ci</code>
175+
* @param context the specified <code>Graphics</code> context
176+
* @return a <code>LineMetrics</code> object created with the
177+
* specified arguments.
178+
* @see java.awt.Font#getLineMetrics(CharacterIterator, int, int, FontRenderContext)
179+
*/
180+
public LineMetrics getLineMetrics(CharacterIterator ci,
181+
int beginIndex, int limit,
182+
Graphics context) {
183+
return font.getLineMetrics(ci, beginIndex, limit, null);
184+
}
185+
186+
116187
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package swingjs;
2+
3+
import java.awt.Font;
4+
import java.awt.FontMetrics;
5+
import java.awt.font.LineMetrics;
6+
7+
public class JSLineMetrics extends LineMetrics {
8+
9+
private FontMetrics fm;
10+
private String s;
11+
12+
public JSLineMetrics() {
13+
}
14+
15+
public JSLineMetrics(Font font, String s) {
16+
fm = font.getFontMetrics();
17+
}
18+
19+
@Override
20+
public int getNumChars() {
21+
return s.length();
22+
}
23+
24+
@Override
25+
public float getHeight() {
26+
return fm.getHeight();
27+
}
28+
29+
@Override
30+
public int getBaselineIndex() {
31+
return Font.ROMAN_BASELINE;
32+
}
33+
34+
@Override
35+
public float[] getBaselineOffsets() {
36+
return new float[s.length()];
37+
}
38+
39+
@Override
40+
public float getStrikethroughOffset() {
41+
return fm.getAscent()/2f;
42+
}
43+
44+
@Override
45+
public float getStrikethroughThickness() {
46+
return 1;
47+
}
48+
49+
@Override
50+
public float getUnderlineOffset() {
51+
return 2;
52+
}
53+
54+
@Override
55+
public float getUnderlineThickness() {
56+
return 1;
57+
}
58+
59+
@Override
60+
public float getAscent() {
61+
return fm.getAscent();
62+
}
63+
64+
@Override
65+
public float getDescent() {
66+
return fm.getDescent();
67+
}
68+
69+
@Override
70+
public float getLeading() {
71+
return fm.getLeading();
72+
}
73+
74+
}

0 commit comments

Comments
 (0)