/*
 * The command to paste text from the clip tray.
 */
package htmleditor.commands;

import htmleditor.HTMLEditor;
import javafx.event.Event;

/**
 * @author aac6012
 */
public class PasteCommand extends UndoableCommand {

    public PasteCommand(HTMLEditor e) {
        this.editor = e ;
    }

    @Override
    public void execute(Event t) {
        int pos = this.editor.getCarrotPosition() ;
        String oldBuff = this.editor.getText().getText() ;
        
        String newBuff = oldBuff.substring(0,pos) ;
        newBuff += this.editor.getClipboard() ;
        newBuff += oldBuff.substring(pos) ;
        
        this.editor.setBuffer(newBuff) ;
        
        this.saveState() ; //Save after state changes.
    }   
}
@@ -370,137 +370,1697 @@
     
-int pos = this.editor.getCarrotPosition() ;%0D%0A        String oldBuff = this.editor.getText().getText() ;%0D%0A        %0D%0A        String
+String oldBuff = this.editor.getText().getText() ;%0D%0A        String newBuff ;%0D%0A        %0D%0A        String selection = this.editor.getBufferSelection() ;%0D%0A        int pos = this.editor.getCarrotPosition() ;%0D%0A        %0D%0A        //Overwrite the selection with the pasted text.%0D%0A        if (!selection.equals(%22%22)) %7B%0D%0A            %0D%0A            //Text is to the left of cursor%0D%0A            if(pos + selection.length() %3C= oldBuff.length()%0D%0A            &&%0D%0A            selection.equals(oldBuff.substring(pos, pos+selection.length())))%7B%0D%0A                //Remove text from right of cursor and insert pasted text%0D%0A                newBuff = oldBuff.substring(0,pos) ;%0D%0A                newBuff += this.editor.getClipboard() ;%0D%0A                newBuff += oldBuff.substring(pos+selection.length()) ;%0D%0A            %7D%0D%0A            %0D%0A            //Text is to the right of cursor%0D%0A            else if(pos - selection.length() %3E 0 %0D%0A            &&%0D%0A            selection.equals(oldBuff.substring(pos-selection.length(), pos)))%7B%0D%0A                //Remove text from left of cursor and insert pasted text%0D%0A                newBuff = oldBuff.substring(0,pos-selection.length()) ;%0D%0A                newBuff += this.editor.getClipboard() ;%0D%0A                newBuff += oldBuff.substring(pos) ;%0D%0A                %0D%0A            %7D%0D%0A            %0D%0A            //Unable to find text with respect to cursor%0D%0A            else %7B%0D%0A                System.out.println(%22You broke it.%22) ;%0D%0A                //Don't change the buffer if something broke (damage control)%0D%0A                newBuff = oldBuff ;%0D%0A            %7D%0D%0A            %0D%0A        %7D%0D%0A        %0D%0A        //No text to overwrite because no selection.%0D%0A        else %7B%0D%0A           
  new
@@ -2086,32 +2086,36 @@
 tring(0,pos) ;%0D%0A
+    
         newBuff 
@@ -2147,32 +2147,36 @@
 rd() ;%0D%0A        
+ 
+   
 newBuff += oldBu
@@ -2188,32 +2188,53 @@
 bstring(pos) ;%0D%0A
+        %7D%0D%0A        %0D%0A
         %0D%0A      
@@ -2340,14 +2340,11 @@
 .%0D%0A    %7D
-   
 %0D%0A%7D

Paste will now override selected text.