Guest User

Untitled

a guest
Jan 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package serie03;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public class Clear extends AbstractCommand {
  6.  
  7.     // attribut
  8.     private LinkedList<String> backUp;
  9.    
  10.     // constructeur
  11.     public Clear(Text text) {
  12.         super(text);
  13.         backUp = new LinkedList<String>();
  14.     }
  15.    
  16.     // requete
  17.     public LinkedList<String> getBackUp() {
  18.         return backUp;
  19.     }
  20.    
  21.     // commande
  22.     @Override
  23.     void doIt() {
  24.         assert canDo();
  25.         this.getBackUp().clear();
  26.         for (int i = 1; i <= getText().getLinesNb(); i++) {
  27.             this.getBackUp().add(getText().getLine(i));
  28.         }
  29.         this.getText().clear();
  30.     }
  31.  
  32.     @Override
  33.     void undoIt() {
  34.          assert canUndo();
  35.          for (int i = 1; i <= this.getBackUp().size(); i++) {
  36.                 this.getText().insertLine(i, this.getBackUp().get(i - 1));
  37.          }
  38.          backUp.clear();
  39.     }
  40.  
  41. }
  42.  
  43.  
  44. package serie03;
  45.  
  46. public class InsertLine extends AbstractCommand {
  47.  
  48.     // attribut
  49.     private int index;
  50.     private String line;
  51.    
  52.     // constructeur
  53.     public InsertLine(Text text, int lines, String chain) {
  54.         super(text);
  55.         if (lines <= 0) {
  56.             throw new IllegalArgumentException("ligne non accessible");
  57.         } else if (chain == null) {
  58.             throw new IllegalArgumentException("aucune chaine a ecrire");
  59.         }
  60.         index = lines;
  61.         line = chain;
  62.     }
  63.    
  64.     // requete
  65.     public int getIndex() {
  66.         return index;
  67.     }
  68.    
  69.     public String getLine() {
  70.         return line;
  71.     }
  72.    
  73.     // commmande
  74.     public boolean canDo() {
  75.         return
  76.             super.canDo()
  77.             && getIndex() <= getText().getLinesNb() + 1;
  78.     }
  79.  
  80.     public boolean canUndo() {
  81.         return
  82.             super.canUndo()
  83.             && getIndex() <= getText().getLinesNb();
  84.     }
  85.  
  86.     @ Override
  87.     void doIt() {
  88.         assert canDo();
  89.         getText().insertLine(getIndex(), getLine());
  90.     }
  91.  
  92.     @Override
  93.     void undoIt() {
  94.         assert canUndo();
  95.         getText().deleteLine(getIndex());
  96.        
  97.     }
  98. }
Add Comment
Please, Sign In to add comment