1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package de.jaret.examples.timebars.timeline;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import de.jaret.util.date.Interval;
28 import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
29 import de.jaret.util.ui.timebars.model.TimeBarRow;
30 import de.jaret.util.ui.timebars.strategy.IOverlapStrategy;
31 import de.jaret.util.ui.timebars.strategy.OverlapInfo;
32
33 /***
34 * Implementation of an overlap strategy simply distributing the intervals over a number of rows.
35 *
36 * @author kliem
37 * @version $Id: $
38 */
39 public class DistributeOverlapStrategy implements IOverlapStrategy{
40 /*** the delegate the stragey works for. */
41 protected TimeBarViewerDelegate _delegate;
42 /*** Cache for overlap infos. */
43 protected Map<TimeBarRow, Map<Interval, OverlapInfo>> _oiRowCache = new HashMap<TimeBarRow, Map<Interval, OverlapInfo>>();
44
45 protected int _numRows;
46
47
48 /***
49 * Construct the strategy.
50 *
51 * @param delegate the delegate the strategy works for
52 * @param numRows number of rows to distribute the intervals on
53 */
54 public DistributeOverlapStrategy(TimeBarViewerDelegate delegate, int numRows) {
55 _delegate = delegate;
56 _numRows = numRows;
57 }
58
59 /***
60 * {@inheritDoc}
61 */
62 public OverlapInfo getOverlapInfo(TimeBarRow row, Interval interval) {
63 Map<Interval, OverlapInfo> oiIntervalMap = _oiRowCache.get(row);
64 if (oiIntervalMap == null) {
65 oiIntervalMap = updateOICache(row);
66 }
67 OverlapInfo oi = oiIntervalMap.get(interval);
68 return oi;
69 }
70
71 /***
72 * {@inheritDoc}
73 */
74 public int getMaxOverlapCount(TimeBarRow row) {
75
76 Map<Interval, OverlapInfo> oiIntervalMap = _oiRowCache.get(row);
77 if (oiIntervalMap == null) {
78 oiIntervalMap = updateOICache(row);
79 }
80 int max = 0;
81 Collection<OverlapInfo> infos = oiIntervalMap.values();
82 for (OverlapInfo overlapInfo : infos) {
83 if (overlapInfo.maxOverlapping > max) {
84 max = overlapInfo.maxOverlapping;
85 }
86 }
87 return max + 1;
88 }
89
90 /***
91 * {@inheritDoc} Assumes sorted Intervals.
92 */
93 public Map<Interval, OverlapInfo> updateOICache(TimeBarRow row) {
94 List<Interval> intervals = _delegate.filterIntervals(row.getIntervals());
95 Map<Interval, OverlapInfo> result = new HashMap<Interval, OverlapInfo>();
96
97 for (int i = 0; i < intervals.size(); i++) {
98 Interval interval = intervals.get(i);
99 OverlapInfo oi = new OverlapInfo();
100 oi.interval = interval;
101 result.put(interval, oi);
102 oi.maxOverlapping = _numRows;
103 oi.overlappingCount=_numRows;
104 oi.pos = i % _numRows;
105 }
106
107
108 _oiRowCache.put(row, result);
109
110 return result;
111 }
112
113 /***
114 * {@inheritDoc}
115 */
116 public void clearCachedData() {
117 _oiRowCache.clear();
118 }
119
120 /***
121 * {@inheritDoc} Simply helps the garbage collector.
122 */
123 public void dispose() {
124 _delegate = null;
125 _oiRowCache = null;
126 }
127
128
129 }