Skip to content

Commit cd31e38

Browse files
hansonrhansonr
authored andcommitted
JEditorPane setPage with url #reference
1 parent 1269324 commit cd31e38

File tree

8 files changed

+90
-35
lines changed

8 files changed

+90
-35
lines changed
459 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210321064238
1+
20210322204729
459 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210321064238
1+
20210322204729

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import javax.swing.event.HyperlinkListener;
5454
import javax.swing.plaf.ComponentUI;
5555
import javax.swing.text.AbstractDocument;
56+
import javax.swing.text.AttributeSet;
5657
import javax.swing.text.BadLocationException;
5758
import javax.swing.text.BoxView;
5859
import javax.swing.text.Caret;
@@ -68,6 +69,7 @@
6869
import javax.swing.text.View;
6970
import javax.swing.text.ViewFactory;
7071
import javax.swing.text.WrappedPlainView;
72+
import javax.swing.text.html.HTML;
7173
import javax.swing.text.html.HTMLDocument;
7274
import javax.swing.text.html.HTMLEditorKit;
7375

@@ -77,6 +79,7 @@
7779
import swingjs.api.Interface;
7880
//import swingjs.api.JSMinimalAbstractDocument;
7981
//import java.io.ObjectOutputStream;
82+
import swingjs.plaf.JSEditorPaneUI;
8083

8184
/**
8285
* A text component to edit various kinds of content. You can find how-to
@@ -906,8 +909,11 @@ private Object getPostData() {
906909
* @param reference the named location to scroll to
907910
*/
908911
public void scrollToReference(String reference) {
909-
// Document d = getDocument();
910-
// if (d instanceof HTMLDocument) {
912+
Document d = getDocument();
913+
if (d instanceof HTMLDocument) {
914+
//SwingUtilities.invokeLater(() -> {
915+
((JSEditorPaneUI) getUI()).scrollToReference(reference);
916+
//});
911917
// HTMLDocument doc = (HTMLDocument) d;
912918
// HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A);
913919
// for (; iter.isValid(); iter.next()) {
@@ -919,6 +925,7 @@ public void scrollToReference(String reference) {
919925
// int pos = iter.getStartOffset();
920926
// Rectangle r = modelToView(pos);
921927
// if (r != null) {
928+
//
922929
// // the view is visible, scroll it to the
923930
// // center of the current visible area.
924931
// Rectangle vis = getVisibleRect();
@@ -932,7 +939,7 @@ public void scrollToReference(String reference) {
932939
// }
933940
// }
934941
// }
935-
// }
942+
}
936943
}
937944

938945
/**

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,32 @@ private String getCSS(String id) {
220220
return css.replaceAll("NamedStyle:", "#" + id + " ").replaceAll("=",":").replaceAll(",", ";");
221221
}
222222

223+
public static String fixLinks(String html, URL page) {
224+
if (page != null && page.getProtocol().equals("file") && html.indexOf("src=\".") >= 0) {
225+
String rp = JSUtil.J2S.getResourcePath("", true);
226+
if (rp.indexOf("://") < 0)
227+
rp = "file:/" + rp;
228+
try {
229+
page = new URL(rp + page.getPath().substring(1));
230+
} catch (MalformedURLException e1) {
231+
}
232+
String[] srcs = PT.split(html, "src=\"");
233+
String out = srcs[0];
234+
for (int i = 1; i < srcs.length; i++) {
235+
int pt = srcs[i].indexOf('"');
236+
String src = srcs[i].substring(0, pt);
237+
try {
238+
if (!src.startsWith("http://") && !src.startsWith("https://"))
239+
src = new URL(page, src).getPath().substring(1);
240+
} catch (MalformedURLException e) {
241+
}
242+
out += "src=\"" + src + srcs[i].substring(pt);
243+
}
244+
html = out;
245+
}
246+
return html;
247+
}
248+
223249

224250
}
225251

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package swingjs.api.js;
22

3+
import java.awt.Insets;
4+
import java.awt.Rectangle;
5+
36
public interface JQueryObject {
47

58
public interface JQEvent {
@@ -19,7 +22,7 @@ public interface JQEvent {
1922

2023
public abstract int width();
2124
public abstract int height();
22-
public abstract Object offset();
25+
public abstract Insets offset();
2326

2427

2528
public abstract void html(String html);
@@ -36,7 +39,7 @@ public interface JQEvent {
3639
public abstract JQueryObject show();
3740
public abstract JQueryObject hide();
3841

39-
public abstract void resize(Object fHandleResize);
42+
public abstract JQueryObject resize(Object fHandleResize);
4043

4144

4245
/**
@@ -56,24 +59,26 @@ public interface JQEvent {
5659
public abstract JQueryObject find(String selector);
5760

5861
public abstract JQueryObject parent();
59-
public abstract void before(Object obj);
60-
public abstract void after(Object div);
62+
public abstract JQueryObject before(Object obj);
63+
public abstract JQueryObject after(Object div);
64+
public abstract JQueryObject scrollTop(int top);
6165

6266

6367
/**
6468
* remove from tree, but do not clear events
6569
*/
66-
public abstract void detach(); // like remove(), but does not change event settings
70+
public abstract JQueryObject detach(); // like remove(), but does not change event settings
6771

6872
/**
6973
* remove from tree and clear all events -- for disposal only
7074
*/
71-
public abstract void remove();
75+
public abstract JQueryObject remove();
7276

73-
/**
77+
78+
/**
7479
* fully remove all children, clearing all events
7580
*/
76-
public abstract void empty();
81+
public abstract JQueryObject empty();
7782

7883
public abstract DOMNode getElement();
7984

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.awt.JSComponent;
88
import java.awt.Point;
99
import java.awt.Rectangle;
10+
import java.awt.Shape;
1011
import java.awt.event.KeyEvent;
1112
import java.awt.event.KeyListener;
1213
import java.awt.event.MouseEvent;
@@ -23,6 +24,7 @@
2324
import javax.swing.event.CaretEvent;
2425
import javax.swing.plaf.InputMapUIResource;
2526
import javax.swing.plaf.UIResource;
27+
import javax.swing.text.AbstractDocument;
2628
import javax.swing.text.AbstractDocument.BranchElement;
2729
import javax.swing.text.AttributeSet;
2830
import javax.swing.text.BadLocationException;
@@ -40,8 +42,10 @@
4042
import javajs.util.PT;
4143
import javajs.util.SB;
4244
import sun.swing.DefaultLookup;
45+
import swingjs.JSHTMLHelper;
4346
import swingjs.JSToolkit;
4447
import swingjs.api.js.DOMNode;
48+
import swingjs.api.js.JQueryObject;
4549
import swingjs.api.js.JQueryObject.JQEvent;
4650
import swingjs.api.js.JSFunction;
4751

@@ -410,33 +414,14 @@ public void setText(String text) {
410414
}
411415
if (isHTML) {
412416
setBackgroundDOM(domNode, jc.getBackground());
413-
if (html.equals(rawHTML))
417+
if (currentHTML != null && html.equals(rawHTML))
414418
return;
415419
rawHTML = html;
416420
html = fixText(html);
417421
if (isHtmlKit) {
418422
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-
}
423+
if (page != null)
424+
html = JSHTMLHelper.fixLinks(html, page);
440425
}
441426
}
442427
// System.out.println(html);
@@ -832,6 +817,17 @@ private static boolean isJSTAB(Object node) {
832817
&& (/** @j2sNative node.nodeType != 3 && ("" + node.getAttribute("class")).indexOf("j2stab") >= 0 && */true);
833818
}
834819

820+
public void scrollToReference(String ref) {
821+
JQueryObject fn = $(focusNode);
822+
JQueryObject r = fn.find("[name=\"" + ref + "\"]");
823+
if (r.get(0) != null) {
824+
SwingUtilities.invokeLater(()->{
825+
fn.scrollTop(0);
826+
System.out.println("later jsepui s2r " + r.offset().top + " " + fn.offset());
827+
fn.scrollTop(r.offset().top - fn.offset().top);
828+
});
829+
}
830+
}
835831

836832
/**
837833
* no backward selections in a div with contentEditable TRUE
@@ -1371,6 +1367,27 @@ public Dimension getMinimumSize(JComponent jc) {
13711367
return d;
13721368
}
13731369

1370+
@Override
1371+
public Rectangle modelToView(JTextComponent tc, int pos, Position.Bias bias) throws BadLocationException {
1372+
Rectangle alloc = getVisibleEditorRect();
1373+
Document doc = editor.getDocument();
1374+
Object[] r1 = getJSNodePt(focusNode, pos, null, 0);
1375+
try {
1376+
if (alloc != null) {
1377+
rootView.setSize(alloc.width, alloc.height);
1378+
Shape s = rootView.modelToView(pos, alloc, bias);
1379+
if (s != null) {
1380+
return s.getBounds();
1381+
}
1382+
}
1383+
} finally {
1384+
if (doc instanceof AbstractDocument) {
1385+
((AbstractDocument) doc).readUnlock();
1386+
}
1387+
}
1388+
return null;
1389+
}
1390+
13741391
@Override
13751392
protected Boolean handleTab(Object jqEvent, String type) {
13761393
if (type == "keydown")

0 commit comments

Comments
 (0)