/*
 * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *  
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

define(function (require, exports) {
    "use strict";

    var descriptor = require("adapter/ps/descriptor"),
        tool = require("adapter/lib/tool"),
        events = require("../events");

    var synchronization = require("js/util/synchronization");
        
    var selectToolCommand = function (toolName) {
        return descriptor.playObject(
            tool.setTool(toolName + "Tool")
        ).then(function () {
            var payload = {
                newTool: toolName
            };
            
            this.dispatch(events.tools.SELECT_TOOL, payload);
        }.bind(this));
    };
    
    var selectTool = {
        command: selectToolCommand,
        reads: [synchronization.LOCKS.APP],
        writes: []
    };
    
    exports.select = selectTool;
});

@@ -1332,161 +1332,620 @@
     
-events = require(%22../events%22);%0A%0A    var synchronization = require(%22js/util/synchronization%22);%0A        %0A    var selectToolCommand = function (toolName) %7B%0A
+photoshopEvent = require(%22adapter/lib/photoshopEvent%22),%0A        adapterPS = require(%22adapter/ps%22),%0A        events = require(%22../events%22),%0A        Promise = require(%22bluebird%22);%0A%0A    var synchronization = require(%22js/util/synchronization%22);%0A        %0A    /**%0A     * Activates the given tool in Photoshop%0A     *%0A     * @param %7Bstring%7D toolName %0A     *%0A     * @return %7BPromise%7D Resolves to tool change%0A     */%0A    var selectToolCommand = function (toolName) %7B%0A        return adapterPS.endModalToolState(true)%0A            .then(function () %7B%0A                var setToolObj = tool.setTool(toolName + %22Tool%22);%0A%0A        
     
@@ -1977,16 +1977,28 @@
 yObject(
+setToolObj);
 %0A       
@@ -2006,39 +2006,732 @@
     
-tool.setTool(toolName + 
+%7D)%0A            .then(function () %7B%0A                var payload = %7B%0A                    newTool: toolName%0A                %7D;%0A                this.dispatch(events.tools.SELECT_TOOL, payload);%0A            %7D.bind(this));%0A    %7D;%0A%0A    /**%0A     * Registers to %22select%22 events in Photoshop to dispatch when%0A     * a tool is selected through Photoshop UI%0A     * %0A     * Also grabs the current tool from Photoshop and dispatches it%0A     *%0A     * return %7BPromise%7D Resolves when the current tool is grabbed from Photoshop%0A     */%0A    var listenToTools = function () %7B%0A        descriptor.addListener(%22select%22, function (event) %7B%0A            var target = photoshopEvent.targetOf(event);%0A            var toolIndex = target.indexOf(
 %22Tool%22)
+;
 %0A   
@@ -2735,17 +2735,407 @@
         
-)
+    if (toolIndex %3E -1) %7B%0A                var payload = %7B%0A                    newTool: target.substr(0, toolIndex)%0A                %7D;%0A                this.dispatch(events.tools.SELECT_TOOL, payload);%0A            %7D else %7B%0A                Promise.reject(%22Not a tool select event%22);%0A            %7D%0A        %7D.bind(this));%0A%0A        return descriptor.getProperty(%22application%22, %22tool%22)%0A            
 .then(fu
@@ -3134,32 +3134,42 @@
 .then(function (
+toolObject
 ) %7B%0A            
@@ -3160,36 +3160,146 @@
 ) %7B%0A            
+   
+ 
 var 
+toolName = toolObject.enum,%0A                    toolIndex = toolName.indexOf(%22Tool%22),%0A                    
 payload = %7B%0A    
@@ -3302,32 +3302,40 @@
                 
+ 
+       
 newTool: toolNam
@@ -3327,33 +3327,62 @@
 ewTool: toolName
-%0A
+.substr(0, toolIndex)%0A        
             %7D;%0A 
@@ -3392,25 +3392,16 @@
         
-%0A        
     this
@@ -3438,32 +3438,36 @@
 TOOL, payload);%0A
+    
         %7D.bind(t

Make the style changes and jsdoc the tool actions