-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pasteDocument action and test #322
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
784e724
Add pasteDocument action and test
jperedadnr 5e9ebc0
Add action
jperedadnr dfd34a0
add more assertions
jperedadnr 413c1bc
fix test on Linux
jperedadnr b199647
Merge branch 'main' into 98-pastedocument
jperedadnr 279784e
Fix internal selection issue and add more tests
jperedadnr 5d9bf9d
more tests
jperedadnr 7ea8cc8
more tests
jperedadnr 43599ab
Better approach for paste document action
jperedadnr cbd7ed7
Merge branch 'main' into 98-pastedocument
jperedadnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
rta/src/main/java/com/gluonhq/richtextarea/viewmodel/ActionCmdPasteDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2024, Gluon | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.gluonhq.richtextarea.viewmodel; | ||
|
||
import com.gluonhq.richtextarea.model.Document; | ||
import javafx.beans.binding.Bindings; | ||
import javafx.beans.binding.BooleanBinding; | ||
|
||
import java.util.Objects; | ||
|
||
class ActionCmdPasteDocument implements ActionCmd { | ||
|
||
private final Document document; | ||
|
||
public ActionCmdPasteDocument(Document document) { | ||
this.document = document; | ||
} | ||
|
||
@Override | ||
public void apply(RichTextAreaViewModel viewModel) { | ||
if (viewModel.isEditable()) { | ||
viewModel.getCommandManager().execute(new PasteDocumentCmd(Objects.requireNonNull(document))); | ||
} | ||
} | ||
|
||
@Override | ||
public BooleanBinding getDisabledBinding(RichTextAreaViewModel viewModel) { | ||
return Bindings.createBooleanBinding(() -> !viewModel.clipboardHasDocument() || !viewModel.isEditable(), viewModel.editableProperty()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
rta/src/main/java/com/gluonhq/richtextarea/viewmodel/PasteDocumentCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2024, Gluon | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.gluonhq.richtextarea.viewmodel; | ||
|
||
import com.gluonhq.richtextarea.Selection; | ||
import com.gluonhq.richtextarea.model.Document; | ||
|
||
import java.util.Objects; | ||
|
||
class PasteDocumentCmd extends AbstractEditCmd { | ||
|
||
private final Document content; | ||
|
||
public PasteDocumentCmd(Document content) { | ||
this.content = Objects.requireNonNull(content); | ||
} | ||
|
||
@Override | ||
public void doRedo(RichTextAreaViewModel viewModel) { | ||
Objects.requireNonNull(viewModel); | ||
// Go through all decorations: inserting unit and decorating it | ||
content.getDecorations().forEach(dm -> { | ||
int caretPosition = viewModel.getCaretPosition(); | ||
int initialLength = viewModel.getTextLength(); | ||
// 1. insert unit | ||
String text = content.getText().substring(dm.getStart(), dm.getStart() + dm.getLength()); | ||
viewModel.insert(text); | ||
// 1. decorate unit | ||
int addedLength = viewModel.getTextLength() - initialLength; | ||
Selection newSelection = new Selection(caretPosition, Math.min(caretPosition + addedLength, viewModel.getTextLength())); | ||
viewModel.setSelection(newSelection); | ||
viewModel.decorate(dm.getDecoration()); | ||
// For now: ignore paragraph decoration | ||
// viewModel.decorate(dm.getParagraphDecoration()); | ||
viewModel.setSelection(Selection.UNDEFINED); | ||
viewModel.setCaretPosition(newSelection.getEnd()); | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public void doUndo(RichTextAreaViewModel viewModel) { | ||
Objects.requireNonNull(viewModel); | ||
|
||
content.getDecorations().forEach(dm -> { | ||
// 1. remove unit | ||
viewModel.undo(); | ||
// 2. delete decoration | ||
viewModel.undoDecoration(); | ||
}); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PasteDocumentCmd[" + super.toString() + ", " + content + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 3 calls to
getTextBuffer()
in this method. Wondering if we should instead create a variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed. In this class
getTextBuffer()
it is used 30+ times, without extracting variables. After all, it is just a getter from a property, with a null check.