source: trunk/src/org/expeditee/items/widgets/WidgetCacheManager.java@ 284

Last change on this file since 284 was 219, checked in by bjn8, 16 years ago

Cleaned up Display IO and the setCurrentFrame method - now anything can hook into frame changed events if they want to

File size: 2.7 KB
Line 
1package org.expeditee.items.widgets;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.LinkedList;
7import java.util.Set;
8
9import org.expeditee.gui.DisplayIOObserver;
10import org.expeditee.gui.FrameIO;
11
12
13/**
14 * Some heavy duty widgets can hold alot of data and must only be in Expeditee memory for
15 * a little while ... e.g. Not cached once the user has traversed 50 or so frames after
16 * where the widget was first loaded.
17 *
18 * @author Brook Novak
19 *
20 */
21public final class WidgetCacheManager implements DisplayIOObserver {
22
23 private static HashMap<HeavyDutyInteractiveWidget, Integer> transientWidgets = new HashMap<HeavyDutyInteractiveWidget, Integer>();
24
25 private static WidgetCacheManager _instance = new WidgetCacheManager();
26
27 private WidgetCacheManager() {
28 }
29
30 public static WidgetCacheManager getInstance() {
31 return _instance;
32 }
33
34 /**
35 * @return
36 * An unmodifiable set of the current transientWidgets that are registered.
37 */
38 public static Set<HeavyDutyInteractiveWidget> getTransientWidgets() {
39 return Collections.unmodifiableSet(transientWidgets.keySet());
40 }
41
42 public static void cacheWidget(HeavyDutyInteractiveWidget widget) {
43 if (widget == null) throw new NullPointerException("widget");
44
45 if (widget.getCacheDepth() > FrameIO.MAX_CACHE ||
46 widget.getCacheDepth() <= 0) return;
47
48 // If widget already exists, then the current count will be reset (overriden to zero)
49 transientWidgets.put(widget, new Integer(0));
50
51 }
52
53 public static void uncacheWidget(HeavyDutyInteractiveWidget widget) {
54 if (widget == null) throw new NullPointerException("widget");
55 transientWidgets.remove(widget);
56 }
57
58
59 /**
60 * invoked when the frame changes. After the new frame is actually set.
61 * Expires heavy duty widgets that have been cached for to long....
62 *
63 * Intention: to be called from the DisplayIO frame change code...
64 *
65 */
66 public void frameChanged() {
67
68 // Increment all cache counters
69 Collection<HeavyDutyInteractiveWidget> toCheck = new LinkedList<HeavyDutyInteractiveWidget>(
70 transientWidgets.keySet());
71
72 // Check all heavyduty widgets that have limited cache
73 for (HeavyDutyInteractiveWidget tw : toCheck) {
74
75 Integer count = transientWidgets.remove(tw); // get rid of reference
76 if (count == null) continue;
77
78 int newCount = count;
79 newCount++;
80
81 if (tw.isVisible()) { // Reset cache counter if visible
82
83 transientWidgets.put(tw, new Integer(0));
84
85 } else if (newCount >= tw.getCacheDepth()) { // Expire if stale
86
87 // Ensure that the heavy duty widgets data is handled correctly (i.e. saving)
88 tw.expire();
89
90 } else { // keep reference, with new count
91
92 transientWidgets.put(tw, newCount);
93
94 }
95
96 }
97 }
98
99
100}
101
Note: See TracBrowser for help on using the repository browser.