View Javadoc

1   package com.explosion.utilities;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  /***
24   * @author Stephen Cowx Date created:@06-Feb-2003
25   */
26  
27  import java.util.LinkedList;
28  
29  import javax.swing.event.UndoableEditEvent;
30  import javax.swing.event.UndoableEditListener;
31  import javax.swing.undo.UndoableEdit;
32  
33  public class UndoRedoBuffer implements UndoableEditListener
34  {
35  
36      private LinkedList undoBuffer = new LinkedList();
37  
38      private LinkedList redoBuffer = new LinkedList();
39  
40      private int maxSize = -1;
41  
42      public void undoableEditHappened(UndoableEditEvent edit)
43      {
44          undoBuffer.addFirst(edit.getEdit());
45          redoBuffer.clear();
46          constrainToMaximum();
47      }
48  
49      public void undo()
50      {
51          if (undoBuffer.size() > 0)
52          {
53              UndoableEdit edit = (UndoableEdit) undoBuffer.getFirst();
54              edit.undo();
55              undoBuffer.removeFirst();
56              redoBuffer.addFirst(edit);
57          }
58          constrainToMaximum();
59      }
60  
61      public void redo()
62      {
63          if (redoBuffer.size() > 0)
64          {
65              UndoableEdit edit = (UndoableEdit) redoBuffer.getFirst();
66              edit.redo();
67              undoBuffer.addFirst(edit);
68              redoBuffer.removeFirst();
69          }
70          constrainToMaximum();
71      }
72  
73      /***
74       * Returns the redoBuffer.
75       * 
76       * @return LinkedList
77       */
78      public LinkedList getRedoBuffer()
79      {
80          return redoBuffer;
81      }
82  
83      /***
84       * Returns the undoBuffer.
85       * 
86       * @return LinkedList
87       */
88      public LinkedList getUndoBuffer()
89      {
90          return undoBuffer;
91      }
92  
93      private void constrainToMaximum()
94      {
95          if (maxSize >= 0 && undoBuffer.size() > maxSize) undoBuffer.removeLast();
96  
97          if (maxSize >= 0 && redoBuffer.size() > maxSize) redoBuffer.removeLast();
98      }
99  
100     /***
101      * Returns the maxSize.
102      * 
103      * @return int
104      */
105     public int getMaxSize()
106     {
107         return maxSize;
108     }
109 
110     /***
111      * Sets the maxSize.
112      * 
113      * @param maxSize The maxSize to set
114      */
115     public void setMaxSize(int maxSize)
116     {
117         this.maxSize = maxSize;
118     }
119 
120 }