Customize delete keyboard button operation

Are you missing a feature? Request its implementation here.
Prateeksha_Dixit
Posts: 4
Joined: Thu Oct 05, 2023 8:18 am

Customize delete keyboard button operation

Post by Prateeksha_Dixit »

Hi,

I have added below code for customizing delete operation. The code is working fine in no tag panel view and achieving following scenarios.
  • At cursor location it is checking if the element is required it is showing element can't be deleted.
  • By selecting the element from outline panel and pressing delete button from keyboard, it is deleting element only if the element is not required.

But in partial tag panel or full tag panel if I am selecting any tag, it is getting deleted even if it is required element.

Code: Select all

public class CustomDeleteHandler implements AuthorSchemaAwareEditingHandler {

	@Override
	public AuthorDocumentFragment handleCreateDocumentFragment(int arg0, int arg1, int arg2, AuthorAccess authorAcess)
			throws BadLocationException {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public boolean handleDelete(int arg0, int arg1, AuthorAccess authorAccess, boolean arg3) throws InvalidEditException {
		// TODO Auto-generated method stub
		try {
			int caretOffset = authorAccess.getEditorAccess().getCaretOffset();
			AuthorElement nodeAtCaret = (AuthorElement) authorAccess.getEditorAccess().getFullySelectedNode();
			if (nodeAtCaret == null) {
				nodeAtCaret = (AuthorElement) authorAccess.getDocumentController().getNodeAtOffset(caretOffset);
			}
			if(authorAccess.getDocumentController().getAuthorSchemaManager().isRequiredElement(nodeAtCaret))
    		{
				authorAccess.getWorkspaceAccess().showInformationMessage("Selected element can not be deleted");
    			return true;
    		}
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 throw new InvalidEditException
				(e.getMessage(), "Invalid Delete Operation: " + e.getMessage(), e, false);
		}
		authorAccess.getWorkspaceAccess().showInformationMessage("Selected element can be deleted");
		return false;
	}
Please refer below screenshot having selected Title element through title tag. Even if this element is required this Title element will get deleted since I have selected it through Title tag.
image.png
image.png (20.58 KiB) Viewed 346 times
mihaela
Posts: 490
Joined: Wed May 20, 2009 2:40 pm

Re: Customize delete keyboard button operation

Post by mihaela »

Hello,

There are 4 handle delete methods in ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandler API. You should implement each one and add your delete restriction code. It is possible that when you delete the node in the second case, another delete method to be called than the one you already implemented.

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
Prateeksha_Dixit
Posts: 4
Joined: Thu Oct 05, 2023 8:18 am

Re: Customize delete keyboard button operation

Post by Prateeksha_Dixit »

Thanks Mihaela!!

I'll implement each of the methods as in when required and will see if this gets resolved.

Thanks in advance!!
Post Reply