Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Undo.java
1 
3 package ij;
4 import ij.process.*;
5 import java.awt.*;
6 import java.awt.image.*;
7 import ij.gui.*;
8 
11 public class Undo {
12 
13  public static final int NOTHING = 0;
14  public static final int FILTER = 1;
15  public static final int TYPE_CONVERSION = 2;
16  public static final int PASTE = 3;
17  public static final int COMPOUND_FILTER = 4;
18  public static final int COMPOUND_FILTER_DONE = 5;
19  public static final int TRANSFORM = 6;
20 
21  private static int whatToUndo = NOTHING;
22  private static int imageID;
23  private static ImageProcessor ipCopy = null;
24  private static ImagePlus impCopy;
25 
26  public static void setup(int what, ImagePlus imp) {
27  if (imp==null)
28  {reset(); return;}
29  //IJ.log(imp.getTitle() + ": set up undo (" + what + ")");
30  if (what==FILTER && whatToUndo==COMPOUND_FILTER)
31  return;
32  if (what==COMPOUND_FILTER_DONE) {
33  if (whatToUndo==COMPOUND_FILTER)
34  whatToUndo = what;
35  return;
36  }
37  whatToUndo = what;
38  imageID = imp.getID();
39  if (what==TYPE_CONVERSION)
40  ipCopy = imp.getProcessor();
41  else if (what==TRANSFORM) {
42  impCopy = new ImagePlus(imp.getTitle(), imp.getProcessor().duplicate());
43  Object fht = imp.getProperty("FHT");
44  if (fht!=null) {
45  fht = new FHT((ImageProcessor)fht); // duplicate
46  impCopy.setProperty("FHT", fht);
47  }
48  } else if (what==COMPOUND_FILTER) {
49  ImageProcessor ip = imp.getProcessor();
50  if (ip!=null)
51  ipCopy = ip.duplicate();
52  else
53  ipCopy = null;
54  } else
55  ipCopy = null;
56  }
57 
58 
59  public static void reset() {
60  if (whatToUndo==COMPOUND_FILTER)
61  return;
62  whatToUndo = NOTHING;
63  imageID = 0;
64  ipCopy = null;
65  impCopy = null;
66  //IJ.log("Undo: reset");
67  }
68 
69 
70  public static void undo() {
71  ImagePlus imp = IJ.getInstance().getImagePlus();
72  //IJ.log(imp.getTitle() + ": undo (" + whatToUndo + ") "+(imageID!=imp.getID()));
73  if (imageID!=imp.getID()) {
74  reset();
75  return;
76  }
77  switch (whatToUndo) {
78  case FILTER:
79  ImageProcessor ip = imp.getProcessor();
80  if (ip!=null) {
81  ip.reset();
82  imp.updateAndDraw();
83  }
84  break;
85  case TYPE_CONVERSION:
86  case COMPOUND_FILTER:
87  case COMPOUND_FILTER_DONE:
88  if (ipCopy!=null)
89  imp.setProcessor(null, ipCopy);
90  break;
91  case TRANSFORM:
92  if (impCopy!=null) {
93  imp.setProcessor(impCopy.getTitle(), impCopy.getProcessor());
94  Object fht = impCopy.getProperty("FHT");
95  if (fht!=null)
96  imp.setProperty("FHT", fht);
97  else if (imp.getProperty("FHT")!=null)
98  imp.getProperties().remove("FHT");
99  }
100  break;
101  case PASTE:
102  Roi roi = imp.getRoi();
103  if (roi!=null)
104  roi.abortPaste();
105  break;
106  }
107  reset();
108  }
109 
110 }