|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| package org.ericmignot.modeler.util.action; |
|
20 |
| |
|
21 |
| import java.util.*; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public class ActionHistoric extends ArrayList |
|
27 |
| { |
|
28 |
| |
|
29 |
| private int lastExecutedActionIndex; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
125
| public ActionHistoric()
|
|
36 |
| { |
|
37 |
125
| super();
|
|
38 |
125
| this.lastExecutedActionIndex = -1;
|
|
39 |
| } |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
29
| public Object execute(Action action, Object actionParameter) throws ActionException
|
|
50 |
| { |
|
51 |
29
| Object toReturn = action.execute(actionParameter);
|
|
52 |
29
| if (action instanceof UndoableAction)
|
|
53 |
| { |
|
54 |
28
| if (size() > 0)
|
|
55 |
| { |
|
56 |
4
| this.removeRange(this.lastExecutedActionIndex + 1, size());
|
|
57 |
| } |
|
58 |
28
| this.add(action);
|
|
59 |
28
| this.lastExecutedActionIndex ++;
|
|
60 |
| } |
|
61 |
| else |
|
62 |
| { |
|
63 |
1
| clear();
|
|
64 |
1
| this.lastExecutedActionIndex = -1;
|
|
65 |
| } |
|
66 |
29
| return toReturn;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
24
| public boolean canUndo()
|
|
74 |
| { |
|
75 |
24
| return (this.lastExecutedActionIndex != -1);
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
7
| public UndoableAction getNextUndoAction()
|
|
83 |
| { |
|
84 |
7
| if (canUndo())
|
|
85 |
| { |
|
86 |
7
| return (UndoableAction) get(this.lastExecutedActionIndex);
|
|
87 |
| } |
|
88 |
| else |
|
89 |
| { |
|
90 |
0
| return null;
|
|
91 |
| } |
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
11
| public Object undo(Object actionParameter) throws ActionException
|
|
100 |
| { |
|
101 |
11
| if (canUndo())
|
|
102 |
| { |
|
103 |
11
| UndoableAction lastAction = (UndoableAction) get(this.lastExecutedActionIndex);
|
|
104 |
11
| this.lastExecutedActionIndex--;
|
|
105 |
11
| return lastAction.cancel(actionParameter);
|
|
106 |
| } |
|
107 |
| else |
|
108 |
| { |
|
109 |
0
| throw new ActionException("No Undoable action available");
|
|
110 |
| } |
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
5
| public boolean canRedo()
|
|
118 |
| { |
|
119 |
5
| return (this.lastExecutedActionIndex != (size() -1));
|
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
2
| public Action getNextRedoAction()
|
|
127 |
| { |
|
128 |
2
| if (canRedo())
|
|
129 |
| { |
|
130 |
2
| return (Action) get(this.lastExecutedActionIndex + 1);
|
|
131 |
| } |
|
132 |
| else |
|
133 |
| { |
|
134 |
0
| return null;
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
2
| public Object redo(Object actionParameter) throws ActionException
|
|
144 |
| { |
|
145 |
2
| if (canRedo())
|
|
146 |
| { |
|
147 |
2
| Action action = (Action) get(this.lastExecutedActionIndex + 1);
|
|
148 |
2
| this.lastExecutedActionIndex++;
|
|
149 |
2
| return action.execute(actionParameter);
|
|
150 |
| } |
|
151 |
| else |
|
152 |
| { |
|
153 |
0
| throw new ActionException("No Redoable action available");
|
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
0
| public String toString()
|
|
159 |
| { |
|
160 |
0
| String toReturn = "\n*********** ActionHistoric";
|
|
161 |
| |
|
162 |
0
| for (int i = 0; i < size(); i ++)
|
|
163 |
| { |
|
164 |
0
| toReturn += "\n" + get(i).toString();
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| toReturn += "\n" + "lastExecutionIndex = " + this.lastExecutedActionIndex;
|
|
168 |
0
| return toReturn;
|
|
169 |
| } |
|
170 |
| } |