Skip to content

Commit f0b6655

Browse files
hansonrhansonr
authored andcommitted
JSEditorPane fix for setPage(URL)
1 parent 371f107 commit f0b6655

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import javax.swing.text.html.HTMLEditorKit;
7373

7474
import swingjs.JSHTMLHelper;
75+
import swingjs.JSUtil;
7576
//import swingjs.JSAbstractDocument;
7677
import swingjs.api.Interface;
7778
//import swingjs.api.JSMinimalAbstractDocument;
@@ -423,6 +424,7 @@ public void fireHyperlinkUpdate(HyperlinkEvent e) {
423424
* @see #getPage
424425
* @beaninfo description: the URL used to set content bound: true expert: true
425426
*/
427+
@SuppressWarnings("unused")
426428
public void setPage(URL page) throws IOException {
427429
if (page == null) {
428430
throw new IOException("invalid url");
@@ -464,6 +466,7 @@ public void setPage(URL page) throws IOException {
464466
// load asynchronously
465467
setDocument(doc);
466468
synchronized (this) {
469+
秘setPage(doc, page);
467470
loading = new PageStream(in);
468471
Thread pl = new PageLoader(doc, loading, p, loaded, page);
469472
pl.start();
@@ -472,6 +475,7 @@ public void setPage(URL page) throws IOException {
472475
}
473476
read(in, doc);
474477
setDocument(doc);
478+
秘setPage(doc, page);
475479
reloaded = true;
476480
}
477481
} else {
@@ -501,6 +505,16 @@ public void run() {
501505
firePropertyChange("page", loaded, page);
502506
}
503507

508+
private void 秘setPage(Document doc, URL page) {
509+
if (doc instanceof HTMLDocument) {
510+
byte[] bytes = JSUtil.getBytes(page);
511+
if (bytes != null) {
512+
if (秘jsHTMLHelper != null)
513+
秘jsHTMLHelper.setText(new String(bytes));
514+
}
515+
}
516+
}
517+
504518
/**
505519
* Create model and initialize document properties from page properties.
506520
*/
@@ -1225,6 +1239,7 @@ public static EditorKit createEditorKitForContentType(String type) {
12251239
// try to dynamically load the support
12261240
String classname = (String) getKitTypeRegistry().get(type);
12271241
// ClassLoader loader = (ClassLoader) getKitLoaderRegistry().get(type);
1242+
if (classname != null)
12281243
try {
12291244
k = (EditorKit) Interface.getInstance(classname, false);
12301245
// Class c;
@@ -1436,12 +1451,7 @@ public void setText(String t) {
14361451
try {
14371452
Document doc = getDocument();
14381453
if (doc instanceof HTMLDocument) {
1439-
if (t.indexOf("<body")< 0)
1440-
t = "<body>" + t + "</body>";
1441-
if (t.indexOf("<head")< 0)
1442-
t = "<head>" + t + "</head>";
1443-
if (t.indexOf("<html")< 0)
1444-
t = "<html>" + t + "</html>";
1454+
t = 秘fixHTML(t);
14451455
}
14461456
if (秘jsHTMLHelper != null)
14471457
秘jsHTMLHelper.setText(t);
@@ -1459,6 +1469,16 @@ public void setText(String t) {
14591469
}
14601470
}
14611471

1472+
private static String 秘fixHTML(String t) {
1473+
if (t.indexOf("<body")< 0)
1474+
t = "<body>" + t + "</body>";
1475+
if (t.indexOf("<head")< 0)
1476+
t = "<head>" + t + "</head>";
1477+
if (t.indexOf("<html")< 0)
1478+
t = "<html>" + t + "</html>";
1479+
return t;
1480+
}
1481+
14621482
/**
14631483
* Returns the text contained in this <code>TextComponent</code> in terms of the
14641484
* content type of this editor. If an exception is thrown while attempting to

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public String indexAnchors(String text) {
149149
for (int i = 0; iter.isValid(); i++) {
150150
aTagElements.add(iter.秘getElement());
151151
int pt = html.indexOf("href=#_JSINDEX_#");
152-
html = html.substring(0, pt + 14) + i + html.substring(pt + 16);
152+
if (pt > 0)
153+
html = html.substring(0, pt + 14) + i + html.substring(pt + 16);
153154
iter.next();
154155
}
155156
return html;

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.awt.event.KeyListener;
1212
import java.awt.event.MouseEvent;
1313
import java.beans.PropertyChangeEvent;
14+
import java.net.MalformedURLException;
15+
import java.net.URL;
1416

1517
import javax.swing.InputMap;
1618
import javax.swing.JComponent;
@@ -35,6 +37,7 @@
3537
import javax.swing.text.StyledEditorKit;
3638
import javax.swing.text.View;
3739

40+
import javajs.util.PT;
3841
import javajs.util.SB;
3942
import sun.swing.DefaultLookup;
4043
import swingjs.JSToolkit;
@@ -271,6 +274,7 @@ public void propertyChange(PropertyChangeEvent e) {
271274
private boolean isStyled;
272275
private String mytext;
273276
private DOMNode bodyNode;
277+
private String rawHTML;
274278

275279
// private int epTimer;
276280
// @Override
@@ -405,8 +409,35 @@ public void setText(String text) {
405409
}
406410
}
407411
if (isHTML) {
408-
html = fixText(html);
409412
setBackgroundDOM(domNode, jc.getBackground());
413+
if (html.equals(rawHTML))
414+
return;
415+
rawHTML = html;
416+
html = fixText(html);
417+
if (isHtmlKit) {
418+
URL page = (URL) editor.getDocument().getProperty("stream");
419+
if (page != null && page.getProtocol().equals("file") && text.indexOf("src=\".") >= 0) {
420+
String rp = J2S.getResourcePath("", true);
421+
if (rp.indexOf("://") < 0)
422+
rp = "file:/" + rp;
423+
try {
424+
page = new URL(rp + page.getPath().substring(1));
425+
} catch (MalformedURLException e1) {
426+
}
427+
String[] srcs = PT.split(text, "src=\".");
428+
String out = srcs[0];
429+
for (int i = 1; i < srcs.length; i++) {
430+
int pt = srcs[i].indexOf('"');
431+
String src = "." + srcs[i].substring(0, pt);
432+
try {
433+
src = new URL(page, src).getPath().substring(1);
434+
} catch (MalformedURLException e) {
435+
}
436+
out += "src=\"" +src+ srcs[i].substring(pt);
437+
}
438+
html = out;
439+
}
440+
}
410441
}
411442
// System.out.println(html);
412443
if (html == currentHTML)

0 commit comments

Comments
 (0)