Skip to content

Commit f6a316c

Browse files
hansonrhansonr
authored andcommitted
JEditorPane <div>....<br><div>
1 parent 611c20d commit f6a316c

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ else if (isSup)
200200
}
201201
if (isSup)
202202
sb.append("</sup>");
203-
if (isSub)
203+
else if (isSub)
204204
sb.append("</sub>");
205-
sb.append(isDiv ? "</div>" : "</span>");
206205
}
207206

208207
private static String getCSSStyle(AttributeSet a, AttributeSet currAttr) {
@@ -282,12 +281,17 @@ protected Object[] getJSNodePt(DOMNode node, int off, int pt) {
282281
lastTextNode = null;
283282
off = 0;
284283
}
284+
// Must consider several cases for BR and DIV:
285+
// <br>
286+
// <div><br><div> --> [div, 0]
287+
// <div>.....<br><div>
288+
285289
/**
286290
* @j2sNative
287291
var nodes = node.childNodes;
288292
var n = nodes.length;
289-
if (n == 1 && nodes[0].tagName == "BR" || node.tagName == "BR") {
290-
return (pt == off ? [node, 0] : [null, 1]);
293+
if (n > 0 && nodes[n - 1].tagName == "BR" || node.tagName == "BR") {
294+
return (pt == off ? [node, n == 0 ? 0 : n - 1] : [null, 1]);
291295
}
292296
var ipt = off;
293297
var nlen = 0;

sources/net.sf.j2s.java.core/src/test/Test_Editor.java

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
import javax.swing.BoxLayout;
1313
import javax.swing.JButton;
14-
import javax.swing.JEditorPane;
1514
import javax.swing.JFrame;
1615
import javax.swing.JPanel;
1716
import javax.swing.JTextArea;
1817
import javax.swing.JTextField;
1918
import javax.swing.JTextPane;
2019
import javax.swing.event.CaretEvent;
2120
import javax.swing.event.CaretListener;
22-
import javax.swing.text.AttributeSet;
2321
import javax.swing.text.Document;
2422
import javax.swing.text.Element;
2523
import javax.swing.text.MutableAttributeSet;
@@ -29,70 +27,69 @@
2927

3028
public class Test_Editor extends JFrame {
3129

32-
3330
public Test_Editor() {
34-
31+
3532
String test = "line1\nline2\nline3\n\nline5\n";
36-
33+
3734
setTitle("testing editor");
3835
setLocation(100, 100);
3936
JTextPane editor = new JTextPane();
4037
System.out.println(editor.getDocument());
4138
editor.setText(test);
4239
System.out.println("count = " + editor.getDocument().getRootElements()[0].getElementCount());
43-
editor.setBackground(new Color(200,200,200));
40+
editor.setBackground(new Color(200, 200, 200));
4441
editor.setPreferredSize(new Dimension(300, 300));
45-
editor.setFont(new Font(Font.SANS_SERIF,Font.PLAIN, 16));
46-
47-
Style style = editor.addStyle("Red", null);
48-
StyleConstants.setForeground(style, Color.red);
49-
42+
editor.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 16));
43+
44+
Style style = editor.addStyle("Red", null);
45+
StyleConstants.setForeground(style, Color.red);
46+
5047
editor.addPropertyChangeListener(new PropertyChangeListener() {
5148

5249
@Override
5350
public void propertyChange(PropertyChangeEvent evt) {
5451
System.out.println(evt.getPropertyName() + " " + evt);
5552
}
56-
53+
5754
});
58-
55+
5956
editor.addCaretListener(new CaretListener() {
6057

6158
@Override
6259
public void caretUpdate(CaretEvent e) {
6360
System.out.println("JTextPane caret:" + e);
64-
//dumpRoot(editor.getDocument());
61+
// dumpRoot(editor.getDocument());
6562
}
66-
63+
6764
});
68-
65+
6966
JTextArea area = new JTextArea();
70-
area.setFont(new Font(Font.MONOSPACED,Font.PLAIN, 16));
67+
area.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
7168
area.setText(test);
72-
area.setBackground(new Color(200,200,180));
73-
area.setPreferredSize(new Dimension(300,300));
69+
area.setBackground(new Color(200, 200, 180));
70+
area.setPreferredSize(new Dimension(300, 300));
7471
area.addCaretListener(new CaretListener() {
7572

7673
@Override
7774
public void caretUpdate(CaretEvent e) {
7875
System.out.println("JTextArea caret:" + e);
7976
}
80-
77+
8178
});
82-
79+
8380
JTextField field = new JTextField("testing");
8481
field.addCaretListener(new CaretListener() {
8582

8683
@Override
8784
public void caretUpdate(CaretEvent e) {
8885
System.out.println("JTextField caret:" + e);
8986
}
90-
87+
9188
});
9289

9390
add(editor);
9491
add(BorderLayout.EAST, area);
95-
92+
9693
JPanel panel = new JPanel();
9794
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
9895
JButton b = new JButton("caret+1");
@@ -103,7 +100,7 @@ public void actionPerformed(ActionEvent e) {
103100
editor.getCaret().setDot(editor.getCaret().getDot() + 1);
104101
editor.requestFocus();
105102
}
106-
103+
107104
});
108105
panel.add(b);
109106
b = new JButton("sel7-10");
@@ -117,7 +114,7 @@ public void actionPerformed(ActionEvent e) {
117114
editor.setSelectionColor(Color.red);
118115
editor.requestFocus();
119116
}
120-
117+
121118
});
122119
panel.add(b);
123120
b = new JButton("sel7-10-area");
@@ -129,9 +126,9 @@ public void actionPerformed(ActionEvent e) {
129126
area.getCaret().moveDot(10);
130127
area.getCaret().setSelectionVisible(true);
131128
area.setSelectionColor(Color.blue);
132-
area.requestFocus();
129+
area.requestFocus();
133130
}
134-
131+
135132
});
136133
panel.add(b);
137134

@@ -144,9 +141,9 @@ public void actionPerformed(ActionEvent e) {
144141
field.getCaret().moveDot(5);
145142
field.getCaret().setSelectionVisible(true);
146143
field.setSelectionColor(Color.blue);
147-
field.requestFocus();
144+
field.requestFocus();
148145
}
149-
146+
150147
});
151148
panel.add(b);
152149

@@ -156,7 +153,7 @@ public void actionPerformed(ActionEvent e) {
156153
@Override
157154
public void actionPerformed(ActionEvent e) {
158155
int start = editor.getSelectionStart();
159-
int end = editor.getSelectionEnd();
156+
int end = editor.getSelectionEnd();
160157
if (end == start)
161158
return;
162159
Element ch = editor.getStyledDocument().getCharacterElement(start);
@@ -166,7 +163,7 @@ public void actionPerformed(ActionEvent e) {
166163
StyleConstants.setBold(attrs, isBold);
167164
editor.getStyledDocument().setCharacterAttributes(start, end - start, attrs, false);
168165
}
169-
166+
170167
});
171168
panel.add(b);
172169

@@ -176,20 +173,20 @@ public void actionPerformed(ActionEvent e) {
176173
@Override
177174
public void actionPerformed(ActionEvent e) {
178175
int start = editor.getSelectionStart();
179-
int end = editor.getSelectionEnd();
176+
int end = editor.getSelectionEnd();
180177
if (end == start)
181178
return;
182179
MutableAttributeSet attrs = new SimpleAttributeSet();
183180
Element ch = editor.getStyledDocument().getCharacterElement(start);
184181
StyleConstants.setItalic(attrs, !StyleConstants.isItalic(ch.getAttributes()));
185182
editor.getStyledDocument().setCharacterAttributes(start, end - start, attrs, false);
186183
}
187-
184+
188185
});
189186
panel.add(b);
190187

191188
panel.add(field);
192-
189+
193190
add(panel, BorderLayout.SOUTH);
194191
pack();
195192
setVisible(true);
@@ -202,15 +199,11 @@ protected void dumpRoot(Document document) {
202199
private void dumpElement(int index, Element element) {
203200
System.out.println("Test_Editor i=" + index + " e=" + element.toString());
204201
for (int i = 0, n = element.getElementCount(); i < n; i++)
205-
dumpElement((index+1)*100, element.getElement(i));
206-
202+
dumpElement((index + 1) * 100, element.getElement(i));
203+
207204
}
208205

209206
public static void main(String[] args) {
210207
new Test_Editor();
211208
}
212209
}
213-
214-
215-
216-

0 commit comments

Comments
 (0)