Subversion Repositories wimsdev

Rev

Rev 3662 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     Sketch Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2008 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem.ds;
  12.  
  13. import WIMSchem.*;
  14.  
  15. import java.io.*;
  16. import java.util.*;
  17.  
  18. /*
  19.     Caching system for DataSheet instances, which is intended for undo/redo purposes.
  20.    
  21.     NOTE: current implementation is very inefficient, as it stores a copy of the datasheet for every undo/redo instance.
  22.     At some point this should be rewritten to store differences.
  23. */
  24.  
  25. public class DataSheetCache
  26. {
  27.     private final int MAX_UNDO=10;
  28.     private Stack<String> undo=new Stack<String>(),redo=new Stack<String>();
  29.    
  30.     public DataSheetCache()
  31.     {
  32.     }
  33.        
  34.     public boolean canUndo() {return !undo.empty();}
  35.     public boolean canRedo() {return !redo.empty();}
  36.    
  37.     /* !! figure something out...
  38.     public void NotifySaved() {saveidx=curidx;}
  39.     public boolean IsSaved() {return curidx==saveidx;}*/
  40.    
  41.     public void cacheUndo(DataSheet DS)
  42.     {
  43.         String str=toString(DS);
  44.         undo.push(str);
  45.         while (undo.size()>MAX_UNDO) undo.remove(0);
  46.         redo.clear();
  47.     }
  48.    
  49.     public DataSheet performUndo(DataSheet Cur)
  50.     {
  51.         if (undo.empty()) return null;
  52.         redo.push(toString(Cur));
  53.         return fromString(undo.pop());
  54.     }
  55.    
  56.     public DataSheet performRedo(DataSheet Cur)
  57.     {
  58.         if (redo.empty()) return null;
  59.         undo.push(toString(Cur));
  60.         return fromString(redo.pop());
  61.     }
  62.    
  63.     private String toString(DataSheet DS)
  64.     {
  65.         StringWriter sw=new StringWriter();
  66.         try {DataSheetStream.writeXML(new BufferedWriter(sw),DS);}
  67.         catch (IOException e) {return null;}
  68.         return sw.toString();
  69.     }
  70.    
  71.     private DataSheet fromString(String StrDS)
  72.     {
  73.         DataSheet ds=null;
  74.         try
  75.         {
  76.             if (DataSheetStream.examineIsXMLDS(new BufferedReader(new StringReader(StrDS))))
  77.                 ds=DataSheetStream.readXML(new BufferedReader(new StringReader(StrDS)));
  78.         }
  79.         catch (IOException e) {e.printStackTrace();}
  80.         return ds;
  81.     }
  82. }
  83.