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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.5 KB
Line 
1/**
2 * WidgetCacheManager.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items.widgets;
20
21import java.util.Collection;
22import java.util.Collections;
23import java.util.HashMap;
24import java.util.LinkedList;
25import java.util.Set;
26
27import org.expeditee.gui.DisplayIOObserver;
28import org.expeditee.gui.FrameIO;
29
30
31/**
32 * Some heavy duty widgets can hold alot of data and must only be in Expeditee memory for
33 * a little while ... e.g. Not cached once the user has traversed 50 or so frames after
34 * where the widget was first loaded.
35 *
36 * @author Brook Novak
37 *
38 */
39public final class WidgetCacheManager implements DisplayIOObserver {
40
41 private static HashMap<HeavyDutyInteractiveWidget, Integer> transientWidgets = new HashMap<HeavyDutyInteractiveWidget, Integer>();
42
43 private static WidgetCacheManager _instance = new WidgetCacheManager();
44
45 private WidgetCacheManager() {
46 }
47
48 public static WidgetCacheManager getInstance() {
49 return _instance;
50 }
51
52 /**
53 * @return
54 * An unmodifiable set of the current transientWidgets that are registered.
55 */
56 public static Set<HeavyDutyInteractiveWidget> getTransientWidgets() {
57 return Collections.unmodifiableSet(transientWidgets.keySet());
58 }
59
60 public static void cacheWidget(HeavyDutyInteractiveWidget widget) {
61 if (widget == null) throw new NullPointerException("widget");
62
63 if (widget.getCacheDepth() > FrameIO.MAX_CACHE ||
64 widget.getCacheDepth() <= 0) return;
65
66 // If widget already exists, then the current count will be reset (overriden to zero)
67 transientWidgets.put(widget, new Integer(0));
68
69 }
70
71 public static void uncacheWidget(HeavyDutyInteractiveWidget widget) {
72 if (widget == null) throw new NullPointerException("widget");
73 transientWidgets.remove(widget);
74 }
75
76
77 /**
78 * invoked when the frame changes. After the new frame is actually set.
79 * Expires heavy duty widgets that have been cached for to long....
80 *
81 * Intention: to be called from the DisplayIO frame change code...
82 *
83 */
84 public void frameChanged() {
85
86 // Increment all cache counters
87 Collection<HeavyDutyInteractiveWidget> toCheck = new LinkedList<HeavyDutyInteractiveWidget>(
88 transientWidgets.keySet());
89
90 // Check all heavyduty widgets that have limited cache
91 for (HeavyDutyInteractiveWidget tw : toCheck) {
92
93 Integer count = transientWidgets.remove(tw); // get rid of reference
94 if (count == null) continue;
95
96 int newCount = count;
97 newCount++;
98
99 if (tw.isVisible()) { // Reset cache counter if visible
100
101 transientWidgets.put(tw, new Integer(0));
102
103 } else if (newCount >= tw.getCacheDepth()) { // Expire if stale
104
105 // Ensure that the heavy duty widgets data is handled correctly (i.e. saving)
106 tw.expire();
107
108 } else { // keep reference, with new count
109
110 transientWidgets.put(tw, newCount);
111
112 }
113
114 }
115 }
116
117
118}
119
Note: See TracBrowser for help on using the repository browser.