/* This notice must be untouched at all times.

Open-jACOB Draw2D
The latest version is available at
http://www.openjacob.org

Copyright (c) 2006 Andreas Herz. All rights reserved.
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
or see http://www.gnu.org/copyleft/lesser.html
*/

draw2d.VectorPalette=function()
{
  draw2d.ToolPalette.call(this, "Tools");

  this.tool1 = new draw2d.ToolLine(this);
  this.tool2 = new draw2d.ToolRectangle(this);
  this.tool3 = new draw2d.ToolOval(this);

  this.tool1.setPosition(62,0);

  this.tool2.setPosition(90,2);
  this.tool3.setPosition(120,0);

  this.addChild(this.tool1);
  this.addChild(this.tool2);
  this.addChild(this.tool3);

  // undo / redo support
  //
  this.undoTool = new draw2d.ToolUndo(this);
  this.undoTool.setPosition(3,0);
  this.undoTool.setEnabled(false);
  this.addChild(this.undoTool);


  this.redoTool = new draw2d.ToolRedo(this);
  this.redoTool.setPosition(32,0);
  this.redoTool.setEnabled(false);
  this.addChild(this.redoTool);
  
  
   	this.setDimension(150,30);
	this.setCanDrag(false);
  	this.setLineWidth(0);
}

draw2d.VectorPalette.prototype = new draw2d.ToolPalette;
/** @private */
draw2d.VectorPalette.prototype.type="VectorPalette";


draw2d.VectorPalette.prototype.onSetDocumentDirty=function()
{
  this.undoTool.setEnabled(this.workflow.getCommandStack().canUndo());
  this.redoTool.setEnabled(this.workflow.getCommandStack().canRedo());
}


/* This notice must be untouched at all times.

Open-jACOB Draw2D
The latest version is available at
http://www.openjacob.org

Copyright (c) 2006 Andreas Herz. All rights reserved.
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
or see http://www.gnu.org/copyleft/lesser.html
*/




/** override Canvas for alpha tansparency
 * Will be called after a drag and drop action.<br>
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 * the super implementation via <code>Figure.prototype.onDragend.call(this);</code>
 * @private
 **/
 
 /**
 * @param {draw2d.Figure} figure The figure object to add to the canvas
 * @param {int} xPos The x coordinate for the figure
 * @param {int} yPos The y coordinate for the figure
 **/
draw2d.Canvas.prototype.addFigure= function( /*:draw2d.Figure*/figure,/*:int*/ xPos,/*:int*/ yPos, /*:boolean*/ avoidPaint)
{
   if(this.enableSmoothFigureHandling==true)
   {
      if(figure.timer<=0)
         figure.setAlpha(0.001);
      var oFigure = figure;
      var slowShow = function()
      {
         if(oFigure.alpha<1.0)
            oFigure.setAlpha(Math.min(0.40,oFigure.alpha+0.05));
         else
         {
            window.clearInterval(oFigure.timer);
            oFigure.timer = -1;
         }
      };
      if(oFigure.timer>0)
         window.clearInterval(oFigure.timer);
      oFigure.timer = window.setInterval(slowShow,30);
   }

   figure.setCanvas(this);
   figure.setAlpha(0.40);
   if(xPos && yPos)
     figure.setPosition(xPos,yPos);

   this.html.appendChild(figure.getHTMLElement());
   if(!avoidPaint)
      figure.paint();
}

/** override Figure for alpha tansparency
 * Will be called after a drag and drop action.<br>
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 * the super implementation via <code>Figure.prototype.onDragend.call(this);</code>
 * @private
 **/
draw2d.Figure.prototype.onDragend = function()
{
   if(this.getWorkflow().getEnableSmoothFigureHandling()==true)
   {
      var oFigure = this;
      var slowShow = function()
      {
         if(oFigure.alpha<1.0)
            oFigure.setAlpha(Math.min(0.40,oFigure.alpha+0.05));
         else
         {
            window.clearInterval(oFigure.timer);
            oFigure.timer = -1;
         }
      };
      if(oFigure.timer>0)
         window.clearInterval(oFigure.timer);
      oFigure.timer = window.setInterval(slowShow,20);
  }
  else
  {
      this.setAlpha(0.40);
  }
  // Element ist zwar schon an seine Position, das Command muss aber trotzdem
  // in dem CommandStack gelegt werden damit das Undo funktioniert.
  //
  this.command.setPosition(this.x, this.y);
  this.workflow.commandStack.execute(this.command);
  this.command = null;
  this.isMoving = false;
  this.workflow.hideSnapToHelperLines();
  this.fireMoveEvent();
}


	/** 
	* ToolOval class 
	**/
draw2d.ToolOval=function(/*:draw2d.PaletteWindow*/ palette)
{
  draw2d.ToolGeneric.call(this,palette);
}

draw2d.ToolOval.prototype = new draw2d.ToolGeneric;

/** @private */
draw2d.ToolOval.prototype.type="ToolOval";


draw2d.ToolOval.prototype.execute=function(/*:int*/ x,/*:int*/ y)
{
  var figure= new draw2d.Oval();
  figure.setDimension(100,60);
  figure.setBackgroundColor(new  draw2d.Color(255,255,255));

  // 1.) Undo/Redo support
  this.palette.workflow.getCommandStack().execute(new draw2d.CommandAdd(this.palette.workflow,figure,x,y));

  // 2.) without undo/redo
//  this.palette.workflow.addFigure(figure,x,y);

  draw2d.ToolGeneric.prototype.execute.call(this,x,y);
}



/** 
 * Tool Definition
**/

/** override Canvas for alpha tansparency
 * Will be called after a drag and drop action.<br>
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 * the super implementation via <code>Figure.prototype.onDragend.call(this);</code>
 * @private
 **/
 
 /**
 * @param {draw2d.Figure} figure The figure object to add to the canvas
 * @param {int} xPos The x coordinate for the figure
 * @param {int} yPos The y coordinate for the figure
 **/
draw2d.Canvas.prototype.addFigure= function( /*:draw2d.Figure*/figure,/*:int*/ xPos,/*:int*/ yPos, /*:boolean*/ avoidPaint)
{
   if(this.enableSmoothFigureHandling==true)
   {
      if(figure.timer<=0)
         figure.setAlpha(0.001);
      var oFigure = figure;
      var slowShow = function()
      {
         if(oFigure.alpha<1.0)
            oFigure.setAlpha(Math.min(0.40,oFigure.alpha+0.05));
         else
         {
            window.clearInterval(oFigure.timer);
            oFigure.timer = -1;
         }
      };
      if(oFigure.timer>0)
         window.clearInterval(oFigure.timer);
      oFigure.timer = window.setInterval(slowShow,30);
   }

   figure.setCanvas(this);
   figure.setAlpha(0.40);
   if(xPos && yPos)
     figure.setPosition(xPos,yPos);

   this.html.appendChild(figure.getHTMLElement());
   if(!avoidPaint)
      figure.paint();
}
/** override Figure for alpha tansparency
 * Will be called after a drag and drop action.<br>
 * Sub classes can override this method to implement additional stuff. Don't forget to call
 * the super implementation via <code>Figure.prototype.onDragend.call(this);</code>
 * @private
 **/
draw2d.Figure.prototype.onDragend = function()
{
   if(this.getWorkflow().getEnableSmoothFigureHandling()==true)
   {
      var oFigure = this;
      var slowShow = function()
      {
         if(oFigure.alpha<1.0)
            oFigure.setAlpha(Math.min(0.40,oFigure.alpha+0.05));
         else
         {
            window.clearInterval(oFigure.timer);
            oFigure.timer = -1;
         }
      };
      if(oFigure.timer>0)
         window.clearInterval(oFigure.timer);
      oFigure.timer = window.setInterval(slowShow,20);
  }
  else
  {
      this.setAlpha(0.40);
  }
  // Element ist zwar schon an seine Position, das Command muss aber trotzdem
  // in dem CommandStack gelegt werden damit das Undo funktioniert.
  //
  this.command.setPosition(this.x, this.y);
  this.workflow.commandStack.execute(this.command);
  this.command = null;
  this.isMoving = false;
  this.workflow.hideSnapToHelperLines();
  this.fireMoveEvent();
}
/** 
 * ToolRectangle class 
 **/
draw2d.ToolRectangle=function(/*:draw2d.PaletteWindow*/ palette)
{
  draw2d.ToolGeneric.call(this,palette);
}
draw2d.ToolRectangle.prototype=new draw2d.ToolGeneric;

/** @private */
draw2d.ToolRectangle.prototype.type="ToolRectangle";

draw2d.ToolRectangle.prototype.execute=function(/*:int*/ x ,/*:int*/ y)
{
  var figure= new draw2d.Rectangle();
  figure.setDimension(100,60);
  figure.setBackgroundColor(new  draw2d.Color(255,255,255));
  // 1.) Undo/Redo support
  this.palette.workflow.getCommandStack().execute(new draw2d.CommandAdd(this.palette.workflow,figure,x,y));

  // 2.) without undo/redo
  draw2d.ToolGeneric.prototype.execute.call(this,x,y);
}
/** 
 * ToolOval class 
 **/
draw2d.ToolOval=function(/*:draw2d.PaletteWindow*/ palette)
{
  draw2d.ToolGeneric.call(this,palette);
}
draw2d.ToolOval.prototype = new draw2d.ToolGeneric;

/** @private */
draw2d.ToolOval.prototype.type="ToolOval";

draw2d.ToolOval.prototype.execute=function(/*:int*/ x,/*:int*/ y)
{
  var figure= new draw2d.Oval();
  figure.setDimension(100,60);
  figure.setBackgroundColor(new  draw2d.Color(255,255,255));
  // 1.) Undo/Redo support
  this.palette.workflow.getCommandStack().execute(new draw2d.CommandAdd(this.palette.workflow,figure,x,y));
  // 2.) without undo/redo
  draw2d.ToolGeneric.prototype.execute.call(this,x,y);
}
/** 
 * ToolLine class 
 **/
draw2d.ToolLine=function(palette /*:draw2d.PaletteWindow*/)
{
  draw2d.ToolGeneric.call(this,palette);
}

draw2d.ToolLine.prototype = new draw2d.ToolGeneric;
/** @private */
draw2d.ToolLine.prototype.type="ToolLine";

draw2d.ToolLine.prototype.execute=function(x /*:int*/, y/*:int*/)
{
  var figure= new draw2d.Line();
  figure.setStartPoint(x,y);
  figure.setEndPoint(x+100,y+100);
  // 1.) Undo/Redo support
  this.palette.workflow.getCommandStack().execute(new draw2d.CommandAdd(this.palette.workflow,figure));
  // 2.) without undo/redo
  draw2d.ToolGeneric.prototype.execute.call(this,x,y);
}
/** 
 * ToolRedo class 
 **/
draw2d.ToolRedo=function(/*:draw2d.PaletteWindow*/ palette)
{
  draw2d.Button.call(this,palette);
}
draw2d.ToolRedo.prototype = new draw2d.Button;
/** @private */
draw2d.ToolRedo.prototype.type="ToolRedo";

draw2d.ToolRedo.prototype.execute=function()
{
  this.palette.workflow.getCommandStack().redo();
  draw2d.ToolGeneric.prototype.execute.call(this);
}
/** 
 * ToolUndo class 
 **/
draw2d.ToolUndo=function(/*:draw2d.PaletteWindow*/ palette)
{
  draw2d.Button.call(this,palette);
}
draw2d.ToolUndo.prototype = new draw2d.Button;
/** @private **/
draw2d.ToolUndo.prototype.type="ToolUndo";

draw2d.ToolUndo.prototype.execute=function()
{
  this.palette.workflow.getCommandStack().undo();
  draw2d.ToolGeneric.prototype.execute.call(this);
}

/** 
 * Button  class 
 **/
draw2d.Button.prototype.getImageUrl=function()
{
  if(this.enabled)
    return "scripts/draw2d/vectordraw/" + this.type+".png";
  else
    return "scripts/draw2d/vectordraw/" + this.type+"_disabled.png";
}

draw2d.ToolGeneric.prototype.getImageUrl=function()
{
  if(this.enabled)
    return "scripts/draw2d/vectordraw/" + this.type+".png";
  else
    return "scripts/draw2d/vectordraw/" + this.type+"_disabled.png";
}
