Skip to content

Commit 61767af

Browse files
hansonrhansonr
authored andcommitted
JLabel, JButton, JMenu, JMenuItem text horizontal alignment much better
1 parent dda0a6d commit 61767af

19 files changed

+783
-587
lines changed

sources/net.sf.j2s.java.core/src/swingjs/api/js/DOMNode.java

Lines changed: 113 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@
1010

1111
import swingjs.JSUtil;
1212

13+
/**
14+
* A mix of direct DOM calls on DOM nodes and convenience methods to do that.
15+
*
16+
* NOTE: DO NOT OVERLOAD THESE METHODS, as this package will not be qualified.
17+
*
18+
* @author hansonr
19+
*
20+
*/
1321
public abstract class DOMNode {
1422

1523

24+
// "abstract" in the sense that these are the exact calls to JavaScript
25+
1626
public abstract void appendChild(DOMNode node);
1727

1828
public abstract boolean hasFocus();
@@ -24,111 +34,52 @@ public abstract class DOMNode {
2434
public abstract void setSelectionRange(int pt0, int pt1);
2535

2636
public abstract Rectangle getBoundingClientRect();
27-
37+
38+
// static convenience methods
2839

2940
public static DOMNode createElement(String key, String id) {
3041
DOMNode node = null;
3142
/**
32-
* adding __CLASS_NAME__ allows a node to be used as a parameter in an overloaded method
33-
*
3443
* @j2sNative
3544
* node = document.createElement(key);
36-
* node.__CLASS_NAME__ = "swingjs.api.DOMNode";
37-
* if(id)node.id = id;
45+
* id && (node.id = id);
3846
*/
39-
{
40-
}
4147
return node;
4248
}
4349

4450
public static DOMNode createTextNode(String text) {
45-
DOMNode node = null;
46-
/**
47-
*
48-
* @j2sNative
49-
* node = document.createTextNode(text);
50-
*/
51-
{
52-
}
53-
return node;
51+
return (/** @j2sNative document.createTextNode(text) || */ null);
5452
}
5553

54+
public static void addJqueryHandledEvent(Object me, DOMNode node, String event) {
55+
JSUtil.jQuery.$(node).on(event, /** @j2sNative function(ev) {me.handleJSEvent$O$I$O(node, -1, ev)} || */null);
56+
}
5657

5758
public static DOMNode getParent(DOMNode node) {
58-
/**
59-
* @j2sNative
60-
*
61-
* return node.parentNode;
62-
*
63-
*/
64-
{
65-
return null;
66-
}
59+
return (/** @j2sNative node.parentNode ||*/ null);
6760
}
6861

69-
/**
70-
*
71-
* @param node
72-
* @param container
73-
* @return parent if container is null, or container if it is not null
74-
*/
75-
public static DOMNode transferTo(DOMNode node, DOMNode container) {
76-
if (node == null)
77-
return null;
78-
DOMNode p = getParent(node);
79-
try {
80-
JSUtil.jQuery.$(node).detach();
81-
} catch (Throwable e) {
82-
// ignore
83-
}
84-
if (container == null)
85-
return p;
86-
JSUtil.jQuery.$(container).append(node);
87-
return container;
62+
public static DOMNode firstChild(DOMNode node) {
63+
return (/** @j2sNative node.firstChild ||*/ null);
8864
}
8965

90-
/**
91-
* remove this node and return its parent
92-
* @param node
93-
* @return parent or null
94-
*/
95-
public static void remove(DOMNode node) {
96-
if (node != null)
97-
JSUtil.jQuery.$(node).remove();
66+
public static DOMNode lastChild(DOMNode node) {
67+
return (/** @j2sNative node.lastChild ||*/ null);
9868
}
99-
100-
/**
101-
* note: this works with 'checked' as well
102-
*
103-
* @param node
104-
* @param attr
105-
* @return
106-
*/
69+
70+
public static DOMNode setZ(DOMNode node, int z) {
71+
return setStyles(node, "z-index", "" + z);
72+
}
73+
10774
public static Object getAttr(DOMNode node, String attr) {
108-
/**
109-
* @j2sNative
110-
*
111-
* if (node)return node[attr];
112-
*
113-
*/
114-
{
115-
return null;
116-
}
75+
return (/** @j2sNative node && node[attr] ||*/ null);
11776
}
11877

11978
public static String getStyle(DOMNode node, String style) {
120-
/**
121-
* @j2sNative
122-
*
123-
* if (node)return node.style[style];
124-
*
125-
*/
126-
{
127-
return null;
128-
}
79+
return (/** @j2sNative node && node.style[style] ||*/ null);
12980
}
13081

131-
public static void getRectangle(DOMNode node, Rectangle r) {
82+
public static void getCSSRectangle(DOMNode node, Rectangle r) {
13283
/**
13384
* @j2sNative
13485
*
@@ -138,11 +89,8 @@ public static void getRectangle(DOMNode node, Rectangle r) {
13889
* r.height = parseInt(node.style.height.split("p")[0]);
13990
*
14091
*/
141-
{
142-
}
14392
}
14493

145-
14694
public static DOMNode setAttr(DOMNode node, String attr, Object val) {
14795
/**
14896
* @j2sNative
@@ -153,6 +101,17 @@ public static DOMNode setAttr(DOMNode node, String attr, Object val) {
153101
return node;
154102
}
155103

104+
105+
public static void setAttrInt(DOMNode node, String attr, int val) {
106+
/**
107+
* @j2sNative
108+
*
109+
* node[attr] = val;
110+
*
111+
*/
112+
}
113+
114+
156115
/**
157116
* allows for null key to be skipped (used in audio)
158117
*
@@ -169,93 +128,42 @@ public static DOMNode setAttrs(DOMNode node, Object... attr) {
169128
* var val = attr[i++];
170129
* key && (node[key] = val);
171130
* }
172-
*
173131
*/
174-
{
175-
}
176132
return node;
177133
}
178134

179135
public static DOMNode setStyles(DOMNode node, String... attr) {
180-
if (node != null)
181136
/**
182137
* @j2sNative
183138
*
184-
* for (var i = 0; i < attr.length;) {
139+
* if (node) for (var i = 0; i < attr.length;) {
185140
* node.style[attr[i++]] = attr[i++]; }
186141
*
187142
*/
188-
{
189-
}
190143
return node;
191144
}
192145

193146
public static DOMNode setSize(DOMNode node, int width, int height) {
194147
return setStyles(node, "width", width + "px", "height", height + "px");
195148
}
196149

197-
public static DOMNode setPositionAbsolute(DOMNode node, int top, int left) {
198-
if (top != Integer.MIN_VALUE) {
199-
DOMNode.setStyles(node, "top", top + "px");
200-
DOMNode.setStyles(node, "left", left + "px");
201-
}
150+
public static DOMNode setPositionAbsolute(DOMNode node) {
202151
return DOMNode.setStyles(node, "position", "absolute");
203152
}
204153

205-
public static DOMNode firstChild(DOMNode node) {
206-
/**
207-
* @j2sNative
208-
*
209-
* return node.firstChild;
210-
*
211-
*/
212-
{
213-
return null;
214-
}
215-
}
216-
217-
public static DOMNode lastChild(DOMNode node) {
218-
/**
219-
* @j2sNative
220-
*
221-
* return node.lastChild;
222-
*
223-
*/
224-
{
225-
return null;
226-
}
227-
}
228-
229-
public static void addJqueryHandledEvent(Object me, DOMNode node, String event) {
230-
Object f = null;
231-
/**
232-
* @j2sNative
233-
*
234-
* f = function(ev) {me.handleJSEvent$O$I$O(node, -1, ev)};
235-
*/
236-
{}
237-
JSUtil.jQuery.$(node).on(event, f);
238-
}
239-
240-
public static DOMNode setZ(DOMNode node, int z) {
241-
return setStyles(node, "z-index", "" + z);
154+
public static void setVisible(DOMNode node, boolean visible) {
155+
setStyles(node, "display", visible ? "block" : "none");
242156
}
243157

244-
public static AudioClip getAudioElement(String filePath, boolean isLoop) {
245-
AudioClip clip = (AudioClip) DOMNode.setAttrs(DOMNode.createElement("audio", null),
246-
"controls", "true", (isLoop ? "loop" : null), (isLoop ? "true" : null), "src", filePath);
247-
// alias the actual audio element methods to SwingJS-qualified methods.
248-
/**
249-
* @j2sNative
250-
* clip.play$ = clip.play;
251-
* clip.stop$ = clip.stop;
252-
* clip.loop$ = clip.loop;
253-
*/
254-
return clip;
158+
public static DOMNode setTopLeftAbsolute(DOMNode node, int top, int left) {
159+
DOMNode.setStyles(node, "top", top + "px");
160+
DOMNode.setStyles(node, "left", left + "px");
161+
return DOMNode.setStyles(node, "position", "absolute");
255162
}
256163

257164
public static void setCursor(String c, Component comp) {
258165
ComponentUI ui = (comp == null ? null : ((JSComponent) comp).getUI());
166+
@SuppressWarnings("unused")
259167
DOMNode node = (ui == null ? null : ui.getDOMNode());
260168
/**
261169
* @j2sNative
@@ -265,20 +173,11 @@ public static void setCursor(String c, Component comp) {
265173
*/
266174
}
267175

268-
public static DOMNode getImageNode(Image img) {
269-
270-
/**
271-
* note that canvas takes precedence over imgNode, because
272-
* imgNode is a placeholder for the original image, but canvas
273-
* will be an op-filtered image
274-
*
275-
* @j2sNative
276-
*
277-
* return (img._canvas || img._imgNode);
278-
*/
279-
{
280-
return null;
281-
}
176+
public static DOMNode getImageNode(Image img) {
177+
// note that canvas takes precedence over imgNode, because
178+
// imgNode is a placeholder for the original image, but canvas
179+
// will be an op-filtered image
180+
return (/** @j2sNative img._canvas || img._imgNode ||*/ null);
282181
}
283182

284183
public static void addHorizontalGap(DOMNode domNode, int gap) {
@@ -288,27 +187,75 @@ public static void addHorizontalGap(DOMNode domNode, int gap) {
288187
domNode.appendChild(label);
289188
}
290189

291-
public static void seTabIndex(DOMNode node, int i) {
190+
public static AudioClip getAudioElement(String filePath, boolean isLoop) {
191+
AudioClip clip = (AudioClip) DOMNode.setAttrs(DOMNode.createElement("audio", null),
192+
"controls", "true", (isLoop ? "loop" : null), (isLoop ? "true" : null), "src", filePath);
193+
// alias the actual audio element methods to SwingJS-qualified methods.
292194
/**
293195
* @j2sNative
294-
*
295-
* node.tabIndex = i;
196+
* clip.play$ = clip.play;
197+
* clip.stop$ = clip.stop;
198+
* clip.loop$ = clip.loop;
296199
*/
297-
{}
298-
}
299-
300-
public static void setVisible(DOMNode node, boolean visible) {
301-
setStyles(node, "display", visible ? "block" : "none");
200+
return clip;
302201
}
303202

203+
// static jQuery calls
204+
205+
/**
206+
* jQuery height()
207+
*
208+
* @param node
209+
* @return height
210+
*/
304211
public static int getHeight(DOMNode node) {
305-
// TODO Auto-generated method stub
306212
return JSUtil.jQuery.$(node).height();
307213
}
308214

215+
/**
216+
* jQuery width()
217+
*
218+
* @param node
219+
* @return width
220+
*/
309221
public static int getWidth(DOMNode node) {
310-
// TODO Auto-generated method stub
311222
return JSUtil.jQuery.$(node).width();
312223
}
313224

225+
/**
226+
* jQuery remove()
227+
*
228+
* Remove this node and return its parent. Automatically removing all events
229+
* attached to it.
230+
*
231+
* @param node
232+
* @return parent or null
233+
*/
234+
public static void remove(DOMNode node) {
235+
if (node != null)
236+
JSUtil.jQuery.$(node).remove();
237+
}
238+
239+
/**
240+
* jQuery detach() + append()
241+
*
242+
* @param node
243+
* @param container
244+
* @return parent if container is null, or container if it is not null
245+
*/
246+
public static DOMNode transferTo(DOMNode node, DOMNode container) {
247+
if (node == null)
248+
return null;
249+
DOMNode p = getParent(node);
250+
try {
251+
JSUtil.jQuery.$(node).detach();
252+
} catch (Throwable e) {
253+
// ignore
254+
}
255+
if (container == null)
256+
return p;
257+
JSUtil.jQuery.$(container).append(node);
258+
return container;
259+
}
260+
314261
}

0 commit comments

Comments
 (0)