Skip to content

Commit 2d917fa

Browse files
committed
Fix stream editor caret not appearing in read-only mode
Default caret never becomes visible, if the text area is not editable, which is very odd...
1 parent c494890 commit 2d917fa

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

src/main/java/com/itextpdf/rups/view/itext/StreamTextEditorPane.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This file is part of the iText (R) project.
5656
import com.itextpdf.rups.view.Language;
5757
import com.itextpdf.rups.view.contextmenu.ContextMenuMouseListener;
5858
import com.itextpdf.rups.view.contextmenu.StreamPanelContextMenu;
59+
import com.itextpdf.rups.view.itext.editor.CustomConfigurableCaret;
5960
import com.itextpdf.rups.view.itext.editor.Latin1Filter;
6061
import com.itextpdf.rups.view.itext.editor.PdfFoldParser;
6162
import com.itextpdf.rups.view.itext.editor.PdfParser;
@@ -403,6 +404,12 @@ private static RSyntaxTextArea createTextArea() {
403404
* by default we will just assume a PDF content stream.
404405
*/
405406
setContentType(textArea, SYNTAX_STYLE_PDF);
407+
/*
408+
* Pretty important to install our custom caret. The default one is
409+
* invisible, when the text area is not visible, which is very odd and
410+
* inconvenient. The custom one fixes that.
411+
*/
412+
textArea.setCaret(new CustomConfigurableCaret());
406413
textArea.addParser(new PdfParser());
407414
// This will allow to fold code blocks (like BT/ET blocks)
408415
textArea.setCodeFoldingEnabled(true);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2025 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
APRYSE GROUP. APRYSE GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.rups.view.itext.editor;
44+
45+
import java.awt.event.FocusEvent;
46+
import org.fife.ui.rtextarea.ConfigurableCaret;
47+
48+
/**
49+
* Our custom {@link ConfigurableCaret}, which remains visible, if the text
50+
* area is not editable.
51+
*/
52+
public final class CustomConfigurableCaret extends ConfigurableCaret {
53+
private static final int DEFAULT_BLINK_RATE = 500;
54+
55+
public CustomConfigurableCaret() {
56+
/*
57+
* The situation is a bit odd. Usually a caret is created via the UI
58+
* class, and then the blink rate is set manually in that class after
59+
* creation based on some component properties.
60+
*
61+
* But what it also means is that if you replace the caret in a text
62+
* area afterward, it will not blink, even though it is the default
63+
* behavior. So for simplicity we will set it here.
64+
*/
65+
setBlinkRate(DEFAULT_BLINK_RATE);
66+
}
67+
68+
@Override
69+
public void focusGained(FocusEvent e) {
70+
super.focusGained(e);
71+
if (getComponent().isEnabled()) {
72+
setVisible(true);
73+
}
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2025 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
APRYSE GROUP. APRYSE GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.rups.view.itext.editor;
44+
45+
import java.awt.event.FocusEvent;
46+
import org.fife.ui.rtextarea.ConfigurableCaret;
47+
import org.fife.ui.rtextarea.RTextArea;
48+
import org.junit.jupiter.api.Assertions;
49+
import org.junit.jupiter.api.Tag;
50+
import org.junit.jupiter.api.Test;
51+
52+
@Tag("UnitTest")
53+
class CustomConfigurableCaretTest {
54+
@Test
55+
void focusGained() {
56+
final ConfigurableCaret caret = new CustomConfigurableCaret();
57+
final RTextArea textArea = new RTextArea();
58+
textArea.setCaret(caret);
59+
Assertions.assertFalse(caret.isVisible());
60+
61+
// Making sure visibility changes for read-only text areas
62+
textArea.setEditable(false);
63+
textArea.setEnabled(false);
64+
caret.focusGained(new FocusEvent(textArea, FocusEvent.FOCUS_GAINED));
65+
Assertions.assertFalse(caret.isVisible());
66+
textArea.setEnabled(true);
67+
caret.focusGained(new FocusEvent(textArea, FocusEvent.FOCUS_GAINED));
68+
Assertions.assertTrue(caret.isVisible());
69+
}
70+
}

0 commit comments

Comments
 (0)