Skip to content

Commit

Permalink
step-12 Deleting an Item
Browse files Browse the repository at this point in the history
* Adding a CommandHandler to remove selected items from the current
StringListContext
* Tying that CommandHandler to the
"core.editor.entities.Commands.DELETE" Command, meaning items can be
deleted by pressing the Delete Key or navigating to "Edit/Delete" in the
MenuBar.
  • Loading branch information
robsilv committed Jul 3, 2014
1 parent efdce47 commit 2c6aeaf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/HelloWorldExtension.as
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ package
import core.appEx.resources.CommandHandlerFactory;
import core.appEx.resources.FileType;
import core.appEx.resources.KeyBinding;
import core.appEx.validators.ContextSelectionValidator;
import core.appEx.validators.ContextValidator;
import core.editor.CoreEditor;
import core.editor.core.IGlobalViewContainer;
import core.editor.entities.Commands;
import core.editor.icons.CoreEditorIcons;
import core.editor.resources.ActionFactory;
import core.editor.resources.EditorFactory;

import helloWorld.commandHandlers.AddStringCommandHandler;
import helloWorld.commandHandlers.DeleteStringCommandHandler;
import helloWorld.commandHandlers.MyCommandHandler;
import helloWorld.contexts.HelloWorldContext;
import helloWorld.contexts.StringListContext;
Expand All @@ -28,16 +31,20 @@ package
CoreApp.resourceManager.addResource( new FactoryResource( HelloWorldContext, "Hello World" ) );
CoreApp.resourceManager.addResource( new EditorFactory( StringListContext, "String List", "strlist", CoreEditorIcons.Text ) );

CoreApp.resourceManager.addResource( new ActionFactory( IGlobalViewContainer, Commands.MY_COMMAND, "My Action", "myActions", "Actions/myActions", CoreEditorIcons.Resource ) );
CoreApp.resourceManager.addResource( new ActionFactory( StringListContext, Commands.ADD_STRING, "Add String", "myActions" ) );
CoreApp.resourceManager.addResource( new ActionFactory( IGlobalViewContainer, helloWorld.entities.Commands.MY_COMMAND, "My Action", "myActions", "Actions/myActions", CoreEditorIcons.Resource ) );
CoreApp.resourceManager.addResource( new ActionFactory( StringListContext, helloWorld.entities.Commands.ADD_STRING, "Add String", "myActions" ) );

CoreApp.resourceManager.addResource( new KeyBinding( Commands.MY_COMMAND, 77, KeyModifier.CTRL ) );
CoreApp.resourceManager.addResource( new KeyBinding( helloWorld.entities.Commands.MY_COMMAND, 77, KeyModifier.CTRL ) );

CoreApp.resourceManager.addResource( new CommandHandlerFactory( Commands.MY_COMMAND, MyCommandHandler ) );
var commandHandlerFactory:CommandHandlerFactory = new CommandHandlerFactory( Commands.ADD_STRING, AddStringCommandHandler );
CoreApp.resourceManager.addResource( new CommandHandlerFactory( helloWorld.entities.Commands.MY_COMMAND, MyCommandHandler ) );
var commandHandlerFactory:CommandHandlerFactory = new CommandHandlerFactory( helloWorld.entities.Commands.ADD_STRING, AddStringCommandHandler );
commandHandlerFactory.validators.push( new ContextValidator( CoreEditor.contextManager, StringListContext ) );
CoreApp.resourceManager.addResource( commandHandlerFactory );

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

CoreApp.resourceManager.addResource( new FileType( "String List File", "strlist", CoreEditorIcons.Text ) );
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/helloWorld/commandHandlers/DeleteStringCommandHandler.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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);
}
}
}

0 comments on commit 2c6aeaf

Please sign in to comment.