Я не могу заставить разрыв строки и подсветку синтаксиса работать вместе.
Если я пишу return new XmlView (elem);
, работает только выделение.
Если я пишу return new section (elem, View.Y_AXIS);
, работает только разрыв строки.
jEditorPane1.setEditorKitForContentType("text/html", new SyntaxKit());
jEditorPane1.setContentType("text/html");
jEditorPane1.setText("pow(sqr(9),6)+(6.256^2)/(9.254)");
public class SyntaxKit extends StyledEditorKit {
private final ViewFactory vf;
public SyntaxKit() {
vf = new XmlViewFactory();
}
@Override
public ViewFactory getViewFactory() {
return vf;
}
@Override
public String getContentType() {
return "text/html";
}
private class XmlViewFactory implements ViewFactory {
@Override
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
switch (kind) {
case AbstractDocument.ContentElementName:
return new WrapLabelView(elem);
case AbstractDocument.ParagraphElementName:
return new ParagraphView(elem);
case AbstractDocument.SectionElementName:
return new XmlView(elem);
//return new section(elem, View.Y_AXIS);
case StyleConstants.ComponentElementName:
return new ComponentView(elem);
case StyleConstants.IconElementName:
return new IconView(elem);
default:
break;
}
}
return new LabelView(elem);
}
}
private class WrapLabelView extends LabelView {
public WrapLabelView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
private class section extends BoxView {
public section(Element elmnt, int i) {
super(elmnt, i);
}
}
private class XmlView extends PlainView {
private final HashMap<Pattern, Color> pattern;
private final String TEXT = "([a-zA-Z])";
private final String NUMBERS = "(\\d)";
private final String FUNCTIONS = "([a-z]{3,5}[(])";
private final String PARENTHESIS = "([(]|[)]|[,])";
private final String OPERATORS = "([+]|[-]|[/]|[*])";
@Override
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
public XmlView(Element element) {
super(element);
pattern = new HashMap<>();
pattern.put(Pattern.compile(TEXT), new Color(50, 127, 127));
pattern.put(Pattern.compile(NUMBERS), new Color(0, 0, 255));
pattern.put(Pattern.compile(PARENTHESIS), new Color(255, 0, 0));
pattern.put(Pattern.compile(OPERATORS), new Color(0, 0, 0));
}
@Override
protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
Graphics2D g2;
Document documento = getDocument();
String texto = documento.getText(p0, p1 - p0);
Segment segment = getLineBuffer();
SortedMap<Integer, Integer> startMap = new TreeMap<>();
SortedMap<Integer, Color> colorMap = new TreeMap<>();
pattern.entrySet().forEach((entry) -> {
Matcher matcher = entry.getKey().matcher(texto);
while (matcher.find()) {
startMap.put(matcher.start(1), matcher.end());
colorMap.put(matcher.start(1), entry.getValue());
}
});
int i = 0;
for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) {
int start = entry.getKey();
int end = entry.getValue();
if (i < start) {
g.setColor(Color.black);
documento.getText(p0 + i, start - i, segment);
g2 = (Graphics2D) g;
x = Utilities.drawTabbedText(segment, x, y, g2, this, i);
}
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(colorMap.get(start));
i = end;
documento.getText(p0 + start, i - start, segment);
x = Utilities.drawTabbedText(segment, x, y, g, this, start);
}
if (i < texto.length()) {
g.setColor(Color.black);
documento.getText(p0 + i, texto.length() - i, segment);
g2 = (Graphics2D) g;
x = Utilities.drawTabbedText(segment, x, y, g2, this, i);
}
return x;
}
@Override
protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
Graphics2D g2;
Document document = getDocument();
String text = document.getText(p0, p1 - p0);
Segment segment = getLineBuffer();
SortedMap<Integer, Integer> startMap = new TreeMap<>();
SortedMap<Integer, Color> colorMap = new TreeMap<>();
pattern.entrySet().forEach((entry) -> {
Matcher matcher = entry.getKey().matcher(text);
while (matcher.find()) {
startMap.put(matcher.start(1), matcher.end());
colorMap.put(matcher.start(1), entry.getValue());
}
});
int i = 0;
for (Map.Entry<Integer, Integer> entry : startMap.entrySet()) {
int start = entry.getKey();
int end = entry.getValue();
if (i < start) {
g.setColor(Color.black);
document.getText(p0 + i, start - i, segment);
g2 = (Graphics2D) g;
x = Utilities.drawTabbedText(segment, x, y, g2, this, i);
}
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setColor(colorMap.get(start));
i = end;
document.getText(p0 + start, i - start, segment);
x = Utilities.drawTabbedText(segment, x, y, g, this, start);
}
if (i < text.length()) {
g.setColor(Color.black);
document.getText(p0 + i, text.length() - i, segment);
g2 = (Graphics2D) g;
x = Utilities.drawTabbedText(segment, x, y, g2, this, i);
}
return x;
}
}
}