Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
Expand Down Expand Up @@ -66,7 +66,7 @@ public void setModified(boolean on) {
}

public void setText(String text) {
replace(null, TextPos.ZERO, TextPos.ZERO, text, false);
replace(null, TextPos.ZERO, TextPos.ZERO, text);
setModified(false);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
Expand Down Expand Up @@ -64,7 +64,7 @@ public void setModified(boolean on) {
}

public void setText(String text) {
replace(null, TextPos.ZERO, TextPos.ZERO, text, false);
replace(null, TextPos.ZERO, TextPos.ZERO, text);
setModified(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,6 @@ private static StyledTextModel writingSystems() {
// TODO add to StyledModel?
private static void append(StyledTextModel m, String text, StyleAttributeMap style) {
TextPos p = m.getDocumentEnd();
m.replace(null, p, p, StyledInput.of(text, style), false);
m.replace(null, p, p, StyledInput.of(text, style));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
Expand Down Expand Up @@ -85,9 +85,11 @@ static RichTextArea appendStyledText() {

RichTextArea textArea = new RichTextArea();
// build the content
textArea.setUndoRedoEnabled(false);
textArea.appendText("RichTextArea\n", heading);
textArea.appendText("Example:\nText is ", StyleAttributeMap.EMPTY);
textArea.appendText("monospaced.\n", mono);
textArea.setUndoRedoEnabled(true);
return textArea;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected boolean handleTypedChar(String typed) {
end = start;
}

TextPos p = m.replace(vflow, start, end, typed, true);
TextPos p = m.replace(vflow, start, end, typed);
moveCaret(p, false);

clearPhantomX();
Expand Down Expand Up @@ -355,7 +355,7 @@ public void insertLineBreak() {
return;
}

TextPos pos = m.replace(vflow, start, end, StyledInput.of("\n"), true);
TextPos pos = m.replace(vflow, start, end, StyledInput.of("\n"));
moveCaret(pos, false);
clearPhantomX();
}
Expand Down Expand Up @@ -943,7 +943,7 @@ public void backspace() {
start = TextPos.ofLeading(ix, off);
}

control.getModel().replace(vflow, start, p, StyledInput.EMPTY, true);
control.getModel().replace(vflow, start, p, StyledInput.EMPTY);
moveCaret(start, false);
clearPhantomX();
}
Expand All @@ -959,7 +959,7 @@ public void delete() {
TextPos start = control.getCaretPosition();
TextPos end = nextCharacterVisually(start, true);
if (end != null) {
control.getModel().replace(vflow, start, end, StyledInput.EMPTY, true);
control.getModel().replace(vflow, start, end, StyledInput.EMPTY);
moveCaret(start, false);
clearPhantomX();
}
Expand All @@ -985,7 +985,7 @@ public void deleteParagraph() {
TextPos p0 = TextPos.ofLeading(ix0, 0);
TextPos p1 = clamp(TextPos.ofLeading(ix1 + 1, 0));
RichTextArea control = getControl();
control.getModel().replace(vflow, p0, p1, StyledInput.EMPTY, true);
control.getModel().replace(vflow, p0, p1, StyledInput.EMPTY);
clearPhantomX();
moveCaret(p0, false);
}
Expand All @@ -1002,7 +1002,7 @@ protected void deleteSelection() {
TextPos start = sel.getMin();
TextPos end = sel.getMax();
RichTextArea control = getControl();
control.getModel().replace(vflow, start, end, StyledInput.EMPTY, true);
control.getModel().replace(vflow, start, end, StyledInput.EMPTY);
clearPhantomX();
moveCaret(start, false);
}
Expand Down Expand Up @@ -1189,7 +1189,7 @@ private void pasteLocal(DataFormat f) {

StyleAttributeMap a = control.getActiveStyleAttributeMap();
try (StyledInput in = h.createStyledInput(text, a)) {
TextPos p = m.replace(vflow, start, end, in, true);
TextPos p = m.replace(vflow, start, end, in);
moveCaret(p, false);
} catch (IOException e) {
control.errorFeedback();
Expand Down Expand Up @@ -1591,7 +1591,7 @@ private void deleteIgnoreSelection(BiFunction<RichTextArea, TextPos, TextPos> ge
if (p != null) {
control.clearSelection();
clearPhantomX();
p = control.replaceText(caret, p, "", true);
p = control.replaceText(caret, p, "");
control.select(p);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.jfx.incubator.scene.control.richtext;

import com.sun.javafx.util.Utils;
import jfx.incubator.scene.control.richtext.StyleResolver;
import jfx.incubator.scene.control.richtext.TextPos;
import jfx.incubator.scene.control.richtext.model.StyledInput;
import jfx.incubator.scene.control.richtext.model.StyledTextModel;

/**
* Provides access to internal methods in StyledTextModel.
*/
public class StyledTextModelHelper {
public interface Accessor {
public TextPos replace(StyledTextModel m, StyleResolver r, TextPos start, TextPos end, StyledInput in, boolean allowUndo);
}

static {
Utils.forceInit(StyledTextModel.class);
}

private static Accessor accessor;

public static void setAccessor(Accessor a) {
if (accessor != null) {
throw new IllegalStateException();
}
accessor = a;
}

public static TextPos replace(StyledTextModel m, StyleResolver r, TextPos start, TextPos end, StyledInput in, boolean allowUndo) {
return accessor.replace(m, r, start, end, in, allowUndo);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -102,12 +102,12 @@ public void undo(StyleResolver resolver) throws IOException {

// undo
SegmentStyledInput in = new SegmentStyledInput(undo);
model.replace(resolver, start, endAfter, in, false);
StyledTextModelHelper.replace(model, resolver, start, endAfter, in, false);
}

public void redo(StyleResolver resolver) throws IOException {
SegmentStyledInput in = new SegmentStyledInput(redo);
model.replace(resolver, start, endBefore, in, false);
StyledTextModelHelper.replace(model, resolver, start, endBefore, in, false);
}

public UndoableChange getPrev() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,14 @@ public final String getText() {
}

/**
* Replaces text in this CodeArea.
* Replaces text in this CodeArea. This method creates an undo/redo entry.
* <p>
* The caret gets reset to the start of the document, selection gets cleared, and an undo event gets created.
* @param text the text string
*/
public final void setText(String text) {
TextPos end = getDocumentEnd();
getModel().replace(null, TextPos.ZERO, end, text, true);
getModel().replace(null, TextPos.ZERO, end, text);
}

private CodeTextModel codeModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@
*
* RichTextArea textArea = new RichTextArea();
* // build the content
* textArea.appendText("RichTextArea\n", heading);
* textArea.appendText("Example:\nText is ", StyleAttributeMap.EMPTY);
* textArea.appendText("monospaced.\n", mono);
* textArea.setUndoRedoEnabled(false);
* textArea.appendText("RichTextArea\n", heading, false);
* textArea.appendText("Example:\nText is ", StyleAttributeMap.EMPTY, false);
* textArea.appendText("monospaced.\n", mono, false);
* textArea.setUndoRedoEnabled(true);
* }</pre>
* Which results in the following visual representation:
* <p>
Expand Down Expand Up @@ -789,6 +791,32 @@ public final boolean isUndoable() {
return undoableProperty().get();
}

/**
* Indicates whether undo/redo functionality is enabled in the model.
* Returns {@code false} if the model is {@code null}.
* @return true if undo/redo functionality is enabled in the model
* @since 26
*/
public final boolean isUndoRedoEnabled() {
StyledTextModel m = getModel();
return (m == null ? false : m.isUndoRedoEnabled());
}

/**
* Controls whether undo/redo functionality is enabled in the model.
* Setting the value to {@code false} clears existing undo/redo entries.
* This method does nothing if the model is {@code null}.
* @param on true to enable undo/redo
* @since 26
* @see #clearUndoRedo()
*/
public final void setUndoRedoEnabled(boolean on) {
StyledTextModel m = getModel();
if (m != null) {
m.setUndoRedoEnabled(on);
}
}

/**
* Determines whether the preferred height is the same as the content height.
* When set to true, the vertical scroll bar is disabled.
Expand Down Expand Up @@ -1044,7 +1072,7 @@ public StyleableProperty<Boolean> getStyleableProperty(RichTextArea t) {

/**
* Appends the styled text to the end of the document. Any embedded {@code "\n"} or {@code "\r\n"}
* sequences result in a new paragraph being added.
* sequences result in a new paragraph being added. This method creates an undo/redo entry.
* <p>
* It is up to the model to decide whether to accept all, some, or none of the
* {@link jfx.incubator.scene.control.richtext.model.StyleAttribute StyleAttribute}s.
Expand All @@ -1062,10 +1090,10 @@ public final TextPos appendText(String text, StyleAttributeMap attrs) {

/**
* Appends the styled text to the end of the document. Any embedded {@code "\n"} or {@code "\r\n"}
* sequences result in a new paragraph being added.
* sequences result in a new paragraph being added. This method creates an undo/redo entry.
* <p>
* This convenience method is equivalent to calling
* {@code appendText(text, StyleAttributeMap.EMPTY);}
* {@code appendText(text, StyleAttributeMap.EMPTY, true);}
*
* @param text the text to append
* @return the text position at the end of the appended text, or null if editing is disabled
Expand All @@ -1078,7 +1106,7 @@ public final TextPos appendText(String text) {

/**
* Appends the styled content to the end of the document. Any embedded {@code "\n"} or {@code "\r\n"}
* sequences result in a new paragraph being added.
* sequences result in a new paragraph being added. This method creates an undo/redo entry.
*
* @param in the input stream
* @return the text position at the end of the appended text, or null if editing is disabled
Expand Down Expand Up @@ -1123,14 +1151,14 @@ public void backspace() {
}

/**
* Clears the document, creating an undo entry.
* Clears the document, creating an undo/redo entry.
*
* @throws NullPointerException if the model is {@code null}
* @throws UnsupportedOperationException if the model is not {@link StyledTextModel#isWritable() writable}
*/
public final void clear() {
TextPos end = getDocumentEnd();
replaceText(TextPos.ZERO, end, StyledInput.EMPTY, true);
replaceText(TextPos.ZERO, end, StyledInput.EMPTY);
}

/**
Expand Down Expand Up @@ -1471,7 +1499,7 @@ public void insertTab() {

/**
* Inserts the styled text at the specified position. Any embedded {@code "\n"} or {@code "\r\n"}
* sequences result in a new paragraph being added.
* sequences result in a new paragraph being added. This method creates an undo/redo entry.
*
* @param pos the insert position
* @param text the text to inser
Expand All @@ -1482,11 +1510,11 @@ public void insertTab() {
*/
public final TextPos insertText(TextPos pos, String text, StyleAttributeMap attrs) {
StyledInput in = StyledInput.of(text, attrs);
return replaceText(pos, pos, in, true);
return replaceText(pos, pos, in);
}

/**
* Inserts the styled content at the specified position.
* Inserts the styled content at the specified position. This method creates an undo/redo entry.
*
* @param pos the insert position
* @param in the input stream
Expand All @@ -1495,7 +1523,7 @@ public final TextPos insertText(TextPos pos, String text, StyleAttributeMap attr
* @throws UnsupportedOperationException if the model is not {@link StyledTextModel#isWritable() writable}
*/
public final TextPos insertText(TextPos pos, StyledInput in) {
return replaceText(pos, pos, in, true);
return replaceText(pos, pos, in);
}

/**
Expand Down Expand Up @@ -1819,35 +1847,33 @@ public void redo() {
}

/**
* Replaces the specified range with the new text.
* Replaces the specified range with the new text. This method creates an undo entry.
*
* @param start the start text position
* @param end the end text position
* @param text the input text
* @param allowUndo when true, creates an undo-redo entry
* @return the new caret position at the end of inserted text, or null if the change cannot be made
* @throws NullPointerException if the model is {@code null}
* @throws UnsupportedOperationException if the model is not {@link StyledTextModel#isWritable() writable}
*/
public final TextPos replaceText(TextPos start, TextPos end, String text, boolean allowUndo) {
public final TextPos replaceText(TextPos start, TextPos end, String text) {
StyledTextModel m = getModel();
return m.replace(vflow(), start, end, text, allowUndo);
return m.replace(vflow(), start, end, text);
}

/**
* Replaces the specified range with the new input.
* Replaces the specified range with the new input. This method creates an undo entry.
*
* @param start the start text position
* @param end the end text position
* @param in the input stream
* @param createUndo when true, creates an undo-redo entry
* @return the new caret position at the end of inserted text, or null if the change cannot be made
* @throws NullPointerException if the model is {@code null}
* @throws UnsupportedOperationException if the model is not {@link StyledTextModel#isWritable() writable}
*/
public final TextPos replaceText(TextPos start, TextPos end, StyledInput in, boolean createUndo) {
public final TextPos replaceText(TextPos start, TextPos end, StyledInput in) {
StyledTextModel m = getModel();
return m.replace(vflow(), start, end, in, createUndo);
return m.replace(vflow(), start, end, in);
}

/**
Expand Down
Loading