Skip to content

Commit bae02f2

Browse files
authored
Merge pull request #167 from BobHanson/master
more fixes for JSDialog and HTMLEditorKit
2 parents 3478abb + fb0e0ab commit bae02f2

File tree

14 files changed

+161
-65
lines changed

14 files changed

+161
-65
lines changed
4.24 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200313003834
1+
20200313123132
4.24 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200313003834
1+
20200313123132
4.24 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/lang/Class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2465,7 +2465,7 @@ public InputStream getResourceAsStream(String name) {
24652465
var javapath = fname;
24662466
try {
24672467
if (fname.indexOf(":/") < 0) {
2468-
var d = document.location.href.split("?")[0].split("/");
2468+
var d = document.location.href.split("#")[0].split("?")[0].split("/");
24692469
d[d.length - 1] = fname;
24702470
fname = d.join("/");
24712471
}
Binary file not shown.

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

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
import java.net.MalformedURLException;
55
import java.net.URL;
66
import java.util.ArrayList;
7+
import java.util.Enumeration;
78
import java.util.List;
89

910
import javax.swing.JEditorPane;
1011
import javax.swing.event.HyperlinkEvent;
1112
import javax.swing.event.HyperlinkEvent.EventType;
1213
import javax.swing.text.Element;
14+
import javax.swing.text.Style;
15+
import javax.swing.text.StyleContext;
1316
import javax.swing.text.html.HTML;
1417
import javax.swing.text.html.HTMLDocument;
1518
import javax.swing.text.html.HTMLDocument.Iterator;
1619
import javax.swing.text.html.HTMLEditorKit;
20+
import javax.swing.text.html.StyleSheet;
1721

1822
import javajs.util.PT;
1923

@@ -65,7 +69,7 @@ public String getText() {
6569
* @param target
6670
* @param eventType
6771
* @param jQueryEvent
68-
* @return
72+
* @return true if handled
6973
*/
7074
@SuppressWarnings("unused")
7175
public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
@@ -93,20 +97,28 @@ public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
9397
try {
9498
int pt = href.indexOf("#_JSHREF_");
9599
if (pt < 0) {
96-
System.out.println("JSHTMLHelper could not find '#_JSREF_' in " + href);
100+
System.out.println("JSHTMLHelper could not find '#_JSHREF_' in " + href);
97101
return true;
98102
}
99103
String left = href.substring(0, pt);
100104
elem = getElementFromHref(left);
101-
href = href.substring(pt + 9);
105+
href = trimHRef(href.substring(pt + 9));
106+
102107
url = new URL(href);
103108
} catch (MalformedURLException e) {
104-
System.out.println("JSHTMLHelper invalidURL: " + href);
109+
// ignore -- could be anything the developer wants.
105110
}
106111
editor.fireHyperlinkUpdate(new HyperlinkEvent(editor, type, url, href, elem));
107112
return true;
108113
}
109114

115+
private String trimHRef(String href) {
116+
href = PT.trim(href,"'\"");
117+
if (href.startsWith("%22") && href.endsWith("%22"))
118+
href = href.substring(3, href.length() - 3);
119+
return href;
120+
}
121+
110122
/**
111123
* Retrieve the anchor element index
112124
* @param left
@@ -119,7 +131,7 @@ private Element getElementFromHref(String left) {
119131
if (n >= 0 && n < aTagElements.size())
120132
return aTagElements.get(n);
121133
}
122-
System.out.println("JSHTMLHelper could not find 'href=#n' in " + left);
134+
System.out.println("JSHTMLHelper could not find '#_JSINDEX' in " + left);
123135
return null;
124136
}
125137

@@ -131,7 +143,7 @@ private Element getElementFromHref(String left) {
131143
* @return
132144
*/
133145
public String indexAnchors(String text) {
134-
String html = PT.rep(text, "href=", "href=#_JSINDEX_##_JSHREF_");
146+
String html = PT.rep(text, "href=", "onclick='return false' href=#_JSINDEX_##_JSHREF_");
135147
Iterator iter = doc.getIterator(HTML.Tag.A);
136148
aTagElements = new ArrayList<Element>();
137149
for (int i = 0; iter.isValid(); i++) {
@@ -143,4 +155,70 @@ public String indexAnchors(String text) {
143155
return html;
144156
}
145157

158+
/**
159+
* Generic method to get information from the HTMLDocument model.
160+
* @param what "html", "css", or "styles"
161+
* @param data secondary information
162+
* @return appropriate object (String or String[])
163+
*/
164+
public Object get(String what, String data) {
165+
switch (what) {
166+
case "html":
167+
return indexAnchors(data);
168+
case "css":
169+
return getCSS(data);
170+
case "styles":
171+
return getStyles(data);
172+
default:
173+
System.out.println("JSHTMLHelper.get what? " + what);
174+
return null;
175+
}
176+
177+
}
178+
179+
/**
180+
* Return styles as an array of key/value pairs suitable for DOMNode.setStyles(node, []).
181+
* This will just be the Java doc.getStyleSheet().getStyle("body").attributes.attributes.
182+
*
183+
* @return [key,value,key,value,...]
184+
*/
185+
private String[] getStyles(String name) {
186+
StyleSheet sheet = doc.getStyleSheet();
187+
if (sheet == null)
188+
return null;
189+
Style bodyStyle = sheet.getStyle(name);
190+
if (bodyStyle != null)
191+
{
192+
return( /** @j2sNative bodyStyle.attributes.attributes || */null);
193+
}
194+
return null;
195+
}
196+
197+
/**
198+
* retrieve all CSS styles not including "default" or "body" that would appear
199+
* within <style></style> in Java, scoped to this id.
200+
*
201+
* @param id
202+
* @return CSS string
203+
*/
204+
private String getCSS(String id) {
205+
String css = "";
206+
StyleSheet sheet = doc.getStyleSheet();
207+
if (sheet == null)
208+
return null;
209+
Enumeration styles = sheet.getStyleNames();
210+
while (styles.hasMoreElements()) {
211+
String name = (String) styles.nextElement();
212+
// Don't write out the default style.
213+
if (!name.equals(StyleContext.DEFAULT_STYLE)
214+
&& !name.equals("body")) {
215+
Style s = sheet.getStyle(name);
216+
css += s + "\n";
217+
}
218+
}
219+
return PT.rep(css, "NamedStyle:", "#" + id + " ").replace('=',':').replace(',',';');
220+
}
221+
222+
146223
}
224+

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package swingjs.plaf;
22

33
import java.util.List;
4-
4+
import java.awt.Color;
5+
import java.awt.Dimension;
6+
import java.awt.Toolkit;
57
import java.awt.Window;
68
import java.awt.peer.DialogPeer;
79

@@ -10,6 +12,8 @@
1012
import javax.swing.JFrame;
1113
import javax.swing.LookAndFeel;
1214

15+
import swingjs.api.js.DOMNode;
16+
1317

1418
public class JSDialogUI extends JSFrameUI implements DialogPeer {
1519

@@ -28,14 +32,13 @@ public JSDialogUI() {
2832
setDoc();
2933
}
3034

31-
@Override
32-
public void installUI(JComponent jc) {
33-
frame = (JFrame) c;
34-
isModal = ((JDialog) c).isModal();
35-
LookAndFeel.installColors(jc,
36-
"Frame.background",
37-
"Frame.foreground");
38-
}
35+
@Override
36+
public void installUI(JComponent jc) {
37+
frame = (JFrame) jc;
38+
LookAndFeel.installColors(jc,
39+
"Frame.background",
40+
"Frame.foreground");
41+
}
3942

4043
@Override
4144
public void uninstallUI(JComponent jc) {
@@ -56,4 +59,16 @@ public void setVisible(boolean b) {
5659

5760
}
5861

62+
@Override
63+
protected boolean isModal() {
64+
boolean isModal = ((JDialog) jc).isModal();
65+
if (isModal && modalNode == null) {
66+
modalNode = DOMNode.createElement("div", id + "_modaldiv");
67+
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
68+
DOMNode.setStyles(modalNode, "position", "sticky", "left","0px", "top", "0px",
69+
"background", toCSSString(new Color(100, 100, 100, 100)));
70+
DOMNode.setSize(modalNode, screen.width, screen.height);
71+
}
72+
return isModal;
73+
}
5974
}

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

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ public void propertyChange(PropertyChangeEvent e) {
242242
private String currentHTML;
243243
private boolean isStyled;
244244
private String mytext;
245-
private String[] styles;
246-
private String css;
247245
private DOMNode styleNode;
248246

249247

@@ -343,13 +341,15 @@ public void setText(String text) {
343341
if (editor.秘jsHTMLHelper != null) {
344342
mytext = html = text;
345343
isHTML = true;
346-
html = editor.秘jsHTMLHelper.indexAnchors(getInner(text, "body"));
347-
getStyles();
344+
html = (String) editor.秘jsHTMLHelper.get("html", getInner(text, "body"));
348345
DOMNode.setAttrs(domNode, "contentEditable", FALSE);
349346
styleNode = DOMNode.createElement("div", id + "_style");
350347
domNode.appendChild(styleNode);
348+
String[] styles = (String[]) editor.秘jsHTMLHelper.get("styles", "body");
351349
if (styles != null)
352350
DOMNode.setStyles(styleNode, styles);
351+
String css = (String) editor.秘jsHTMLHelper.get("css", id);
352+
setStyle(id + "_style", css);
353353
} else {
354354
styleNode = domNode;
355355
mytext = text;
@@ -387,38 +387,13 @@ public void setText(String text) {
387387
}
388388
}
389389

390-
private void getStyles() {
391-
styles = null;
392-
css = "";
393-
StyleSheet sheet = editor.秘jsHTMLHelper.doc.getStyleSheet();
394-
if (sheet == null)
395-
return;
396-
Enumeration styles = sheet.getStyleNames();
397-
while (styles.hasMoreElements()) {
398-
String name = (String) styles.nextElement();
399-
// Don't write out the default style.
400-
if (!name.equals(StyleContext.DEFAULT_STYLE)
401-
&& !name.equals("body")) {
402-
Style s = sheet.getStyle(name);
403-
css += s + "\n";
404-
}
405-
}
406-
css = PT.rep(css, "NamedStyle:", "#" + id + " ").replace('=',':');
407-
setStyle(id + "_style", css);
408-
Style bodyStyle = sheet.getStyle("body");
409-
if (bodyStyle != null)
410-
{
411-
this.styles = /** @j2sNative bodyStyle.attributes.attributes || */null;
412-
}
413-
}
414-
415390
private void setStyle(String id, String css) {
416391
DOMNode d = DOMNode.getElement(id);
417392
if (d == null) {
418-
d = DOMNode.createElement("style", id);
419-
$(body).append(d);
393+
$(body).append("<style id=" + id +">" + css + "</style>");
394+
} else {
395+
DOMNode.setAttr(d, "innerText", css);
420396
}
421-
DOMNode.setAttr(d, "innerText", css);
422397
}
423398

424399
private String getInner(String html, String body) {

0 commit comments

Comments
 (0)