Skip to content

Commit fe8f56a

Browse files
committed
HTMLEditorKit fixes for CTRL-C and CTRL-A; JMenuItem anchor 90% not 95%
1 parent d6d9fcb commit fe8f56a

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.swing.text.html.StyleSheet;
2121

2222
import javajs.util.PT;
23+
import swingjs.plaf.JSComponentUI;
2324

2425
/**
2526
* A class to help with HTMLEditorKit-derived JEditorPane.
@@ -77,7 +78,7 @@ public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
7778
String href = /** @j2sNative jQueryEvent.target.href || */
7879
null;
7980
if (href == null)
80-
return false;
81+
return JSComponentUI.HANDLED;
8182
switch (eventType) {
8283
// these don't get registered, apparently. TODO
8384
// case MouseEvent.MOUSE_ENTERED:
@@ -90,7 +91,7 @@ public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
9091
type = EventType.ACTIVATED;
9192
break;
9293
default:
93-
return false;
94+
return JSComponentUI.HANDLED;
9495
}
9596
URL url = null;
9697
Element elem = null;

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

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,24 @@
77
import java.awt.event.KeyEvent;
88
import java.awt.event.MouseEvent;
99
import java.beans.PropertyChangeEvent;
10-
import java.util.Enumeration;
1110

1211
import javax.swing.InputMap;
1312
import javax.swing.JComponent;
1413
import javax.swing.JEditorPane;
1514
import javax.swing.event.CaretEvent;
1615
import javax.swing.plaf.InputMapUIResource;
1716
import javax.swing.text.AbstractDocument.BranchElement;
18-
import javax.swing.text.AbstractDocument;
1917
import javax.swing.text.AttributeSet;
2018
import javax.swing.text.BadLocationException;
2119
import javax.swing.text.Caret;
2220
import javax.swing.text.Document;
2321
import javax.swing.text.Element;
2422
import javax.swing.text.JTextComponent;
2523
import javax.swing.text.Keymap;
26-
import javax.swing.text.Style;
24+
import javax.swing.text.Position;
2725
import javax.swing.text.StyleConstants;
28-
import javax.swing.text.StyleContext;
2926
import javax.swing.text.StyledEditorKit;
3027
import javax.swing.text.View;
31-
import javax.swing.text.html.StyleSheet;
3228

3329
import javajs.util.PT;
3430
import javajs.util.SB;
@@ -71,6 +67,8 @@ public class JSEditorPaneUI extends JSTextUI {
7167
private static final int SPACES_PER_TAB = 4;
7268

7369
protected boolean isTextPane = false;
70+
71+
protected boolean isHtmlKit = false;
7472

7573
public JSEditorPaneUI() {
7674
isEditorPane = isTextView = true;
@@ -213,7 +211,7 @@ public DOMNode updateDOMNode() {
213211
}
214212
textListener.checkDocument();
215213
setCssFont(domNode, c.getFont());
216-
DOMNode.setAttrs(domNode, "contentEditable", editor.isEditable() ? TRUE : FALSE, "spellcheck", FALSE);
214+
DOMNode.setAttrs(domNode, "contentEditable", isHtmlKit || editor.isEditable() ? TRUE : FALSE, "spellcheck", FALSE);
217215
if (jc.getTopLevelAncestor() != null) {
218216
if (editor.getText() != mytext) {
219217
setText(null);
@@ -231,6 +229,9 @@ public void propertyChange(PropertyChangeEvent e) {
231229
String prop = e.getPropertyName();
232230
//System.out.println("JSEPUI prop " + prop);
233231
switch(prop) {
232+
case "editorKit":
233+
isHtmlKit = (editor.秘jsHTMLHelper != null);
234+
return;
234235
case "text":
235236
setCurrentText();
236237
return;
@@ -338,11 +339,11 @@ public void setText(String text) {
338339
String html;
339340
if (text == null)
340341
text = editor.getText();
341-
if (editor.秘jsHTMLHelper != null) {
342+
if (isHtmlKit) {
342343
mytext = html = text;
343344
isHTML = true;
344345
html = (String) editor.秘jsHTMLHelper.get("html", getInner(text, "body"));
345-
DOMNode.setAttrs(domNode, "contentEditable", FALSE);
346+
DOMNode.setAttrs(domNode, "contentEditable", TRUE);
346347
styleNode = DOMNode.createElement("div", id + "_style");
347348
domNode.appendChild(styleNode);
348349
String[] styles = (String[]) editor.秘jsHTMLHelper.get("styles", "body");
@@ -726,6 +727,8 @@ private static boolean isJSTAB(Object node) {
726727
*/
727728
@Override
728729
protected void setJSSelection(int mark, int dot, boolean andScroll) {
730+
if (isHtmlKit)
731+
return;
729732
super.setJSSelection(Math.min(mark, dot), Math.max(mark, dot), andScroll);
730733
}
731734

@@ -841,6 +844,8 @@ public void caretUpdatedByProgram(CaretEvent e) {
841844
@SuppressWarnings("unused")
842845
@Override
843846
boolean getJSMarkAndDot(Point pt, int keycode) {
847+
if (isHtmlKit)
848+
return false;
844849
int dot = 0, mark = 0, apt = 0, fpt = 0;
845850
DOMNode anode = null, fnode = null, apar = null, fpar = null;
846851
String atag = null, ftag = null;
@@ -891,6 +896,8 @@ boolean getJSMarkAndDot(Point pt, int keycode) {
891896

892897
@Override
893898
void setJSMarkAndDot(int mark, int dot, boolean andScroll) {
899+
if (isHtmlKit)
900+
return;
894901
//System.out.println("setJSMarkAndDot " + mark + " " + dot + " " + andScroll);
895902
// key up with text change -- need to refresh data-ui attributes
896903
// for all childNodes and also set the java caret, which will then
@@ -907,6 +914,8 @@ public boolean isFocusable() {
907914

908915
@Override
909916
public void action(String what, int data) {
917+
if (isHtmlKit)
918+
return;
910919
int p = -1;
911920
switch (what) {
912921
case "paste":
@@ -985,6 +994,12 @@ private int[] getJavaMarkAndDot() {
985994
private String stemp;
986995
private int[] xyTemp;
987996

997+
@Override
998+
public int viewToModel(JTextComponent t, Point pt,
999+
Position.Bias[] biasReturn) {
1000+
return (isHtmlKit ? 0 : super.viewToModel(t, pt, biasReturn));
1001+
}
1002+
9881003
/**
9891004
* CTRL-V insertion requires knowledge of the text length at the time of keypress and
9901005
* then comparing that to the value at the time of keyup. Hopefully no repeating!
@@ -993,7 +1008,9 @@ private int[] getJavaMarkAndDot() {
9931008
@Override
9941009
protected boolean handleCtrlV(int mode) {
9951010
//System.out.println(getJavaMarkAndDot());
996-
1011+
if (isHtmlKit)
1012+
return false;
1013+
9971014
getJSMarkAndDot(markDot, 0);
9981015
//System.out.println(markDot);
9991016
String s = (String) DOMNode.getAttr(domNode, "innerText");
@@ -1060,33 +1077,43 @@ void setJSText() {
10601077

10611078
boolean isPressConsumed;
10621079

1080+
protected final static boolean DO_KEY_DEFAULT = true;
1081+
protected final static boolean STOP_KEY_DEFAULT_AND_PREVENT_PROPAGATION = false;
1082+
1083+
1084+
/**
1085+
* This method is entered from the j2sApplet mouse listeners,
1086+
*
1087+
*/
10631088
@Override
10641089
public boolean handleJSEvent(Object target, int eventType, Object jQueryEvent) {
10651090
Boolean b = checkAllowEvent(jQueryEvent);
10661091
if (b != null)
10671092
return b;
1068-
System.out.println("handling event type " + eventType);
10691093
switch (eventType) {
10701094
default:
10711095
return NOT_HANDLED;
10721096
case MouseEvent.MOUSE_ENTERED:
1097+
case MouseEvent.MOUSE_DRAGGED:
10731098
case MouseEvent.MOUSE_EXITED:
1099+
case MouseEvent.MOUSE_PRESSED:
10741100
case MouseEvent.MOUSE_RELEASED:
1075-
if (editor.秘jsHTMLHelper != null) {
1101+
if (isHtmlKit) {
1102+
// The idea here is to disallow mouse-driven editing
1103+
DOMNode.setAttrs(domNode, "contentEditable", FALSE);
10761104
editor.秘jsHTMLHelper.handleJSEvent(target, eventType, jQueryEvent);
10771105
return HANDLED;
10781106
}
1107+
return HANDLED;
10791108
case SOME_KEY_EVENT:
1080-
//System.out.println("JSEPUI dispatching " + jQueryEvent);
1081-
JSKeyEvent.dispatchKeyEvent(jc, 0, jQueryEvent, System.currentTimeMillis());
1082-
/**
1083-
* @j2sNative
1084-
*
1085-
* jQueryEvent.preventDefault();
1086-
* jQueryEvent.stopPropagation();
1087-
*/
10881109
setIgnoreEvent(jQueryEvent);
1089-
return HANDLED;
1110+
if (isHtmlKit) {
1111+
// Allow CTRL-A to select just the JEditorPane, not the whole page
1112+
DOMNode.setAttrs(domNode, "contentEditable", TRUE);
1113+
return DO_KEY_DEFAULT;
1114+
}
1115+
JSKeyEvent.dispatchKeyEvent(jc, 0, jQueryEvent, System.currentTimeMillis());
1116+
return STOP_KEY_DEFAULT_AND_PREVENT_PROPAGATION;
10901117
}
10911118
}
10921119

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,22 @@ protected void handleJSTextEvent(int eventType, Object jQueryEvent, int keyCode,
233233

234234

235235
/**
236+
* mouse events only -- called by j2sApplet.js
237+
*
236238
* Handle stopPropagation and preventDefault here.
237239
*
240+
* By existing at all, this method prevents j2sApplet.checkStopPropagation
241+
* from acting, and by returning false, it indicates
242+
* that other components (such as the JRootPane) should
243+
* handle this mouse action.
238244
*
239-
* mouse events only -- called by j2sApplet.js
240245
*
241246
* @param ev
242247
* @param handled
243248
* @return true only if no further processing is desired
244249
*/
245250
public boolean checkStopPropagation(Object ev, boolean handled) {
246-
// ev.stopPropagation();
247-
// ev.preventDefault();
248-
return false;
251+
return NOT_HANDLED;
249252
}
250253

251254
@Override
@@ -1303,6 +1306,12 @@ protected Boolean checkAllowEvent(Object jQueryEvent) {
13031306
else if (type == "keyup")
13041307
handleCtrlV(KeyEvent.KEY_RELEASED);
13051308
return NOT_CONSUMED;
1309+
case KeyEvent.VK_A: // a
1310+
if (!isCTRL)
1311+
return null;
1312+
return null;
1313+
// allowKeyEvent(jQueryEvent);
1314+
// return NOT_CONSUMED; // allow standard browser CTRL-C, with no Java-Event processing
13061315
case KeyEvent.VK_C: // copy
13071316
if (!isCTRL)
13081317
return null;
@@ -1380,7 +1389,6 @@ protected boolean handleTab(Object jQueryEvent) {
13801389
*/
13811390
public int viewToModel(JTextComponent t, Point pt,
13821391
Position.Bias[] biasReturn) {
1383-
13841392
// from DefaultCursor mouse event
13851393
pt.x = Integer.MAX_VALUE;
13861394
getJSMarkAndDot(pt, 0);
@@ -1427,9 +1435,6 @@ Point getNewCaretPosition(int eventType, int keyCode) {
14271435
return pt;
14281436
}
14291437

1430-
public void updateJSCursorFromCaret() {
1431-
}
1432-
14331438
public void caretUpdatedByProgram(CaretEvent e) {
14341439
//System.out.println("JSTextUI "+ e);
14351440
//

0 commit comments

Comments
 (0)