Skip to content

12 Deleting an Item

Robert Silverton edited this page Jul 3, 2014 · 3 revisions

View this tutorial's changes on GitHub.

If you're working with the tutorial repo, open a Git Shell in the CoreEditor-HelloWorld-Example-as folder and run the following command:

git checkout -f step-12

Otherwise, follow the instructions below.


Overview

In the previous tutorial we saw how we can use ContextSelectionValidators to monitor the selection on a particular type of Context. In this tutorial we will build a CommandHandler that implements the DELETE Command on our StringListContext. To do this we will see how to access the current selection on the current Context during a CommandHandler’s execute() function.


Create the following class:

package helloWorld.commandHandlers
{
	import core.app.operations.ChangePropertyOperation;
	import core.app.operations.RemoveItemOperation;
	import core.app.operations.UndoableCompoundOperation;
	import core.appEx.core.commandHandlers.ICommandHandler;
	import core.editor.CoreEditor;
	
	import helloWorld.contexts.StringListContext;
	
	public class DeleteStringCommandHandler implements ICommandHandler
	{
		public function DeleteStringCommandHandler()
		{
		}
		
		public function execute(parameters:Object):void
		{
			// Grab a reference to the Context we're interested in
			var context:StringListContext = CoreEditor.contextManager.getLatestContextOfType(StringListContext);
			
			// Because we could be deleting multiple items, it's worth creating a CompoundOperation.
			// Even though we add each individual 'RemoveItemOperation' to this compound, removing
			// all these items will be seen as a single Operation, and so calling the UNDO Command
			// will add all the items back in one go.
			var compoundOperation:UndoableCompoundOperation = new UndoableCompoundOperation();
			compoundOperation.label = "Delete Item(s)";
			
			var selectedItems:Array = context.selection.source;
			for each ( var item:String in selectedItems )
			{
				var removeItemOperation:RemoveItemOperation = new RemoveItemOperation( item, context.dataProvider );
				compoundOperation.addOperation(removeItemOperation);
			}
			
			// Even though we have removed the items from the dataProvider, the Context's selection still contains them.
			// We need to make sure that the Context's selection is set to [].
			var clearSelectionOperation:ChangePropertyOperation = new ChangePropertyOperation( context.selection, "source", [] );
			compoundOperation.addOperation(clearSelectionOperation);
			
			context.operationManager.addOperation(compoundOperation);
		}
	}
}

The included comments explain most of what’s going on here, but it’s worth highlighting that last Operation we’re using, the ChangePropertyOperation. This is an incredibly versatile operation, and is used in many situations. As input it takes a reference to the object with the property you wish to be changed, the name of the property, and finally the new value you want this property to be set to.

The ChangePropertyOperation stores a reference to the property’s current value, which it can use during it’s undo() method. In this example you can see we’re setting the ‘source’ property of the Context’s selection property (which is an ArrayCollection), to an empty array. This effectively clears the selection.


Now let’s contribute this CommandHandler to our Extension. Add the following code to the end of your Extension’s constructor.

commandHandlerFactory = new CommandHandlerFactory( core.editor.entities.Commands.DELETE, DeleteStringCommandHandler );
commandHandlerFactory.validators.push( new ContextSelectionValidator( CoreEditor.contextManager, StringListContext, String ) );
CoreApp.resourceManager.addResource( commandHandlerFactory );

You might have noticed that we’re not defining our own DELETE Command, and are instead using the one already defined in the CoreEditor Command class. We also don’t need to add an Action for this Command, as this has already been contributed by the CoreEditor_Extensions in the same way as UNDO and REDO.


If you build and run the application, you should now be able to select items in your list and delete them using the pre-existing DELETE actions. You can also simply use the Delete key on your keyboard.


< Previous | Next >