From 801c6b801545167010101808dd269199f9c4b0a0 Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Wed, 4 Mar 2020 11:57:59 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-27411=20=E5=AF=8C=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8=E4=B8=AD=E8=A2=AB=E9=80=89=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=96=87=E6=9C=AC=E5=A4=B1=E5=8E=BB=E7=84=A6=E7=82=B9?= =?UTF-8?q?=E5=90=8E=E8=A2=AB=E6=A0=B7=E5=BC=8F=E4=B8=A2=E5=A4=B1=E4=BA=86?= =?UTF-8?q?=EF=BC=88=E8=A2=AB=E9=80=89=E4=B8=AD=E6=97=B6=E9=AB=98=E4=BA=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/design/report/RichTextEditingPane.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/designer-realize/src/main/java/com/fr/design/report/RichTextEditingPane.java b/designer-realize/src/main/java/com/fr/design/report/RichTextEditingPane.java index 0406abffb..39061a4b6 100644 --- a/designer-realize/src/main/java/com/fr/design/report/RichTextEditingPane.java +++ b/designer-realize/src/main/java/com/fr/design/report/RichTextEditingPane.java @@ -14,9 +14,14 @@ import com.fr.log.FineLoggerFactory; import com.fr.report.cell.cellattr.core.RichText; import com.fr.report.cell.cellattr.core.RichTextConverter; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; +import javax.swing.text.DefaultHighlighter; import javax.swing.text.Element; +import javax.swing.text.Highlighter; import javax.swing.text.StyledDocument; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; @@ -44,6 +49,7 @@ public class RichTextEditingPane extends UITextPane{ */ public RichTextEditingPane() { this.addMouseListener(doubleClickFormulaListener); + this.addFocusListener(focusListener); } /** @@ -195,4 +201,34 @@ public class RichTextEditingPane extends UITextPane{ } } }; + + private FocusListener focusListener = new FocusAdapter() { + /** + * 移除高亮,重新选中文本 + * @param e + */ + public void focusGained(FocusEvent e) { + RichTextEditingPane richTextPane = RichTextEditingPane.this; + richTextPane.getHighlighter().removeAllHighlights(); + richTextPane.select(richTextPane.getSelectionStart(), richTextPane.getSelectionEnd()); + } + + /** + * 失去焦点时,被选中的文本保持着被选中时的样式 + * @param e + */ + public void focusLost(FocusEvent e) { + RichTextEditingPane richTextPane = RichTextEditingPane.this; + int start = richTextPane.getSelectionStart(); + int end = richTextPane.getSelectionEnd(); + richTextPane.select(start, end); + Highlighter highlighter = richTextPane.getHighlighter(); + richTextPane.getHighlighter().removeAllHighlights(); + try { + highlighter.addHighlight(start, end, DefaultHighlighter.DefaultPainter); + } catch (BadLocationException exception) { + FineLoggerFactory.getLogger().error(exception.getMessage(), exception); + } + } + }; } \ No newline at end of file