|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model; |
|
38 |
| |
|
39 |
| import java.awt.EventQueue; |
|
40 |
| |
|
41 |
| import java.util.*; |
|
42 |
| |
|
43 |
| import edu.rice.cs.plt.lambda.Lambda; |
|
44 |
| import edu.rice.cs.util.swing.Utilities; |
|
45 |
| |
|
46 |
| |
|
47 |
| public class BrowserHistoryManager extends EventNotifier<RegionManagerListener<BrowserDocumentRegion>> { |
|
48 |
| |
|
49 |
| public static final int DIFF_THRESHOLD = 5; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| private volatile Stack<BrowserDocumentRegion> _pastRegions = new Stack<BrowserDocumentRegion>(); |
|
54 |
| private volatile Stack<BrowserDocumentRegion> _futureRegions = new Stack<BrowserDocumentRegion>(); |
|
55 |
| |
|
56 |
| private volatile int _maxSize; |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
159
| public BrowserHistoryManager(int size) { _maxSize = size; }
|
|
62 |
| |
|
63 |
| |
|
64 |
159
| public BrowserHistoryManager() { this(0); }
|
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
27
| public void addBrowserRegion(final BrowserDocumentRegion r, final GlobalEventNotifier notifier) {
|
|
69 |
27
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
70 |
| |
|
71 |
27
| final BrowserDocumentRegion current = getCurrentRegion();
|
|
72 |
27
| if ((current != null) && (similarRegions(current, r))) {
|
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
7
| current.update(r);
|
|
77 |
| } |
|
78 |
| else { |
|
79 |
20
| _pastRegions.push(r);
|
|
80 |
20
| r.getDocument().addBrowserRegion(r);
|
|
81 |
| |
|
82 |
| |
|
83 |
20
| Utilities.invokeLater(new Runnable() {
|
|
84 |
20
| public void run() {
|
|
85 |
20
| _lock.startRead();
|
|
86 |
0
| try { for (RegionManagerListener<BrowserDocumentRegion> l: _listeners) { l.regionAdded(r); } }
|
|
87 |
20
| finally { _lock.endRead(); }
|
|
88 |
| } |
|
89 |
| }); |
|
90 |
| |
|
91 |
| |
|
92 |
20
| shrinkManager();
|
|
93 |
| } |
|
94 |
27
| notifier.browserChanged();
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
0
| public void addBrowserRegionBefore(final BrowserDocumentRegion r, final GlobalEventNotifier notifier) {
|
|
100 |
0
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
101 |
| |
|
102 |
0
| final BrowserDocumentRegion current = getCurrentRegion();
|
|
103 |
0
| if ((current != null) && (similarRegions(current, r))) {
|
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
0
| current.update(r);
|
|
108 |
| } |
|
109 |
| else { |
|
110 |
0
| if (_pastRegions.size() == 0) {
|
|
111 |
0
| _pastRegions.push(r);
|
|
112 |
| } |
|
113 |
| else { |
|
114 |
0
| _futureRegions.push(_pastRegions.pop());
|
|
115 |
0
| _pastRegions.push(r);
|
|
116 |
| } |
|
117 |
0
| r.getDocument().addBrowserRegion(r);
|
|
118 |
| |
|
119 |
| |
|
120 |
0
| Utilities.invokeLater(new Runnable() {
|
|
121 |
0
| public void run() {
|
|
122 |
0
| _lock.startRead();
|
|
123 |
0
| try { for (RegionManagerListener<BrowserDocumentRegion> l: _listeners) { l.regionAdded(r); } }
|
|
124 |
0
| finally { _lock.endRead(); }
|
|
125 |
| } |
|
126 |
| }); |
|
127 |
| |
|
128 |
| |
|
129 |
0
| shrinkManager();
|
|
130 |
| } |
|
131 |
0
| notifier.browserChanged();
|
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
179
| private void shrinkManager() {
|
|
136 |
179
| if (_maxSize > 0) {
|
|
137 |
179
| int size = _pastRegions.size() + _futureRegions.size();
|
|
138 |
179
| int diff = size - _maxSize;
|
|
139 |
179
| for (int i = 0; i < diff; ++i) {
|
|
140 |
| |
|
141 |
0
| remove(((_pastRegions.size()>_futureRegions.size())?_pastRegions:_futureRegions).get(0));
|
|
142 |
| } |
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
1
| public void remove(final BrowserDocumentRegion r) {
|
|
150 |
1
| OpenDefinitionsDocument doc = r.getDocument();
|
|
151 |
0
| if (!_pastRegions.remove(r)) _futureRegions.remove(r);
|
|
152 |
1
| doc.removeBrowserRegion(r);
|
|
153 |
| |
|
154 |
1
| Utilities.invokeLater(new Runnable() {
|
|
155 |
1
| public void run() {
|
|
156 |
1
| _lock.startRead();
|
|
157 |
0
| try { for (RegionManagerListener<BrowserDocumentRegion> l: _listeners) { l.regionRemoved(r); } }
|
|
158 |
1
| finally { _lock.endRead(); }
|
|
159 |
| } |
|
160 |
| }); |
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
3
| public SortedSet<BrowserDocumentRegion> getRegions() {
|
|
165 |
3
| TreeSet<BrowserDocumentRegion> ts = new TreeSet<BrowserDocumentRegion>(_pastRegions);
|
|
166 |
3
| ts.addAll(_futureRegions);
|
|
167 |
3
| return ts;
|
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
0
| public void clearBrowserRegions() {
|
|
172 |
0
| while(_pastRegions.size()+_futureRegions.size() > 0) {
|
|
173 |
0
| remove(((_pastRegions.size()>_futureRegions.size())?_pastRegions:_futureRegions).get(0));
|
|
174 |
| } |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
27
| public BrowserDocumentRegion getCurrentRegion() {
|
|
179 |
13
| if (_pastRegions.isEmpty()) return null;
|
|
180 |
14
| return _pastRegions.peek();
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
19
| public boolean isCurrentRegionFirst() { return (_pastRegions.size()<2); }
|
|
185 |
| |
|
186 |
| |
|
187 |
19
| public boolean isCurrentRegionLast() { return (_futureRegions.size()<1); }
|
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
0
| public BrowserDocumentRegion nextCurrentRegion(final GlobalEventNotifier notifier) {
|
|
192 |
0
| if (isCurrentRegionLast()) return null;
|
|
193 |
0
| _pastRegions.push(_futureRegions.pop());
|
|
194 |
0
| notifier.browserChanged();
|
|
195 |
0
| return _pastRegions.peek();
|
|
196 |
| } |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
0
| public BrowserDocumentRegion prevCurrentRegion(final GlobalEventNotifier notifier) {
|
|
201 |
0
| if (isCurrentRegionFirst()) return null;
|
|
202 |
0
| _futureRegions.push(_pastRegions.pop());
|
|
203 |
0
| notifier.browserChanged();
|
|
204 |
0
| return _pastRegions.peek();
|
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
159
| public void setMaximumSize(int size) {
|
|
211 |
159
| _maxSize = size;
|
|
212 |
| |
|
213 |
| |
|
214 |
159
| shrinkManager();
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
0
| public int getMaximumSize() { return _maxSize; }
|
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
0
| public void changeRegion(final BrowserDocumentRegion region, Lambda<BrowserDocumentRegion,Object> cmd) {
|
|
224 |
0
| cmd.value(region);
|
|
225 |
0
| Utilities.invokeLater(new Runnable() { public void run() {
|
|
226 |
| |
|
227 |
0
| _lock.startRead();
|
|
228 |
0
| try {
|
|
229 |
0
| for (RegionManagerListener<BrowserDocumentRegion> l: _listeners) { l.regionChanged(region); }
|
|
230 |
0
| } finally { _lock.endRead(); }
|
|
231 |
| } }); |
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
14
| public static boolean similarRegions(BrowserDocumentRegion r1, BrowserDocumentRegion r2) {
|
|
236 |
14
| OpenDefinitionsDocument d = r1.getDocument();
|
|
237 |
7
| if (d!=r2.getDocument()) return false;
|
|
238 |
7
| int l1 = d.getLineOfOffset(r1.getStartOffset());
|
|
239 |
7
| int l2 = d.getLineOfOffset(r2.getStartOffset());
|
|
240 |
7
| return (Math.abs(l1-l2) <= DIFF_THRESHOLD);
|
|
241 |
| } |
|
242 |
| |
|
243 |
0
| public String toString() {
|
|
244 |
0
| ArrayList<BrowserDocumentRegion> future = new ArrayList<BrowserDocumentRegion>(_futureRegions);
|
|
245 |
0
| Collections.reverse(future);
|
|
246 |
0
| return "Past: " + _pastRegions.toString() + ", Future: " + future.toString();
|
|
247 |
| } |
|
248 |
| } |